Add redir model, remove children albums
This commit is contained in:
parent
684982eca5
commit
1dbd1c1779
9 changed files with 84 additions and 23 deletions
|
@ -4,8 +4,8 @@ from django.contrib import admin
|
||||||
from django.urls import include, path
|
from django.urls import include, path
|
||||||
|
|
||||||
urlpatterns = [
|
urlpatterns = [
|
||||||
path('', include(('gallery.urls', 'gallery'), namespace='gallery')),
|
|
||||||
path('admin/', admin.site.urls),
|
path('admin/', admin.site.urls),
|
||||||
|
path('', include(('gallery.urls', 'gallery'), namespace='gallery')),
|
||||||
]
|
]
|
||||||
|
|
||||||
if settings.DEBUG:
|
if settings.DEBUG:
|
||||||
|
|
|
@ -5,7 +5,7 @@ from imagekit.admin import AdminThumbnail
|
||||||
from imagekit.cachefiles import ImageCacheFile
|
from imagekit.cachefiles import ImageCacheFile
|
||||||
from imagekit.processors import ResizeToFit
|
from imagekit.processors import ResizeToFit
|
||||||
|
|
||||||
from gallery.models import Album, City, Location, Photo
|
from gallery.models import Album, City, Location, Photo, Redir
|
||||||
|
|
||||||
|
|
||||||
class AdminThumbnailSpec(ImageSpec):
|
class AdminThumbnailSpec(ImageSpec):
|
||||||
|
@ -20,6 +20,26 @@ def cached_admin_thumb(instance):
|
||||||
return cached
|
return cached
|
||||||
|
|
||||||
|
|
||||||
|
class RedirAdmin(admin.ModelAdmin):
|
||||||
|
list_display = ('path', 'album', 'test_url')
|
||||||
|
search_fields = ('path',)
|
||||||
|
ordering = ('path',)
|
||||||
|
list_per_page = 10
|
||||||
|
list_editable = ('album',)
|
||||||
|
|
||||||
|
def formfield_for_foreignkey(self, db_field, request, **kwargs):
|
||||||
|
if db_field.name == "album":
|
||||||
|
kwargs["queryset"] = Album.objects.all().order_by('name') # Järjestä albumit aakkosjärjestykseen
|
||||||
|
return super().formfield_for_foreignkey(db_field, request, **kwargs)
|
||||||
|
|
||||||
|
def test_url(self, obj):
|
||||||
|
return format_html(
|
||||||
|
'<a href="/{}" target="_blank">http://nyymix.net/{}</a>',
|
||||||
|
obj.path, obj.path
|
||||||
|
)
|
||||||
|
test_url.short_description = "Test redirection"
|
||||||
|
|
||||||
|
|
||||||
class CityAdmin(admin.ModelAdmin):
|
class CityAdmin(admin.ModelAdmin):
|
||||||
list_display = ('name',)
|
list_display = ('name',)
|
||||||
search_fields = ('name',)
|
search_fields = ('name',)
|
||||||
|
@ -37,39 +57,53 @@ class LocationAdmin(admin.ModelAdmin):
|
||||||
|
|
||||||
class AlbumAdmin(admin.ModelAdmin):
|
class AlbumAdmin(admin.ModelAdmin):
|
||||||
prepopulated_fields = {'slug': ('name',)}
|
prepopulated_fields = {'slug': ('name',)}
|
||||||
list_display = ('__str__', 'location', 'album_date', 'is_public', 'thumbnail', )
|
list_display = ('__str__', 'name', 'location', 'album_date', 'is_public', 'thumbnail', )
|
||||||
search_fields = ('name',)
|
search_fields = ('name',)
|
||||||
ordering = ('-album_date',)
|
ordering = ('-album_date',)
|
||||||
list_per_page = 30
|
list_per_page = 20
|
||||||
list_editable = ('is_public', 'location')
|
list_editable = ('name', 'is_public', 'location')
|
||||||
|
readonly_fields = ['cover_preview'] # Lisätään esikatselukuva readonly_fieldsiin
|
||||||
|
|
||||||
|
def cover_preview(self, obj):
|
||||||
|
if obj.cover and obj.cover.photo:
|
||||||
|
return format_html(
|
||||||
|
'<img src="{}" style="max-width: 300px; height: auto;"/>',
|
||||||
|
obj.cover.photo.url,
|
||||||
|
)
|
||||||
|
return "No cover image available"
|
||||||
|
cover_preview.short_description = "Cover Preview"
|
||||||
|
|
||||||
def formfield_for_foreignkey(self, db_field, request, **kwargs):
|
def formfield_for_foreignkey(self, db_field, request, **kwargs):
|
||||||
if db_field.name == "cover":
|
if db_field.name == "cover":
|
||||||
if hasattr(request, 'resolver_match') and request.resolver_match.kwargs.get('object_id'):
|
if hasattr(request, 'resolver_match') and request.resolver_match.kwargs.get('object_id'):
|
||||||
album_id = request.resolver_match.kwargs['object_id']
|
album_id = request.resolver_match.kwargs['object_id']
|
||||||
kwargs["queryset"] = Photo.objects.filter(album_id=album_id)
|
kwargs["queryset"] = Photo.objects.filter(album_id=album_id)
|
||||||
|
|
||||||
return super().formfield_for_foreignkey(db_field, request, **kwargs)
|
return super().formfield_for_foreignkey(db_field, request, **kwargs)
|
||||||
|
|
||||||
def thumbnail(self, obj):
|
def thumbnail(self, obj):
|
||||||
if obj.cover and obj.cover.photo:
|
if obj.cover and obj.cover.photo:
|
||||||
return format_html(
|
return format_html(
|
||||||
'<img src="{}" style="width: 50px; height: 50px; object-fit: cover;"/>',
|
'<img src="{}" style="width: 50px; height: auto;"/>',
|
||||||
obj.cover.photo.url,
|
obj.cover.photo.url,
|
||||||
)
|
)
|
||||||
return "-"
|
return "-"
|
||||||
thumbnail.short_description = "Thumbnail"
|
thumbnail.short_description = "Thumbnail"
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
class PhotoAdmin(admin.ModelAdmin):
|
class PhotoAdmin(admin.ModelAdmin):
|
||||||
list_display = ('slug', 'album', 'admin_thumbnail',)
|
list_display = ('slug', 'album', 'is_favorite', 'admin_thumbnail',)
|
||||||
list_display_links = ('slug',)
|
list_display_links = ('slug',)
|
||||||
search_fields = ('slug',)
|
search_fields = ('slug', 'photo',)
|
||||||
readonly_fields = ['slug', 'taken_at', 'height', 'width', 'exif', ]
|
readonly_fields = ['slug', 'taken_at', 'height', 'width', 'exif', ]
|
||||||
admin_thumbnail = AdminThumbnail(image_field=cached_admin_thumb)
|
admin_thumbnail = AdminThumbnail(image_field=cached_admin_thumb)
|
||||||
|
list_filter = ('album',)
|
||||||
|
list_per_page = 30
|
||||||
|
list_editable = ('is_favorite',)
|
||||||
|
|
||||||
|
|
||||||
admin.site.register(City, CityAdmin)
|
admin.site.register(City, CityAdmin)
|
||||||
admin.site.register(Location, LocationAdmin)
|
admin.site.register(Location, LocationAdmin)
|
||||||
admin.site.register(Album, AlbumAdmin)
|
admin.site.register(Album, AlbumAdmin)
|
||||||
admin.site.register(Photo, PhotoAdmin)
|
admin.site.register(Photo, PhotoAdmin)
|
||||||
|
admin.site.register(Redir, RedirAdmin)
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
# Generated by Django 5.1.4 on 2025-01-18 07:39
|
# Generated by Django 5.1.4 on 2025-01-20 19:09
|
||||||
|
|
||||||
import datetime
|
import datetime
|
||||||
import django.db.models.deletion
|
import django.db.models.deletion
|
||||||
|
@ -33,7 +33,7 @@ class Migration(migrations.Migration):
|
||||||
],
|
],
|
||||||
options={
|
options={
|
||||||
'verbose_name_plural': 'Locations',
|
'verbose_name_plural': 'Locations',
|
||||||
'unique_together': {('place', 'city')},
|
'unique_together': {('city', 'place')},
|
||||||
},
|
},
|
||||||
),
|
),
|
||||||
migrations.CreateModel(
|
migrations.CreateModel(
|
||||||
|
@ -44,7 +44,6 @@ class Migration(migrations.Migration):
|
||||||
('slug', models.SlugField(max_length=150, unique=True, verbose_name='Slug')),
|
('slug', models.SlugField(max_length=150, unique=True, verbose_name='Slug')),
|
||||||
('album_date', models.DateField(default=datetime.datetime.now, verbose_name='Album Date')),
|
('album_date', models.DateField(default=datetime.datetime.now, verbose_name='Album Date')),
|
||||||
('is_public', models.BooleanField(default=False, verbose_name='Published')),
|
('is_public', models.BooleanField(default=False, verbose_name='Published')),
|
||||||
('parent', models.ForeignKey(blank=True, limit_choices_to={'parent__isnull': True}, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='children', to='gallery.album', verbose_name='Parent Album')),
|
|
||||||
('location', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='albums', to='gallery.location', verbose_name='Location')),
|
('location', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='albums', to='gallery.location', verbose_name='Location')),
|
||||||
],
|
],
|
||||||
options={
|
options={
|
||||||
|
@ -77,4 +76,15 @@ class Migration(migrations.Migration):
|
||||||
name='cover',
|
name='cover',
|
||||||
field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='cover_to', to='gallery.photo', verbose_name='Album cover'),
|
field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='cover_to', to='gallery.photo', verbose_name='Album cover'),
|
||||||
),
|
),
|
||||||
|
migrations.CreateModel(
|
||||||
|
name='Redir',
|
||||||
|
fields=[
|
||||||
|
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
|
||||||
|
('path', models.CharField(db_index=True, max_length=255, unique=True, verbose_name='Path')),
|
||||||
|
('album', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='pathurl', to='gallery.album', verbose_name='Album')),
|
||||||
|
],
|
||||||
|
options={
|
||||||
|
'verbose_name_plural': 'Redirs',
|
||||||
|
},
|
||||||
|
),
|
||||||
]
|
]
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
from .album import *
|
from .album import *
|
||||||
from .location import *
|
from .location import *
|
||||||
from .photo import *
|
from .photo import *
|
||||||
|
from .redir import *
|
||||||
|
|
|
@ -12,7 +12,6 @@ from gallery.models.location import Location
|
||||||
class Album(models.Model):
|
class Album(models.Model):
|
||||||
name = models.CharField(max_length=150, unique=True, verbose_name="Album")
|
name = models.CharField(max_length=150, unique=True, verbose_name="Album")
|
||||||
slug = models.SlugField(max_length=150, unique=True, verbose_name="Slug")
|
slug = models.SlugField(max_length=150, unique=True, verbose_name="Slug")
|
||||||
parent = models.ForeignKey('self', on_delete=models.SET_NULL, blank=True, null=True, limit_choices_to={'parent__isnull': True}, related_name='children', verbose_name="Parent Album")
|
|
||||||
location = models.ForeignKey(Location, blank=True, null=True, on_delete=models.SET_NULL, related_name='albums', verbose_name="Location")
|
location = models.ForeignKey(Location, blank=True, null=True, on_delete=models.SET_NULL, related_name='albums', verbose_name="Location")
|
||||||
album_date = models.DateField(default=datetime.now, verbose_name="Album Date")
|
album_date = models.DateField(default=datetime.now, verbose_name="Album Date")
|
||||||
cover = models.ForeignKey("Photo", blank=True, null=True, on_delete=models.SET_NULL, related_name='cover_to', verbose_name="Album cover")
|
cover = models.ForeignKey("Photo", blank=True, null=True, on_delete=models.SET_NULL, related_name='cover_to', verbose_name="Album cover")
|
||||||
|
@ -36,11 +35,6 @@ class Album(models.Model):
|
||||||
return random_cover.photo_md.url
|
return random_cover.photo_md.url
|
||||||
return static('img/placeholder.png')
|
return static('img/placeholder.png')
|
||||||
|
|
||||||
def clean(self):
|
|
||||||
super().clean()
|
|
||||||
if self.parent and self.parent == self:
|
|
||||||
raise ValidationError({'parent': "An album cannot be its own parent."})
|
|
||||||
|
|
||||||
def save(self, *args, **kwargs):
|
def save(self, *args, **kwargs):
|
||||||
if not self.slug:
|
if not self.slug:
|
||||||
self.slug = slugify(self.name)
|
self.slug = slugify(self.name)
|
||||||
|
|
|
@ -18,6 +18,8 @@ class Location(models.Model):
|
||||||
class Meta:
|
class Meta:
|
||||||
verbose_name_plural = "Locations"
|
verbose_name_plural = "Locations"
|
||||||
unique_together = ('place', "city")
|
unique_together = ('place', "city")
|
||||||
|
ordering = ['city']
|
||||||
|
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
if self.place:
|
if self.place:
|
||||||
|
|
14
gallery/models/redir.py
Normal file
14
gallery/models/redir.py
Normal file
|
@ -0,0 +1,14 @@
|
||||||
|
from django.db import models
|
||||||
|
|
||||||
|
from gallery.models import Album
|
||||||
|
|
||||||
|
|
||||||
|
class Redir(models.Model):
|
||||||
|
path = models.CharField(max_length=255, db_index=True, unique=True, verbose_name="Path")
|
||||||
|
album = models.ForeignKey(Album, on_delete=models.CASCADE, related_name='pathurl', verbose_name="Album")
|
||||||
|
|
||||||
|
class Meta:
|
||||||
|
verbose_name_plural = "Redirs"
|
||||||
|
|
||||||
|
def __str__(self):
|
||||||
|
return '{0} - {1}'.format(self.path, self.album.slug)
|
|
@ -11,5 +11,5 @@ urlpatterns = [
|
||||||
path('albums/<path:album_slug>/<int:photo_slug>/', views.PhotoDetail.as_view(), name='photo_url'),
|
path('albums/<path:album_slug>/<int:photo_slug>/', views.PhotoDetail.as_view(), name='photo_url'),
|
||||||
path('albums/<path:album_slug>/', views.AlbumDetail.as_view(), name='album_url'),
|
path('albums/<path:album_slug>/', views.AlbumDetail.as_view(), name='album_url'),
|
||||||
path('albums/', views.AlbumsList.as_view(), name='albums_url'),
|
path('albums/', views.AlbumsList.as_view(), name='albums_url'),
|
||||||
|
path('<path:redir_path>/', views.redirect_to_album, name='redirect_to_album'),
|
||||||
]
|
]
|
||||||
|
|
|
@ -2,7 +2,7 @@ from django.core.paginator import Paginator
|
||||||
from django.shortcuts import get_object_or_404, redirect, render
|
from django.shortcuts import get_object_or_404, redirect, render
|
||||||
from django.views.generic import DetailView, ListView, TemplateView
|
from django.views.generic import DetailView, ListView, TemplateView
|
||||||
|
|
||||||
from .models import Album, Photo
|
from .models import Album, Photo, Redir
|
||||||
|
|
||||||
|
|
||||||
class Main(TemplateView):
|
class Main(TemplateView):
|
||||||
|
@ -86,3 +86,9 @@ class PhotoDetail(DetailView):
|
||||||
photo.save()
|
photo.save()
|
||||||
|
|
||||||
return redirect('gallery:photo_url', album_slug=self.kwargs['album_slug'], photo_slug=self.kwargs['photo_slug'])
|
return redirect('gallery:photo_url', album_slug=self.kwargs['album_slug'], photo_slug=self.kwargs['photo_slug'])
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
def redirect_to_album(request, redir_path):
|
||||||
|
redir = get_object_or_404(Redir, path=redir_path)
|
||||||
|
return redirect('gallery:album_url', album_slug=redir.album.slug)
|
Loading…
Reference in a new issue