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