Browse Source

EmType: create, save and load

ArnAud 10 years ago
parent
commit
d0a43614a1
1 changed files with 57 additions and 27 deletions
  1. 57
    27
      EditorialModel/types.py

+ 57
- 27
EditorialModel/types.py View File

@@ -1,6 +1,9 @@
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.classes
4 7
 
5 8
 class EmType(EmComponent):
6 9
     """ Represents type of documents
@@ -10,48 +13,77 @@ class EmType(EmComponent):
10 13
         EmType with special fields called relation_to_type fields
11 14
         @see EmComponent
12 15
     """
16
+    table = 'em_type'
13 17
 
14
-    def __init__(id_or_name):
18
+    def __init__(self, id_or_name):
15 19
         """  Instanciate an EmType with data fetched from db
16 20
             @param id_or_name str|int: Identify the EmType by name or by global_id
17 21
             @throw TypeError
18 22
             @see EmComponent::__init__()
19 23
         """
20
-        super(EmType, self).__init__()
21
-        pass
24
+        self.table = EmType.table
25
+        super(EmType, self).__init__(id_or_name)
22 26
 
23 27
     @staticmethod
24
-    def create(name, em_class, ml_repr = None, ml_help = None, icon = None, sort_field = None):
28
+    def create(name, em_class):
25 29
         """ Create a new EmType and instanciate it
26 30
 
27 31
             @param name str: The name of the new type
28 32
             @param em_class EmClass: The class that the new type will specialize
29
-            @param ml_repr MlString: Multilingual representation of the type
30
-            @param ml_help MlString: Multilingual help for the type
31
-            @param icon str|None: The filename of the icon
32
-            @param sort_field EmField|None: The field used to sort by default
33 33
 
34 34
             @see EmComponent::__init__()
35 35
 
36 36
             @todo Change the icon param type
37 37
             @todo change staticmethod to classmethod
38 38
         """
39
-        pass
39
+        try:
40
+            exists = EmType(name)
41
+        except EmComponentNotExistError:
42
+            uids = SqlObject('uids')
43
+            res = uids.wexec(uids.table.insert().values(table=EmType.table))
44
+            uid = res.inserted_primary_key
40 45
 
41
-    def field_groups():
46
+            emtype = SqlObject(EmType.table)
47
+            res = emtype.wexec(emtype.table.insert().values(uid=uid, name=name, class_id=em_class.id))
48
+            return EmType(name)
49
+
50
+        return exists
51
+
52
+    """ Use dictionary (from database) to populate the object
53
+    """
54
+    def populate(self):
55
+        row = super(EmType, self).populate()
56
+        self.em_class = EditorialModel.classes.EmClass(int(row.class_id))
57
+        self.icon = row.icon
58
+        self.sortcolumn = row.sortcolumn
59
+
60
+    def save(self):
61
+        # should not be here, but cannot see how to do this
62
+        if self.name is None:
63
+            self.populate()
64
+
65
+        values = {
66
+            'class_id' : self.em_class.id,
67
+            'icon' : self.icon,
68
+            'sortcolumn' : self.sortcolumn,
69
+        }
70
+
71
+        return super(EmType, self).save(values)
72
+
73
+    def field_groups(self):
42 74
         """ Get the list of associated fieldgroups
43 75
             @return A list of EmFieldGroup
44 76
         """
45 77
         pass
46 78
 
47 79
 
48
-    def fields():
80
+    def fields(self):
49 81
         """ Get the list of associated fields
50 82
             @return A list of EmField
51 83
         """
52 84
         pass
53 85
 
54
-    def select_field(field):
86
+    def select_field(self, field):
55 87
         """ Indicate that an optionnal field is used
56 88
 
57 89
             @param field EmField: The optional field to select
@@ -60,7 +92,7 @@ class EmType(EmComponent):
60 92
         """
61 93
         pass
62 94
 
63
-    def unselect_field(field):
95
+    def unselect_field(self, field):
64 96
         """ Indicate that an optionnal field will not be used
65 97
             @param field EmField: The optional field to unselect
66 98
             @throw ValueError, TypeError
@@ -68,18 +100,17 @@ class EmType(EmComponent):
68 100
         """
69 101
         pass
70 102
 
71
-        
72
-    def hooks():
103
+
104
+    def hooks(self):
73 105
         """Get the list of associated hooks"""
74 106
         pass
75 107
 
76
-    def add_hook(hook):
108
+    def add_hook(self, hook):
77 109
         """ Add a new hook
78 110
             @param hook EmHook: A EmHook instance
79 111
             @throw TypeError
80 112
         """
81 113
         pass
82
-    
83 114
 
84 115
     def del_hook(hook):
85 116
         """ Delete a hook
@@ -90,16 +121,16 @@ class EmType(EmComponent):
90 121
         pass
91 122
 
92 123
 
93
-    def superiors():
124
+    def superiors(self):
94 125
         """ Get the list of superiors EmType in the type hierarchy
95 126
             @return A list of EmType
96 127
         """
97 128
         pass
98 129
 
99 130
 
100
-    def add_superior(em_type, relation_nature):
131
+    def add_superior(self, em_type, relation_nature):
101 132
         """ Add a superior in the type hierarchy
102
-            
133
+
103 134
             @param em_type EmType: An EmType instance
104 135
             @param relation_nature str: The name of the relation's nature
105 136
             @throw TypeError
@@ -107,22 +138,21 @@ class EmType(EmComponent):
107 138
         """
108 139
         pass
109 140
 
110
-    def del_superior(em_type):
141
+    def del_superior(self, em_type):
111 142
         """ Delete a superior in the type hierarchy
112
-            
143
+
113 144
             @param em_type EmType: An EmType instance
114 145
             @throw TypeError
115 146
             @todo define return value and raise condition
116 147
         """
117 148
         pass
118 149
 
119
-    def linked_types():
150
+    def linked_types(self):
120 151
         """ Get the list of linked type
121
-            
152
+
122 153
             Types are linked with special fields called relation_to_type fields
123
-            
154
+
124 155
             @return a list of EmType
125 156
             @see EmFields
126 157
         """
127 158
         pass
128
-

Loading…
Cancel
Save