Browse Source

Optimisations de code dans EmField

Roland Haroutiounian 9 years ago
parent
commit
ec69ca5837
1 changed files with 6 additions and 13 deletions
  1. 6
    13
      EditorialModel/fields.py

+ 6
- 13
EditorialModel/fields.py View File

1
 #-*- coding: utf-8 -*-
1
 #-*- coding: utf-8 -*-
2
 
2
 
3
 from EditorialModel.components import EmComponent, EmComponentNotExistError
3
 from EditorialModel.components import EmComponent, EmComponentNotExistError
4
-from EditorialModel.fieldtypes import *
5
-from EditorialModel.fields_types import Em_Field_Type
4
+from EditorialModel.fieldtypes import EmField_boolean, EmField_char, EmField_integer, get_field_type
6
 from EditorialModel.fieldgroups import EmFieldGroup
5
 from EditorialModel.fieldgroups import EmFieldGroup
7
 from EditorialModel.classes import EmClass
6
 from EditorialModel.classes import EmClass
8
-from EditorialModel.types import EmType
9
 
7
 
10
 from Database import sqlutils
8
 from Database import sqlutils
11
 from Database.sqlwrapper import SqlWrapper
9
 from Database.sqlwrapper import SqlWrapper
12
-from Database.sqlquerybuilder import SqlQueryBuilder
13
 from Database.sqlalter import DropColumn
10
 from Database.sqlalter import DropColumn
14
 
11
 
15
 import sqlalchemy as sql
12
 import sqlalchemy as sql
16
 
13
 
17
-import EditorialModel
18
 import logging
14
 import logging
19
 
15
 
20
 logger = logging.getLogger('Lodel2.EditorialModel')
16
 logger = logging.getLogger('Lodel2.EditorialModel')
73
 
69
 
74
             created_field = super(EmField, cls).create(**values)
70
             created_field = super(EmField, cls).create(**values)
75
             if created_field:
71
             if created_field:
76
-                # The field was created, we then add its column in the corresponding class' table
77
                 is_field_column_added = created_field.add_field_column_to_class_table()
72
                 is_field_column_added = created_field.add_field_column_to_class_table()
78
                 if is_field_column_added:
73
                 if is_field_column_added:
79
                     return created_field
74
                     return created_field
81
             exists = created_field
76
             exists = created_field
82
 
77
 
83
         return exists
78
         return exists
84
-    
79
+
85
     ## @brief Delete a field if it's not linked
80
     ## @brief Delete a field if it's not linked
86
     # @return bool : True if deleted False if deletion aborded
81
     # @return bool : True if deleted False if deletion aborded
87
     # @todo Check if unconditionnal deletion is correct
82
     # @todo Check if unconditionnal deletion is correct
92
         ddl = DropColumn(class_table, field_col)
87
         ddl = DropColumn(class_table, field_col)
93
         sqlutils.ddl_execute(ddl, self.__class__.db_engine())
88
         sqlutils.ddl_execute(ddl, self.__class__.db_engine())
94
         return super(EmField, self).delete()
89
         return super(EmField, self).delete()
95
-    
90
+
96
     ## add_field_column_to_class_table (Function)
91
     ## add_field_column_to_class_table (Function)
97
     #
92
     #
98
     # Adds a column representing the field in its class' table
93
     # Adds a column representing the field in its class' table
100
     # @param emField EmField: the object representing the field
95
     # @param emField EmField: the object representing the field
101
     # @return True in case of success, False if not
96
     # @return True in case of success, False if not
102
     def add_field_column_to_class_table(self):
97
     def add_field_column_to_class_table(self):
103
-        field_type = "%s%s" % (EditorialModel.fieldtypes.get_field_type(self.fieldtype).sql_column(), " DEFAULT 0" if self.fieldtype=='integer' else '')
98
+        field_type = "%s%s" % (get_field_type(self.fieldtype).sql_column(), " DEFAULT 0" if self.fieldtype == 'integer' else '')
104
         field_uid = self.uid
99
         field_uid = self.uid
105
         field_class_table = self.get_class_table()
100
         field_class_table = self.get_class_table()
106
         return SqlWrapper().addColumn(tname=field_class_table, colname=self.name, coltype=field_type)
101
         return SqlWrapper().addColumn(tname=field_class_table, colname=self.name, coltype=field_type)
121
     def _get_class_table_db(self):
116
     def _get_class_table_db(self):
122
         dbe = self.db_engine()
117
         dbe = self.db_engine()
123
         conn = dbe.connect()
118
         conn = dbe.connect()
124
-        #fieldtable = sql.Table(EmField.table, sqlutils.meta(dbe))
125
-        fieldgrouptable = sql.Table(EmFieldGroup.table, sqlutils.meta(dbe))
126
-        request_get_class_id = fieldgrouptable.select().where(fieldgrouptable.c.uid == self.fieldgroup_id)
119
+        field_group_table = sql.Table(EmFieldGroup.table, sqlutils.meta(dbe))
120
+        request_get_class_id = field_group_table.select().where(field_group_table.c.uid == self.fieldgroup_id)
127
         result_get_class_id = conn.execute(request_get_class_id).fetchall()
121
         result_get_class_id = conn.execute(request_get_class_id).fetchall()
128
         class_id = dict(zip(result_get_class_id[0].keys(), result_get_class_id[0]))['class_id']
122
         class_id = dict(zip(result_get_class_id[0].keys(), result_get_class_id[0]))['class_id']
129
 
123
 
133
         class_table_name = dict(zip(result_get_class_table[0].keys(), result_get_class_table[0]))['name']
127
         class_table_name = dict(zip(result_get_class_table[0].keys(), result_get_class_table[0]))['name']
134
 
128
 
135
         return class_table_name
129
         return class_table_name
136
-

Loading…
Cancel
Save