Browse Source

Passage en commentaire du code obsolète

Roland Haroutiounian 9 years ago
parent
commit
116795af12
3 changed files with 217 additions and 5 deletions
  1. 94
    4
      EditorialModel/classes.py
  2. 32
    1
      EditorialModel/fieldgroups.py
  3. 91
    0
      EditorialModel/fields.py

+ 94
- 4
EditorialModel/classes.py View File

@@ -5,7 +5,7 @@
5 5
 
6 6
 from EditorialModel.components import EmComponent
7 7
 import EditorialModel.fieldtypes as ftypes
8
-
8
+import EditorialModel
9 9
 
10 10
 ## @brief Manipulate Classes of the Editorial Model
11 11
 # Create classes of object.
@@ -24,16 +24,68 @@ class EmClass(EmComponent):
24 24
         ('sortcolumn', ftypes.EmField_char)
25 25
     ]
26 26
 
27
+    ## Create a new class
28
+    # @param name str: name of the new class
29
+    # @param class_type EmClasstype: type of the class
30
+    # @return an EmClass instance
31
+    # @throw EmComponentExistError if an EmClass with this name and a different classtype exists
32
+    @classmethod
33
+    def create(cls, name, classtype, icon=None, sortcolumn='rank', **em_component_args):
34
+        pass
35
+        # return cls._create_db(name=name, classtype=classtype['name'], icon=icon, sortcolumn=sortcolumn, **em_component_args)
36
+
37
+    # @classmethod
38
+    # def _create_db(cls, name, classtype, icon, sortcolumn, **em_component_args):
39
+    #     result = super(Em, cls).create(name=name, classtype=classtype, icon=icon, sortcolumn=sortcolumn, **em_component_args)
40
+    #
41
+    #     dbe = result.db_engine
42
+    #     conn= dbe.connect()
43
+    #
44
+    #     meta = sql.MetaData()
45
+    #     emclasstable = sql.Table(result.class_table_name, meta, sql.Column('uid', sql.VARCHAR(50), primary_key=True))
46
+    #     emclasstable.create(conn)
47
+    #     conn.close()
48
+    #     return result
49
+
27 50
     @property
28 51
     ## @brief Return the table name used to stores data on this class
29 52
     def class_table_name(self):
30 53
         return self.name
31 54
 
55
+    ## @brief Delete a class if it's ''empty''
56
+    # If a class has no fieldgroups delete it
57
+    # @return bool : True if deleted False if deletion aborded
58
+    def delete(self):
59
+        pass
60
+        # fieldgroups = self.fieldgroups()
61
+        # if len(fieldgroups) > 0:
62
+        #     return False
63
+        #
64
+        # dbe = self.db_engine
65
+        # meta = sqlutils.meta(dbe)
66
+        # Here we have to give a connection
67
+        # class_table = sql.Table(self.name, meta)
68
+        # meta.drop_all(tables=[class_table], bind=dbe)
69
+        # return super(EmClass, self).delete()
32 70
 
33 71
     ## Retrieve list of the field_groups of this class
34 72
     # @return A list of fieldgroups instance
35 73
     def fieldgroups(self):
36
-        pass
74
+        records = self._fieldgroups_db()  # TODO Modifier l'appel
75
+        fieldgroups = [EditorialModel.fieldgroups.EmFieldGroup(int(record.uid)) for record in records]
76
+
77
+        return fieldgroups
78
+
79
+    ## Isolate SQL for EmClass::fieldgroups
80
+    # @return An array of dict (sqlalchemy fetchall)
81
+    # def _fieldgroups_db(self):
82
+    #     dbe = self.db_engine
83
+    #     emfg = sql.Table(EditorialModel.fieldgroups.EmFieldGroup.table, sqlutils.meta(dbe))
84
+    #     req = emfg.select().where(emfg.c.class_id == self.uid)
85
+    #
86
+    #     conn = dbe.connect()
87
+    #     res = conn.execute(req)
88
+    #     return res.fetchall()
37 89
 
38 90
     ## Retrieve list of fields
39 91
     # @return fields [EmField]:
@@ -48,15 +100,53 @@ class EmClass(EmComponent):
48 100
     # @return types [EmType]:
49 101
     def types(self):
50 102
         pass
