1
0
Fork 0
mirror of https://github.com/yweber/lodel2.git synced 2026-06-13 22:20:47 +02:00

Passage en commentaire du code obsolète

This commit is contained in:
Roland Haroutiounian 2015-07-16 11:43:23 +02:00
commit 116795af12
3 changed files with 217 additions and 5 deletions

View file

@ -5,7 +5,7 @@
from EditorialModel.components import EmComponent
import EditorialModel.fieldtypes as ftypes
import EditorialModel
## @brief Manipulate Classes of the Editorial Model
# Create classes of object.
@ -24,16 +24,68 @@ class EmClass(EmComponent):
('sortcolumn', ftypes.EmField_char)
]
## Create a new class
# @param name str: name of the new class
# @param class_type EmClasstype: type of the class
# @return an EmClass instance
# @throw EmComponentExistError if an EmClass with this name and a different classtype exists
@classmethod
def create(cls, name, classtype, icon=None, sortcolumn='rank', **em_component_args):
pass
# return cls._create_db(name=name, classtype=classtype['name'], icon=icon, sortcolumn=sortcolumn, **em_component_args)
# @classmethod
# def _create_db(cls, name, classtype, icon, sortcolumn, **em_component_args):
# result = super(Em, cls).create(name=name, classtype=classtype, icon=icon, sortcolumn=sortcolumn, **em_component_args)
#
# dbe = result.db_engine
# conn= dbe.connect()
#
# meta = sql.MetaData()
# emclasstable = sql.Table(result.class_table_name, meta, sql.Column('uid', sql.VARCHAR(50), primary_key=True))
# emclasstable.create(conn)
# conn.close()
# return result
@property
## @brief Return the table name used to stores data on this class
def class_table_name(self):
return self.name
## @brief Delete a class if it's ''empty''
# If a class has no fieldgroups delete it
# @return bool : True if deleted False if deletion aborded
def delete(self):
pass
# fieldgroups = self.fieldgroups()
# if len(fieldgroups) > 0:
# return False
#
# dbe = self.db_engine
# meta = sqlutils.meta(dbe)
# Here we have to give a connection
# class_table = sql.Table(self.name, meta)
# meta.drop_all(tables=[class_table], bind=dbe)
# return super(EmClass, self).delete()
## Retrieve list of the field_groups of this class
# @return A list of fieldgroups instance
def fieldgroups(self):
pass
records = self._fieldgroups_db() # TODO Modifier l'appel
fieldgroups = [EditorialModel.fieldgroups.EmFieldGroup(int(record.uid)) for record in records]
return fieldgroups
## Isolate SQL for EmClass::fieldgroups
# @return An array of dict (sqlalchemy fetchall)
# def _fieldgroups_db(self):
# dbe = self.db_engine
# emfg = sql.Table(EditorialModel.fieldgroups.EmFieldGroup.table, sqlutils.meta(dbe))
# req = emfg.select().where(emfg.c.class_id == self.uid)
#
# conn = dbe.connect()
# res = conn.execute(req)
# return res.fetchall()
## Retrieve list of fields
# @return fields [EmField]:
@ -48,15 +100,53 @@ class EmClass(EmComponent):
# @return types [EmType]:
def types(self):
pass
records = self._types_db() # TODO Modifier l'appel
types = [EditorialModel.types.EmType(int(record.uid)) for record in records]
return types
## Isolate SQL for EmCLass::types
# @return An array of dict (sqlalchemy fetchall)
# def _types_db(self):
# dbe = self.db_engine
# emtype = sql.Table(EditorialModel.types.EmType.table, sqlutils.meta(dbe))
# req = emtype.select().where(emtype.c.class_id == self.uid)
# conn = dbe.connect()
# res = conn.execute(req)
# return res.fetchall()
## Add a new EmType that can ben linked to this class
# @param em_type EmType: type to link
# @return success bool: done or not
def link_type(self, em_type):
pass
table_name = self.name + '_' + em_type.name
self._link_type_db(table_name) # TODO Modifier l'appel
return True
# def _link_type_db(self, table_name):
# Create a new table storing additionnal fields for the relation between the linked type and this EmClass
# conn = self.db_engine.connect()
# meta = sql.MetaData()
# emlinketable = sql.Table(table_name, meta, sql.Column('uid', sql.VARCHAR(50), primary_key=True))
# emlinketable.create(conn)
# conn.close()
## Retrieve list of EmType that are linked to this class
# @return types [EmType]:
def linked_types(self):
pass
return self._linked_types_db() # TODO Modifier l'appel
# def _linked_types_db(self):
# dbe = self.db_engine
# meta = sql.MetaData()
# meta.reflect(dbe)
#
# linked_types = []
# for table in meta.tables.values():
# table_name_elements = table.name.split('_')
# if len(table_name_elements) == 2:
# linked_types.append(EditorialModel.types.EmType(table_name_elements[1]))
#
# return linked_types

View file

