Browse Source

[not tested] Added 3 more fieldtypes : file, datetime, text

Yann Weber 9 years ago
parent
commit
60e23b4946

+ 3
- 1
EditorialModel/fields.py View File

35
     ## Instanciate a new EmField
35
     ## Instanciate a new EmField
36
     # @todo define and test type for icon and fieldtype
36
     # @todo define and test type for icon and fieldtype
37
     # @warning nullable == True by default
37
     # @warning nullable == True by default
38
-    def __init__(self, model, uid, name, fieldgroup_id, optional=False, internal=False, rel_field_id=None, icon='0', string=None, help_text=None, date_update=None, date_create=None, rank=None, nullable = True, default = None, **kwargs):
38
+    def __init__(self, model, uid, name, fieldgroup_id, optional=False, internal=False, rel_field_id=None, icon='0', string=None, help_text=None, date_update=None, date_create=None, rank=None, nullable = True, default = None, uniq = False, **kwargs):
39
 
39
 
40
         self.fieldgroup_id = fieldgroup_id
40
         self.fieldgroup_id = fieldgroup_id
41
         self.check_type('fieldgroup_id', int)
41
         self.check_type('fieldgroup_id', int)
47
         self.check_type('rel_field_id', (int, type(None)))
47
         self.check_type('rel_field_id', (int, type(None)))
48
         self.icon = icon
48
         self.icon = icon
49
 
49
 
50
+        #Field type elements
50
         self.nullable = nullable
51
         self.nullable = nullable
51
         self.default = default
52
         self.default = default
53
+        self.uniq = uniq
52
 
54
 
53
         self.options = kwargs
55
         self.options = kwargs
54
 
56
 

+ 3
- 1
EditorialModel/fieldtypes/char.py View File

5
 class EmFieldChar(EmField):
5
 class EmFieldChar(EmField):
6
     
6
     
7
     ftype = 'char'
7
     ftype = 'char'
8
-
8
+    
9
+    ## @brief A char field
10
+    # @brief max_length int : The maximum length of this field
9
     def __init__(self, max_length=64, **kwargs):
11
     def __init__(self, max_length=64, **kwargs):
10
         self.max_length = max_length
12
         self.max_length = max_length
11
         super(EmFieldChar, self).__init__(**kwargs)
13
         super(EmFieldChar, self).__init__(**kwargs)

+ 17
- 0
EditorialModel/fieldtypes/datetime.py View File

1
+#-*- coding: utf-8 -*-
2
+
3
+from EditorialModel.fields import EmField
4
+
5
+class EmFieldDatetime(EmField):
6
+    
7
+    ftype = 'datetime'
8
+
9
+    ## @brief A datetime field
10
+    # @param now_on_update bool : If true the date is set to NOW on update
11
+    # @param now_on_create bool : If true the date is set to NEW on creation
12
+    def __init__(self, now_on_update = False, now_on_create = False, **kwargs):
13
+        self.now_on_update = now_on_update
14
+        self.now_on_create = now_on_create
15
+        super(EmFieldDatetime, self).__init__(**kwargs)
16
+
17
+fclass = EmFieldDatetime

+ 15
- 0
EditorialModel/fieldtypes/file.py View File

1
+#-*- coding: utf-8 -*-
2
+
3
+from EditorialModel.fields import EmField
4
+
5
+class EmFieldFile(EmField):
6
+    
7
+    ftype = 'file'
8
+    
9
+    ## @brief A char field
10
+    # @brief max_length int : The maximum length of this field
11
+    def __init__(self, upload_path = None,**kwargs):
12
+        self.upload_path = upload_path
13
+        super(EmFieldChar, self).__init__(**kwargs)
14
+
15
+fclass=EmFieldFile

+ 3
- 0
EditorialModel/fieldtypes/regexchar.py View File

5
 
5
 
6
 class EmFieldCharRegex(EmFieldChar):
6
 class EmFieldCharRegex(EmFieldChar):
7
     
7
     
8
+    ## @brief A char field validated with a regex
9
+    # @param regex str : a regex string (passed as argument to re.compile() )
10
+    # @param max_length int : the maximum length for this field
8
     def __init__(self, regex = '', **kwargs):
11
     def __init__(self, regex = '', **kwargs):
9
         self.regex = regex
12
         self.regex = regex
10
         v_re = re.compile(regex) #trigger an error if invalid regex
13
         v_re = re.compile(regex) #trigger an error if invalid regex

+ 12
- 0
EditorialModel/fieldtypes/text.py View File

1
+#-*- coding: utf-8 -*-
2
+
3
+from EditorialModel.fields import EmField
4
+
5
+class EmFieldText(EmField):
6
+    
7
+    ftype = 'text'
8
+
9
+    def __init__(self, **kwargs):
10
+        super(EmFieldText, self).__init__(**kwargs)
11
+
12
+fclass = EmFieldText

+ 16
- 4
EditorialModel/migrationhandler/django.py View File

278
     ## @brief Return a good django field type given a field
278
     ## @brief Return a good django field type given a field
279
     # @param f EmField : an EmField object
279
     # @param f EmField : an EmField object
280
     # @param assoc_comp EmComponent : The associated component (type or class)
280
     # @param assoc_comp EmComponent : The associated component (type or class)
281
+    # @return A django field instance
282
+    # @note The manytomany fields created with the rel2type field has no related_name associated to it
281
     def field_to_django(self, f, assoc_comp):
283
     def field_to_django(self, f, assoc_comp):
282
 
284
 
285
+        #Building the args dictionnary for django field instanciation
283
         args = dict()
286
         args = dict()
284
         args['null'] = f.nullable
287
         args['null'] = f.nullable
285
         if not (f.default is None):
288
         if not (f.default is None):
287
         v_fun = f.validation_function(raise_e = ValidationError)
290
         v_fun = f.validation_function(raise_e = ValidationError)
288
         if v_fun:
291
         if v_fun:
289
             args['validators'] = [v_fun]
292
             args['validators'] = [v_fun]
290
-
291
-        if f.ftype == 'char':
293
+        if f.uniq:
294
+            args['unique'] = True
295
+        
296
+        # Field instanciation
297
+        if f.ftype == 'char': #varchar field
292
             args['max_length'] = f.max_length
298
             args['max_length'] = f.max_length
293
             return models.CharField(**args)
299
             return models.CharField(**args)
294
-        elif f.ftype == 'int':
300
+        elif f.ftype == 'int': #integer field
295
             return models.IntegerField(**args)
301
             return models.IntegerField(**args)
296
-        elif f.ftype == 'rel2type':
302
+        elif f.ftype == 'text': #text field
303
+            return models.TextField(**args)
304
+        elif f.ftype == 'datetime': #Datetime field
305
+            args['auto_now'] = f.now_on_update
306
+            args['auto_now_add'] = f.now_on_create
307
+            return models.DateTimeField(**args)
308
+        elif f.ftype == 'rel2type': #Relation to type
297
 
309
 
298
             if assoc_comp == None:
310
             if assoc_comp == None:
299
                 raise RuntimeError("Rel2type field in a rel2type table is not allowed")
311
                 raise RuntimeError("Rel2type field in a rel2type table is not allowed")

Loading…
Cancel
Save