Update templates
This commit is contained in:
parent
0c00bc20df
commit
e19776aae3
9 changed files with 113 additions and 20 deletions
|
@ -15,6 +15,10 @@ class Album(models.Model):
|
||||||
cover = models.ImageField(upload_to="covers/", blank=True, null=True, verbose_name="Album Cover")
|
cover = models.ImageField(upload_to="covers/", blank=True, null=True, verbose_name="Album Cover")
|
||||||
is_public = models.BooleanField(default=False, verbose_name="Published")
|
is_public = models.BooleanField(default=False, verbose_name="Published")
|
||||||
|
|
||||||
|
@property
|
||||||
|
def photos_in_album(self):
|
||||||
|
return self.photos.count()
|
||||||
|
|
||||||
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)
|
||||||
|
|
|
@ -24,7 +24,9 @@ class Photo(models.Model):
|
||||||
album = models.ForeignKey(Album, on_delete=models.CASCADE, related_name='photos', verbose_name="Album")
|
album = models.ForeignKey(Album, on_delete=models.CASCADE, related_name='photos', verbose_name="Album")
|
||||||
slug = models.CharField(max_length=15, editable=False, verbose_name="Photo Slug")
|
slug = models.CharField(max_length=15, editable=False, verbose_name="Photo Slug")
|
||||||
photo = models.ImageField(upload_to=_get_upload_path, height_field='height', width_field='width', verbose_name="Photo")
|
photo = models.ImageField(upload_to=_get_upload_path, height_field='height', width_field='width', verbose_name="Photo")
|
||||||
photo_thumbnail = ImageSpecField(source='photo', processors=[ResizeToFill(100, 100)], format='JPEG', options={'quality': 70})
|
photo_sm = ImageSpecField(source='photo', processors=[ResizeToFill(320, 320)], format='JPEG', options={'quality': 70})
|
||||||
|
photo_md = ImageSpecField(source='photo', processors=[ResizeToFill(720, 720)], format='JPEG', options={'quality': 80})
|
||||||
|
photo_bg = ImageSpecField(source='photo', processors=[ResizeToFill(1920, 1920)], format='JPEG', options={'quality': 90})
|
||||||
width = models.PositiveIntegerField(default=0, editable=False, verbose_name="Photo Width")
|
width = models.PositiveIntegerField(default=0, editable=False, verbose_name="Photo Width")
|
||||||
height = models.PositiveIntegerField(default=0, editable=False, verbose_name="Photo Height")
|
height = models.PositiveIntegerField(default=0, editable=False, verbose_name="Photo Height")
|
||||||
taken_at = models.DateTimeField(blank=True, null=True, editable=False, verbose_name="Taken at")
|
taken_at = models.DateTimeField(blank=True, null=True, editable=False, verbose_name="Taken at")
|
||||||
|
@ -33,7 +35,7 @@ class Photo(models.Model):
|
||||||
views = models.PositiveIntegerField(default=0, verbose_name="Views")
|
views = models.PositiveIntegerField(default=0, verbose_name="Views")
|
||||||
|
|
||||||
def save(self, *args, **kwargs):
|
def save(self, *args, **kwargs):
|
||||||
self.exif_data = Exif(self.photo.file)
|
self.exif_data = Exif(self.photo.path)
|
||||||
datetime_taken = getattr(self.exif_data, 'datetimeoriginal', datetime.now)()
|
datetime_taken = getattr(self.exif_data, 'datetimeoriginal', datetime.now)()
|
||||||
self.slug = self.slug or self._generate_unique_slug(datetime_taken)
|
self.slug = self.slug or self._generate_unique_slug(datetime_taken)
|
||||||
self.taken_at = self.taken_at or datetime_taken
|
self.taken_at = self.taken_at or datetime_taken
|
||||||
|
@ -45,3 +47,4 @@ class Photo(models.Model):
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return f'{self.slug} ({self.album.name})'
|
return f'{self.slug} ({self.album.name})'
|
||||||
|
|
||||||
|
|
39
gallery/templates/gallery/_pagination.html
Normal file
39
gallery/templates/gallery/_pagination.html
Normal file
|
@ -0,0 +1,39 @@
|
||||||
|
{% 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 %}
|
|
@ -13,8 +13,16 @@
|
||||||
{% block content %}
|
{% block content %}
|
||||||
|
|
||||||
<h1>{{ album.name }}</h1>
|
<h1>{{ album.name }}</h1>
|
||||||
{% for photo in photos %}
|
|
||||||
<a href="{{ photo.get_absolute_url }}"> {{ photo.slug }} </a>
|
<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">
|
||||||
{% endfor %}
|
{% for photo in photos %}
|
||||||
|
<a href="{{ photo.get_absolute_url }}">
|
||||||
|
<img loading="lazy" srcset="{{ photo.photo_bg.url }} 320w, {{ photo.photo_md.url }} 720w, {{ photo.photo_md.url }} 1920w"
|
||||||
|
src="{{ photo.photo_md.url }}" alt="{{ photo.album.name }} - {{ photo.slug }}">
|
||||||
|
</a>
|
||||||
|
{% endfor %}
|
||||||
|
</div>
|
||||||
|
|
||||||
|
{% include "./_pagination.html" %}
|
||||||
|
|
||||||
{% endblock %}
|
{% endblock %}
|
|
@ -10,13 +10,28 @@
|
||||||
|
|
||||||
<!-- Content -->
|
<!-- Content -->
|
||||||
{% block content %}
|
{% block content %}
|
||||||
<ul>
|
|
||||||
|
|
||||||
|
<div class="uk-grid-small uk-child-width-1-2@s uk-child-width-1-3@m" uk-grid="masonry: true">
|
||||||
{% for album in album_list %}
|
{% for album in album_list %}
|
||||||
|
<div>
|
||||||
<li>
|
<div class="uk-card uk-card-default">
|
||||||
<a href="{{ album.get_absolute_url }}"> {{ album.name }} </a> - {{ album.album_date|date:"d.m.Y" }}
|
<div class="uk-card-media-top">
|
||||||
</li>
|
<a href="{{ album.get_absolute_url }}">
|
||||||
|
<img src="{{ album.cover.url }}" width="1800" height="1200" alt="">
|
||||||
|
</a>
|
||||||
|
</div>
|
||||||
|
<div class="uk-overlay uk-overlay-primary uk-position-bottom uk-padding-small">
|
||||||
|
<h3 class="uk-card-title"><a href="{{ album.get_absolute_url }}">{{ album }}</a></h3>
|
||||||
|
<p>{{ album.album_date|date:"d.m.Y" }} • {{ album.photos_in_album }} photos</p>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
</ul>
|
</div>
|
||||||
|
|
||||||
|
|
||||||
|
{% include "./_pagination.html" %}
|
||||||
|
|
||||||
|
|
||||||
{% endblock %}
|
{% endblock %}
|
|
@ -6,4 +6,7 @@
|
||||||
<!-- Content -->
|
<!-- Content -->
|
||||||
{% block content %}
|
{% block content %}
|
||||||
Hello World!
|
Hello World!
|
||||||
|
|
||||||
|
<p>Istunto vanhenee: {{ session_expiry|date:"d.m.Y H:i" }}</p>
|
||||||
|
|
||||||
{% endblock %}
|
{% endblock %}
|
|
@ -15,5 +15,6 @@
|
||||||
{% block content %}
|
{% block content %}
|
||||||
|
|
||||||
<h1>{{ photo.slug }}</h1>
|
<h1>{{ photo.slug }}</h1>
|
||||||
|
<img src="{{ photo.photo.url }}" alt="{{ photo.slug }}" class="height='100%'" />
|
||||||
|
|
||||||
{% endblock %}
|
{% endblock %}
|
|
@ -10,7 +10,17 @@
|
||||||
|
|
||||||
<!-- Content -->
|
<!-- Content -->
|
||||||
{% block content %}
|
{% block content %}
|
||||||
|
|
||||||
|
<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 %}
|
{% for photo in object_list %}
|
||||||
<a href="{{ photo.get_absolute_url }}">{{ photo.slug }}</a>
|
<a href="{{ photo.get_absolute_url }}">
|
||||||
|
<img loading="lazy" srcset="{{ photo.photo_bg.url }} 320w, {{ photo.photo_md.url }} 720w, {{ photo.photo_md.url }} 1920w"
|
||||||
|
src="{{ photo.photo_md.url }}" alt="{{ photo.album.name }} - {{ photo.slug }}">
|
||||||
|
</a>
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
|
</div>
|
||||||
|
|
||||||
|
{% include "./_pagination.html" %}
|
||||||
|
|
||||||
{% endblock %}
|
{% endblock %}
|
||||||
|
|
||||||
|
|
|
@ -1,20 +1,26 @@
|
||||||
|
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
|
||||||
|
|
||||||
# Create your views here.
|
|
||||||
|
|
||||||
|
|
||||||
class Main(TemplateView):
|
class Main(TemplateView):
|
||||||
template_name = "gallery/main.html"
|
template_name = "gallery/main.html"
|
||||||
|
|
||||||
|
def get_context_data(self, **kwargs):
|
||||||
|
context = super().get_context_data(**kwargs)
|
||||||
|
session_expiry = self.request.session.get_expiry_date()
|
||||||
|
context['session_expiry'] = session_expiry
|
||||||
|
return context
|
||||||
|
|
||||||
|
|
||||||
class AlbumsList(ListView):
|
class AlbumsList(ListView):
|
||||||
model = Album
|
model = Album
|
||||||
template_name = 'gallery/album_list.html'
|
template_name = 'gallery/album_list.html'
|
||||||
queryset = Album.objects.filter(is_public=True)
|
queryset = Album.objects.filter(is_public=True)
|
||||||
ordering = ['-album_date']
|
ordering = ['-album_date']
|
||||||
|
paginate_by = 30
|
||||||
|
|
||||||
|
|
||||||
class AlbumDetail(DetailView):
|
class AlbumDetail(DetailView):
|
||||||
|
@ -26,7 +32,12 @@ class AlbumDetail(DetailView):
|
||||||
|
|
||||||
def get_context_data(self, **kwargs):
|
def get_context_data(self, **kwargs):
|
||||||
context = super().get_context_data(**kwargs)
|
context = super().get_context_data(**kwargs)
|
||||||
context['photos'] = self.object.photos.all()
|
photos = self.object.photos.all().order_by('taken_at')
|
||||||
|
paginator = Paginator(photos, 30)
|
||||||
|
page_number = self.request.GET.get('page')
|
||||||
|
page_obj = paginator.get_page(page_number)
|
||||||
|
context['photos'] = page_obj.object_list
|
||||||
|
context['page_obj'] = page_obj
|
||||||
return context
|
return context
|
||||||
|
|
||||||
|
|
||||||
|
@ -41,5 +52,4 @@ class PhotoDetail(DetailView):
|
||||||
class PhotosList(ListView):
|
class PhotosList(ListView):
|
||||||
model = Photo
|
model = Photo
|
||||||
paginate_by = 30
|
paginate_by = 30
|
||||||
|
ordering = ['-taken_at']
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue