parent
9a9ff6e013
commit
b07e841419
5 changed files with 84 additions and 7 deletions
@ -0,0 +1,7 @@ |
|||||||
|
import sendgrid |
||||||
|
|
||||||
|
from django.conf import settings |
||||||
|
|
||||||
|
|
||||||
|
def get_sendgrid_client(): |
||||||
|
return sendgrid.SendGridAPIClient(apikey=settings.SENDGRID_API_KEY) |
||||||
@ -0,0 +1,17 @@ |
|||||||
|
from datetime import datetime, timedelta |
||||||
|
from collections import Counter |
||||||
|
|
||||||
|
|
||||||
|
def date_range(start, end): |
||||||
|
if isinstance(start, datetime): |
||||||
|
start = start.date() |
||||||
|
if isinstance(end, datetime): |
||||||
|
end = end.date() |
||||||
|
delta = end - start |
||||||
|
for d in range(delta.days + 1): |
||||||
|
yield start + timedelta(days=d) |
||||||
|
return |
||||||
|
|
||||||
|
|
||||||
|
def weekdays_in_date_range(start, end): |
||||||
|
return Counter([d.isoweekday() for d in date_range(start, end)]) |
||||||
@ -0,0 +1,39 @@ |
|||||||
|
from django.db.models.base import ModelBase |
||||||
|
from django.db import connection |
||||||
|
|
||||||
|
|
||||||
|
class ModelFieldsNames(object): |
||||||
|
def __init__(self, model): |
||||||
|
self.alias = None |
||||||
|
self._meta = model._meta |
||||||
|
|
||||||
|
def __getattr__(self, name): |
||||||
|
alias = '__as__' |
||||||
|
if name.startswith(alias): |
||||||
|
self.alias = name[len(alias):] |
||||||
|
return '%s as %s' % (self._meta.db_table, self.alias) |
||||||
|
field = self._meta.get_field(name).get_attname_column()[1] |
||||||
|
if self.alias: |
||||||
|
return u"%s.%s" % (self.alias, field) |
||||||
|
else: |
||||||
|
return u"%s.%s" % (self._meta.db_table, field) |
||||||
|
|
||||||
|
def __str__(self): |
||||||
|
return '%s' % self._meta.db_table |
||||||
|
|
||||||
|
|
||||||
|
def format_sql(sql, **kwargs): |
||||||
|
for name, value in kwargs.items(): |
||||||
|
if issubclass(type(value), ModelBase): |
||||||
|
kwargs[name] = ModelFieldsNames(value) |
||||||
|
elif isinstance(value, (tuple, list)): |
||||||
|
kwargs[name] = ','.join(map(lambda x: repr(x) if isinstance(x, str) else str(x), value)) |
||||||
|
elif not isinstance(value, str): |
||||||
|
kwargs[name] = value |
||||||
|
return sql.format(**kwargs) |
||||||
|
|
||||||
|
|
||||||
|
def execute_sql(sql, args=()): |
||||||
|
cursor = connection.cursor() |
||||||
|
cursor.execute(sql, args) |
||||||
|
return cursor.fetchall() |
||||||
Loading…
Reference in new issue