Changeset View
Changeset View
Standalone View
Standalone View
swh/deposit/models.py
# Copyright (C) 2017-2020 The Software Heritage developers | # Copyright (C) 2017-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 General Public License version 3, or any later version | # License: GNU 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 | ||||
# Generated from: | # Generated from: | ||||
# cd swh_deposit && \ | # cd swh_deposit && \ | ||||
# python3 -m manage inspectdb | # python3 -m manage inspectdb | ||||
import datetime | import datetime | ||||
from typing import Optional | |||||
from django.contrib.auth.models import User, UserManager | from django.contrib.auth.models import User, UserManager | ||||
from django.contrib.postgres.fields import ArrayField, JSONField | from django.contrib.postgres.fields import ArrayField, JSONField | ||||
from django.db import models | from django.db import models | ||||
from django.utils.timezone import now | from django.utils.timezone import now | ||||
from swh.auth.django.models import OIDCUser | |||||
from .config import ( | from .config import ( | ||||
ARCHIVE_TYPE, | ARCHIVE_TYPE, | ||||
DEPOSIT_STATUS_DEPOSITED, | DEPOSIT_STATUS_DEPOSITED, | ||||
DEPOSIT_STATUS_LOAD_FAILURE, | DEPOSIT_STATUS_LOAD_FAILURE, | ||||
DEPOSIT_STATUS_LOAD_SUCCESS, | DEPOSIT_STATUS_LOAD_SUCCESS, | ||||
DEPOSIT_STATUS_PARTIAL, | DEPOSIT_STATUS_PARTIAL, | ||||
DEPOSIT_STATUS_REJECTED, | DEPOSIT_STATUS_REJECTED, | ||||
DEPOSIT_STATUS_VERIFIED, | DEPOSIT_STATUS_VERIFIED, | ||||
▲ Show 20 Lines • Show All 62 Lines • ▼ Show 20 Lines | class DepositClient(User): | ||||
collections = ArrayField(models.IntegerField(), null=True) | collections = ArrayField(models.IntegerField(), null=True) | ||||
objects = UserManager() # type: ignore | objects = UserManager() # type: ignore | ||||
# this typing hint is due to a mypy/django-stubs limitation, | # this typing hint is due to a mypy/django-stubs limitation, | ||||
# see https://github.com/typeddjango/django-stubs/issues/174 | # see https://github.com/typeddjango/django-stubs/issues/174 | ||||
provider_url = models.TextField(null=False) | provider_url = models.TextField(null=False) | ||||
domain = models.TextField(null=False) | domain = models.TextField(null=False) | ||||
oidc_user: Optional[OIDCUser] = None | |||||
class Meta: | class Meta: | ||||
db_table = "deposit_client" | db_table = "deposit_client" | ||||
def __str__(self): | def __str__(self): | ||||
return str( | return str( | ||||
{ | { | ||||
"id": self.id, | "id": self.id, | ||||
"collections": self.collections, | "collections": self.collections, | ||||
"username": super().username, | "username": super().username, | ||||
"domain": self.domain, | "domain": self.domain, | ||||
"provider_url": self.provider_url, | "provider_url": self.provider_url, | ||||
} | } | ||||
) | ) | ||||
class Deposit(models.Model): | class Deposit(models.Model): | ||||
"""Deposit reception table | """Deposit reception table | ||||
anlambert: this should rather be named `set_oidc_profile` as it is not a factory method and moved to `swh. | |||||
""" | """ | ||||
id = models.BigAutoField(primary_key=True) | id = models.BigAutoField(primary_key=True) | ||||
# First deposit reception date | # First deposit reception date | ||||
Done Inline Actionss/_state/session_state/ anlambert: s/_state/session_state/ | |||||
Done Inline ActionsThe id should not be filtered out. ardumont: The id should not be filtered out. | |||||
reception_date = models.DateTimeField(auto_now_add=True) | reception_date = models.DateTimeField(auto_now_add=True) | ||||
# Date when the deposit is deemed complete and ready for loading | # Date when the deposit is deemed complete and ready for loading | ||||
complete_date = models.DateTimeField(null=True) | complete_date = models.DateTimeField(null=True) | ||||
Done Inline ActionsThis should rather be moved in the OIDCUser class with the following signature and name: def set_oidc_profile(self, oidc_profile: Dict[str, Any]) -> "OIDCUser": Then in your backend code: deposit_client.set_oidc_profile(oidc_user.oidc_profile()) anlambert: This should rather be moved in the `OIDCUser` class with the following signature and name… | |||||
Done Inline Actionscorrection de signature def set_oidc_profile(self, oidc_profile: Dict[str, Any]) -> None anlambert: correction de signature
```lang=python
def set_oidc_profile(self, oidc_profile: Dict[str, Any])… | |||||
# collection concerned by the deposit | # collection concerned by the deposit | ||||
collection = models.ForeignKey("DepositCollection", models.DO_NOTHING) | collection = models.ForeignKey("DepositCollection", models.DO_NOTHING) | ||||
Done Inline Actionsthis could be moved as an oidc_profile method in the OIDCUser class. anlambert: this could be moved as an `oidc_profile` method in the `OIDCUser` class. | |||||
# Deprecated: Deposit's external identifier | # Deprecated: Deposit's external identifier | ||||
external_id = models.TextField() | external_id = models.TextField(null=True) | ||||
Done Inline Actionsabout that, manage makemigrations complained about it... something got misses, it's actually the case in db... ardumont: about that, `manage makemigrations` complained about it... something got misses, it's actually… | |||||
# URL of the origin of this deposit, null if this is a metadata-only deposit | # URL of the origin of this deposit, null if this is a metadata-only deposit | ||||
origin_url = models.TextField(null=True) | origin_url = models.TextField(null=True) | ||||
# Deposit client | # Deposit client | ||||
Done Inline Actionssame here anlambert: same here | |||||
client = models.ForeignKey("DepositClient", models.DO_NOTHING) | client = models.ForeignKey("DepositClient", models.DO_NOTHING) | ||||
# SWH's loading result identifier | # SWH's loading result identifier | ||||
swhid = models.TextField(blank=True, null=True) | swhid = models.TextField(blank=True, null=True) | ||||
Done Inline Actionsif you move that code to OIDCUser, you only need to return attributes proper to that class in a dict. Oh and you should keep session_state, I thought you made a typo in my first review round (did not know about _state storing a django ModelState). anlambert: if you move that code to `OIDCUser`, you only need to return attributes proper to that class in… | |||||
swhid_context = models.TextField(blank=True, null=True) | swhid_context = models.TextField(blank=True, null=True) | ||||
# Deposit's status regarding loading | # Deposit's status regarding loading | ||||
status = models.TextField(choices=DEPOSIT_STATUS, default=DEPOSIT_STATUS_PARTIAL) | status = models.TextField(choices=DEPOSIT_STATUS, default=DEPOSIT_STATUS_PARTIAL) | ||||
status_detail = JSONField(null=True) | status_detail = JSONField(null=True) | ||||
# deposit can have one parent | # deposit can have one parent | ||||
parent = models.ForeignKey("self", on_delete=models.PROTECT, null=True) | parent = models.ForeignKey("self", on_delete=models.PROTECT, null=True) | ||||
check_task_id = models.TextField( | check_task_id = models.TextField( | ||||
blank=True, null=True, verbose_name="Scheduler's associated checking task id" | blank=True, null=True, verbose_name="Scheduler's associated checking task id" | ||||
▲ Show 20 Lines • Show All 100 Lines • Show Last 20 Lines |
this should rather be named set_oidc_profile as it is not a factory method and moved to swh.auth.models.OIDCUser