Browse Source

Added a check method to EmComponent and EmClass

EmComponent::check() always return True but rearrange ranks if they are not continuous
Yann Weber 9 years ago
parent
commit
a2529cf8eb
3 changed files with 30 additions and 53 deletions
  1. 7
    6
      EditorialModel/classes.py
  2. 17
    46
      EditorialModel/components.py
  3. 6
    1
      EditorialModel/model.py

+ 7
- 6
EditorialModel/classes.py View File

@@ -13,7 +13,6 @@ import EditorialModel
13 13
 # @todo sortcolumn handling
14 14
 class EmClass(EmComponent):
15 15
 
16
-    table = 'em_class'
17 16
     ranked_in = 'classtype'
18 17
 
19 18
     ## @brief Specific EmClass fields
@@ -23,11 +22,13 @@ class EmClass(EmComponent):
23 22
         ('icon', ftypes.EmField_icon),
24 23
         ('sortcolumn', ftypes.EmField_char)
25 24
     ]
26
-
27
-    @property
28
-    ## @brief Return the table name used to stores data on this class
29
-    def class_table_name(self):
30
-        return self.name
25
+    
26
+    ## Check if the EmComponent is valid
27
+    # @return True if valid False if not
28
+    def check(self):
29
+        super(EmClass, self).check()
30
+        return True
31
+        
31 32
 
32 33
     ## @brief Delete a class if it's ''empty''
33 34
     # If a class has no fieldgroups delete it

+ 17
- 46
EditorialModel/components.py View File

@@ -116,52 +116,18 @@ class EmComponent(object):
116 116
 
117 117
         super(EmComponent, self).__setattr__('deleted', False)
118 118
 
119
-    ## Insert a new component
120
-    #
121
-    # This function create and assign a new UID and handle the date_create and date_update values
122
-    # @warning There is a mandatory argument dbconf that indicate wich database configuration to use
123
-    # @param **kwargs : Names arguments representing object properties
124
-    # @return An instance of the created component
125
-    # @throw TypeError if an element of kwargs isn't a valid object propertie or if a mandatory argument is missing
126
-    # @throw RuntimeError if the creation fails at database level
127
-    # @todo Check that every mandatory _fields are given in args
128
-    # @todo Stop using datetime.datetime.utcnow() for date_update and date_create init
129
-    @classmethod
130
-    def create(cls, **kwargs):
131
-        #Checking for invalid arguments
132
-        valid_args = [ name for name,_ in (cls._fields + EmComponent._fields)]
133
-
134
-        for argname in kwargs:
135
-            if argname in ['date_update', 'date_create', 'rank', 'uid']:  # Automatic properties
136
-                raise TypeError("Invalid argument : " + argname)
137
-            elif argname not in valid_args:
138
-                raise TypeError("Unexcepted keyword argument '" + argname + "' for " + cls.__name__ + " creation")
139
-
140
-        #Check uniq names constraint
141
-        try:
142
-            name = kwargs['name']
143
-            exist = cls(name)
144
-            for kname in kwargs:
145
-                if not (getattr(exist, kname) == kwargs[kname]):
146
-                    raise EmComponentExistError("An " + cls.__name__ + " named " + name + " allready exists with a different " + kname)
147
-            logger.info("Trying to create an " + cls.__name__ + " that allready exist with same attribute. Returning the existing one")
148
-            return exist
149
-        except EmComponentNotExistError:
150
-            pass
151
-
152
-        kwargs['uid'] = cls.new_uid()
153
-        kwargs['date_update'] = kwargs['date_create'] = datetime.datetime.utcnow()
154
-
155
-        kwargs['rank'] = cls._get_max_rank( kwargs[cls.ranked_in], dbe )+1
156
-
157
-        return cls(kwargs['name'], dbconf)
158
-
159
-    ## @brief Get the maximum rank given an EmComponent child class and a ranked_in filter
160
-    # @param ranked_in_value mixed: The rank "family"
161
-    # @return -1 if no EmComponent found else return an integer >= 0
162
-    @classmethod
163
-    def _get_max_rank(cls, ranked_in_value):
164
-        pass
119
+    ## Check if the EmComponent is valid
120
+    # This function has to check that rank are correct and continuous other checks are made in childs classes
121
+    # @warning Hardcoded minimum rank
122
+    # @warning Rank modified by _fields['rank'].value
123
+    # @return True
124
+    def check(self):
125
+        if self.get_max_rank() > len(self.same_rank_group()):
126
+            #Non continuous ranks
127
+            for i, component in enumerate(self.same_rank_group()):
128
+                component._fields['rank'].value = i + 1
129
+        # No need to sort again here
130
+        return True
165 131
 
166 132
     ## @brief Get the maximum rank given an EmComponent child class and a ranked_in filter
167 133
     # @return A positive integer or -1 if no components
@@ -178,7 +144,12 @@ class EmComponent(object):
178 144
 
179 145
     ## Set a new rank for this component
180 146
     # @note This function assume that ranks are properly set from 1 to x with no gap
147
+    #
148
+    # @warning Hardcoded minimum rank
149
+    # @warning Rank modified by _fields['rank'].value
150
+    #
181 151
     # @param new_rank int: The new rank
152
+    #
182 153
     # @throw TypeError If bad argument type
183 154
     # @throw ValueError if out of bound value
184 155
     def set_rank(self, new_rank):

+ 6
- 1
EditorialModel/model.py View File

@@ -47,6 +47,8 @@ class Model(object):
47 47
     ## Loads the structure of the Editorial Model
48 48
     #
49 49
     # Gets all the objects contained in that structure and creates a dict indexed by their uids
50
+    # @todo Change the thrown exception when a components check fails
51
+    # @throw ValueError When a component class don't exists
50 52
     def load(self):
51 53
         data = self.backend.load()
52 54
         for uid, component in data.items():
@@ -64,7 +66,10 @@ class Model(object):
64 66
         for component_class in Model.components_class:
65 67
             self.sort_components(component_class)
66 68
 
67
-        # TODO : check integrity
69
+        #Check integrity
70
+        for uid, component in self._components['uids'].items():
71
+            if not component.check():
72
+                raise RuntimeError("EmComponent with uid '"+str(uid)+"' is not valid !!!")
68 73
 
69 74
     ## Saves data using the current backend
70 75
     def save(self):

Loading…
Cancel
Save