Changeset View
Changeset View
Standalone View
Standalone View
swh/web/common/models.py
# Copyright (C) 2018-2021 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 | from swh.web.common.typing import AddForgeRequestInfo, 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 111 Lines • ▼ Show 20 Lines | def to_dict(self) -> SaveOriginRequestInfo: | ||||
visit_status=self.visit_status, | visit_status=self.visit_status, | ||||
visit_date=visit_date.isoformat() if visit_date else None, | visit_date=visit_date.isoformat() if visit_date else None, | ||||
loading_task_id=self.loading_task_id, | loading_task_id=self.loading_task_id, | ||||
note=self.note, | note=self.note, | ||||
) | ) | ||||
def __str__(self) -> str: | def __str__(self) -> str: | ||||
return str(self.to_dict()) | return str(self.to_dict()) | ||||
class AddAuthorizedForge(models.Model): | |||||
"""Model table holding authorized forge urls to be loaded into the archive. | |||||
""" | |||||
url = models.CharField(max_length=200, null=False) | |||||
class Meta: | |||||
app_label = "swh_web_common" | |||||
db_table = "add_authorized_forge" | |||||
indexes = [models.Index(fields=["url"])] | |||||
def __str__(self): | |||||
return self.url | |||||
class AddUnauthorizedForge(models.Model): | |||||
"""Model table holding unauthorized forge urls to be loaded into the archive. | |||||
""" | |||||
url = models.CharField(max_length=200, null=False) | |||||
class Meta: | |||||
app_label = "swh_web_common" | |||||
db_table = "add_unauthorized_forge" | |||||
indexes = [models.Index(fields=["url"])] | |||||
def __str__(self): | |||||
return self.url | |||||
ADD_FORGE_REQUEST_ACCEPTED = "accepted" | |||||
ADD_FORGE_REQUEST_REJECTED = "rejected" | |||||
ADD_FORGE_REQUEST_PENDING = "pending" | |||||
ADD_FORGE_REQUEST_STATUS = [ | |||||
(ADD_FORGE_REQUEST_ACCEPTED, ADD_FORGE_REQUEST_ACCEPTED), | |||||
(ADD_FORGE_REQUEST_REJECTED, ADD_FORGE_REQUEST_REJECTED), | |||||
(ADD_FORGE_REQUEST_PENDING, ADD_FORGE_REQUEST_PENDING), | |||||
] | |||||
ADD_FORGE_TASK_NOT_CREATED = "not created" | |||||
ADD_FORGE_TASK_NOT_YET_SCHEDULED = "not yet scheduled" | |||||
ADD_FORGE_TASK_SCHEDULED = "scheduled" | |||||
ADD_FORGE_TASK_SUCCEEDED = "succeeded" | |||||
ADD_FORGE_TASK_FAILED = "failed" | |||||
ADD_FORGE_TASK_RUNNING = "running" | |||||
ADD_FORGE_TASK_STATUS = SAVE_TASK_STATUS | |||||
class AddForgeRequest(models.Model): | |||||
"""The "add forge" users issued requests table. | |||||
""" | |||||
id = models.BigAutoField(primary_key=True) | |||||
request_date = models.DateTimeField(auto_now_add=True) | |||||
forge_type = models.CharField(max_length=200, null=False) | |||||
forge_url = models.CharField(max_length=200, null=False) | |||||
request_status = models.TextField( | |||||
choices=ADD_FORGE_REQUEST_STATUS, default=ADD_FORGE_REQUEST_PENDING | |||||
) | |||||
task_id = models.IntegerField(default=-1) | |||||
task_date = models.DateTimeField(null=True) | |||||
task_status = models.TextField( | |||||
choices=ADD_FORGE_TASK_STATUS, default=ADD_FORGE_TASK_NOT_CREATED | |||||
) | |||||
# store ids of users that submitted the request as string list | |||||
user_ids = models.TextField(null=True) | |||||
note = models.TextField(null=True) | |||||
class Meta: | |||||
app_label = "swh_web_common" | |||||
db_table = "add_forge_request" | |||||
ordering = ["-id"] | |||||
indexes = [models.Index(fields=["forge_url", "request_status"])] | |||||
def to_dict(self) -> AddForgeRequestInfo: | |||||
"""Map the request save model object to a json serializable dict. | |||||
Returns: | |||||
The corresponding AddForgeRequestInfo json serializable dict. | |||||
""" | |||||
return AddForgeRequestInfo( | |||||
id=self.id, | |||||
forge_url=self.forge_url, | |||||
forge_type=self.forge_type, | |||||
request_date=self.request_date.isoformat(), | |||||
request_status=self.request_status, | |||||
task_id=self.task_id, | |||||
task_date=self.task_date.isoformat() if self.task_date else None, | |||||
task_status=self.task_status, | |||||
note=self.note, | |||||
) | |||||
def __str__(self) -> str: | |||||
return str(self.to_dict()) |