Changeset View
Changeset View
Standalone View
Standalone View
swh/deposit/models.py
Show All 10 Lines | |||||
from django.contrib.postgres.fields import JSONField, ArrayField | from django.contrib.postgres.fields import JSONField, ArrayField | ||||
from django.contrib.auth.models import User, UserManager | from django.contrib.auth.models import User, UserManager | ||||
from django.db import models | from django.db import models | ||||
from django.utils.timezone import now | from django.utils.timezone import now | ||||
from .config import ( | from .config import ( | ||||
DEPOSIT_STATUS_VERIFIED, DEPOSIT_STATUS_DEPOSITED, DEPOSIT_STATUS_PARTIAL, | DEPOSIT_STATUS_VERIFIED, DEPOSIT_STATUS_DEPOSITED, DEPOSIT_STATUS_PARTIAL, | ||||
DEPOSIT_STATUS_LOAD_SUCCESS, DEPOSIT_STATUS_LOAD_FAILURE, | DEPOSIT_STATUS_LOAD_SUCCESS, DEPOSIT_STATUS_LOAD_FAILURE, | ||||
DEPOSIT_STATUS_REJECTED | DEPOSIT_STATUS_REJECTED, ARCHIVE_TYPE, METADATA_TYPE | ||||
) | ) | ||||
class Dbversion(models.Model): | class Dbversion(models.Model): | ||||
"""Db version | """Db version | ||||
""" | """ | ||||
version = models.IntegerField(primary_key=True) | version = models.IntegerField(primary_key=True) | ||||
▲ Show 20 Lines • Show All 108 Lines • ▼ Show 20 Lines | def __str__(self): | ||||
'status': self.status, | 'status': self.status, | ||||
} | } | ||||
if self.status in (DEPOSIT_STATUS_REJECTED): | if self.status in (DEPOSIT_STATUS_REJECTED): | ||||
d['status_detail'] = self.status_detail | d['status_detail'] = self.status_detail | ||||
return str(d) | return str(d) | ||||
class DepositRequestType(models.Model): | |||||
"""Deposit request type made by clients (either archive or metadata) | |||||
""" | |||||
id = models.BigAutoField(primary_key=True) | |||||
name = models.TextField() | |||||
class Meta: | |||||
db_table = 'deposit_request_type' | |||||
def __str__(self): | |||||
return str({'id': self.id, 'name': self.name}) | |||||
def client_directory_path(instance, filename): | def client_directory_path(instance, filename): | ||||
"""Callable to upload archive in MEDIA_ROOT/user_<id>/<filename> | """Callable to upload archive in MEDIA_ROOT/user_<id>/<filename> | ||||
Args: | Args: | ||||
instance (DepositRequest): DepositRequest concerned by the upload | instance (DepositRequest): DepositRequest concerned by the upload | ||||
filename (str): Filename of the uploaded file | filename (str): Filename of the uploaded file | ||||
Returns: | Returns: | ||||
A path to be prefixed by the MEDIA_ROOT to access physically | A path to be prefixed by the MEDIA_ROOT to access physically | ||||
to the file uploaded. | to the file uploaded. | ||||
""" | """ | ||||
return 'client_{0}/{1}'.format(instance.deposit.client.id, filename) | return 'client_{0}/{1}'.format(instance.deposit.client.id, filename) | ||||
REQUEST_TYPES = [(ARCHIVE_TYPE, ARCHIVE_TYPE), | |||||
(METADATA_TYPE, METADATA_TYPE)] | |||||
class DepositRequest(models.Model): | class DepositRequest(models.Model): | ||||
"""Deposit request associated to one deposit. | """Deposit request associated to one deposit. | ||||
""" | """ | ||||
id = models.BigAutoField(primary_key=True) | id = models.BigAutoField(primary_key=True) | ||||
# Deposit concerned by the request | # Deposit concerned by the request | ||||
deposit = models.ForeignKey(Deposit, models.DO_NOTHING) | deposit = models.ForeignKey(Deposit, models.DO_NOTHING) | ||||
date = models.DateTimeField(auto_now_add=True) | date = models.DateTimeField(auto_now_add=True) | ||||
# Deposit request information on the data to inject | # Deposit request information on the data to inject | ||||
# this can be null when type is 'archive' | # this can be null when type is 'archive' | ||||
metadata = JSONField(null=True) | metadata = JSONField(null=True) | ||||
raw_metadata = models.TextField(null=True) | raw_metadata = models.TextField(null=True) | ||||
# this can be null when type is 'metadata' | # this can be null when type is 'metadata' | ||||
archive = models.FileField(null=True, upload_to=client_directory_path) | archive = models.FileField(null=True, upload_to=client_directory_path) | ||||
type = models.ForeignKey( | type = models.CharField(max_length=8, choices=REQUEST_TYPES) | ||||
'DepositRequestType', models.DO_NOTHING) | |||||
class Meta: | class Meta: | ||||
db_table = 'deposit_request' | db_table = 'deposit_request' | ||||
def __str__(self): | def __str__(self): | ||||
meta = None | meta = None | ||||
if self.metadata: | if self.metadata: | ||||
from json import dumps | from json import dumps | ||||
Show All 24 Lines |