1
0
Fork 0
mirror of https://github.com/yweber/lodel2.git synced 2026-06-16 15:30:49 +02:00

EM: first draft for a Model Class

This commit is contained in:
ArnAud 2015-07-16 14:36:05 +02:00
commit c73251795c
5 changed files with 67 additions and 9 deletions

View file

View file

@ -0,0 +1,18 @@
# -*- coding: utf-8 -*-
## @file json.py
# Load representation of an EditorialModel from a json file
import json
class EmBackendJson(object):
def __init__(self, json_file):
json_data = open(json_file).read()
self.data = json.loads(json_data)
def load(self):
data = {}
for index, component in self.data.items():
data[int(index)] = component
return data

View file

@ -45,7 +45,7 @@ class EmComponent(object):
# @param id_or_name int|str: name or id of the object
# @throw TypeError if id_or_name is not an integer nor a string
# @throw NotImplementedError if called with EmComponent
def __init__(self, id_or_name):
def __init__(self, data):
if type(self) == EmComponent:
raise NotImplementedError('Abstract class')
@ -55,14 +55,10 @@ class EmComponent(object):
# @see EmComponent::_fields EditorialModel::fieldtypes::EmFieldType
self._fields = OrderedDict([(name, ftype()) for (name, ftype) in (EmComponent._fields + self.__class__._fields)])
# populate
if isinstance(id_or_name, int):
self._fields['uid'].value = id_or_name # read only propertie set
elif isinstance(id_or_name, str):
self.name = id_or_name
else:
raise TypeError('Bad argument: expecting <int> or <str> but got : ' + str(type(id_or_name)))
self.populate()
for name, value in data.items():
if name in self._fields:
self._fields[name].from_string(value)
## @brief Access an attribute of an EmComponent
# This method is overloads the default __getattr__ to search in EmComponents::_fields . If there is an EditorialModel::EmField with a corresponding name in the component

36
EditorialModel/model.py Normal file
View file

@ -0,0 +1,36 @@
# -*- coding: utf-8 -*-
## @file editorialmodel.py
# Manage instance of an editorial model
from EditorialModel.classes import EmClass
class Model(object):
componentClass = EmClass
def __init__(self, backend):
self.backend = backend
self.load()
def load(self):
data = self.backend.load()
self.uids = {}
for uid, component in data.items():
cls_name = 'component' + component['component']
cls = getattr(Model, cls_name)
if cls:
component['uid'] = uid
self.uids[uid] = cls(component)
# save data using the current backend
def save(self):
return self.backend.save()
# change the current backend
def set_backend(self, backend):
self.backend = backend
# return a list of all EmClass of the model
def classes():
classes = [component for component in self.data.uids if isinstance(component, EmClass)]
return classes

View file

@ -0,0 +1,8 @@
{
"1" : {
"component":"Class", "name":"textes", "string":"{\"fre\":\"Texte\"}", "help":"{}", "rank":"1", "date_update":"", "date_create":"", "classtype":"entity", "icon":"0", "sortcolumn":"rank"
},
"2" : {
"component":"Class", "name":"publications", "string":"{\"fre\":\"Rubriques\"}", "help":"{}", "rank":"1", "date_update":"", "date_create":"", "classtype":"entity", "icon":"0", "sortcolumn":"rank"
}
}