From a86ff9ee97e5cb3308c6b08ad9dac5b3b0edcbc1 Mon Sep 17 00:00:00 2001 From: ArnAud Date: Fri, 29 May 2015 17:09:10 +0200 Subject: [PATCH] ME : renommage et import --- EditorialModel/{class.py => classes.py} | 26 ++++++--- EditorialModel/classtypes.py | 55 +++++++++++++++++++ .../{component.py => components.py} | 34 ++++++++++-- .../{fieldgroup.py => fieldgroups.py} | 3 +- EditorialModel/{field.py => fields.py} | 2 +- EditorialModel/test/test_component.py | 2 +- EditorialModel/{type.py => types.py} | 6 +- 7 files changed, 107 insertions(+), 21 deletions(-) rename EditorialModel/{class.py => classes.py} (66%) create mode 100644 EditorialModel/classtypes.py rename EditorialModel/{component.py => components.py} (77%) rename EditorialModel/{fieldgroup.py => fieldgroups.py} (96%) rename EditorialModel/{field.py => fields.py} (98%) rename EditorialModel/{type.py => types.py} (98%) diff --git a/EditorialModel/class.py b/EditorialModel/classes.py similarity index 66% rename from EditorialModel/class.py rename to EditorialModel/classes.py index 483ba42..1c21896 100644 --- a/EditorialModel/class.py +++ b/EditorialModel/classes.py @@ -5,32 +5,40 @@ @see EmClass, EmType, EmFieldGroup, EmField """ -from component import EmComponent +from EditorialModel.components import EmComponent, EmComponentNotExistError +import EditorialModel.classtypes +from Database.sqlwrapper import SqlWrapper -class EmClass(EmComponent) - def __init(id_or_name): +class EmClass(EmComponent): + def __init__(self, id_or_name): self.table = 'em_class' - pass + super(EmClass, self).__init__(id_or_name) """ create a new class @param name str: name of the new class @param class_type EmClasstype: type of the class """ @staticmethod - def create(self, name, class_type): - pass + def create(name, class_type): + #try: + exists = EmClass(name) + #except EmComponentNotExistError: + #print ("bin") + #pass + print (name, class_type) + pass """ retrieve list of the field_groups of this class @return field_groups [EmFieldGroup]: """ def field_groups(): - pass + pass """ retrieve list of fields @return fields [EmField]: """ def fields(): - pass + pass """ retrieve list of type of this class @return types [EmType]: @@ -42,7 +50,7 @@ class EmClass(EmComponent) @param t EmType: type to link @return success bool: done or not """ - def link_type(t ): + def link_type(t): pass """ retrieve list of EmType that are linked to this class diff --git a/EditorialModel/classtypes.py b/EditorialModel/classtypes.py new file mode 100644 index 0000000..18f6e4c --- /dev/null +++ b/EditorialModel/classtypes.py @@ -0,0 +1,55 @@ +# -*- coding: utf-8 -*- + +""" + representation of the classTypes (sic…) +""" + +class EmNature(object): + PARENT = 'parent' + TRANSLATION = 'translation' + IDENTITY = 'identity' + +class EmClassType(object): + + entity = { + 'name' : 'entity', + 'hierarchy' : [ + { + 'nature' : EmNature.PARENT, + 'attach' : 'type', + 'editable' : True, + }, + { + 'nature' : EmNature.TRANSLATION, + 'attach' : 'type', + 'editable' : True, + }, + ], + } + + entry = { + 'name' : 'entry', + 'hierarchy' : [ + { + 'nature' : EmNature.PARENT, + 'attach' : 'type', + 'editable' : True, + }, + { + 'nature' : EmNature.TRANSLATION, + 'attach' : 'type', + 'editable' : True, + }, + ], + } + + person = { + 'name' : 'person', + 'hierarchy' : [ + { + 'nature' : EmNature.IDENTITY, + 'attach' : 'classtype', + 'editable' : False, + }, + ], + } diff --git a/EditorialModel/component.py b/EditorialModel/components.py similarity index 77% rename from EditorialModel/component.py rename to EditorialModel/components.py index 7efe869..fddc094 100644 --- a/EditorialModel/component.py +++ b/EditorialModel/components.py @@ -6,7 +6,10 @@ """ from Lodel.utils.mlstring import MlString +from Database.sqlwrapper import SqlWrapper +from Database.sqlobject import SqlObject import logging +import sqlalchemy as sql logger = logging.getLogger('Lodel2.EditorialModel') @@ -17,11 +20,13 @@ class EmComponent(object): @exception TypeError """ def __init__(self, id_or_name): + SqlWrapper.start() if self is EmComponent: raise EnvironmentError('Abstract class') if type(id_or_name) is int: self.id = id_or_name elif type(id_or_name) is str: + self.id = None self.name = id_or_name self.populate() else: @@ -30,12 +35,28 @@ class EmComponent(object): """ Lookup in the database properties of the object to populate the properties """ def populate(self): - if self.id is None: - where = "name = " + db.quote(self.name) - else: - where = "id = " + self.id + dbo = SqlObject(self.table) + + t = dbo.table + + req = dbo.sel + print(t.c.__dict__) + + if self.id is None: + req.where(t.c.name == self.name) + else: + req.where(dbo.col.id == self.id) + + sqlresult = dbo.rexec(req) + print (sqlresult) + + # Transformation du résultat en une liste de dictionnaires + records = sqlresult.fetchall() + print (records) + + for record in records: + selected_lines.append(dict(zip(record.keys(), record))) - row = db.query('*', self.table, where) if not row: # could have two possible Error message for id and for name raise EmComponentNotExistError("Bad id_or_name: could not find the component") @@ -89,3 +110,6 @@ class EmComponent(object): """ def get_string(self, lang): pass + +class EmComponentNotExistError(Exception): + pass diff --git a/EditorialModel/fieldgroup.py b/EditorialModel/fieldgroups.py similarity index 96% rename from EditorialModel/fieldgroup.py rename to EditorialModel/fieldgroups.py index d653fc6..73f1eff 100644 --- a/EditorialModel/fieldgroup.py +++ b/EditorialModel/fieldgroups.py @@ -1,7 +1,6 @@ #-*- coding: utf-8 -*- -from component import EmComponent - +from EditorialModel.components import EmComponent class EmFieldGroup(EmComponent): """ Represents groups of EmField diff --git a/EditorialModel/field.py b/EditorialModel/fields.py similarity index 98% rename from EditorialModel/field.py rename to EditorialModel/fields.py index cdc3d71..c390d33 100644 --- a/EditorialModel/field.py +++ b/EditorialModel/fields.py @@ -1,6 +1,6 @@ #-*- coding: utf-8 -*- -from component import EmComponent +from EditorialModel.components import EmComponent """Represent one data for a lodel2 document""" class EmField(EmComponent): diff --git a/EditorialModel/test/test_component.py b/EditorialModel/test/test_component.py index 7d19690..084aa83 100644 --- a/EditorialModel/test/test_component.py +++ b/EditorialModel/test/test_component.py @@ -1,6 +1,6 @@ #from django.test import TestCase from unittest import TestCase -from EditorialModel.lib.component import EmComponent +from EditorialModel.component import EmComponent class ComponentTestCase(TestCase): diff --git a/EditorialModel/type.py b/EditorialModel/types.py similarity index 98% rename from EditorialModel/type.py rename to EditorialModel/types.py index e8d562a..47df4b3 100644 --- a/EditorialModel/type.py +++ b/EditorialModel/types.py @@ -1,16 +1,16 @@ #-*- coding: utf-8 -*- -from component import EmComponent +from EditorialModel.components import EmComponent class EmType(EmComponent): """ Represents type of documents - + A type is a specialisation of a class, it can select optional field, they have hooks, are organized in hierarchy and linked to other EmType with special fields called relation_to_type fields @see EmComponent """ - + def __init__(id_or_name): """ Instanciate an EmType with data fetched from db @param id_or_name str|int: Identify the EmType by name or by global_id