|
|
@@ -1,9 +1,16 @@
|
|
|
import yaml
|
|
|
+from jinja2 import Template
|
|
|
import uuid
|
|
|
from planner import db, validator
|
|
|
|
|
|
|
|
|
class SchemaMixin:
|
|
|
+ template_term = """{{ obj.uuid }} {{ obj }}"""
|
|
|
+
|
|
|
+ @property
|
|
|
+ def as_term(self):
|
|
|
+ return Template(self.template_term).render(obj=self)
|
|
|
+
|
|
|
@classmethod
|
|
|
def class_name(cls):
|
|
|
return cls.__qualname__.lower()
|
|
|
@@ -32,24 +39,29 @@ class DbMixin:
|
|
|
|
|
|
@classmethod
|
|
|
def _get_cache_key(cls, document):
|
|
|
- return tuple([cls] + [document[k] for k in cls.cache_keys])
|
|
|
+ return tuple([cls] + [document[k] if k in document else 'None' for k in cls.cache_keys])
|
|
|
|
|
|
def load_reverse(self):
|
|
|
...
|
|
|
|
|
|
+ @classmethod
|
|
|
+ def get_class(cls, document):
|
|
|
+ return cls
|
|
|
+
|
|
|
@classmethod
|
|
|
def get(cls, document):
|
|
|
- key = cls._get_cache_key(document)
|
|
|
- if key in cls.cache:
|
|
|
- instance = cls.cache[key]
|
|
|
+ kls = cls.get_class(document)
|
|
|
+ key = kls._get_cache_key(document)
|
|
|
+ if key in kls.cache:
|
|
|
+ instance = kls.cache[key]
|
|
|
else:
|
|
|
- instance = cls(document=document, **document)
|
|
|
+ instance = kls(document=document, **document)
|
|
|
# The cache is updated a first time so that instances looking up
|
|
|
# for this instance during the load_reverse can find it
|
|
|
- cls.cache[key] = instance
|
|
|
+ kls.cache[key] = instance
|
|
|
instance.load_reverse()
|
|
|
# The cache value is updated with its final value
|
|
|
- cls.cache[key] = instance
|
|
|
+ kls.cache[key] = instance
|
|
|
return instance
|
|
|
|
|
|
@classmethod
|