Browse Source

Added type check in EmComponent and childs class type check. Added some cast functions to Backend

Yann Weber 9 years ago
parent
commit
405dd318dd

+ 24
- 1
EditorialModel/backend/json_backend.py View File

@@ -4,7 +4,20 @@
4 4
 # Load representation of an EditorialModel from a json file
5 5
 
6 6
 import json
7
+import datetime
8
+from Lodel.utils.mlstring import MlString
7 9
 
10
+def date_cast(date):
11
+    if len(date):
12
+        return datetime.datetime(date)
13
+    else:
14
+        return None
15
+
16
+def int_or_none(i):
17
+    if len(i):
18
+        return int(i)
19
+    else:
20
+        return None
8 21
 
9 22
 ## Manages a Json file based backend structure
10 23
 class EmBackendJson(object):
@@ -13,9 +26,19 @@ class EmBackendJson(object):
13 26
         'uid' : int,
14 27
         'rank' : int,
15 28
         'class_id' : int,
16
-        'fieldgroup_id' : int
29
+        'fieldgroup_id' : int,
30
+        'rel_to_type_id' : int_or_none,
31
+        'rel_field_id' : int_or_none,
32
+        'optional' : bool,
33
+        'internal': bool,
34
+        'string' : MlString.load,
35
+        'help_text' : MlString.load,
36
+        'date_create' : date_cast,
37
+        'date_update' : date_cast,
17 38
     }
18 39
 
40
+        
41
+
19 42
     ## Constructor
20 43
     #
21 44
     # @param json_file str: path to the json_file used as data source

+ 9
- 0
EditorialModel/classes.py View File

@@ -4,6 +4,7 @@
4 4
 # @see EditorialModel::classes::EmClass
5 5
 
6 6
 from EditorialModel.components import EmComponent
7
+from EditorialModel.classtypes import EmClassType
7 8
 import EditorialModel.fieldtypes as ftypes
8 9
 import EditorialModel
9 10
 
@@ -25,7 +26,15 @@ class EmClass(EmComponent):
25 26
     ]
26 27
 
27 28
     ## EmClass instanciation
29
+    # @todo Classtype initialisation and test is not good EmClassType should give an answer or something like that
30
+    # @todo defines types check for icon and sortcolumn
28 31
     def __init__(self, model, uid, name, classtype, icon = '0', sortcolumn = 'rank', string = None, help_text = None, date_update = None, date_create = None, rank = None):
32
+        
33
+        try:
34
+            getattr(EmClassType, classtype)
35
+        except AttributeError:
36
+            raise AttributeError("Unknown classtype '%s'" %classtype)
37
+
29 38
         self.classtype = classtype
30 39
         self.icon = icon
31 40
         self.sortcolumn = 'rank'

+ 20
- 1
EditorialModel/components.py View File

@@ -27,16 +27,35 @@ class EmComponent(object):
27 27
         if type(self) == EmComponent:
28 28
            raise NotImplementedError('Abstract class')
29 29
  
30
-        self.model = model
30
+        self.model = model # AHAH cannot check type without importing Model ? 
31
+
31 32
         self.uid = uid
33
+        self.check_type('uid', int)
32 34
         self.name = name
35
+        self.check_type('name', str)
33 36
         self.string = MlString() if string is None else string
37
+        self.check_type('string', MlString)
34 38
         self.help_text = MlString() if help_text is None else help_text
39
+        self.check_type('help_text', MlString)
35 40
         self.date_update = datetime.datetime.now() if date_update is None else date_update #WARNING timezone !
41
+        self.check_type('date_update', datetime.datetime)
36 42
         self.date_create = datetime.datetime.now() if date_create is None else date_create #WARNING timezone !
43
+        self.check_type('date_create', datetime.datetime)
37 44
 
38 45
         #Handling specials ranks for component creation
39 46
         self.rank = rank
