Skip to content
Snippets Groups Projects
Unverified Commit fa9b1bef authored by Marco Ciotola's avatar Marco Ciotola
Browse files

[DJ1.10] SubfieldBase has been deprecated. Use Field.from_db_value instead.

parent d973dd48
No related branches found
No related tags found
No related merge requests found
...@@ -33,7 +33,7 @@ from helios.crypto import utils as cryptoutils ...@@ -33,7 +33,7 @@ from helios.crypto import utils as cryptoutils
## utility function ## utility function
## ##
def recursiveToDict(obj): def recursiveToDict(obj):
if obj == None: if obj is None:
return None return None
if type(obj) == list: if type(obj) == list:
...@@ -53,7 +53,7 @@ def get_class(datatype): ...@@ -53,7 +53,7 @@ def get_class(datatype):
dynamic_module = __import__(".".join(parsed_datatype[:-1]), globals(), locals(), [], level=-1) dynamic_module = __import__(".".join(parsed_datatype[:-1]), globals(), locals(), [], level=-1)
if not dynamic_module: if not dynamic_module:
raise Exception("no module for %s" % datatpye) raise Exception("no module for %s" % datatype)
# go down the attributes to get to the class # go down the attributes to get to the class
try: try:
...@@ -130,7 +130,7 @@ class LDObject(object): ...@@ -130,7 +130,7 @@ class LDObject(object):
raise Exception("no datatype found") raise Exception("no datatype found")
# nulls # nulls
if obj == None: if obj is None:
return None return None
# the class # the class
...@@ -171,7 +171,7 @@ class LDObject(object): ...@@ -171,7 +171,7 @@ class LDObject(object):
self.structured_fields[f] = sub_ld_object self.structured_fields[f] = sub_ld_object
# set the field on the wrapped object too # set the field on the wrapped object too
if sub_ld_object != None: if sub_ld_object is not None:
self._setattr_wrapped(f, sub_ld_object.wrapped_obj) self._setattr_wrapped(f, sub_ld_object.wrapped_obj)
else: else:
self._setattr_wrapped(f, None) self._setattr_wrapped(f, None)
...@@ -190,7 +190,7 @@ class LDObject(object): ...@@ -190,7 +190,7 @@ class LDObject(object):
fields = self.FIELDS fields = self.FIELDS
if not self.structured_fields: if not self.structured_fields:
if self.wrapped_obj.alias != None: if self.wrapped_obj.alias is not None:
fields = self.ALIASED_VOTER_FIELDS fields = self.ALIASED_VOTER_FIELDS
for f in (alternate_fields or fields): for f in (alternate_fields or fields):
...@@ -214,7 +214,7 @@ class LDObject(object): ...@@ -214,7 +214,7 @@ class LDObject(object):
@classmethod @classmethod
def fromDict(cls, d, type_hint=None): def fromDict(cls, d, type_hint=None):
# null objects # null objects
if d == None: if d is None:
return None return None
# the LD type is either in d or in type_hint # the LD type is either in d or in type_hint
...@@ -248,11 +248,11 @@ class LDObject(object): ...@@ -248,11 +248,11 @@ class LDObject(object):
""" """
process some fields on the way into the object process some fields on the way into the object
""" """
if field_value == None: if field_value is None:
return None return None
val = self._process_value_in(field_name, field_value) val = self._process_value_in(field_name, field_value)
if val != None: if val is not None:
return val return val
else: else:
return field_value return field_value
...@@ -264,11 +264,11 @@ class LDObject(object): ...@@ -264,11 +264,11 @@ class LDObject(object):
""" """
process some fields on the way out of the object process some fields on the way out of the object
""" """
if field_value == None: if field_value is None:
return None return None
val = self._process_value_out(field_name, field_value) val = self._process_value_out(field_name, field_value)
if val != None: if val is not None:
return val return val
else: else:
return field_value return field_value
...@@ -280,7 +280,7 @@ class LDObject(object): ...@@ -280,7 +280,7 @@ class LDObject(object):
if not hasattr(self, 'uuid'): if not hasattr(self, 'uuid'):
return super(LDObject, self) == other return super(LDObject, self) == other
return other != None and self.uuid == other.uuid return other is not None and self.uuid == other.uuid
class BaseArrayOfObjects(LDObject): class BaseArrayOfObjects(LDObject):
......
...@@ -6,15 +6,12 @@ http://www.djangosnippets.org/snippets/377/ ...@@ -6,15 +6,12 @@ http://www.djangosnippets.org/snippets/377/
and adapted to LDObject and adapted to LDObject
""" """
import datetime
import json import json
from django.db import models from django.db import models
from django.db.models import signals
from django.conf import settings
from django.core.serializers.json import DjangoJSONEncoder
from . import LDObject from . import LDObject
class LDObjectField(models.TextField): class LDObjectField(models.TextField):
""" """
LDObject is a generic textfield that neatly serializes/unserializes LDObject is a generic textfield that neatly serializes/unserializes
...@@ -23,9 +20,6 @@ class LDObjectField(models.TextField): ...@@ -23,9 +20,6 @@ class LDObjectField(models.TextField):
deserialization_params added on 2011-01-09 to provide additional hints at deserialization time deserialization_params added on 2011-01-09 to provide additional hints at deserialization time
""" """
# Used so to_python() is called
__metaclass__ = models.SubfieldBase
def __init__(self, type_hint=None, **kwargs): def __init__(self, type_hint=None, **kwargs):
self.type_hint = type_hint self.type_hint = type_hint
super(LDObjectField, self).__init__(**kwargs) super(LDObjectField, self).__init__(**kwargs)
...@@ -37,7 +31,11 @@ class LDObjectField(models.TextField): ...@@ -37,7 +31,11 @@ class LDObjectField(models.TextField):
if not isinstance(value, basestring): if not isinstance(value, basestring):
return value return value
if value == None: return self.from_db_value(value)
# noinspection PyUnusedLocal
def from_db_value(self, value, *args, **kwargs):
if value is None:
return None return None
# in some cases, we're loading an existing array or dict, # in some cases, we're loading an existing array or dict,
...@@ -50,8 +48,8 @@ class LDObjectField(models.TextField): ...@@ -50,8 +48,8 @@ class LDObjectField(models.TextField):
else: else:
parsed_value = value parsed_value = value
if parsed_value != None: if parsed_value is not None:
"we give the wrapped object back because we're not dealing with serialization types" # we give the wrapped object back because we're not dealing with serialization types
return_val = LDObject.fromDict(parsed_value, type_hint=self.type_hint).wrapped_obj return_val = LDObject.fromDict(parsed_value, type_hint=self.type_hint).wrapped_obj
return return_val return return_val
else: else:
...@@ -62,7 +60,7 @@ class LDObjectField(models.TextField): ...@@ -62,7 +60,7 @@ class LDObjectField(models.TextField):
if isinstance(value, basestring): if isinstance(value, basestring):
return value return value
if value == None: if value is None:
return None return None
# instantiate the proper LDObject to dump it appropriately # instantiate the proper LDObject to dump it appropriately
...@@ -71,4 +69,4 @@ class LDObjectField(models.TextField): ...@@ -71,4 +69,4 @@ class LDObjectField(models.TextField):
def value_to_string(self, obj): def value_to_string(self, obj):
value = self._get_val_from_obj(obj) value = self._get_val_from_obj(obj)
return self.get_db_prep_value(value) return self.get_db_prep_value(value, None)
...@@ -77,7 +77,7 @@ class Voter(LegacyObject): ...@@ -77,7 +77,7 @@ class Voter(LegacyObject):
""" """
depending on whether the voter is aliased, use different fields depending on whether the voter is aliased, use different fields
""" """
if self.wrapped_obj.alias != None: if self.wrapped_obj.alias is not None:
return super(Voter, self).toDict(self.ALIASED_VOTER_FIELDS, complete = complete) return super(Voter, self).toDict(self.ALIASED_VOTER_FIELDS, complete = complete)
else: else:
return super(Voter,self).toDict(complete = complete) return super(Voter,self).toDict(complete = complete)
......
...@@ -7,14 +7,14 @@ Ben Adida ...@@ -7,14 +7,14 @@ Ben Adida
""" """
import datetime import datetime
import io
import random
import uuid
import bleach import bleach
import copy import copy
import csv import csv
import io
import random
import unicodecsv import unicodecsv
import uuid
from django.conf import settings from django.conf import settings
from django.db import models, transaction from django.db import models, transaction
...@@ -22,8 +22,8 @@ from crypto import algs, utils ...@@ -22,8 +22,8 @@ from crypto import algs, utils
from helios import datatypes from helios import datatypes
from helios import utils as heliosutils from helios import utils as heliosutils
from helios.datatypes.djangofield import LDObjectField from helios.datatypes.djangofield import LDObjectField
from helios_auth.jsonfield import JSONField
# useful stuff in helios_auth # useful stuff in helios_auth
from helios_auth.jsonfield import JSONField
from helios_auth.models import User, AUTH_SYSTEMS from helios_auth.models import User, AUTH_SYSTEMS
......
...@@ -4,11 +4,11 @@ taken from ...@@ -4,11 +4,11 @@ taken from
http://www.djangosnippets.org/snippets/377/ http://www.djangosnippets.org/snippets/377/
""" """
import datetime, json import json
from django.db import models from django.core.exceptions import ValidationError
from django.db.models import signals
from django.conf import settings
from django.core.serializers.json import DjangoJSONEncoder from django.core.serializers.json import DjangoJSONEncoder
from django.db import models
class JSONField(models.TextField): class JSONField(models.TextField):
""" """
...@@ -18,9 +18,6 @@ class JSONField(models.TextField): ...@@ -18,9 +18,6 @@ class JSONField(models.TextField):
deserialization_params added on 2011-01-09 to provide additional hints at deserialization time deserialization_params added on 2011-01-09 to provide additional hints at deserialization time
""" """
# Used so to_python() is called
__metaclass__ = models.SubfieldBase
def __init__(self, json_type=None, deserialization_params=None, **kwargs): def __init__(self, json_type=None, deserialization_params=None, **kwargs):
self.json_type = json_type self.json_type = json_type
self.deserialization_params = deserialization_params self.deserialization_params = deserialization_params
...@@ -36,13 +33,17 @@ class JSONField(models.TextField): ...@@ -36,13 +33,17 @@ class JSONField(models.TextField):
if isinstance(value, dict) or isinstance(value, list): if isinstance(value, dict) or isinstance(value, list):
return value return value
if value == "" or value == None: return self.from_db_value(value)
# noinspection PyUnusedLocal
def from_db_value(self, value, *args, **kwargs):
if value == "" or value is None:
return None return None
try: try:
parsed_value = json.loads(value) parsed_value = json.loads(value)
except: except Exception as e:
raise Exception("not JSON") raise ValidationError("Received value is not JSON", e)
if self.json_type and parsed_value: if self.json_type and parsed_value:
parsed_value = self.json_type.fromJSONDict(parsed_value, **self.deserialization_params) parsed_value = self.json_type.fromJSONDict(parsed_value, **self.deserialization_params)
...@@ -57,7 +58,7 @@ class JSONField(models.TextField): ...@@ -57,7 +58,7 @@ class JSONField(models.TextField):
if isinstance(value, basestring): if isinstance(value, basestring):
return value return value
if value == None: if value is None:
return None return None
if self.json_type and isinstance(value, self.json_type): if self.json_type and isinstance(value, self.json_type):
...@@ -70,5 +71,4 @@ class JSONField(models.TextField): ...@@ -70,5 +71,4 @@ class JSONField(models.TextField):
def value_to_string(self, obj): def value_to_string(self, obj):
value = self._get_val_from_obj(obj) value = self._get_val_from_obj(obj)
return self.get_db_prep_value(value) return self.get_db_prep_value(value, None)
...@@ -7,9 +7,9 @@ Ben Adida ...@@ -7,9 +7,9 @@ Ben Adida
(ben@adida.net) (ben@adida.net)
""" """
from django.db import models from django.db import models
from jsonfield import JSONField
from auth_systems import AUTH_SYSTEMS from auth_systems import AUTH_SYSTEMS
from jsonfield import JSONField
# an exception to catch when a user is no longer authenticated # an exception to catch when a user is no longer authenticated
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment