Browse Source

EmType: implement fieldgroups()

ArnAud 9 years ago
parent
commit
338bccd785
2 changed files with 25 additions and 21 deletions
  1. 5
    2
      EditorialModel/test/me.json
  2. 20
    19
      EditorialModel/types.py

+ 5
- 2
EditorialModel/test/me.json View File

@@ -30,10 +30,10 @@
30 30
     "component":"EmField", "name":"prenom", "string":"{\"fre\":\"Preom\"}", "help":"{}", "rank":"2", "date_update":"", "date_create":"", "fieldtype":"", "fieldgroup_id":"8", "rel_to_type_id":"", "rel_field_id":"", "optional":"1", "internal":"", "icon":"0"
31 31
   },
32 32
   "11" : {
33
-    "component":"EmField", "name":"auteur", "string":"{\"fre\":\"Auteur\"}", "help":"{}", "rank":"3", "date_update":"", "date_create":"", "fieldtype":"", "fieldgroup_id":"3", "rel_to_type_id":"6", "rel_field_id":"", "optional":"1", "internal":"", "icon":"0"
33
+    "component":"EmField", "name":"auteur", "string":"{\"fre\":\"Auteur\"}", "help":"{}", "rank":"3", "date_update":"", "date_create":"", "fieldtype":"", "fieldgroup_id":"17", "rel_to_type_id":"6", "rel_field_id":"", "optional":"1", "internal":"", "icon":"0"
34 34
   },
35 35
   "12" : {
36
-    "component":"EmField", "name":"adresse", "string":"{\"fre\":\"Adresse\"}", "help":"{}", "rank":"4", "date_update":"", "date_create":"", "fieldtype":"", "fieldgroup_id":"3", "rel_to_type_id":"", "rel_field_id":"11", "optional":"1", "internal":"", "icon":"0"
36
+    "component":"EmField", "name":"adresse", "string":"{\"fre\":\"Adresse\"}", "help":"{}", "rank":"4", "date_update":"", "date_create":"", "fieldtype":"", "fieldgroup_id":"17", "rel_to_type_id":"", "rel_field_id":"11", "optional":"1", "internal":"", "icon":"0"
37 37
   },
38 38
   "13" : {
39 39
     "component":"EmClass", "name":"publication", "string":"{\"fre\":\"Publication\"}", "help":"{}", "rank":"2", "date_update":"", "date_create":"", "classtype":"entity", "icon":"0", "sortcolumn":"rank"
@@ -46,5 +46,8 @@
46 46
   },
47 47
   "16" : {
48 48
     "component":"EmField", "name":"titre", "string":"{\"fre\":\"Titre\"}", "help":"{}", "rank":"1", "date_update":"", "date_create":"", "fieldtype":"", "fieldgroup_id":"15", "rel_to_type_id":"", "rel_field_id":"", "optional":"0", "internal":"", "icon":"0"
49
+  },
50
+  "17" : {
51
+    "component":"EmFieldGroup", "name":"gens", "string":"{\"fre\":\"Gens\"}", "help":"{}", "rank":"2", "date_update":"", "date_create":"", "class_id":"1"
49 52
   }
50 53
 }

+ 20
- 19
EditorialModel/types.py View File

@@ -32,8 +32,8 @@ class EmType(EmComponent):
32 32
         ('sortcolumn', ftypes.EmField_char)
33 33
     ]
34 34
 
35
-    def __init__(self, data, components):
36
-        super(EmType, self).__init__(data, components)
35
+    def __init__(self, data, model):
36
+        super(EmType, self).__init__(data, model)
37 37
         for link in ['fields', 'subordinates']:
38 38
             if link in data:
39 39
                 self._fields[link] = data[link]
@@ -57,8 +57,13 @@ class EmType(EmComponent):
57 57
     ## Return the EmClassType of the type
58 58
     # @return EditorialModel.classtypes.*
59 59
     def classtype(self):
60
-        my_class = self.components['uids'][self.class_id]
61
-        return getattr(EmClassType, my_class.classtype)
60
+        return getattr(EmClassType, self.em_class.classtype)
61
+
62
+    @property
63
+    ## Return an instance of the class this type belongs to
64
+    # @return EditorialModel.EmClass
65
+    def em_class(self):
66
+        return self.model.component(self.class_id)
62 67
 
63 68
     ## @brief Delete an EmType
64 69
     # The deletion is only possible if a type is not linked by any EmClass
@@ -76,33 +81,29 @@ class EmType(EmComponent):
76 81
                 self.del_superior(sup, nature)
77 82
         return super(EmType, self).delete()
78 83
 
79
-    ## Get the list of associated fieldgroups
84
+    ## Get the list of non empty associated fieldgroups
80 85
     # @return A list of EmFieldGroup instance
81
-    def field_groups(self):
82
-        # TODO Réimplémenter
83
-        pass
84
-        # meta = sqlutils.meta(self.db_engine)
85
-        # fg_table = sql.Table(EmFieldGroup.table, meta)
86
-        # req = fg_table.select(fg_table.c.uid).where(fg_table.c.class_id == self.class_id)
87
-        # conn = self.db_engine.connect()
88
-        # res = conn.execute(req)
89
-        # rows = res.fetchall()
90
-        # conn.close()
91
-        #
92
-        # return [EmFieldGroup(row['uid']) for row in rows]
86
+    def fieldgroups(self):
87
+        fieldgroups = []
88
+        for fieldgroup in self.em_class.fieldgroups():
89
+            for field in fieldgroup.fields():
90
+                if not field.optional or field.uid in self._fields['fields']:
91
+                    fieldgroups.append(fieldgroup)
92
+                    break
93
+        return fieldgroups
93 94
 
94 95
     ## Get the list of all Emfield possibly associated with this type
95 96
     # @return A list of EmField instance
96 97
     def all_fields(self):
97 98
         res = []
98
-        for fieldgroup in self.field_groups():
99
+        for fieldgroup in self.fieldgroups():
99 100
             res += fieldgroup.fields()
100 101
         return res
101 102
 
102 103
     ## Return selected optional field
103 104
     # @return A list of EmField instance
104 105
     def selected_fields(self):
105
-        selected = [ self.components['uids'][field_id] for field_id in self._fields['fields'] ]
106
+        selected = [ self.model.component(field_id) for field_id in self._fields['fields'] ]
106 107
         return selected
107 108
 
108 109
     ## Return the list of associated fields

Loading…
Cancel
Save