update to django 1.9

This commit is contained in:
j 2016-02-19 18:37:41 +00:00
parent 1053673af1
commit 8055e1dd54
5 changed files with 28 additions and 45 deletions

View file

@ -11,7 +11,7 @@ from ...utils import json
def autodiscover(): def autodiscover():
# Register api actions from all installed apps # Register api actions from all installed apps
from django.utils.importlib import import_module from importlib import import_module
from django.utils.module_loading import module_has_submodule from django.utils.module_loading import module_has_submodule
for app in settings.INSTALLED_APPS: for app in settings.INSTALLED_APPS:
if app != 'api': if app != 'api':

View file

@ -1,13 +1,13 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
# vi:si:et:sw=4:sts=4:ts=4 # vi:si:et:sw=4:sts=4:ts=4
from django.conf.urls import patterns from django.conf.urls import url
import views import views
import actions import actions
actions.autodiscover() actions.autodiscover()
urlpatterns = patterns("", urlpatterns = [
(r'^$', views.api), url(r'^$', views.api),
) ]

View file

@ -26,7 +26,7 @@ def admin_required_json(function=None):
""" """
def _wrapped_view(request, *args, **kwargs): def _wrapped_view(request, *args, **kwargs):
if request.user.is_authenticated() and request.user.get_profile().get_level() == 'admin': if request.user.is_authenticated() and request.user.profile.get_level() == 'admin':
return function(request, *args, **kwargs) return function(request, *args, **kwargs)
return render_to_json_response({'status': {'code': 403, 'text': 'permission denied'}}) return render_to_json_response({'status': {'code': 403, 'text': 'permission denied'}})
return wraps(function)(_wrapped_view) return wraps(function)(_wrapped_view)

View file

@ -2,6 +2,7 @@
# vi:si:et:sw=4:sts=4:ts=4 # vi:si:et:sw=4:sts=4:ts=4
import time import time
import datetime import datetime
import copy
from django.db import models from django.db import models
from django.utils import datetime_safe from django.utils import datetime_safe
@ -45,20 +46,15 @@ def from_json(json_object):
return json_object return json_object
class DictField(models.TextField): class DictField(models.TextField):
"""DictField is a textfield that contains JSON-serialized dictionaries."""
# Used so to_python() is called
__metaclass__ = models.SubfieldBase
_type = dict _type = dict
def loads(self, value): def loads(self, value):
return json.loads(value, object_hook=from_json) return json.loads(value, object_hook=from_json)
def dumps(self, obj): def dumps(self, obj):
return json.dumps(obj, default=to_json) return json.dumps(obj, default=to_json, ensure_ascii=False)
def to_python(self, value): def from_db_value(self, value, expression, connection, context):
# django 1.7
if value is None: if value is None:
return value return value
if isinstance(value, self._type): if isinstance(value, self._type):
@ -71,10 +67,6 @@ class DictField(models.TextField):
assert isinstance(value, self._type) assert isinstance(value, self._type)
return value return value
def from_db_value(self, value, expression, connection, context):
# django 1.8
return self.to_python(value)
def get_prep_value(self, value): def get_prep_value(self, value):
if isinstance(value, self._type): if isinstance(value, self._type):
value = self.dumps(value) value = self.dumps(value)
@ -83,9 +75,15 @@ class DictField(models.TextField):
value = models.TextField.get_prep_value(self, value) value = models.TextField.get_prep_value(self, value)
return value return value
def get_default(self):
if self.has_default():
if callable(self.default):
return self.default()
return copy.deepcopy(self.default)
return super(DictField, self).get_default()
class TupleField(DictField): class TupleField(DictField):
_type = tuple _type = (tuple, list)
def loads(self, value): def loads(self, value):
value = DictField.loads(self, value) value = DictField.loads(self, value)

View file

@ -20,31 +20,21 @@ class Model(models.Model):
objects = Manager() objects = Manager()
''' '''
class SQLCompiler(SQLCompiler): class NullLastSQLCompiler(SQLCompiler):
def get_ordering(self): def get_order_by(self):
result, group_by = super(SQLCompiler, self).get_ordering() result = super(NullLastSQLCompiler, self).get_order_by()
if self.query.nulls_last and len(result): if self.query.nulls_last and result \
if self.connection.vendor == 'sqlite': and self.connection.vendor == 'postgresql':
_result = [] return [(expr, (sql + ' NULLS LAST', params, is_ref))
for r in result: for (expr, (sql, params, is_ref)) in result]
if r.endswith(' DESC'): return result
_r = r[:-len(' DESC')]
elif r.endswith(' ASC'):
_r = r[:-len(' ASC')]
_result.append(_r + ' IS NULL')
_result.append(r)
result = _result class NullsLastQuery(Query):
else:
result = map(lambda e: e + ' NULLS LAST', result)
return result, group_by
class Query(Query):
nulls_last = False nulls_last = False
def clone(self, *args, **kwargs): def clone(self, *args, **kwargs):
obj = super(Query, self).clone(*args, **kwargs) obj = super(NullsLastQuery, self).clone(*args, **kwargs)
obj.nulls_last = self.nulls_last obj.nulls_last = self.nulls_last
return obj return obj
@ -53,18 +43,13 @@ class Query(Query):
raise ValueError("Need either using or connection") raise ValueError("Need either using or connection")
if using: if using:
connection = connections[using] connection = connections[using]
# Check that the compiler will be able to execute the query return NullLastSQLCompiler(self, connection, using)
for alias, aggregate in self.aggregate_select.items():
connection.ops.check_aggregate_support(aggregate)
return SQLCompiler(self, connection, using)
class QuerySet(django.db.models.query.QuerySet): class QuerySet(django.db.models.query.QuerySet):
def __init__(self, model=None, query=None, using=None, **kwargs): def __init__(self, model=None, query=None, using=None, **kwargs):
super(QuerySet, self).__init__(model=model, query=query, using=None, **kwargs) super(QuerySet, self).__init__(model=model, query=query, using=None, **kwargs)
self.query = query or Query(self.model) self.query = query or NullsLastQuery(self.model)
def order_by(self, *args, **kwargs): def order_by(self, *args, **kwargs):
nulls_last = kwargs.pop('nulls_last', False) nulls_last = kwargs.pop('nulls_last', False)