Skip to content
Snippets Groups Projects
LinkModel.py 967 B
Newer Older
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
import logging, operator
from typing import Dict, List
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
from common.orm.fields.PrimaryKeyField import PrimaryKeyField
from common.orm.fields.StringField import StringField
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
from common.orm.model.Model import Model, get_related_instances
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed

LOGGER = logging.getLogger(__name__)

class LinkModel(Model):
    pk = PrimaryKeyField()
    link_uuid = StringField(required=True, allow_empty=False)

    def dump_id(self) -> Dict:
        return {'link_uuid': {'uuid': self.link_uuid}}

Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
    def dump_endpoint_ids(self) -> List[Dict]:
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
        from .RelationModels import LinkEndPointModel # pylint: disable=import-outside-toplevel
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
        db_endpoints = get_related_instances(self, LinkEndPointModel, 'endpoint_fk')
        return [db_endpoint.dump_id() for db_endpoint in sorted(db_endpoints, key=operator.attrgetter('pk'))]
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed

    def dump(self) -> Dict:
        return {
            'link_id': self.dump_id(),
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
            'link_endpoint_ids': self.dump_endpoint_ids(),
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
        }