2024-04-17 13:17:40 +00:00
|
|
|
import copy
|
2024-04-14 18:31:43 +00:00
|
|
|
import datetime
|
|
|
|
import uuid
|
2024-04-17 13:17:40 +00:00
|
|
|
from typing import Any
|
|
|
|
from typing import Dict
|
2024-04-14 18:31:43 +00:00
|
|
|
|
2024-04-16 20:42:29 +00:00
|
|
|
from canaille.backends import Backend
|
2023-04-15 11:00:02 +00:00
|
|
|
|
|
|
|
|
2024-04-16 20:42:29 +00:00
|
|
|
class MemoryBackend(Backend):
|
2024-04-17 13:17:40 +00:00
|
|
|
indexes: Dict[str, Dict[str, Any]] = None
|
|
|
|
"""Associates ids and states."""
|
|
|
|
|
|
|
|
attribute_indexes = None
|
|
|
|
"""Associates attribute values and ids."""
|
|
|
|
|
|
|
|
def index(self, model):
|
|
|
|
if not self.indexes:
|
|
|
|
self.indexes = {}
|
|
|
|
|
|
|
|
model_name = model if isinstance(model, str) else model.__name__
|
|
|
|
return self.indexes.setdefault(model_name, {})
|
|
|
|
|
|
|
|
def attribute_index(self, model, attribute="id"):
|
|
|
|
if not self.attribute_indexes:
|
|
|
|
self.attribute_indexes = {}
|
|
|
|
|
|
|
|
model_name = model if isinstance(model, str) else model.__name__
|
|
|
|
return self.attribute_indexes.setdefault(model_name, {}).setdefault(
|
|
|
|
attribute, {}
|
|
|
|
)
|
|
|
|
|
2023-04-15 11:00:02 +00:00
|
|
|
@classmethod
|
2023-12-27 09:57:22 +00:00
|
|
|
def install(cls, config):
|
2023-04-15 11:00:02 +00:00
|
|
|
pass
|
|
|
|
|
|
|
|
def setup(self):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def teardown(self):
|
|
|
|
pass
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def validate(cls, config):
|
|
|
|
pass
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def login_placeholder(cls):
|
|
|
|
return ""
|
|
|
|
|
|
|
|
def has_account_lockability(self):
|
|
|
|
return True
|
2024-04-07 17:56:52 +00:00
|
|
|
|
|
|
|
def get_user_from_login(self, login):
|
|
|
|
from .models import User
|
|
|
|
|
2024-04-14 15:30:59 +00:00
|
|
|
return self.get(User, user_name=login)
|
2024-04-07 18:12:13 +00:00
|
|
|
|
|
|
|
def check_user_password(self, user, password):
|
|
|
|
if password != user.password:
|
|
|
|
return (False, None)
|
|
|
|
|
|
|
|
if user.locked:
|
|
|
|
return (False, "Your account has been locked.")
|
|
|
|
|
|
|
|
return (True, None)
|
|
|
|
|
|
|
|
def set_user_password(self, user, password):
|
|
|
|
user.password = password
|
2024-04-14 18:31:43 +00:00
|
|
|
self.save(user)
|
2024-04-10 13:44:11 +00:00
|
|
|
|
|
|
|
def query(self, model, **kwargs):
|
|
|
|
# if there is no filter, return all models
|
|
|
|
if not kwargs:
|
2024-04-17 13:17:40 +00:00
|
|
|
states = self.index(model).values()
|
2024-04-10 13:44:11 +00:00
|
|
|
return [model(**state) for state in states]
|
|
|
|
|
|
|
|
# get the ids from the attribute indexes
|
|
|
|
ids = {
|
|
|
|
id
|
|
|
|
for attribute, values in kwargs.items()
|
|
|
|
for value in model.serialize(model.listify(values))
|
2024-04-17 13:17:40 +00:00
|
|
|
for id in self.attribute_index(model, attribute).get(value, [])
|
2024-04-10 13:44:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
# get the states from the ids
|
2024-04-17 13:17:40 +00:00
|
|
|
states = [self.index(model)[id] for id in ids]
|
2024-04-10 13:44:11 +00:00
|
|
|
|
|
|
|
# initialize instances from the states
|
|
|
|
instances = [model(**state) for state in states]
|
|
|
|
for instance in instances:
|
|
|
|
# TODO: maybe find a way to not initialize the cache in the first place?
|
|
|
|
instance._cache = {}
|
|
|
|
|
|
|
|
return instances
|
2024-04-10 13:52:16 +00:00
|
|
|
|
|
|
|
def fuzzy(self, model, query, attributes=None, **kwargs):
|
|
|
|
attributes = attributes or model.attributes
|
|
|
|
instances = self.query(model, **kwargs)
|
|
|
|
|
|
|
|
return [
|
|
|
|
instance
|
|
|
|
for instance in instances
|
|
|
|
if any(
|
|
|
|
query.lower() in value.lower()
|
|
|
|
for attribute in attributes
|
|
|
|
for value in model.listify(instance._state.get(attribute, []))
|
|
|
|
if isinstance(value, str)
|
|
|
|
)
|
|
|
|
]
|
2024-04-14 15:30:59 +00:00
|
|
|
|
|
|
|
def get(self, model, identifier=None, /, **kwargs):
|
|
|
|
if identifier:
|
|
|
|
return (
|
|
|
|
self.get(model, **{model.identifier_attribute: identifier})
|
|
|
|
or self.get(model, id=identifier)
|
|
|
|
or None
|
|
|
|
)
|
|
|
|
|
|
|
|
results = self.query(model, **kwargs)
|
|
|
|
return results[0] if results else None
|
2024-04-14 18:31:43 +00:00
|
|
|
|
|
|
|
def save(self, instance):
|
|
|
|
if not instance.id:
|
|
|
|
instance.id = str(uuid.uuid4())
|
|
|
|
|
|
|
|
instance.last_modified = datetime.datetime.now(datetime.timezone.utc).replace(
|
|
|
|
microsecond=0
|
|
|
|
)
|
|
|
|
if not instance.created:
|
|
|
|
instance.created = instance.last_modified
|
|
|
|
|
2024-04-17 13:17:40 +00:00
|
|
|
self.index_delete(instance)
|
|
|
|
self.index_save(instance)
|
2024-04-14 18:31:43 +00:00
|
|
|
instance._cache = {}
|
2024-04-14 18:37:52 +00:00
|
|
|
|
|
|
|
def delete(self, instance):
|
|
|
|
# run the instance delete callback if existing
|
|
|
|
delete_callback = instance.delete() if hasattr(instance, "delete") else iter([])
|
|
|
|
next(delete_callback, None)
|
|
|
|
|
2024-04-17 13:17:40 +00:00
|
|
|
self.index_delete(instance)
|
2024-04-14 18:37:52 +00:00
|
|
|
|
|
|
|
# run the instance delete callback again if existing
|
|
|
|
next(delete_callback, None)
|
2024-04-14 20:51:58 +00:00
|
|
|
|
|
|
|
def reload(self, instance):
|
|
|
|
# run the instance reload callback if existing
|
|
|
|
reload_callback = instance.reload() if hasattr(instance, "reload") else iter([])
|
|
|
|
next(reload_callback, None)
|
|
|
|
|
2024-04-16 20:42:29 +00:00
|
|
|
instance._state = Backend.instance.get(
|
2024-04-14 20:51:58 +00:00
|
|
|
instance.__class__, id=instance.id
|
|
|
|
)._state
|
|
|
|
instance._cache = {}
|
|
|
|
|
|
|
|
# run the instance reload callback again if existing
|
|
|
|
next(reload_callback, None)
|
2024-04-17 13:17:40 +00:00
|
|
|
|
|
|
|
def index_save(self, instance):
|
|
|
|
# update the id index
|
|
|
|
self.index(instance.__class__)[instance.id] = copy.deepcopy(instance._state)
|
|
|
|
|
|
|
|
# update the index for each attribute
|
|
|
|
for attribute in instance.attributes:
|
|
|
|
attribute_values = instance.listify(instance._state.get(attribute, []))
|
|
|
|
for value in attribute_values:
|
|
|
|
self.attribute_index(instance.__class__, attribute).setdefault(
|
|
|
|
value, set()
|
|
|
|
).add(instance.id)
|
|
|
|
|
|
|
|
# update the mirror attributes of the submodel instances
|
|
|
|
for attribute in instance.attributes:
|
|
|
|
model, mirror_attribute = instance.get_model_annotations(attribute)
|
|
|
|
if not model or not self.index(model) or not mirror_attribute:
|
|
|
|
continue
|
|
|
|
|
|
|
|
mirror_attribute_index = self.attribute_index(
|
|
|
|
model, mirror_attribute
|
|
|
|
).setdefault(instance.id, set())
|
|
|
|
for subinstance_id in instance.listify(instance._state.get(attribute, [])):
|
|
|
|
# add the current objet in the subinstance state
|
|
|
|
subinstance_state = self.index(model)[subinstance_id]
|
|
|
|
subinstance_state.setdefault(mirror_attribute, [])
|
|
|
|
subinstance_state[mirror_attribute].append(instance.id)
|
|
|
|
|
|
|
|
# add the current objet in the subinstance index
|
|
|
|
mirror_attribute_index.add(subinstance_id)
|
|
|
|
|
|
|
|
def index_delete(self, instance):
|
|
|
|
if instance.id not in self.index(instance.__class__):
|
|
|
|
return
|
|
|
|
|
|
|
|
old_state = self.index(instance.__class__)[instance.id]
|
|
|
|
|
|
|
|
# update the mirror attributes of the submodel instances
|
|
|
|
for attribute in instance.attributes:
|
|
|
|
attribute_values = instance.listify(old_state.get(attribute, []))
|
|
|
|
for value in attribute_values:
|
|
|
|
self.attribute_index(instance.__class__, attribute)[value].remove(
|
|
|
|
instance.id
|
|
|
|
)
|
|
|
|
|
|
|
|
# update the mirror attributes of the submodel instances
|
|
|
|
model, mirror_attribute = instance.get_model_annotations(attribute)
|
|
|
|
if not model or not self.index(model) or not mirror_attribute:
|
|
|
|
continue
|
|
|
|
|
|
|
|
mirror_attribute_index = self.attribute_index(
|
|
|
|
model, mirror_attribute
|
|
|
|
).setdefault(instance.id, set())
|
|
|
|
for subinstance_id in self.index(instance.__class__)[instance.id].get(
|
|
|
|
attribute, []
|
|
|
|
):
|
|
|
|
# remove the current objet from the subinstance state
|
|
|
|
subinstance_state = self.index(model)[subinstance_id]
|
|
|
|
subinstance_state[mirror_attribute].remove(instance.id)
|
|
|
|
|
|
|
|
# remove the current objet from the subinstance index
|
|
|
|
mirror_attribute_index.remove(subinstance_id)
|
|
|
|
|
|
|
|
# update the index for each attribute
|
|
|
|
for attribute in instance.attributes:
|
|
|
|
attribute_values = instance.listify(old_state.get(attribute, []))
|
|
|
|
for value in attribute_values:
|
|
|
|
if (
|
|
|
|
value in self.attribute_index(instance.__class__, attribute)
|
|
|
|
and instance.id
|
|
|
|
in self.attribute_index(instance.__class__, attribute)[value]
|
|
|
|
):
|
|
|
|
self.attribute_index(instance.__class__, attribute)[value].remove(
|
|
|
|
instance.id
|
|
|
|
)
|
|
|
|
|
|
|
|
# update the id index
|
|
|
|
del self.index(instance.__class__)[instance.id]
|