Browse Source

Added the mapping of the django models field types in the EmField class

Roland Haroutiounian 9 years ago
parent
commit
ce07a938b7
1 changed files with 26 additions and 2 deletions
  1. 26
    2
      EditorialModel/fields.py

+ 26
- 2
EditorialModel/fields.py View File

@@ -3,7 +3,7 @@
3 3
 from EditorialModel.components import EmComponent
4 4
 from EditorialModel.exceptions import EmComponentCheckError
5 5
 import EditorialModel
6
-
6
+from django.db import models
7 7
 
8 8
 ## EmField (Class)
9 9
 #
@@ -12,9 +12,25 @@ class EmField(EmComponent):
12 12
 
13 13
     ranked_in = 'fieldgroup_id'
14 14
 
15
+    fieldtypes = {
16
+        'int': models.IntegerField,
17
+        'integer': models.IntegerField,
18
+        'bigint': models.BigIntegerField,
19
+        'smallint': models.SmallIntegerField,
20
+        'boolean': models.BooleanField,
21
+        'bool': models.BooleanField,
22
+        'float': models.FloatField,
23
+        'char': models.CharField,
24
+        'varchar': models.CharField,
25
+        'text': models.TextField,
26
+        'time': models.TimeField,
27
+        'date': models.DateField,
28
+        'datetime': models.DateTimeField,
29
+    }
30
+
15 31
     ## Instanciate a new EmField
16 32
     # @todo define and test type for icon and fieldtype
17
-    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):
33
+    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, **kwargs):
18 34
 
19 35
         self.fieldgroup_id = fieldgroup_id
20 36
         self.check_type('fieldgroup_id', int)
@@ -28,6 +44,8 @@ class EmField(EmComponent):
28 44
         self.rel_field_id = rel_field_id
29 45
         self.check_type('rel_field_id', (int, type(None)))
30 46
         self.icon = icon
47
+        self.options = kwargs
48
+
31 49
         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)
32 50
 
33 51
     ## Check if the EmField is valid
@@ -45,3 +63,9 @@ class EmField(EmComponent):
45 63
     # @todo Check if unconditionnal deletion is correct
46 64
     def delete_check(self):
47 65
         return True
66
+
67
+    def to_django(self):
68
+        if self.fieldtype == 'boolean' and ('nullable' in self.options and self.options['nullable'] == 1):
69
+            return models.NullBooleanField(**self.options)
70
+        
71
+        return self.fieldtypes[self.fieldtype](**self.options)

Loading…
Cancel
Save