Browse Source

EmField: create, save and load

ArnAud 9 years ago
parent
commit
58303db9fa
2 changed files with 73 additions and 54 deletions
  1. 65
    54
      EditorialModel/fields.py
  2. 8
    0
      EditorialModel/fieldtypes.py

+ 65
- 54
EditorialModel/fields.py View File

@@ -1,78 +1,89 @@
1 1
 #-*- coding: utf-8 -*-
2 2
 
3
-from EditorialModel.components import EmComponent
3
+from EditorialModel.components import EmComponent, EmComponentNotExistError
4
+from Database.sqlobject import SqlObject
5
+
6
+import EditorialModel
4 7
 
5 8
 """Represent one data for a lodel2 document"""
6 9
 class EmField(EmComponent):
7 10
 
8
-    
9
-    def __init__(id_or_name):
10
-        """ Instanciate an EmType with data fetched from db
11
+    table = 'em_field'
12
+
13
+    def __init__(self, id_or_name):
14
+        """ Instanciate an EmField with data fetched from db
11 15
             @param id_or_name str|int: Identify the EmType by name or by global_id
12 16
             @throw TypeError
13 17
             @see EmComponent::__init__()
14 18
         """
15
-        super(EmField, self).__init__()
16
-        pass
19
+        self.table = EmField.table
20
+        super(EmField, self).__init__(id_or_name)
17 21
 
18 22
     @staticmethod
19
-    def create( name, em_fieldgroup, ml_repr = None, ml_help = None,
20
-                icon = None, optionnal = False, type_relation = None,
21
-                relationnal_field = None, primary_data = False,
22
-                default_value = None, params = None, value = None):
23
-        """ Create a new EmType and instanciate it
24
-            
25
-            @todo Change the icon param type
26
-            @todo simplify function aguments ?
27
-            @todo typeof default_value argument ?
28
-            @todo typeof params argument ?
29
-            @todo typeof value argument ?
30
-            
23
+    def create(name, em_fieldgroup, em_fieldtype, optional=True, internal=False):
24
+        """ Create a new EmField and instanciate it
31 25
             @static
32
-            
26
+
33 27
             @param name str: The name of the new Type
34 28
             @param em_fieldgroup EmFieldGroup: The new field will belong to this fieldgroup
35 29
             @param ml_repr MlString|None: Multilingual representation of the type
36 30
             @param ml_help MlString|None: Multilingual help for the type
37 31
             @param The string|None: filename of the icon
38
-            
39
-            @param optionnal bool: Is the field optionnal ?
40
-            @param type_relation EmType|None: If not None make a link between the class of the new EmField and this EmType
41
-            @param relationnal_field EmField|None: If not None indicates that the new field defines the relation created by this EmField argument
42
-            @param primary_data bool: Is the new field a primary data field ?
43
-            @param default_value str: The field's default value
44
-            @param params str: Params of the field
45
-            @param value str: Value of the field
46
-            
32
+
33
+            @param optional bool: Is the field optional ?
34
+            @param internal bool: Is the field internal?
35
+
47 36
             @throw TypeError
48 37
             @see EmComponent::__init__()
49 38
             @staticmethod
50 39
         """
51
-        pass
40
+        try:
41
+            exists = EmField(name)
42
+        except EmComponentNotExistError:
43
+            uids = SqlObject('uids')
44
+            res = uids.wexec(uids.table.insert().values(table=EmField.table))
45
+            uid = res.inserted_primary_key
52 46
 
53
-    def set_default(default_value):
54
-        """ Set the default value
55
-            @todo argument type ?
56
-            @todo return type ?
57
-            @param default_value anytype: The default value
58
-        """
59
-        pass
47
+            values = {
48
+                'uid' : uid,
49
+                'name' : name,
50
+                'fieldgroup_id' : em_fieldgroup.id,
51
+                'fieldtype_id' : em_fieldtype.id,
52
+                'optional' : 1 if optional else 0,
53
+                'internal' : 1 if internal else 0,
54
+            }
60 55
 
61
-    def set_param(params):
62
-        """ Set the field parameters
63
-            @todo argument type ? EmFieldParam ?
64
-            @todo return type ?
65
-            @param params anytype: The field parameters
66
-        """
67
-        pass
68
-
69
-    def set_value(v):
70
-        """ Set the field value
71
-            
72
-            @todo Better explanations
73
-            
74
-            Don't set the field value in a document, it's a special kind of value
75
-            
76
-            @param The v: value
77
-        """
78
-        pass
56
+            emfield_req = SqlObject(EmField.table)
57
+            res = emfield_req.wexec(emfield_req.table.insert(values=values))
58
+            return EmField(name)
59
+
60
+        return exists
61
+
62
+    """ Use dictionary (from database) to populate the object
63
+    """
64
+    def populate(self):
65
+        row = super(EmField, self).populate()
66
+        self.em_fieldgroup = EditorialModel.fieldgroups.EmFieldGroup(int(row.fieldgroup_id))
67
+        self.em_fieldtype = EditorialModel.fieldtypes.EmFieldType(int(row.fieldtype_id))
68
+        self.optional = True if row.optional == 1 else False;
69
+        self.internal = True if row.internal == 1 else False;
70
+        self.icon = row.icon
71
+        self.rel_to_type_id = EditorialModel.fieldtypes.EmFieldType(int(row.rel_to_type_id)) if row.rel_to_type_id else ''
72
+        self.rel_field_id = EmField(int(row.rel_field_id)) if row.rel_field_id else ''
73
+
74
+    def save(self):
75
+        # should not be here, but cannot see how to do this
76
+        if self.name is None:
77
+            self.populate()
78
+
79
+        values = {
80
+            'fieldgroup_id' : self.em_fieldgroup.id,
81
+            'fieldtype_id' : self.em_fieldtype.id,
82
+            'optional' : 1 if self.optional else 0,
83
+            'internal' : 1 if self.internal else 0,
84
+            'icon' : self.icon,
85
+            'rel_to_type_id' : self.rel_to_type_id,
86
+            'rel_field_id' : self.rel_field_id
87
+        }
88
+
89
+        return super(EmField, self).save(values)

+ 8
- 0
EditorialModel/fieldtypes.py View File

@@ -0,0 +1,8 @@
1
+#-*- coding: utf-8 -*-
2
+
3
+from EditorialModel.components import EmComponent, EmComponentNotExistError
4
+
5
+class EmFieldType():
6
+
7
+    def __init__(self, id):
8
+        self.id = id

Loading…
Cancel
Save