From f257da35671ef42709ddc0bed92f2e4936a8e5a9 Mon Sep 17 00:00:00 2001 From: j Date: Fri, 22 Apr 2022 18:20:42 +0100 Subject: [PATCH] add media backend --- .gitignore | 1 + app/event/management/commands/extract_urls.py | 40 +++++++++++++++++++ .../migrations/0007_alter_event_options.py | 17 ++++++++ app/media/__init__.py | 0 app/media/admin.py | 10 +++++ app/media/apps.py | 6 +++ app/media/migrations/0001_initial.py | 23 +++++++++++ app/media/migrations/__init__.py | 0 app/media/models.py | 25 ++++++++++++ app/media/tests.py | 3 ++ app/media/views.py | 3 ++ app/settings.py | 2 + app/urls.py | 5 ++- 13 files changed, 133 insertions(+), 2 deletions(-) create mode 100644 app/event/management/commands/extract_urls.py create mode 100644 app/event/migrations/0007_alter_event_options.py create mode 100644 app/media/__init__.py create mode 100644 app/media/admin.py create mode 100644 app/media/apps.py create mode 100644 app/media/migrations/0001_initial.py create mode 100644 app/media/migrations/__init__.py create mode 100644 app/media/models.py create mode 100644 app/media/tests.py create mode 100644 app/media/views.py diff --git a/.gitignore b/.gitignore index 2b24304..41645d9 100644 --- a/.gitignore +++ b/.gitignore @@ -8,3 +8,4 @@ venv secret.txt app/local_settings.py geo/GeoLite2-City.mmdb +data/ diff --git a/app/event/management/commands/extract_urls.py b/app/event/management/commands/extract_urls.py new file mode 100644 index 0000000..6215887 --- /dev/null +++ b/app/event/management/commands/extract_urls.py @@ -0,0 +1,40 @@ +import csv +import re +import sys +import ox + +from django.core.management.base import BaseCommand +from django.conf import settings + +from ... import models + + +class Command(BaseCommand): + help = 'extract urls' + args = '' + + def add_arguments(self, parser): + parser.add_argument('--debug', action='store_true', dest='debug', + default=False, help='debug something') + + def handle(self, **options): + urls = set() + for event in models.Event.objects.all(): + for url in re.compile('href="(.*?)"').findall(event.body): + urls.add(url) + for url in re.compile('src="(.*?)"').findall(event.body): + urls.add(url) + + writer = csv.writer(sys.stdout) + writer.writerow(['url', 'pandora', 'archive']) + for url in sorted(urls): + url = ox.decode_html(url) + if url[0] in ('/', '#'): + continue + if 'youtube' in url or 'vimeo' in url: + p = 'y' + else: + p = '' + writer.writerow([url, p, 'https://web.archive.org/web/*/' + url]) + + diff --git a/app/event/migrations/0007_alter_event_options.py b/app/event/migrations/0007_alter_event_options.py new file mode 100644 index 0000000..05bec62 --- /dev/null +++ b/app/event/migrations/0007_alter_event_options.py @@ -0,0 +1,17 @@ +# Generated by Django 4.0.4 on 2022-04-22 17:11 + +from django.db import migrations + + +class Migration(migrations.Migration): + + dependencies = [ + ('event', '0006_event_media_caption'), + ] + + operations = [ + migrations.AlterModelOptions( + name='event', + options={'ordering': ('position', 'date')}, + ), + ] diff --git a/app/media/__init__.py b/app/media/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/app/media/admin.py b/app/media/admin.py new file mode 100644 index 0000000..e66dfa6 --- /dev/null +++ b/app/media/admin.py @@ -0,0 +1,10 @@ +from django.contrib import admin + +from . import models + + +@admin.decorators.register(models.Image) +class ImageAdmin(admin.ModelAdmin): + search_fields = ['file'] + list_display = ('image_tag', 'file') + readonly_fields = ('image_tag', 'get_absolute_url') diff --git a/app/media/apps.py b/app/media/apps.py new file mode 100644 index 0000000..955fd9e --- /dev/null +++ b/app/media/apps.py @@ -0,0 +1,6 @@ +from django.apps import AppConfig + + +class MediaConfig(AppConfig): + default_auto_field = 'django.db.models.BigAutoField' + name = 'app.media' diff --git a/app/media/migrations/0001_initial.py b/app/media/migrations/0001_initial.py new file mode 100644 index 0000000..71264e0 --- /dev/null +++ b/app/media/migrations/0001_initial.py @@ -0,0 +1,23 @@ +# Generated by Django 4.0.4 on 2022-04-22 17:11 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + initial = True + + dependencies = [ + ] + + operations = [ + migrations.CreateModel( + name='Image', + fields=[ + ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('created', models.DateTimeField(auto_now_add=True)), + ('modified', models.DateTimeField(auto_now=True)), + ('file', models.ImageField(blank=True, null=True, upload_to='image')), + ], + ), + ] diff --git a/app/media/migrations/__init__.py b/app/media/migrations/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/app/media/models.py b/app/media/models.py new file mode 100644 index 0000000..8062e11 --- /dev/null +++ b/app/media/models.py @@ -0,0 +1,25 @@ +from django.db import models +from django.urls import reverse + + +class Image(models.Model): + created = models.DateTimeField(auto_now_add=True) + modified = models.DateTimeField(auto_now=True) + + file = models.ImageField(upload_to='image', null=True, blank=True) + + def get_absolute_url(self): + return self.file.url + get_absolute_url.short_description = 'URL' + + def image_tag(self): + from django.utils.html import mark_safe + return mark_safe('' % self.get_absolute_url()) + image_tag.short_description = 'Preview' + + def __str__(self): + if self.file: + name = self.file.name.split('/')[-1] + else: + name = 'None' + return name diff --git a/app/media/tests.py b/app/media/tests.py new file mode 100644 index 0000000..7ce503c --- /dev/null +++ b/app/media/tests.py @@ -0,0 +1,3 @@ +from django.test import TestCase + +# Create your tests here. diff --git a/app/media/views.py b/app/media/views.py new file mode 100644 index 0000000..91ea44a --- /dev/null +++ b/app/media/views.py @@ -0,0 +1,3 @@ +from django.shortcuts import render + +# Create your views here. diff --git a/app/settings.py b/app/settings.py index 6552122..175e39c 100755 --- a/app/settings.py +++ b/app/settings.py @@ -41,6 +41,7 @@ INSTALLED_APPS = [ 'app.user', 'app.page', 'app.event', + 'app.media', ] @@ -129,6 +130,7 @@ USE_TZ = True STATIC_URL = '/static/' STATIC_ROOT = BASE_DIR / 'www' / 'static' MEDIA_ROOT = BASE_DIR / 'data' / 'media' +MEDIA_URL = '/media/' TITLE = 'Example Timeline' URL_PREFIX = '' diff --git a/app/urls.py b/app/urls.py index 4e9dd1a..433950e 100755 --- a/app/urls.py +++ b/app/urls.py @@ -16,7 +16,7 @@ Including another URLconf from django.contrib import admin from django.urls import path from django.conf import settings - +from django.conf.urls.static import static from .event import views as event_views @@ -25,4 +25,5 @@ urlpatterns = [ path(settings.URL_PREFIX + 'events/', event_views.events, name='event'), path(settings.URL_PREFIX + 'events/', event_views.events, name='events'), path(settings.URL_PREFIX + '', event_views.timeline, name='timeline'), -] +] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) +