From fa9b1bef2e59958d0032e2beb60d622b7cb1b0a7 Mon Sep 17 00:00:00 2001
From: Marco Ciotola <848222@stud.unive.it>
Date: Mon, 4 Mar 2019 22:58:37 +0100
Subject: [PATCH] [DJ1.10] SubfieldBase has been deprecated. Use
 Field.from_db_value instead.

---
 helios/datatypes/__init__.py    | 24 ++++++++++++------------
 helios/datatypes/djangofield.py | 24 +++++++++++-------------
 helios/datatypes/legacy.py      |  2 +-
 helios/models.py                |  8 ++++----
 helios_auth/jsonfield.py        | 28 ++++++++++++++--------------
 helios_auth/models.py           |  2 +-
 6 files changed, 43 insertions(+), 45 deletions(-)

diff --git a/helios/datatypes/__init__.py b/helios/datatypes/__init__.py
index b0ede25..93bca24 100644
--- a/helios/datatypes/__init__.py
+++ b/helios/datatypes/__init__.py
@@ -33,7 +33,7 @@ from helios.crypto import utils as cryptoutils
 ## utility function
 ##
 def recursiveToDict(obj):
-    if obj == None:
+    if obj is None:
         return None
 
     if type(obj) == list:
@@ -53,7 +53,7 @@ def get_class(datatype):
     dynamic_module = __import__(".".join(parsed_datatype[:-1]), globals(), locals(), [], level=-1)
     
     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
     try:
@@ -130,7 +130,7 @@ class LDObject(object):
             raise Exception("no datatype found")
 
         # nulls
-        if obj == None:
+        if obj is None:
             return None
 
         # the class
@@ -171,7 +171,7 @@ class LDObject(object):
                 self.structured_fields[f] = sub_ld_object
 
                 # 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)
                 else:
                     self._setattr_wrapped(f, None)
@@ -190,7 +190,7 @@ class LDObject(object):
         fields = self.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
 
         for f in (alternate_fields or fields):
@@ -214,7 +214,7 @@ class LDObject(object):
     @classmethod
     def fromDict(cls, d, type_hint=None):
         # null objects
-        if d == None:
+        if d is None:
             return None
 
         # the LD type is either in d or in type_hint
@@ -248,11 +248,11 @@ class LDObject(object):
         """
         process some fields on the way into the object
         """
-        if field_value == None:
+        if field_value is None:
             return None
       
         val = self._process_value_in(field_name, field_value)
-        if val != None:
+        if val is not None:
             return val
         else:
             return field_value
@@ -264,11 +264,11 @@ class LDObject(object):
         """
         process some fields on the way out of the object
         """
-        if field_value == None:
+        if field_value is None:
             return None
       
         val = self._process_value_out(field_name, field_value)
-        if val != None:
+        if val is not None:
             return val
         else:
             return field_value
@@ -278,9 +278,9 @@ class LDObject(object):
     
     def __eq__(self, other):
         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):
diff --git a/helios/datatypes/djangofield.py b/helios/datatypes/djangofield.py
index e0eb1b4..05f8cf3 100644
--- a/helios/datatypes/djangofield.py
+++ b/helios/datatypes/djangofield.py
@@ -6,15 +6,12 @@ http://www.djangosnippets.org/snippets/377/
 and adapted to LDObject
 """
 
-import datetime
 import json
 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
 
+
 class LDObjectField(models.TextField):
     """
     LDObject is a generic textfield that neatly serializes/unserializes
@@ -23,9 +20,6 @@ class LDObjectField(models.TextField):
     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):
         self.type_hint = type_hint
         super(LDObjectField, self).__init__(**kwargs)
@@ -37,7 +31,11 @@ class LDObjectField(models.TextField):
         if not isinstance(value, basestring):
             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
 
         # in some cases, we're loading an existing array or dict,
@@ -50,9 +48,9 @@ class LDObjectField(models.TextField):
         else:
             parsed_value = value
 
-        if parsed_value != None:
-            "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
+        if parsed_value is not None:
+            # 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 return_val
         else:
             return None
