Changeset View
Changeset View
Standalone View
Standalone View
swh/web/common/models.py
# Copyright (C) 2018-2019 The Software Heritage developers | # Copyright (C) 2018-2021 The Software Heritage developers | ||||
# See the AUTHORS file at the top-level directory of this distribution | # See the AUTHORS file at the top-level directory of this distribution | ||||
# License: GNU Affero General Public License version 3, or any later version | # License: GNU Affero General Public License version 3, or any later version | ||||
# See top-level LICENSE file for more information | # See top-level LICENSE file for more information | ||||
from django.db import models | from django.db import models | ||||
from swh.web.common.typing import SaveOriginRequestInfo | |||||
class SaveAuthorizedOrigin(models.Model): | class SaveAuthorizedOrigin(models.Model): | ||||
""" | """ | ||||
Model table holding origin urls authorized to be loaded into the archive. | Model table holding origin urls authorized to be loaded into the archive. | ||||
""" | """ | ||||
url = models.CharField(max_length=200, null=False) | url = models.CharField(max_length=200, null=False) | ||||
▲ Show 20 Lines • Show All 84 Lines • ▼ Show 20 Lines | class SaveOriginRequest(models.Model): | ||||
) | ) | ||||
class Meta: | class Meta: | ||||
app_label = "swh_web_common" | app_label = "swh_web_common" | ||||
db_table = "save_origin_request" | db_table = "save_origin_request" | ||||
ordering = ["-id"] | ordering = ["-id"] | ||||
indexes = [models.Index(fields=["origin_url", "status"])] | indexes = [models.Index(fields=["origin_url", "status"])] | ||||
def __str__(self): | def to_dict(self) -> SaveOriginRequestInfo: | ||||
return str( | """Map the request save model object to a json serializable dict. | ||||
{ | |||||
"id": self.id, | Returns: | ||||
"request_date": self.request_date, | The corresponding SaveOriginRequetsInfo json serializable dict. | ||||
"visit_type": self.visit_type, | |||||
"visit_status": self.visit_status, | """ | ||||
"origin_url": self.origin_url, | visit_date = self.visit_date | ||||
"status": self.status, | return SaveOriginRequestInfo( | ||||
"loading_task_id": self.loading_task_id, | id=self.id, | ||||
"visit_date": self.visit_date, | origin_url=self.origin_url, | ||||
} | visit_type=self.visit_type, | ||||
save_request_date=self.request_date.isoformat(), | |||||
save_request_status=self.status, | |||||
save_task_status=self.loading_task_status, | |||||
visit_status=self.visit_status, | |||||
visit_date=visit_date.isoformat() if visit_date else None, | |||||
loading_task_id=self.loading_task_id, | |||||
) | ) | ||||
def __str__(self) -> str: | |||||
return str(self.to_dict()) | |||||
ardumont: I could probably revert this. My initial intent was to use it in the conversion code
below. | |||||
Done Inline ActionsI agree with you, the dict conversion should be handled directly in the model. anlambert: I agree with you, the dict conversion should be handled directly in the model. |
I could probably revert this. My initial intent was to use it in the conversion code
below. Then as i moved towards the SaveOiriginRequestInfo typed dict, i stopped using
it.
I also wondered whether to have a to_dict method which would return directly the
SaveOriginRequestInfo dict. I don't realize if this is reasonable or not (that would
allow to avoid an intermediary function for one which sounds good to me).
@anlambert any thoughts on those matters?