Преглед на файлове

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

Yann Weber преди 9 години
родител
ревизия
60e23b4946

+ 3
- 1
EditorialModel/fields.py Целия файл

@@ -35,7 +35,7 @@ class EmField(EmComponent):
35 35
     ## Instanciate a new EmField
36 36
     # @todo define and test type for icon and fieldtype
37 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 40
         self.fieldgroup_id = fieldgroup_id
41 41
         self.check_type('fieldgroup_id', int)
@@ -47,8 +47,10 @@ class EmField(EmComponent):
47 47
         self.check_type('rel_field_id', (int, type(None)))
48 48
         self.icon = icon
49 49
 
50
+        #Field type elements
50 51
         self.nullable = nullable
51 52
         self.default = default
53
+        self.uniq = uniq
52 54
 
53 55
         self.options = kwargs
54 56
 

+ 3
- 1
EditorialModel/fieldtypes/char.py Целия файл

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

+ 17
- 0
EditorialModel/fieldtypes/datetime.py Целия файл

@@ -0,0 +1,17 @@
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 Целия файл

@@ -0,0 +1,15 @@
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 Целия файл

@@ -5,6 +5,9 @@ from EditorialModel.fieldtypes import EmFieldChar
5 5
 
6 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 11
     def __init__(self, regex = '', **kwargs):
9 12
         self.regex = regex
10 13
         v_re = re.compile(regex) #trigger an error if invalid regex

+ 12
- 0
EditorialModel/fieldtypes/text.py Целия файл

@@ -0,0 +1,12 @@
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 Целия файл

@@ -278,8 +278,11 @@ class DjangoMigrationHandler(object):
278 278
     ## @brief Return a good django field type given a field
279 279
     # @param f EmField : an EmField object
280 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 283
     def field_to_django(self, f, assoc_comp):
282 284
 
285
+        #Building the args dictionnary for django field instanciation
283 286
         args = dict()
284 287
         args['null'] = f.nullable
285 288
         if not (f.default is None):
@@ -287,13 +290,22 @@ class DjangoMigrationHandler(object):
287 290
         v_fun = f.validation_function(raise_e = ValidationError)
288 291
         if v_fun:
289 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 298
             args['max_length'] = f.max_length
293 299
             return models.CharField(**args)
294
-        elif f.ftype == 'int':
300
+        elif f.ftype == 'int': #integer field
295 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 310
             if assoc_comp == None:
299 311
                 raise RuntimeError("Rel2type field in a rel2type table is not allowed")

Loading…
Отказ
Запис