diff --git a/gallery/admin.py b/gallery/admin.py index 65c3b58..90fb38b 100644 --- a/gallery/admin.py +++ b/gallery/admin.py @@ -57,11 +57,11 @@ class LocationAdmin(admin.ModelAdmin): class AlbumAdmin(admin.ModelAdmin): prepopulated_fields = {'slug': ('name',)} - list_display = ('__str__', 'name', 'location', 'album_date', 'is_public', 'thumbnail', ) + list_display = ('name', 'location', 'album_date', 'is_public', 'thumbnail', ) search_fields = ('name',) ordering = ('-album_date',) list_per_page = 20 - list_editable = ('name', 'is_public', 'location') + list_editable = ('is_public',) readonly_fields = ['cover_preview'] # Lisätään esikatselukuva readonly_fieldsiin def cover_preview(self, obj): diff --git a/gallery/models/photo.py b/gallery/models/photo.py index 143cfe9..ac9ed00 100644 --- a/gallery/models/photo.py +++ b/gallery/models/photo.py @@ -35,6 +35,10 @@ class Photo(models.Model): views = models.PositiveIntegerField(default=0, verbose_name="Views") likes = models.PositiveIntegerField(default=0, verbose_name="Likes") + @property + def orientation(self): + return "Portrait" if self.height > self.width else "Landscape" + def add_like(self): self.likes += 1 self.save() @@ -49,7 +53,6 @@ class Photo(models.Model): def get_prev(self): return self.__class__.objects.filter(taken_at__lt=self.taken_at, album=self.album.id).order_by('-taken_at').first() - def save(self, *args, **kwargs): self.exif_data = Exif(self.photo.path) datetime_taken = getattr(self.exif_data, 'datetimeoriginal', datetime.now)() @@ -60,11 +63,10 @@ class Photo(models.Model): def get_absolute_url(self): return reverse('gallery:photo_url', kwargs={'album_slug': self.album.slug, 'photo_slug': self.slug}) - + class Meta: verbose_name_plural = "Photos" ordering = ('-taken_at',) def __str__(self): - return f'{self.slug} ({self.album.name})' - + return f'{self.slug} ({self.orientation}) {self.is_favorite}' diff --git a/gallery/templates/gallery/_pagination.html b/gallery/templates/gallery/_pagination.html deleted file mode 100644 index add7198..0000000 --- a/gallery/templates/gallery/_pagination.html +++ /dev/null @@ -1,39 +0,0 @@ -{% if page_obj.has_other_pages %} - -{% endif %} \ No newline at end of file diff --git a/gallery/templates/gallery/album_detail.html b/gallery/templates/gallery/album_detail.html index 74126b9..4e107bd 100644 --- a/gallery/templates/gallery/album_detail.html +++ b/gallery/templates/gallery/album_detail.html @@ -44,6 +44,6 @@ {% endfor %} - {% include "./_pagination.html" %} + {% include "./partials/pagination.html" %} {% endblock %} \ No newline at end of file diff --git a/gallery/templates/gallery/album_list.html b/gallery/templates/gallery/album_list.html index f900472..72d1e60 100644 --- a/gallery/templates/gallery/album_list.html +++ b/gallery/templates/gallery/album_list.html @@ -28,7 +28,7 @@ -{% include "./_pagination.html" %} + {% include "./partials/pagination.html" %} {% endblock %} \ No newline at end of file diff --git a/gallery/templates/gallery/partials/pagination.html b/gallery/templates/gallery/partials/pagination.html new file mode 100644 index 0000000..5f63a44 --- /dev/null +++ b/gallery/templates/gallery/partials/pagination.html @@ -0,0 +1,43 @@ +{% load link_tags %} + +{% if page_obj.has_other_pages %} +
+ +
+{% endif %} \ No newline at end of file diff --git a/gallery/templates/gallery/photo_list.html b/gallery/templates/gallery/photo_list.html index 263cba7..56a00b8 100644 --- a/gallery/templates/gallery/photo_list.html +++ b/gallery/templates/gallery/photo_list.html @@ -1,11 +1,19 @@ {% extends "base.html" %} + {% block title %} Gallery : Photostream {% endblock %} {% block content %} +{% load link_tags %} + +
{% for photo in object_list %} @@ -15,7 +23,7 @@ {% endfor %}
- {% include "./_pagination.html" %} + {% include "./partials/pagination.html" %} {% endblock %} diff --git a/gallery/templatetags/__init__.py b/gallery/templatetags/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/gallery/templatetags/link_tags.py b/gallery/templatetags/link_tags.py new file mode 100644 index 0000000..bdba215 --- /dev/null +++ b/gallery/templatetags/link_tags.py @@ -0,0 +1,19 @@ +import urllib.parse + +from django import template + +register = template.Library() + + +@register.simple_tag +def paginator_link(page, params={}): + gets = params.copy() + gets['page'] = page + return '?' + urllib.parse.urlencode(gets) + + +@register.simple_tag +def search_link(order, params={}): + gets = params.copy() + gets['order'] = order + return '?' + urllib.parse.urlencode(gets) diff --git a/gallery/views.py b/gallery/views.py index 7298d39..f80403c 100644 --- a/gallery/views.py +++ b/gallery/views.py @@ -48,7 +48,16 @@ class AlbumDetail(DetailView): class PhotosList(ListView): model = Photo paginate_by = 30 - ordering = ['-taken_at'] + + def get_queryset(self): + orderdic = {'latest': '-taken_at', 'liked': 'likes', 'popular': '-views'} + order = orderdic.get(self.request.GET.get('order', 'latest'), '-taken_at') + return Photo.objects.filter(album__is_public=True).order_by(order).select_related('album') + + def get_context_data(self, *args, **kwargs): + context = super().get_context_data(*args, **kwargs) + context['order'] = self.request.GET.get('order', 'latest') + return context class PhotoDetail(DetailView): @@ -88,7 +97,6 @@ class PhotoDetail(DetailView): 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) \ No newline at end of file + redir = get_object_or_404(Redir, path=redir_path) + return redirect('gallery:album_url', album_slug=redir.album.slug)