@@ -62,7 +60,7 @@ class LDObjectField(models.TextField):
         if isinstance(value, basestring):
             return value
 
-        if value == None:
+        if value is None:
             return None
 
         # instantiate the proper LDObject to dump it appropriately
@@ -71,4 +69,4 @@ class LDObjectField(models.TextField):
 
     def value_to_string(self, obj):
         value = self._get_val_from_obj(obj)
-        return self.get_db_prep_value(value)
+        return self.get_db_prep_value(value, None)
diff --git a/helios/datatypes/legacy.py b/helios/datatypes/legacy.py
index c0a24ff..d469b44 100644
--- a/helios/datatypes/legacy.py
+++ b/helios/datatypes/legacy.py
@@ -77,7 +77,7 @@ class Voter(LegacyObject):
         """
         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)
         else:
             return super(Voter,self).toDict(complete = complete)
diff --git a/helios/models.py b/helios/models.py
index 5201ebc..479c0bb 100644
--- a/helios/models.py
+++ b/helios/models.py
@@ -7,14 +7,14 @@ Ben Adida
 """
 
 import datetime
-import io
-import random
-import uuid
 
 import bleach
 import copy
 import csv
+import io
+import random
 import unicodecsv
+import uuid
 from django.conf import settings
 from django.db import models, transaction
 
@@ -22,8 +22,8 @@ from crypto import algs, utils
 from helios import datatypes
 from helios import utils as heliosutils
 from helios.datatypes.djangofield import LDObjectField
-from helios_auth.jsonfield import JSONField
 # useful stuff in helios_auth
+from helios_auth.jsonfield import JSONField
 from helios_auth.models import User, AUTH_SYSTEMS
 
 
diff --git a/helios_auth/jsonfield.py b/helios_auth/jsonfield.py
index 0104ce4..34cecf7 100644
--- a/helios_auth/jsonfield.py
+++ b/helios_auth/jsonfield.py
@@ -4,11 +4,11 @@ taken from
 http://www.djangosnippets.org/snippets/377/
 """
 
-import datetime, json
-from django.db import models
-from django.db.models import signals
-from django.conf import settings
+import json
+from django.core.exceptions import ValidationError
 from django.core.serializers.json import DjangoJSONEncoder
+from django.db import models
+
 
 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
     """
 
-    # Used so to_python() is called
-    __metaclass__ = models.SubfieldBase
-
     def __init__(self, json_type=None, deserialization_params=None, **kwargs):
         self.json_type = json_type
         self.deserialization_params = deserialization_params
@@ -36,17 +33,21 @@ class JSONField(models.TextField):
         if isinstance(value, dict) or isinstance(value, list):
             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
 
         try:
             parsed_value = json.loads(value)
-        except:
-            raise Exception("not JSON")
+        except Exception as e:
+            raise ValidationError("Received value is not JSON", e)
 
         if self.json_type and parsed_value:
             parsed_value = self.json_type.fromJSONDict(parsed_value, **self.deserialization_params)
-                
+
         return parsed_value
 
     # we should never look up by JSON field anyways.
@@ -57,7 +58,7 @@ class JSONField(models.TextField):
         if isinstance(value, basestring):
             return value
 
-        if value == None:
+        if value is None:
             return None
 
         if self.json_type and isinstance(value, self.json_type):
@@ -70,5 +71,4 @@ class JSONField(models.TextField):
 
     def value_to_string(self, obj):
         value = self._get_val_from_obj(obj)
-        return self.get_db_prep_value(value)        
-
+        return self.get_db_prep_value(value, None)
diff --git a/helios_auth/models.py b/helios_auth/models.py
index b5ed397..fb050d2 100644
--- a/helios_auth/models.py
+++ b/helios_auth/models.py
@@ -7,9 +7,9 @@ Ben Adida
 (ben@adida.net)
 """
 from django.db import models
-from jsonfield import JSONField
 
 from auth_systems import AUTH_SYSTEMS
+from jsonfield import JSONField
 
 
 # an exception to catch when a user is no longer authenticated
-- 
GitLab