123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217 |
- # -*- coding: utf-8 -*-
-
- object_uid = 'lodel_id'
- object_em_class_id = 'class_id'
- object_em_type_id = 'type_id'
-
- relation_uid = 'id_relation'
- relation_superior = 'superior'
- relation_subordinate = 'subordinate'
- relation_name = 'relation_name'
-
-
- common_fields = {
- object_uid: {
- 'fieldtype': 'pk',
- 'internal': 'autosql',
- 'immutable' : True,
- 'string': '{"___": "", "fre": "identifiant lodel", "eng": "lodel identifier"}'
- },
- object_em_class_id : {
- 'fieldtype': 'emuid',
- 'is_id_class': True,
- 'internal': 'automatic',
- 'immutable' : True,
- 'string': '{"___": "", "fre": "identifiant de la classe", "eng": "class identifier"}'
- },
- object_em_type_id : {
- 'fieldtype': 'emuid',
- 'is_id_class': False,
- 'internal': 'automatic',
- 'immutable' : True,
- 'string': '{"___": "", "fre": "identifiant de la type", "eng": "type identifier"}'
- },
- 'string': {
- 'fieldtype': 'char',
- 'max_length': 128,
- 'internal': 'automatic',
- 'nullable': True,
- 'immutable' : False,
- 'string': '{"___": "", "fre": "Représentation textuel", "eng": "String representation"}',
- },
- 'creation_date': {
- 'fieldtype': 'datetime',
- 'now_on_create': True,
- 'internal': 'autosql',
- 'immutable' : True,
- 'string': '{"___": "", "fre": "Date de création", "eng": "Creation date"}',
- },
- 'modification_date': {
- 'fieldtype': 'datetime',
- 'now_on_create': True,
- 'now_on_update': True,
- 'internal': 'autosql',
- 'immutable' : True,
- 'string': '{"___": "", "fre": "Date de modification", "eng": "Modification date"}',
- }
- }
-
- relations_common_fields = {
- relation_uid: {
- 'fieldtype': 'pk',
- 'internal': 'autosql',
- 'immutable' : True,
- },
- 'nature': {
- 'fieldtype': 'naturerelation',
- 'immutable' : True,
- },
- 'depth': {
- 'fieldtype': 'integer',
- 'internal': 'automatic',
- 'immutable' : True,
- },
- 'rank': {
- 'fieldtype': 'rank',
- 'internal': 'automatic',
- 'immutable' : True,
- },
- relation_superior : {
- 'fieldtype': 'leo',
- 'superior': True,
- 'immutable' : True,
- },
- relation_subordinate: {
- 'fieldtype': 'leo',
- 'superior': False,
- 'immutable' : True,
- },
- relation_name: {
- 'fieldtype': 'namerelation',
- 'max_length': 128,
- 'immutable' : True,
- }
- }
-
-
- def pk_name():
- for name, option in common_fields.items():
- if option['fieldtype'] == 'pk':
- return name
-
-
- ## EmNature (Class)
- #
- # constant name for the nature of type hierarchy
- class EmNature(object):
- PARENT = 'parent'
- TRANSLATION = 'translation'
- IDENTITY = 'identity'
-
- @classmethod
- def getall(cls):
- return [cls.PARENT, cls.TRANSLATION, cls.IDENTITY]
-
-
- ## EmClassType (Class)
- #
- # Representation of the classTypes
- #
- # Defines 3 generic classtype : entity, entry and person
- # - entity : to define editorial content
- # - entry : to define keywords
- # - person : to define people (in the real world, this classtype will only have one class and one type)
- #
- # The hierarchy dict fixes the possible hierarchical links the types of each classtype can have :
- # - 'attach' : what type of superior a type can have
- # - 'classtype' a type can have superiors of the same classtype
- # - 'type' a type can only have superiors of the same type
- # - automatic : possible superiors
- # - False : possible superiors must be defined
- # - True : possible superiors can not be defined, they will be enforced by the ME automatically
- # - maxdepth : maximum depth
- # - maxchildren : maximum children
- #
- # Classtypes contains default internal fields too
- #
- class EmClassType(object):
-
- entity = {
- 'name': 'entity',
- 'hierarchy': {
- EmNature.PARENT: {
- 'attach': 'classtype',
- 'automatic': False,
- 'maxdepth': -1,
- 'maxchildren': -1
- },
- EmNature.TRANSLATION: {
- 'attach': 'type',
- 'automatic': False,
- 'maxdepth': 1,
- 'maxchildren': -1
- },
- },
- 'default_fields': {}
- }
-
- entry = {
- 'name': 'entry',
- 'hierarchy': {
- EmNature.PARENT: {
- 'attach': 'type',
- 'automatic': False,
- },
- EmNature.TRANSLATION: {
- 'attach': 'type',
- 'automatic': False,
- 'maxdepth': 1,
- 'maxchildren': -1
- },
- },
- 'default_fields': {}
- }
-
- person = {
- 'name': 'person',
- 'hierarchy': {
- EmNature.IDENTITY: {
- 'attach': 'classtype',
- 'automatic': True,
- 'maxdepth': -1,
- 'maxchildren': 1
- },
- },
- 'default_fields': {}
- }
-
- ## @brief return a classtype from its name
- # @param cls
- # @param classtype str : A classtype name
- # @return None if no classtype with this name, else return a dict containing classtype informations
- @classmethod
- def get(cls, classtype):
- try:
- return getattr(cls, classtype.lower())
- except AttributeError:
- return None
-
- ## @brief Get all the classtype
- # @return A list of dict representing classtypes
- @classmethod
- def getall(cls):
- return [cls.entity, cls.entry, cls.person]
-
- ## @brief Return possible nature of relations for a classtype name
- #
- # @param classtype_name str: The classtype name
- # @return A list of EmNature names (list of str)
- @staticmethod
- def natures(classtype_name):
- if not isinstance(classtype_name, str):
- raise TypeError("Excepted <class str> but got %s" % str(type(classtype_name)))
- try:
- classtype = getattr(EmClassType, classtype_name)
- except AttributeError:
- raise AttributeError("Unknown classtype : '%s'" % classtype_name)
- return classtype['hierarchy'].keys()
|