From 35c832515931d5f2a01756c4ca15315e607a9a9e Mon Sep 17 00:00:00 2001 From: Burathar Date: Sat, 27 Mar 2021 13:28:17 +0100 Subject: [PATCH] Make yaml_serializable a recursive_property --- biscd/biscd/models/recursive_property.py | 37 +++++++++++++++++++++ biscd/biscd/models/user.py | 10 ++++++ biscd/biscd/models/yaml_serializable.py | 41 +++++------------------- installation-files/projects_example.yaml | 4 +++ installation-files/users_example.yaml | 5 ++- 5 files changed, 63 insertions(+), 34 deletions(-) create mode 100644 biscd/biscd/models/recursive_property.py diff --git a/biscd/biscd/models/recursive_property.py b/biscd/biscd/models/recursive_property.py new file mode 100644 index 0000000..5dd43bb --- /dev/null +++ b/biscd/biscd/models/recursive_property.py @@ -0,0 +1,37 @@ +class RecursiveProperty: + + def __init__(self, property_key): + self.property_key = property_key + + def config_dict(self, values_only=False): + # pylint: disable=no-member + property_key_value = getattr(self, self.property_key) + if property_key_value is None: + raise TypeError(f"{self.property_key} cannot be None") + property_dict = {k: v for k, v in self.__dict__.items() if v is not None} + property_dict.pop(self.property_key) + property_dict.pop('property_key') + if values_only: + return property_dict + return {property_key_value : property_dict} + + @classmethod + def _from_dict(cls, property_dict, property_key): + if property_dict is None: + return None + + # Extract the name + property_name = [*property_dict][0] + + # Step into object + property_dict = property_dict.get(property_name) + + # Add name to dict + property_dict[property_key] = property_name + + # Create empty instance + recursive_property = cls(property_key) + + # Fill instance with dict + recursive_property.__dict__ = property_dict + return recursive_property diff --git a/biscd/biscd/models/user.py b/biscd/biscd/models/user.py index 0eec111..7a8697f 100644 --- a/biscd/biscd/models/user.py +++ b/biscd/biscd/models/user.py @@ -5,6 +5,7 @@ from flask_login import UserMixin from biscd import config from biscd import login from .yaml_serializable import YamlSerializable +from .project import Project class User(UserMixin, YamlSerializable): @@ -23,10 +24,19 @@ class User(UserMixin, YamlSerializable): return self.name def __init__(self, name=None, email=None): + super().__init__() self.name = name self.email = email self.password_hash = None + @property + def projects(self): + pass #all_projects = Project.get() + + @projects.setter + def projects(self, projects): + pass + def set_password(self, password): self.password_hash = generate_password_hash(password) diff --git a/biscd/biscd/models/yaml_serializable.py b/biscd/biscd/models/yaml_serializable.py index d8dfd5b..61a089d 100644 --- a/biscd/biscd/models/yaml_serializable.py +++ b/biscd/biscd/models/yaml_serializable.py @@ -2,6 +2,8 @@ from abc import ABCMeta, abstractmethod from flask import abort import yaml +from .recursive_property import RecursiveProperty + class MyMeta(metaclass=ABCMeta): required_attributes = [] @@ -13,10 +15,13 @@ class MyMeta(metaclass=ABCMeta): raise ValueError('required attribute (%s) not set' % attr_name) return obj -class YamlSerializable(object): +class YamlSerializable(RecursiveProperty): __metaclass__ = MyMeta required_attributes = ['name'] + def __init__(self): + self.property_key='name' + @classmethod #@property @abstractmethod @@ -29,16 +34,6 @@ class YamlSerializable(object): def _yaml_object_name(cls): pass - def config_dict(self, properties_only=False): - # pylint: disable=no-member - if self.name is None: - raise TypeError("Name cannot be None") - ymlserializable_dict = {k: v for k, v in self.__dict__.items() if v is not None} - ymlserializable_dict.pop('name') - if properties_only: - return ymlserializable_dict - return {self.name: ymlserializable_dict} - def save(self, overwrite=True): # pylint: disable=no-member if self.name is None: @@ -49,7 +44,7 @@ class YamlSerializable(object): if self.name == [*ymlsl][0]: found_match = True if overwrite: - ymlsl[self.name] = self.config_dict(properties_only=True) + ymlsl[self.name] = self.config_dict(values_only=True) else: raise ValueError( f"A {type(self).__name__} with name {self.name} already exists!") @@ -102,7 +97,7 @@ class YamlSerializable(object): ymlsls = [] for ymlsl_dict in ymlsl_dicts: - ymlsls.append(cls._from_dict(ymlsl_dict)) + ymlsls.append(cls._from_dict(ymlsl_dict, 'name')) return ymlsls @classmethod @@ -114,26 +109,6 @@ class YamlSerializable(object): ymlserializables_list.append(name) return ymlserializables_list - @classmethod - def _from_dict(cls, ymldict): - if ymldict is None: - return None - - # Extract the name - ymlsl_name = [*ymldict][0] - - # Step into object - ymldict = ymldict.get(ymlsl_name) - - # Add name to dict - ymldict['name'] = ymlsl_name - - # Create empty instance - ymlsl = cls() - - # Fill instance with dict - ymlsl.__dict__ = ymldict - return ymlsl @classmethod def _get_all_from_file(cls): diff --git a/installation-files/projects_example.yaml b/installation-files/projects_example.yaml index 03a88c4..dd7f46a 100644 --- a/installation-files/projects_example.yaml +++ b/installation-files/projects_example.yaml @@ -5,6 +5,10 @@ projects: requirements_file: requirements.txt secret: thisissecret tests: tests.py + access: + public: true + owners: + - Carl - Foo: branch: master git_repo: https://git.sciuro.org/Burathar/The-Hunt diff --git a/installation-files/users_example.yaml b/installation-files/users_example.yaml index 0381181..16863d1 100644 --- a/installation-files/users_example.yaml +++ b/installation-files/users_example.yaml @@ -4,4 +4,7 @@ users: password_hash: pbkdf2:sha256:150000$uuFRyvLs$ee9863f169db786e82b9e2abe0c2cf3434e925479d0919f3b4046ebbfa0aeb28 - Carl: email: carl@mail.com - password_hash: pbkdf2:sha256:150000$v4UyXVie$898ba71cdf3adefd7c1b6ed611897a3c8ca3151bd2628b490a052f554bae113e \ No newline at end of file + password_hash: pbkdf2:sha256:150000$v4UyXVie$898ba71cdf3adefd7c1b6ed611897a3c8ca3151bd2628b490a052f554bae113e +- Marijn: + email: e2@mail.com + password_hash: pbkdf2:sha256:150000$10ZT4FFC$f96185d4279df8fdef73ad459d7f109d18e92f517ed463d9a0b8de6cc208be6a