Add ordering, templatetags, update pagination
This commit is contained in:
parent
1dbd1c1779
commit
7c7eb0061d
10 changed files with 93 additions and 52 deletions
|
@ -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):
|
||||
|
|
|
@ -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)()
|
||||
|
@ -66,5 +69,4 @@ class Photo(models.Model):
|
|||
ordering = ('-taken_at',)
|
||||
|
||||
def __str__(self):
|
||||
return f'{self.slug} ({self.album.name})'
|
||||
|
||||
return f'{self.slug} ({self.orientation}) {self.is_favorite}'
|
||||
|
|
|
@ -1,39 +0,0 @@
|
|||
{% if page_obj.has_other_pages %}
|
||||
<ul class="uk-pagination uk-flex-center" uk-margin>
|
||||
|
||||
{% if page_obj.has_previous %}
|
||||
<li><a href="?page={{ page_obj.previous_page_number }}"><span uk-pagination-previous></span></a></li>
|
||||
{% else %}
|
||||
<li class="uk-disabled"><span uk-pagination-previous></span></li>
|
||||
{% endif %}
|
||||
|
||||
{% if page_obj.number|add:'-3' > 1 %}
|
||||
<li><a href="?page=1">1</a></li>
|
||||
{% if page_obj.number|add:'-4' > 1 %}
|
||||
<li><a href="?page={{ page_obj.number|add:'-4' }}">…</a></li>
|
||||
{% endif %}
|
||||
{% endif %}
|
||||
|
||||
{% for i in page_obj.paginator.page_range %}
|
||||
{% if page_obj.number == i %}
|
||||
<li class="uk-active"><span>{{ i }}</span></li>
|
||||
{% elif i > page_obj.number|add:'-4' and i < page_obj.number|add:'4' %}
|
||||
<li><a href="?page={{ i }}">{{ i }}</a></li>
|
||||
{% endif %}
|
||||
{% endfor %}
|
||||
|
||||
{% if page_obj.paginator.num_pages > page_obj.number|add:'3' %}
|
||||
{% if page_obj.number|add:'4' < page_obj.paginator.num_pages %}
|
||||
<li><a href="?page={{ page_obj.number|add:'4' }}">…</a></li>
|
||||
{% endif %}
|
||||
<li><a href="?page={{ page_obj.paginator.num_pages }}">{{ page_obj.paginator.num_pages }}</a></li>
|
||||
{% endif %}
|
||||
|
||||
{% if page_obj.has_next %}
|
||||
<li><a href="?page={{ page_obj.next_page_number }}"><span uk-pagination-next></span></a></li>
|
||||
{% else %}
|
||||
<li class="uk-disabled"><span uk-pagination-next></span></li>
|
||||
{% endif %}
|
||||
|
||||
</ul>
|
||||
{% endif %}
|
|
@ -44,6 +44,6 @@
|
|||
{% endfor %}
|
||||
</div>
|
||||
|
||||
{% include "./_pagination.html" %}
|
||||
{% include "./partials/pagination.html" %}
|
||||
|
||||
{% endblock %}
|
|
@ -28,7 +28,7 @@
|
|||
</div>
|
||||
|
||||
|
||||
{% include "./_pagination.html" %}
|
||||
{% include "./partials/pagination.html" %}
|
||||
|
||||
|
||||
{% endblock %}
|
43
gallery/templates/gallery/partials/pagination.html
Normal file
43
gallery/templates/gallery/partials/pagination.html
Normal file
|
@ -0,0 +1,43 @@
|
|||
{% load link_tags %}
|
||||
|
||||
{% if page_obj.has_other_pages %}
|
||||
<div class="uk-margin">
|
||||
<ul class="uk-pagination uk-flex-center">
|
||||
|
||||
{% if page_obj.has_previous %}
|
||||
<li><a href="{% paginator_link page_obj.previous_page_number request.GET %}"><span class="uk-margin-small-right" uk-pagination-previous></span> Previous</a></li>
|
||||
{% else %}
|
||||
<li class="uk-disabled"><a href="#"><span class="uk-margin-small-right" uk-pagination-previous></span> Previous</a></li>
|
||||
{% endif %}
|
||||
|
||||
{% if page_obj.number|add:'-3' > 1 %}
|
||||
<li><a href="{% paginator_link 1 request.GET %}">1</a></li>
|
||||
{% if page_obj.number|add:'-4' > 1 %}
|
||||
<li><a href="{% paginator_link page_obj.number|add:'-4' request.GET %}">…</a></li>
|
||||
{% endif %}
|
||||
{% endif %}
|
||||
|
||||
{% for i in page_obj.paginator.page_range %}
|
||||
{% if page_obj.number == i %}
|
||||
<li class="uk-active"><span>{{ i }}</span></li>
|
||||
{% elif i > page_obj.number|add:'-4' and i < page_obj.number|add:'4' %}
|
||||
<li><a href="{% paginator_link i request.GET %}">{{ i }}</a></li>
|
||||
{% endif %}
|
||||
{% endfor %}
|
||||
|
||||
{% if page_obj.paginator.num_pages > page_obj.number|add:'3' %}
|
||||
{% if page_obj.number|add:'4' < page_obj.paginator.num_pages %}
|
||||
<li><a href="{% paginator_link page_obj.number|add:'4' request.GET %}">…</a></li>
|
||||
{% endif %}
|
||||
<li><a href="{% paginator_link page_obj.paginator.num_pages request.GET %}">{{ page_obj.paginator.num_pages }}</a></li>
|
||||
{% endif %}
|
||||
|
||||
{% if page_obj.has_next %}
|
||||
<li><a href="{% paginator_link page_obj.next_page_number request.GET %}">Next <span uk-pagination-next></span></a></li>
|
||||
{% else %}
|
||||
<li class="uk-disabled"><a href="#">Next <span class="uk-margin-small-left" uk-pagination-next></span></a></li>
|
||||
{% endif %}
|
||||
|
||||
</ul>
|
||||
</div>
|
||||
{% endif %}
|
|
@ -1,11 +1,19 @@
|
|||
{% extends "base.html" %}
|
||||
|
||||
|
||||
<!-- Title -->
|
||||
{% block title %} Gallery : Photostream {% endblock %}
|
||||
|
||||
<!-- Content -->
|
||||
{% block content %}
|
||||
|
||||
{% load link_tags %}
|
||||
<ul class="uk-tab uk-flex-right">
|
||||
<li {% if order == 'latest' %}class="uk-active" {% endif %}><a href="{% search_link 'latest' request.GET %}">Latest</a></li>
|
||||
<li {% if order == 'liked' %}class="uk-active" {% endif %}><a href="{% search_link 'liked' request.GET %}">Top Liked</a></li>
|
||||
<li {% if order == 'popular' %}class="uk-active" {% endif %}><a href="{% search_link 'popular' request.GET %}">Popular</a></li>
|
||||
</ul>
|
||||
|
||||
<div class="uk-grid-small uk-child-width-1-2 uk-child-width-1-3@s uk-child-width-1-4@m uk-child-width-1-5@l" uk-grid="masonry: true">
|
||||
{% for photo in object_list %}
|
||||
<a href="{{ photo.get_absolute_url }}">
|
||||
|
@ -15,7 +23,7 @@
|
|||
{% endfor %}
|
||||
</div>
|
||||
|
||||
{% include "./_pagination.html" %}
|
||||
{% include "./partials/pagination.html" %}
|
||||
|
||||
{% endblock %}
|
||||
|
||||
|
|
0
gallery/templatetags/__init__.py
Normal file
0
gallery/templatetags/__init__.py
Normal file
19
gallery/templatetags/link_tags.py
Normal file
19
gallery/templatetags/link_tags.py
Normal file
|
@ -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)
|
|
@ -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)
|
Loading…
Reference in a new issue