47
+
48
+        pass
49
+
50
+    ## Check the type of attribute named var_name
51
+    # @param var_name str : the attribute name
52
+    # @param excepted_type tuple|type : Tuple of type or a type 
53
+    # @throw AttributeError if wrong type detected
54
+    def check_type(self, var_name, excepted_type):
55
+        var = getattr(self, var_name)
56
+        
57
+        if not isinstance(var, excepted_type):
58
+            raise AttributeError("Excepted %s to be an %s but got %s instead" % (var_name, str(excepted_type), str(type(var))) )
40 59
         pass
41 60
 
42 61
     ## @brief Hash function that allows to compare two EmComponent

+ 2
- 0
EditorialModel/fieldgroups.py View File

@@ -15,7 +15,9 @@ class EmFieldGroup(EmComponent):
15 15
 
16 16
     ## EmFieldGroup instanciation
17 17
     def __init__(self, model, uid, name, class_id, string = None, help_text = None, date_update = None, date_create = None, rank = None):
18
+
18 19
         self.class_id = class_id
20
+        self.check_type('class_id', int)
19 21
         super(EmFieldGroup, self).__init__(model=model, uid=uid, name=name, string=string, help_text=help_text, date_update=date_update, date_create=date_create, rank=rank)
20 22
         pass
21 23
 

+ 8
- 0
EditorialModel/fields.py View File

@@ -11,13 +11,21 @@ class EmField(EmComponent):
11 11
 
12 12
     ranked_in = 'fieldgroup_id'
13 13
 
14
+    ## Instanciate a new EmField
15
+    # @todo define and test type for icon and fieldtype
14 16
     def __init__(self, model, uid, name, fieldgroup_id, fieldtype, optional = False, internal = False, rel_to_type_id = None, rel_field_id = None, icon = '0', string = None, help_text = None, date_update = None, date_create = None, rank = None):
17
+
15 18
         self.fieldgroup_id = fieldgroup_id
19
+        self.check_type('fieldgroup_id', int)
16 20
         self.fieldtype = fieldtype
17 21
         self.optional = optional
22
+        self.check_type('optional', bool)
18 23
         self.internal = internal
24
+        self.check_type('internal', bool)
19 25
         self.rel_to_type_id = rel_to_type_id
26
+        self.check_type('rel_to_type_id', (int, type(None)))
20 27
         self.rel_field_id = rel_field_id
28
+        self.check_type('rel_field_id', (int, type(None)))
21 29
         self.icon = icon
22 30
         super(EmField, self).__init__(model=model, uid=uid, name=name, string=string, help_text=help_text, date_update=date_update, date_create=date_create, rank=rank)
23 31
 

+ 14
- 0
EditorialModel/types.py View File

@@ -22,10 +22,24 @@ import EditorialModel.classes
22 22
 class EmType(EmComponent):
23 23
     ranked_in = 'class_id'
24 24
 
25
+    ## Instanciate a new EmType
26
+    # @todo define and check types for icon and sortcolumn
27
+    # @todo better check self.subordinates
25 28
     def __init__(self, model, uid, name, class_id, selected_fields = [], subordinates = [], icon = '0', sortcolumn = 'rank', string = None, help_text = None, date_update = None, date_create = None, rank = None):
26 29
         self.class_id = class_id
30
+        self.check_type('class_id', int)
27 31
         self.selected_fields = selected_fields
32
+        self.check_type('selected_fields', list)
33
+        for l_uid in self.selected_fields:
34
+            if not isinstance(l_uid, int):
35
+                raise AttributeError("Excepted selected_fields to be a list of integers, but found a +"+str(type(l_uid))+" in it")
36
+
28 37
         self.subordinates = subordinates
38
+        self.check_type('subordinates', list)
39
+        for l_uid in self.subordinates:
40
+            if not isinstance(l_uid[0], str) or not isinstance(l_uid[1], int):
41
+                raise AttributeError("Excepted selected_fields to be a list of nature,int !")
42
+
29 43
         self.icon = icon
30 44
         self.sortcolumn = sortcolumn
31 45
         super(EmType, self).__init__(model=model, uid=uid, name=name, string=string, help_text=help_text, date_update=date_update, date_create=date_create, rank=rank)

Loading…
Cancel
Save