103
+        records = self._types_db()  # TODO Modifier l'appel
104
+        types = [EditorialModel.types.EmType(int(record.uid)) for record in records]
105
+
106
+        return types
107
+
108
+    ## Isolate SQL for EmCLass::types
109
+    # @return An array of dict (sqlalchemy fetchall)
110
+    # def _types_db(self):
111
+    #     dbe = self.db_engine
112
+    #     emtype = sql.Table(EditorialModel.types.EmType.table, sqlutils.meta(dbe))
113
+    #     req = emtype.select().where(emtype.c.class_id == self.uid)
114
+    #     conn = dbe.connect()
115
+    #     res = conn.execute(req)
116
+    #     return res.fetchall()
51 117
 
52 118
     ## Add a new EmType that can ben linked to this class
53 119
     # @param  em_type EmType: type to link
54 120
     # @return success bool: done or not
55 121
     def link_type(self, em_type):
56
-        pass
122
+        table_name = self.name + '_' + em_type.name
123
+        self._link_type_db(table_name)  # TODO Modifier l'appel
124
+
125
+        return True
126
+
127
+    # def _link_type_db(self, table_name):
128
+        #  Create a new table storing additionnal fields for the relation between the linked type and this EmClass
129
+        # conn = self.db_engine.connect()
130
+        # meta = sql.MetaData()
131
+        # emlinketable = sql.Table(table_name, meta, sql.Column('uid', sql.VARCHAR(50), primary_key=True))
132
+        # emlinketable.create(conn)
133
+        # conn.close()
57 134
 
58 135
 
59 136
     ## Retrieve list of EmType that are linked to this class
60 137
     #  @return types [EmType]:
61 138
     def linked_types(self):
62
-        pass
139
+        return self._linked_types_db()  # TODO Modifier l'appel
140
+
141
+    # def _linked_types_db(self):
142
+    #     dbe = self.db_engine
143
+    #     meta = sql.MetaData()
144
+    #     meta.reflect(dbe)
145
+    #
146
+    #     linked_types = []
147
+    #     for table in meta.tables.values():
148
+    #         table_name_elements = table.name.split('_')
149
+    #         if len(table_name_elements) == 2:
150
+    #             linked_types.append(EditorialModel.types.EmType(table_name_elements[1]))
151
+    #
152
+    #     return linked_types

+ 32
- 1
EditorialModel/fieldgroups.py View File

@@ -1,8 +1,14 @@
1 1
 #-*- coding: utf-8 -*-
2 2
 
3 3
 from EditorialModel.components import EmComponent
4
+from EditorialModel.classes import EmClass
4 5
 import EditorialModel.fieldtypes as ftypes
5 6
 
7
+from Database import sqlutils
8
+import sqlalchemy as sql
9
+
10
+import EditorialModel
11
+
6 12
 
7 13
 ## Represents groups of EmField associated with an EmClass
8 14
 #
@@ -17,7 +23,32 @@ class EmFieldGroup(EmComponent):
17 23
     ## List of fields
18 24
     _fields = [('class_id', ftypes.EmField_integer)]
19 25
 
26
+    @classmethod
27
+    ## Create a new EmFieldGroup
28
+    #
29
+    # Save it in database and return an instance*
30
+    # @param name str: The name of the new EmFieldGroup
31
+    # @param em_class EmClass : An EditorialModel::classes::EmClass instance
32
+    # @param **em_component_args : @ref EditorialModel::components::create()
33
+    # @throw EmComponentExistError If an EmFieldGroup with this name allready exists
34
+    # @throw TypeError If an argument is of an unexepted type
35
+    def create(cls, name, em_class, **em_component_args):
36
+        if not isinstance(name, str):
37
+            raise TypeError("Excepting <class str> as name. But got " + str(type(name)))
38
+        if not isinstance(em_class, EmClass):
39
+            raise TypeError("Excepting <class EmClass> as em_class. But got "+str(type(name)))
40
+
41
+        return super(EmFieldGroup, cls).create(name=name, class_id=em_class.uid, **em_component_args)
42
+
20 43
     ## Get the list of associated fields
21 44
     # @return A list of EmField instance
22 45
     def fields(self):
23
-        pass
46
+        pass
47
+        # meta = sqlutils.meta(self.db_engine)
48
+        # field_table = sql.Table(EditorialModel.fields.EmField.table, meta)
49
+        # req = field_table.select(field_table.c.uid).where(field_table.c.fieldgroup_id == self.uid)
50
+        # conn = self.db_engine.connect()
51
+        # res = conn.execute(req)
52
+        # rows = res.fetchall()
53
+        # conn.close()
54
+        # return [EditorialModel.fields.EmField(row['uid']) for row in rows]

+ 91
- 0
EditorialModel/fields.py View File

@@ -2,7 +2,13 @@
2 2
 
3 3
 from EditorialModel.components import EmComponent
