-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathmodels.py
More file actions
66 lines (51 loc) · 1.78 KB
/
models.py
File metadata and controls
66 lines (51 loc) · 1.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
"""
Models that implement units
"""
from __future__ import annotations
from typing import override
from django.core.exceptions import ValidationError
from django.db import models
from ..containers.models import Container, ContainerVersion
from ..publishing.models import PublishableEntity
__all__ = [
"Unit",
"UnitVersion",
]
@Container.register_subclass
class Unit(Container):
"""
A Unit is type of Container that holds Components.
Via Container and its PublishableEntityMixin, Units are also publishable
entities and can be added to other containers.
"""
type_code = "unit"
olx_tag_name = "vertical" # Serializes to OLX as `<unit>...</unit>`.
container = models.OneToOneField(
Container,
on_delete=models.CASCADE,
parent_link=True,
primary_key=True,
)
@override
@classmethod
def validate_entity(cls, entity: PublishableEntity) -> None:
"""Check if the given entity is allowed as a child of a Unit"""
# Units only allow Components as children, so the entity must be 1:1 with Component:
if not hasattr(entity, "component"):
raise ValidationError("Only Components can be added as children of a Unit")
class UnitVersion(ContainerVersion):
"""
A UnitVersion is a specific version of a Unit.
Via ContainerVersion and its EntityList, it defines the list of Components
in this version of the Unit.
"""
container_version = models.OneToOneField(
ContainerVersion,
on_delete=models.CASCADE,
parent_link=True,
primary_key=True,
)
@property
def unit(self) -> Unit:
"""Convenience accessor to the Unit this version is associated with"""
return self.container_version.container.unit # pylint: disable=no-member