add media backend

This commit is contained in:
j 2022-04-22 18:20:42 +01:00
parent 696717d138
commit f257da3567
13 changed files with 133 additions and 2 deletions

1
.gitignore vendored
View file

@ -8,3 +8,4 @@ venv
secret.txt
app/local_settings.py
geo/GeoLite2-City.mmdb
data/

View file

@ -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])

View file

@ -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')},
),
]

0
app/media/__init__.py Normal file
View file

10
app/media/admin.py Normal file
View file

@ -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')

6
app/media/apps.py Normal file
View file

@ -0,0 +1,6 @@
from django.apps import AppConfig
class MediaConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'app.media'

View file

@ -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')),
],
),
]

View file

25
app/media/models.py Normal file
View file

@ -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('<img src="%s" style="max-width:256px" />' % 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

3
app/media/tests.py Normal file
View file

@ -0,0 +1,3 @@
from django.test import TestCase
# Create your tests here.

3
app/media/views.py Normal file
View file

@ -0,0 +1,3 @@
from django.shortcuts import render
# Create your views here.

View file

@ -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 = ''

View file

@ -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/<str:slug>', 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)