4 4
 from EditorialModel.fieldtypes import EmField_boolean, EmField_char, EmField_integer, EmField_icon, get_field_type
5
+from EditorialModel.fieldgroups import EmFieldGroup
6
+from EditorialModel.classes import EmClass
5 7
 
8
+from Database import sqlutils
9
+from Database.sqlalter import DropColumn, AddColumn
10
+
11
+import sqlalchemy as sql
6 12
 
7 13
 ## EmField (Class)
8 14
 #
@@ -20,3 +26,88 @@ class EmField(EmComponent):
20 26
         ('internal', EmField_boolean),
21 27
         ('icon', EmField_icon)
22 28
     ]
29
+
30
+    ## Create (Function)
31
+    #
32
+    # Creates a new EmField and instanciates it
33
+    #
34
+    # @static
35
+    #
36
+    # @param name str: Name of the field
37
+    # @param fieldgroup EmFieldGroup: Field group in which the field is
38
+    # @param fieldtype EmFieldType: Type of the field
39
+    # @param optional int: is the field optional ? (default=0)
40
+    # @param internal int: is the field internal ? (default=0)
41
+    # @param rel_to_type_id int: default=0
42
+    # @param rel_field_id int: default=0
43
+    # @param icon int: default=0
44
+    # @param **em_component_args : @ref EditorialModel::components::create()
45
+    #
46
+    # @throw TypeError
47
+    # @throw RuntimeError if the associated column creation fails
48
+    # @throw EmComponentExistError if an EmField with this name allready exists in this fieldgroup
49
+    # @see EmComponent::__init__()
50
+    # @staticmethod
51
+    @classmethod
52
+    def create(cls, name, fieldgroup, fieldtype, optional=0, internal=0, rel_to_type_id=0, rel_field_id=0, icon=None, **em_component_args):
53
+        created_field = super(EmField, cls).create(
54
+            name=name,
55
+            fieldgroup_id=fieldgroup.uid,
56
+            fieldtype=fieldtype.name,
57
+            optional=optional,
58
+            internal=internal,
59
+            rel_to_type_id=rel_to_type_id,
60
+            rel_field_id=rel_field_id,
61
+            icon=icon,
62
+            **em_component_args
63
+        )
64
+        # if not created_field.add_field_column_to_class_table():
65
+        #     raise RuntimeError("Unable to create the column for the EmField " + str(created_field))
66
+
67
+        return created_field
68
+
69
+    ## @brief Delete a field if it's not linked
70
+    # @return bool : True if deleted False if deletion aborded
71
+    # @todo Check if unconditionnal deletion is correct
72
+    def delete(self):
73
+        # dbe = self.db_engine
74
+        # class_table = sql.Table(self.get_class_table(), sqlutils.meta(dbe))
75
+        # field_col = sql.Column(self.name)
76
+        # ddl = DropColumn(class_table, field_col)
77
+        # sqlutils.ddl_execute(ddl, self.db_engine)
78
+        return super(EmField, self).delete()
79
+
80
+    ## add_field_column_to_class_table (Function)
81
+    #
82
+    # Adds a column representing the field in its class' table
83
+    #
84
+    # @return True in case of success, False if not
85
+    # def add_field_column_to_class_table(self):
86
+        # dbe = self.db_engine
87
+        # fieldtype = get_field_type(self.fieldtype)
88
+        # new_column = sql.Column(name=self.name, **(fieldtype.sqlalchemy_args()))
89
+        # class_table = sql.Table(self.get_class_table(), sqlutils.meta(dbe))
90
+        # ddl = AddColumn(class_table, new_column)
91
+        # return sqlutils.ddl_execute(ddl, dbe)
92
+
93
+    ## get_class_table (Function)
94
+    #
95
+    # Gets the name of the table of the class corresponding to the field
96
+    #
97
+    # @return Name of the table
98
+    # def get_class_table(self):
99
+    #     return self.get_class().class_table_name
100
+
101
+    ## @brief Get the class that contains this field
102
+    # @return An EmClass instance
103
+    # def get_class(self):
104
+        #<SQL>
105
+        # dbe = self.db_engine
106
+        # meta = sqlutils.meta(dbe)
107
+        # conn = dbe.connect()
108
+        # fieldgroup_table = sql.Table(EmFieldGroup.table, meta)
109
+        # req = fieldgroup_table.select().where(fieldgroup_table.c.uid == self.fieldgroup_id)
110
+        # res = conn.execute(req)
111
+        # row = res.fetchone()
112
+        #</SQL>
113
+        # return EmClass(row['class_id'])

Loading…
Cancel
Save