Browse Source

EmComponent: make the __hash__ function always return the same hash !

ArnAud 9 years ago
parent
commit
90fcecd015
2 changed files with 20 additions and 27 deletions
  1. 20
    20
      EditorialModel/components.py
  2. 0
    7
      EditorialModel/test/test_component.py

+ 20
- 20
EditorialModel/components.py View File

@@ -70,23 +70,6 @@ class EmComponent(object):
70 70
             except AttributeError: pass
71 71
         uname += '_'+self.name
72 72
         return uname
73
-        
74
-    ## @brief dumps attr for serialization
75
-    def dumps(self):
76
-        #attr =  {fname: fval for fname, fval in self.__dict__.items() if not (fname.startswith('_'))}
77
-        attr = self.attr_dump
78
-        if 'model' in attr:
79
-            del(attr['model'])
80
-        for attr_f in attr:
81
-            if isinstance(attr[attr_f], EmComponent):
82
-                attr[attr_f] = attr[attr_f].uid
83
-            elif isinstance(attr[attr_f], MlString):
84
-                attr[attr_f] = attr[attr_f].__str__()
85
-        if isinstance(self, EditorialModel.fields.EmField):
86
-            attr['component'] = 'EmField'
87
-        else:
88
-            attr['component'] = self.__class__.__name__
89
-        return attr
90 73
 
91 74
     ## @brief This function has to be called after the instanciation, checks, and init manipulations are done
92 75
     # @note Create a new attribute _inited that allow __setattr__ to know if it has or not to call the migration handler
@@ -117,9 +100,26 @@ class EmComponent(object):
117 100
     ## @brief Hash function that allows to compare two EmComponent
118 101
     # @return EmComponent+ClassName+uid
119 102
     def __hash__(self):
120
-        component_dump = self.dumps()
121
-        del component_dump['date_create']
122
-        del component_dump['date_update']
103
+        # flatten list of attributes of the component to an ordered list
104
+        # so every time we have the same string representation
105
+        attributes_dump = self.attr_dump
106
+        ordered_attributes = sorted(list(attributes_dump.keys()))
107
+
108
+        component_dump = []
109
+        for attr_name in ordered_attributes:
110
+            if isinstance(attributes_dump[attr_name], EmComponent):
111
+                value = attributes_dump[attr_name].uid
112
+            elif isinstance(attributes_dump[attr_name], MlString):
113
+                value = attributes_dump[attr_name].__str__()
114
+            elif isinstance(attributes_dump[attr_name], datetime.datetime):
115
+                continue
116
+            else:
117
+                value = attributes_dump[attr_name]
118
+            component_dump.append((attr_name, value))
119
+
120
+        component_name = 'EmField' if isinstance(self, EditorialModel.fields.EmField) else self.__class__.__name__
121
+        component_dump.append(('component', component_name))
122
+
123 123
         return int(hashlib.md5(str(component_dump).encode('utf-8')).hexdigest(), 16)
124 124
 
125 125
     ## @brief Test if two EmComponent are "equals"

+ 0
- 7
EditorialModel/test/test_component.py View File

@@ -105,13 +105,6 @@ class TestEmComponent(unittest.TestCase):
105 105
             for dmp_f in dmp:
106 106
                 self.assertFalse(dmp_f.startswith('_'))
107 107
 
108
-
109
-            dmp = comp.dumps()
110
-            for dmp_f, dmp_v in dmp.items():
111
-                self.assertFalse(isinstance(dmp_v, EmComponent))
112
-                self.assertFalse(isinstance(dmp_v, Model))
113
-                self.assertFalse(isinstance(dmp_v, MlString))
114
-    
115 108
     def test_uniq_name(self):
116 109
         """ Testing uniq_name method """
117 110
         names_l = []

Loading…
Cancel
Save