Changeset View
Changeset View
Standalone View
Standalone View
swh/model/model.py
Show First 20 Lines • Show All 261 Lines • ▼ Show 20 Lines | class OriginVisit(BaseModel): | ||||
object_type: Final = "origin_visit" | object_type: Final = "origin_visit" | ||||
origin = attr.ib(type=str, validator=type_validator()) | origin = attr.ib(type=str, validator=type_validator()) | ||||
date = attr.ib(type=datetime.datetime, validator=type_validator()) | date = attr.ib(type=datetime.datetime, validator=type_validator()) | ||||
type = attr.ib(type=str, validator=type_validator()) | type = attr.ib(type=str, validator=type_validator()) | ||||
"""Should not be set before calling 'origin_visit_add()'.""" | """Should not be set before calling 'origin_visit_add()'.""" | ||||
visit = attr.ib(type=Optional[int], validator=type_validator(), default=None) | visit = attr.ib(type=Optional[int], validator=type_validator(), default=None) | ||||
@date.validator | |||||
def check_date(self, attribute, value): | |||||
"""Checks the date has a timezone.""" | |||||
if value is not None and value.tzinfo is None: | |||||
raise ValueError("date must be a timezone-aware datetime.") | |||||
def to_dict(self): | def to_dict(self): | ||||
"""Serializes the date as a string and omits the visit id if it is | """Serializes the date as a string and omits the visit id if it is | ||||
`None`.""" | `None`.""" | ||||
ov = super().to_dict() | ov = super().to_dict() | ||||
if ov["visit"] is None: | if ov["visit"] is None: | ||||
del ov["visit"] | del ov["visit"] | ||||
return ov | return ov | ||||
Show All 17 Lines | class OriginVisitStatus(BaseModel): | ||||
snapshot = attr.ib(type=Optional[Sha1Git], validator=type_validator()) | snapshot = attr.ib(type=Optional[Sha1Git], validator=type_validator()) | ||||
metadata = attr.ib( | metadata = attr.ib( | ||||
type=Optional[ImmutableDict[str, object]], | type=Optional[ImmutableDict[str, object]], | ||||
validator=type_validator(), | validator=type_validator(), | ||||
converter=freeze_optional_dict, | converter=freeze_optional_dict, | ||||
default=None, | default=None, | ||||
) | ) | ||||
@date.validator | |||||
def check_date(self, attribute, value): | |||||
"""Checks the date has a timezone.""" | |||||
if value is not None and value.tzinfo is None: | |||||
raise ValueError("date must be a timezone-aware datetime.") | |||||
class TargetType(Enum): | class TargetType(Enum): | ||||
"""The type of content pointed to by a snapshot branch. Usually a | """The type of content pointed to by a snapshot branch. Usually a | ||||
revision or an alias.""" | revision or an alias.""" | ||||
CONTENT = "content" | CONTENT = "content" | ||||
DIRECTORY = "directory" | DIRECTORY = "directory" | ||||
REVISION = "revision" | REVISION = "revision" | ||||
▲ Show 20 Lines • Show All 305 Lines • ▼ Show 20 Lines | class Content(BaseContent): | ||||
) | ) | ||||
@length.validator | @length.validator | ||||
def check_length(self, attribute, value): | def check_length(self, attribute, value): | ||||
"""Checks the length is positive.""" | """Checks the length is positive.""" | ||||
if value < 0: | if value < 0: | ||||
raise ValueError("Length must be positive.") | raise ValueError("Length must be positive.") | ||||
@ctime.validator | |||||
def check_ctime(self, attribute, value): | |||||
"""Checks the ctime has a timezone.""" | |||||
if value is not None and value.tzinfo is None: | |||||
raise ValueError("ctime must be a timezone-aware datetime.") | |||||
def to_dict(self): | def to_dict(self): | ||||
content = super().to_dict() | content = super().to_dict() | ||||
if content["data"] is None: | if content["data"] is None: | ||||
del content["data"] | del content["data"] | ||||
if content["ctime"] is None: | if content["ctime"] is None: | ||||
del content["ctime"] | del content["ctime"] | ||||
return content | return content | ||||
▲ Show 20 Lines • Show All 58 Lines • ▼ Show 20 Lines | def check_reason(self, attribute, value): | ||||
raise ValueError("Must provide a reason if content is absent.") | raise ValueError("Must provide a reason if content is absent.") | ||||
@length.validator | @length.validator | ||||
def check_length(self, attribute, value): | def check_length(self, attribute, value): | ||||
"""Checks the length is positive or -1.""" | """Checks the length is positive or -1.""" | ||||
if value < -1: | if value < -1: | ||||
raise ValueError("Length must be positive or -1.") | raise ValueError("Length must be positive or -1.") | ||||
@ctime.validator | |||||
def check_ctime(self, attribute, value): | |||||
"""Checks the ctime has a timezone.""" | |||||
if value is not None and value.tzinfo is None: | |||||
raise ValueError("ctime must be a timezone-aware datetime.") | |||||
def to_dict(self): | def to_dict(self): | ||||
content = super().to_dict() | content = super().to_dict() | ||||
if content["origin"] is None: | if content["origin"] is None: | ||||
del content["origin"] | del content["origin"] | ||||
if content["ctime"] is None: | if content["ctime"] is None: | ||||
del content["ctime"] | del content["ctime"] | ||||
return content | return content | ||||
▲ Show 20 Lines • Show All 124 Lines • ▼ Show 20 Lines | def check_id(self, attribute, value): | ||||
if self.type == MetadataTargetType.ORIGIN: | if self.type == MetadataTargetType.ORIGIN: | ||||
if isinstance(value, SWHID) or value.startswith("swh:"): | if isinstance(value, SWHID) or value.startswith("swh:"): | ||||
raise ValueError( | raise ValueError( | ||||
"Got SWHID as id for origin metadata (expected an URL)." | "Got SWHID as id for origin metadata (expected an URL)." | ||||
) | ) | ||||
else: | else: | ||||
self._check_pid(self.type.value, value) | self._check_pid(self.type.value, value) | ||||
@discovery_date.validator | |||||
def check_discovery_date(self, attribute, value): | |||||
"""Checks the discovery_date has a timezone.""" | |||||
if value is not None and value.tzinfo is None: | |||||
raise ValueError("discovery_date must be a timezone-aware datetime.") | |||||
@origin.validator | @origin.validator | ||||
def check_origin(self, attribute, value): | def check_origin(self, attribute, value): | ||||
if value is None: | if value is None: | ||||
return | return | ||||
if self.type not in ( | if self.type not in ( | ||||
MetadataTargetType.SNAPSHOT, | MetadataTargetType.SNAPSHOT, | ||||
MetadataTargetType.RELEASE, | MetadataTargetType.RELEASE, | ||||
▲ Show 20 Lines • Show All 152 Lines • Show Last 20 Lines |