Browse Source

EmField add_column code factorization

Yann Weber 9 years ago
parent
commit
3f8aff6813
2 changed files with 7 additions and 56 deletions
  1. 6
    55
      EditorialModel/fields.py
  2. 1
    1
      EditorialModel/fieldtypes.py

+ 6
- 55
EditorialModel/fields.py View File

@@ -96,61 +96,12 @@ class EmField(EmComponent):
96 96
     # @param emField EmField: the object representing the field
97 97
     # @return True in case of success, False if not
98 98
     def add_field_column_to_class_table(self):
99
-        field_type = "%s%s" % (get_field_type(self.fieldtype).sql_column(), " DEFAULT 0" if self.fieldtype == 'integer' else '')
100
-        field_class_table = self.get_class_table()
101
-
102
-        sql_engine = sqlutils.getEngine()
103
-        conn = sql_engine.connect()
104
-        meta_data = sqlutils.meta(sql_engine)
105
-        table = sql.Table(field_class_table, meta_data)
106
-        new_column = self.create_column(name=self.name, type_=field_type)
107
-        ddl = AddColumn(table, new_column)
108
-        sql_query = ddl.compile(dialect=sql_engine.dialect)
109
-        sql_query = str(sql_query)
110
-        logger.debug("Executing SQL : '%s'" % sql_query)
111
-        ret = bool(conn.execute(sql_query))
112
-        return ret
113
-
114
-    def create_column(self, **kwargs):
115
-        if not 'name' in kwargs or ('type' not in kwargs and 'type_' not in kwargs):
116
-            pass
117
-
118
-        if 'type_' not in kwargs and 'type' in kwargs:
119
-            kwargs['type_'] = self.sql_to_sqla_type(kwargs['type'])
120
-            del kwargs['type']
121
-
122
-        if 'extra' in kwargs:
123
-            for extra_name in kwargs['extra']:
124
-                kwargs[extra_name] = kwargs['extra']['name']
125
-            del kwargs['extra']
126
-
127
-        if 'foreignkey' in kwargs:
128
-            foreign_key = sql.ForeignKey(kwargs['foreignkey'])
129
-            del kwargs['foreignkey']
130
-        else:
131
-            foreign_key = None
132
-
133
-        if 'primarykey' in kwargs:
134
-            kwargs['primary_key'] = kwargs['primarykey']
135
-            del kwargs['primarykey']
136
-
137
-        result = sql.Column(**kwargs)
138
-
139
-        if foreign_key is not None:
140
-            result.append_foreign_key(foreign_key)
141
-
142
-        return result
143
-
144
-    def sql_to_sqla_type(self, strtype):
145
-        if 'VARCHAR' in strtype:
146
-            check_length = re.search(re.compile('VARCHAR\(([\d]+)\)', re.IGNORECASE), strtype)
147
-            column_length = int(check_length.groups()[0]) if check_length else None
148
-            return sql.VARCHAR(lengh=column_length)
149
-        else:
150
-            try:
151
-                return getattr(sql, strtype)
152
-            except AttributeError:
153
-                raise NameError("Unknown type '%s'" % strtype)
99
+        dbe = self.db_engine()
100
+        fieldtype = get_field_type(self.fieldtype)
101
+        new_column = sql.Column(name=self.name, **(fieldtype.sqlalchemy_args()) )
102
+        class_table = sql.Table(self.get_class_table(), sqlutils.meta(dbe))
103
+        ddl = AddColumn(class_table, new_column)
104
+        return sqlutils.ddl_execute(ddl, dbe)
154 105
 
155 106
     ## get_class_table (Function)
156 107
     #

+ 1
- 1
EditorialModel/fieldtypes.py View File

@@ -88,7 +88,7 @@ class EmField_integer(EmFieldType):
88 88
 
89 89
     def sqlalchemy_args(self):
90 90
         # TODO Ajouter la prise en charge de la taille max
91
-        return {'type_': INTEGER, 'nullable': False}
91
+        return {'type_': INTEGER, 'nullable': False, 'default': 0}
92 92
 
93 93
 
94 94
 ## EmField_boolean (Class)

Loading…
Cancel
Save