Skip to content

Commit

Permalink
TK gallery port
Browse files Browse the repository at this point in the history
  • Loading branch information
AndersSeverinsen committed Nov 3, 2024
1 parent 0def888 commit d427ff9
Show file tree
Hide file tree
Showing 29 changed files with 1,454 additions and 64 deletions.
11 changes: 11 additions & 0 deletions fredagscafeen/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,9 @@
"events",
"printer",
"rosetta",
"sorl.thumbnail",
"jfu",
"gallery",
)

MIDDLEWARE = (
Expand Down Expand Up @@ -227,6 +230,8 @@
"django.template.context_processors.request",
"django.contrib.auth.context_processors.auth",
"django.contrib.messages.context_processors.messages",
"django.template.context_processors.request",
"django.template.context_processors.static",
],
},
},
Expand Down Expand Up @@ -317,3 +322,9 @@
X_FRAME_OPTIONS = "SAMEORIGIN"

DEFAULT_AUTO_FIELD = "django.db.models.AutoField"

THUMBNAIL_KVSTORE = "sorl.thumbnail.kvstores.dbm_kvstore.KVStore"
# THUMBNAIL_DBM_FILE = '/home/mftutor/web/thumbnails/thumbnail_kvstore'
THUMBNAIL_DBM_FILE = "/Users/andersseverinsen/Library/CloudStorage/OneDrive-Aarhusuniversitet/Uni/tutor/thumbnails/thumbnail_kvstore"

THUMBNAIL_DEBUG = True
2 changes: 2 additions & 0 deletions fredagscafeen/settings/local.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,5 @@

if sys.argv[1:2] != ["test"]:
MIDDLEWARE += ("fredagscafeen.autologin.AutologinMiddleware",)

YEAR = 2024
Empty file added gallery/__init__.py
Empty file.
93 changes: 93 additions & 0 deletions gallery/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
from django import forms
from django.contrib import admin
from django.db import models
from django.urls import reverse
from django.utils.html import format_html

from gallery.models import Album, BaseMedia


class InlineBaseMediaAdmin(admin.TabularInline):
model = BaseMedia
extra = 0
fields = (
"admin_thumbnail",
"date",
"caption",
"visibility",
"slug",
"forcedOrder",
"isCoverFile",
)
readonly_fields = (
"admin_thumbnail",
"slug",
"isCoverFile",
)

def has_add_permission(self, request, obj=None):
return False


class AlbumAdminForm(forms.ModelForm):
class Meta:
model = Album
fields = [
"title",
"publish_date",
"gfyear",
"eventalbum",
"description",
"slug",
]


class AlbumAdmin(admin.ModelAdmin):
# List display of multiple albums
list_display = ("title", "gfyear", "publish_date", "get_visibility_link")
ordering = [
"-gfyear",
"eventalbum",
"-oldFolder",
"-publish_date",
] # Reverse of models.Album.ordering
list_filter = ("gfyear", "eventalbum")

# Form display of single album
inlines = [InlineBaseMediaAdmin]
form = AlbumAdminForm
prepopulated_fields = {
"slug": ("title",),
}
formfield_overrides = {
models.SlugField: {"widget": forms.TextInput(attrs={"readOnly": "True"})}
}

add_form_template = "admin/gallery/add_form.html"

def get_inline_instances(self, request, obj=None):
if obj is None:
# When creating Album, don't display the BaseMedia inlines
return []
return super(AlbumAdmin, self).get_inline_instances(request, obj)

def get_visibility_link(self, album):
file = album.basemedia.first()
if file:
kwargs = dict(
gfyear=album.gfyear, album_slug=album.slug, image_slug=file.slug
)
return format_html(
'<a href="{}?v=1">Udvælg billeder</a>', reverse("image", kwargs=kwargs)
)

get_visibility_link.short_description = "Udvælg billeder"

def save_related(self, request, form, formsets, change):
super().save_related(request, form, formsets, change)
# Update isCoverFile on all images in album
# now that images have been saved to the database.
form.instance.clean()


admin.site.register(Album, AlbumAdmin)
48 changes: 48 additions & 0 deletions gallery/forms.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import re

from django import forms

from gallery.models import BaseMedia


class EditVisibilityForm(forms.Form):
class Missing(Exception):
pass

def __init__(self, file_visibility, **kwargs):
super(EditVisibilityForm, self).__init__(**kwargs)
self.basemedias = []
self.album_pks = set()
for file in file_visibility:
if isinstance(file, BaseMedia):
pk = file.pk
visibility = file.visibility
album = file.album_id
elif isinstance(file, tuple):
pk, visibility, album = file
else:
raise TypeError(type(file))
k = "i%s" % pk
self.album_pks.add(album)
self.fields[k] = forms.ChoiceField(
choices=BaseMedia.VISIBILITY,
initial=visibility,
widget=forms.RadioSelect,
)
self.basemedias.append((pk, k))

@classmethod
def from_POST(cls, post_data):
pattern = r"^i(\d+)$"
pks = []
for k, v in post_data.items():
mo = re.match(pattern, k)
if mo:
pks.append(int(mo.group(1)))
files = BaseMedia.objects.filter(pk__in=pks)
files = list(files.values_list("pk", "visibility", "album_id"))
found_pks = [f[0] for f in files]
missing = set(pks) - set(found_pks)
if missing:
raise cls.Missing("Not found: %r" % (sorted(missing),))
return cls(files, data=post_data)
Empty file added gallery/management/__init__.py
Empty file.
Empty file.
19 changes: 19 additions & 0 deletions gallery/management/commands/delete_marked_images.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import logging

from django.core.management.base import BaseCommand

from gallery.models import BaseMedia

logger = logging.getLogger(__name__)


class Command(BaseCommand):
help = 'Slet billeder som er markeret "Slet"'

def handle(self, *args, **options):
qs = BaseMedia.objects.filter(visibility=BaseMedia.DELETE)
n = qs.count()
if n:
ids = sorted(qs.values_list("id", flat=True))
logger.info("Deleting %s BaseMedia objects with IDs %s", n, ids)
BaseMedia.objects.filter(id__in=ids).delete()
Loading

0 comments on commit d427ff9

Please sign in to comment.