Changeset View
Changeset View
Standalone View
Standalone View
swh/deposit/models.py
# Copyright (C) 2017-2020 The Software Heritage developers | # Copyright (C) 2017-2020 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 | |||||
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_VERIFIED, | ||||
▲ Show 20 Lines • Show All 138 Lines • ▼ Show 20 Lines | def __str__(self): | ||||
d["status_detail"] = self.status_detail | d["status_detail"] = self.status_detail | ||||
return str(d) | return str(d) | ||||
@property | @property | ||||
def origin_url(self): | def origin_url(self): | ||||
return "%s/%s" % (self.client.provider_url.rstrip("/"), self.external_id) | return "%s/%s" % (self.client.provider_url.rstrip("/"), self.external_id) | ||||
def client_directory_path(instance, filename): | def client_directory_path(instance: "DepositRequest", filename: str) -> str: | ||||
"""Callable to upload archive in MEDIA_ROOT/user_<id>/<filename> | """Callable to determine the upload archive path. This defaults to | ||||
MEDIA_ROOT/client_<user_id>/%Y%m%d-%H%M%S.%f/<filename>. | |||||
The format "%Y%m%d-%H%M%S.%f" is the reception date of the associated deposit | |||||
formatted using strftime. | |||||
Args: | Args: | ||||
instance (DepositRequest): DepositRequest concerned by the upload | instance: DepositRequest concerned by the upload | ||||
filename (str): Filename of the uploaded file | filename: Filename of the uploaded file | ||||
Returns: | Returns: | ||||
A path to be prefixed by the MEDIA_ROOT to access physically | The upload archive path. | ||||
to the file uploaded. | |||||
""" | """ | ||||
return "client_{0}/{1}".format(instance.deposit.client.id, filename) | reception_date = instance.deposit.reception_date | ||||
assert isinstance(reception_date, datetime.datetime) | |||||
folder = reception_date.strftime("%Y%m%d-%H%M%S.%f") | |||||
return f"client_{instance.deposit.client.id}/{folder}/{filename}" | |||||
ardumont: lol, pdb-ing this, that does not work...
The strftime thing described in the docs must work if… | |||||
Done Inline ActionsNow we are talking ;) 'client_1/20200601-092624.421886/archive1.zip' Note: Also I was uneasy about the %f which was not in the official doc, so now i know it's working \m/ ardumont: Now we are talking ;)
```
'client_1/20200601-092624.421886/archive1.zip'
```
Note: Also I was… | |||||
REQUEST_TYPES = [(ARCHIVE_TYPE, ARCHIVE_TYPE), (METADATA_TYPE, METADATA_TYPE)] | 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. | ||||
▲ Show 20 Lines • Show All 49 Lines • Show Last 20 Lines |
lol, pdb-ing this, that does not work...
The strftime thing described in the docs must work if we don't use a callable.
Nonetheless, we can do this ourselves...
which I found clearer in the end.