From 2eb4a044c1c3c258de254e17bf899ea3f4dc6df3 Mon Sep 17 00:00:00 2001 From: Thomas Breitner Date: Fri, 10 Mar 2023 19:09:59 +0100 Subject: [PATCH] #1: Mainly prettification --- ddam/core/forms.py | 2 + .../migrations/0003_asset_filename_orig.py | 19 ++++ ddam/core/migrations/0004_asset_source_url.py | 18 +++ ddam/core/models.py | 107 ++++++++++-------- ddam/core/views.py | 2 + ddam/templates/core/asset_detail.html | 23 ++-- ddam/templates/core/includes/asset_audit.html | 11 ++ 7 files changed, 125 insertions(+), 57 deletions(-) create mode 100644 ddam/core/migrations/0003_asset_filename_orig.py create mode 100644 ddam/core/migrations/0004_asset_source_url.py create mode 100644 ddam/templates/core/includes/asset_audit.html diff --git a/ddam/core/forms.py b/ddam/core/forms.py index b4340ce..10fb64d 100644 --- a/ddam/core/forms.py +++ b/ddam/core/forms.py @@ -61,6 +61,7 @@ def __init__(self, *args, **kwargs): self.helper.layout = Layout( FloatingField('title'), Field('file'), + FloatingField('source_url'), FloatingField('copyright_statement'), FloatingField('description'), Div( @@ -92,6 +93,7 @@ class Meta: "file", "description", "copyright_statement", + "source_url", "licence", "usage", ] diff --git a/ddam/core/migrations/0003_asset_filename_orig.py b/ddam/core/migrations/0003_asset_filename_orig.py new file mode 100644 index 0000000..9fcefa4 --- /dev/null +++ b/ddam/core/migrations/0003_asset_filename_orig.py @@ -0,0 +1,19 @@ +# Generated by Django 4.1.7 on 2023-03-10 17:39 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('core', '0002_licence_slug_alter_usage_slug'), + ] + + operations = [ + migrations.AddField( + model_name='asset', + name='filename_orig', + field=models.CharField(default='bla', editable=False, help_text='Original filename. Set automatically while creating an asset.', max_length=255), + preserve_default=False, + ), + ] diff --git a/ddam/core/migrations/0004_asset_source_url.py b/ddam/core/migrations/0004_asset_source_url.py new file mode 100644 index 0000000..e2793b1 --- /dev/null +++ b/ddam/core/migrations/0004_asset_source_url.py @@ -0,0 +1,18 @@ +# Generated by Django 4.1.7 on 2023-03-10 18:00 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('core', '0003_asset_filename_orig'), + ] + + operations = [ + migrations.AddField( + model_name='asset', + name='source_url', + field=models.URLField(blank=True, help_text='Source/Origin of asset. Give an URL.', verbose_name='Source URL'), + ), + ] diff --git a/ddam/core/models.py b/ddam/core/models.py index 88364c7..a968db8 100644 --- a/ddam/core/models.py +++ b/ddam/core/models.py @@ -11,6 +11,51 @@ from django.urls import reverse +class SingletonBaseModel(models.Model): + + def save(self, *args, **kwargs): + self.pk = 1 + super().save(*args, **kwargs) + + def delete(self, *args, **kwargs): + pass + + @classmethod + def load(cls): + obj, _ = cls.objects.get_or_create(pk=1) + return obj + + class Meta: + abstract = True + + +class AbstractUuidModel(models.Model): + id = models.UUIDField( + primary_key=True, + default=uuid.uuid4, + editable=False, + ) + + class Meta: + abstract = True + + +class AbstractTimestampedModel(models.Model): + created_at = models.DateTimeField(auto_now_add=True) + updated_at = models.DateTimeField(auto_now=True) + + class Meta: + abstract = True + + +class AbstractUserTrackedModel(models.Model): + created_by = models.EmailField(blank=True) + updated_by = models.EmailField(blank=True) + + class Meta: + abstract = True + + class Usage(models.Model): class MediaChoices(models.TextChoices): @@ -55,58 +100,13 @@ def save(self, *args, **kwargs): return super().save(*args, **kwargs) def __str__(self): - return self.name + return f"{self.name}" class Meta: verbose_name = _("Usage tag") verbose_name_plural = _("Usage tags") -class SingletonBaseModel(models.Model): - - def save(self, *args, **kwargs): - self.pk = 1 - super().save(*args, **kwargs) - - def delete(self, *args, **kwargs): - pass - - @classmethod - def load(cls): - obj, _ = cls.objects.get_or_create(pk=1) - return obj - - class Meta: - abstract = True - - -class AbstractUuidModel(models.Model): - id = models.UUIDField( - primary_key=True, - default=uuid.uuid4, - editable=False, - ) - - class Meta: - abstract = True - - -class AbstractTimestampedModel(models.Model): - created_at = models.DateTimeField(auto_now_add=True) - updated_at = models.DateTimeField(auto_now=True) - - class Meta: - abstract = True - - -class AbstractUserTrackedModel(models.Model): - created_by = models.EmailField(blank=True) - updated_by = models.EmailField(blank=True) - - class Meta: - abstract = True - - class Licence(models.Model): name = models.CharField( max_length=255, @@ -142,7 +142,7 @@ class UsageRestriction(models.Model): description = models.TextField(blank=True) def __str__(self): - return self.title + return f"{self.title}" class Asset(AbstractTimestampedModel, AbstractUserTrackedModel, AbstractUuidModel, models.Model): @@ -155,6 +155,12 @@ class Asset(AbstractTimestampedModel, AbstractUserTrackedModel, AbstractUuidMode blank=False, upload_to='assets/', ) + filename_orig = models.CharField( + max_length=255, + blank=False, + editable=False, + help_text="Original filename. Set automatically while creating an asset." + ) description = models.TextField( blank=True ) @@ -169,6 +175,11 @@ class Asset(AbstractTimestampedModel, AbstractUserTrackedModel, AbstractUuidMode blank=True, help_text="Appropriate credit/Licence holder (if required by license)", ) + source_url = models.URLField( + blank=True, + verbose_name="Source URL", + help_text="Source/Origin of asset. Give an URL." + ) usage_restriction = models.ForeignKey( 'core.usagerestriction', blank=True, @@ -201,4 +212,4 @@ def get_absolute_url(self): return reverse('core:asset-detail', kwargs={'id': self.id}) def __str__(self): - return self.title + return f"{self.title}" diff --git a/ddam/core/views.py b/ddam/core/views.py index 36191dc..d7b8ca9 100644 --- a/ddam/core/views.py +++ b/ddam/core/views.py @@ -33,6 +33,7 @@ def post(self, request, *args, **kwargs): asset = Asset( file=f, title=f.name, + filename_orig=f.name, created_by=request.user.email, ) assets_upload.append(asset) @@ -82,6 +83,7 @@ class AssetCreate(CreateView): def form_valid(self, form): form.instance.created_by = self.request.user.email + form.instance.filename_orig = form.cleaned_data['file'].name return super().form_valid(form) diff --git a/ddam/templates/core/asset_detail.html b/ddam/templates/core/asset_detail.html index f1efeb5..fdc2a22 100644 --- a/ddam/templates/core/asset_detail.html +++ b/ddam/templates/core/asset_detail.html @@ -37,21 +37,26 @@ n/a {% endfor %}

-

- +

+ Meta -
- Filesize: {{ asset.file.size | filesizeformat }} +
+ Filesize: {{ asset.file.size | filesizeformat }}
- Uploaded: {{ asset.created_at | naturalday }} by {{ asset.created_by }} - {% if asset.updated_at != asset.created_at %} + Orig filename: {{ asset.filename_orig }}
- Updated: {{ asset.updated_at | naturalday }} - {% if asset.updated_by %} by {{ asset.updated_by }}{% endif %} + {% if asset.source_url %} + + Source URL + {% endif %} +
+ {% include 'core/includes/asset_audit.html' with action='uploaded' timestamp=asset.created_at by=asset.created_by %} +
+ {% include 'core/includes/asset_audit.html' with action='updated' timestamp=asset.updated_at by=asset.updated_by %}
-

+
diff --git a/ddam/templates/core/includes/asset_audit.html b/ddam/templates/core/includes/asset_audit.html new file mode 100644 index 0000000..f7a1ff1 --- /dev/null +++ b/ddam/templates/core/includes/asset_audit.html @@ -0,0 +1,11 @@ +{% load humanize %} + + +{{ action|title}}: + {{ timestamp | naturalday }} + +{% if by %} +by + {{ by }} + +{% endif %}