@ -1,8 +1,14 @@
#-*- coding: utf-8 -*-
from EditorialModel.components import EmComponent
from EditorialModel.classes import EmClass
import EditorialModel.fieldtypes as ftypes
from Database import sqlutils
import sqlalchemy as sql
import EditorialModel
## Represents groups of EmField associated with an EmClass
#
@ -17,7 +23,32 @@ class EmFieldGroup(EmComponent):
## List of fields
_fields = [('class_id', ftypes.EmField_integer)]
@classmethod
## Create a new EmFieldGroup
#
# Save it in database and return an instance*
# @param name str: The name of the new EmFieldGroup
# @param em_class EmClass : An EditorialModel::classes::EmClass instance
# @param **em_component_args : @ref EditorialModel::components::create()
# @throw EmComponentExistError If an EmFieldGroup with this name allready exists
# @throw TypeError If an argument is of an unexepted type
def create(cls, name, em_class, **em_component_args):
if not isinstance(name, str):
raise TypeError("Excepting <class str> as name. But got " + str(type(name)))
if not isinstance(em_class, EmClass):
raise TypeError("Excepting <class EmClass> as em_class. But got "+str(type(name)))
return super(EmFieldGroup, cls).create(name=name, class_id=em_class.uid, **em_component_args)
## Get the list of associated fields
# @return A list of EmField instance
def fields(self):
pass
pass
# meta = sqlutils.meta(self.db_engine)
# field_table = sql.Table(EditorialModel.fields.EmField.table, meta)
# req = field_table.select(field_table.c.uid).where(field_table.c.fieldgroup_id == self.uid)
# conn = self.db_engine.connect()
# res = conn.execute(req)
# rows = res.fetchall()
# conn.close()
# return [EditorialModel.fields.EmField(row['uid']) for row in rows]

View file

@ -2,7 +2,13 @@
from EditorialModel.components import EmComponent
from EditorialModel.fieldtypes import EmField_boolean, EmField_char, EmField_integer, EmField_icon, get_field_type
from EditorialModel.fieldgroups import EmFieldGroup
from EditorialModel.classes import EmClass
from Database import sqlutils
from Database.sqlalter import DropColumn, AddColumn
import sqlalchemy as sql
## EmField (Class)
#
@ -20,3 +26,88 @@ class EmField(EmComponent):
('internal', EmField_boolean),
('icon', EmField_icon)
]
## Create (Function)
#
# Creates a new EmField and instanciates it
#
# @static
#
# @param name str: Name of the field
# @param fieldgroup EmFieldGroup: Field group in which the field is
# @param fieldtype EmFieldType: Type of the field
# @param optional int: is the field optional ? (default=0)
# @param internal int: is the field internal ? (default=0)
# @param rel_to_type_id int: default=0
# @param rel_field_id int: default=0
# @param icon int: default=0
# @param **em_component_args : @ref EditorialModel::components::create()
#
# @throw TypeError
# @throw RuntimeError if the associated column creation fails
# @throw EmComponentExistError if an EmField with this name allready exists in this fieldgroup
# @see EmComponent::__init__()
# @staticmethod
@classmethod
def create(cls, name, fieldgroup, fieldtype, optional=0, internal=0, rel_to_type_id=0, rel_field_id=0, icon=None, **em_component_args):
created_field = super(EmField, cls).create(
name=name,
fieldgroup_id=fieldgroup.uid,
fieldtype=fieldtype.name,
optional=optional,
internal=internal,
rel_to_type_id=rel_to_type_id,
rel_field_id=rel_field_id,
icon=icon,
**em_component_args
)
# if not created_field.add_field_column_to_class_table():
# raise RuntimeError("Unable to create the column for the EmField " + str(created_field))
return created_field
## @brief Delete a field if it's not linked
# @return bool : True if deleted False if deletion aborded
# @todo Check if unconditionnal deletion is correct
def delete(self):
# dbe = self.db_engine
# class_table = sql.Table(self.get_class_table(), sqlutils.meta(dbe))
# field_col = sql.Column(self.name)
# ddl = DropColumn(class_table, field_col)
# sqlutils.ddl_execute(ddl, self.db_engine)
return super(EmField, self).delete()
## add_field_column_to_class_table (Function)
#
# Adds a column representing the field in its class' table
#
# @return True in case of success, False if not
# def add_field_column_to_class_table(self):
# dbe = self.db_engine
# fieldtype = get_field_type(self.fieldtype)
# new_column = sql.Column(name=self.name, **(fieldtype.sqlalchemy_args()))
# class_table = sql.Table(self.get_class_table(), sqlutils.meta(dbe))
# ddl = AddColumn(class_table, new_column)
# return sqlutils.ddl_execute(ddl, dbe)
## get_class_table (Function)
#
# Gets the name of the table of the class corresponding to the field
#
# @return Name of the table
# def get_class_table(self):
# return self.get_class().class_table_name
## @brief Get the class that contains this field
# @return An EmClass instance
# def get_class(self):
#<SQL>
# dbe = self.db_engine
# meta = sqlutils.meta(dbe)
# conn = dbe.connect()
# fieldgroup_table = sql.Table(EmFieldGroup.table, meta)
# req = fieldgroup_table.select().where(fieldgroup_table.c.uid == self.fieldgroup_id)
# res = conn.execute(req)
# row = res.fetchone()
#</SQL>
# return EmClass(row['class_id'])