#47 Settinge of Place Hero Image

This commit is contained in:
reverend 2021-04-10 08:23:36 +02:00
parent 2ac39f719f
commit 74d842a668
6 changed files with 73 additions and 8 deletions

View File

@ -8,6 +8,8 @@ from django.db import models
from django.contrib.auth.forms import UserCreationForm, UserChangeForm from django.contrib.auth.forms import UserCreationForm, UserChangeForm
from django.contrib.auth.models import User from django.contrib.auth.models import User
from django.utils.translation import ugettext_lazy as _ from django.utils.translation import ugettext_lazy as _
from lostplaces import widgets
from lostplaces.models import Place, PlaceImage, Voucher, Explorer from lostplaces.models import Place, PlaceImage, Voucher, Explorer
class SignupVoucherForm(UserCreationForm): class SignupVoucherForm(UserCreationForm):
@ -68,6 +70,15 @@ class PlaceForm(forms.ModelForm):
model = Place model = Place
fields = '__all__' fields = '__all__'
exclude = ['submitted_by'] exclude = ['submitted_by']
widgets = {
'hero': widgets.SelectContent()
}
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
if 'instance' in kwargs:
self.fields['hero'].queryset = PlaceImage.objects.filter(place=kwargs['instance'])
self.fields['hero'].widget.attrs['item_template'] = 'partials/select_place_image_item.html'
latitude = forms.DecimalField( latitude = forms.DecimalField(
widget=forms.NumberInput(attrs={'min':-90,'max': 90,'type': 'number', 'step': 'any'}) widget=forms.NumberInput(attrs={'min':-90,'max': 90,'type': 'number', 'step': 'any'})
@ -89,9 +100,24 @@ class PlaceImageForm(forms.ModelForm):
super().__init__(*args, **kwargs) super().__init__(*args, **kwargs)
self.fields['filename'].required = False self.fields['filename'].required = False
class PlaceSetHeroForm(forms.ModelForm):
class Meta:
model = Place
fields = ['hero']
widgets = {
'hero': widgets.SelectContent()
}
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.fields['hero'].queryset = PlaceImage.objects.filter(place=kwargs['instance'])
self.fields['hero'].widget.attrs['item_template'] = 'partials/select_place_image_item.html'
class TagSubmitForm(forms.Form): class TagSubmitForm(forms.Form):
tag_list = forms.CharField( tag_list = forms.CharField(
max_length=500, max_length=500,
required=False, required=False,
widget=forms.TextInput(attrs={'autocomplete':'off'}) widget=forms.TextInput(attrs={'autocomplete':'off'})

View File

@ -11,6 +11,7 @@ from lostplaces.models.abstract_models import Submittable, Taggable, Mapable
from easy_thumbnails.fields import ThumbnailerImageField from easy_thumbnails.fields import ThumbnailerImageField
from easy_thumbnails.files import get_thumbnailer from easy_thumbnails.files import get_thumbnailer
class Place(Submittable, Taggable, Mapable): class Place(Submittable, Taggable, Mapable):
""" """
Place defines a lost place (location, name, description etc.). Place defines a lost place (location, name, description etc.).
@ -25,8 +26,31 @@ class Place(Submittable, Taggable, Mapable):
verbose_name=_('Description'), verbose_name=_('Description'),
) )
hero = models.ForeignKey(
'PlaceImage',
on_delete=models.SET_NULL,
null=True,
blank=True,
related_name='place_heros'
)
def get_hero_image(self):
if self.hero:
return self.hero
elif len(self.placeimages.all()) > 0:
return self.placeimages.first()
else:
return None
def get_absolute_url(self): def get_absolute_url(self):
return reverse('place_detail', kwargs={'pk': self.pk}) return reverse('place_detail', kwargs={'pk': self.pk})
def get_hero_index_in_queryset(self):
for i in range(0, len(self.placeimages.all())):
image = self.placeimages.all()[i]
if image == self.hero:
return i
return None
@classmethod @classmethod
@ -89,7 +113,7 @@ class PlaceImage(PlaceAsset):
upload_to=generate_place_image_filename, upload_to=generate_place_image_filename,
resize_source=dict(size=(2560, 2560), resize_source=dict(size=(2560, 2560),
sharpen=True), sharpen=True),
verbose_name=_('Filename(s)'), verbose_name=_('Images'),
help_text=_('Optional: One or more images to upload') help_text=_('Optional: One or more images to upload')
) )
place = models.ForeignKey( place = models.ForeignKey(
@ -104,7 +128,7 @@ class PlaceImage(PlaceAsset):
of this image as textual representation of this instance of this image as textual representation of this instance
""" """
return 'Image ' + str(self.pk) return 'Image ' + str(self.place.name)
# These two auto-delete files from filesystem when they are unneeded: # These two auto-delete files from filesystem when they are unneeded:

View File

@ -2,8 +2,8 @@
<article class="LP-PlaceTeaser {% if extended %} LP-PlaceTeaser--extended{% endif %}"> <article class="LP-PlaceTeaser {% if extended %} LP-PlaceTeaser--extended{% endif %}">
<div class="LP-PlaceTeaser__Image"> <div class="LP-PlaceTeaser__Image">
{% if place.placeimages.all|length > 0 %} {% if place.get_hero_image %}
{% include 'partials/image.html' with source_url=place.placeimages.first.filename.thumbnail.url link_url=place.get_absolute_url%} {% include 'partials/image.html' with source_url=place.get_hero_image.filename.thumbnail.url link_url=place.get_absolute_url%}
{% else %} {% else %}
<a href="{{place.get_absolute_url}}"> <a href="{{place.get_absolute_url}}">
<img class="LP-Image" src="{% static 'images/missing_image.png' %}" /> <img class="LP-Image" src="{% static 'images/missing_image.png' %}" />

View File

@ -0,0 +1,3 @@
{%load lostplaces %}
{% include 'partials/image.html' with source_url=object.filename.thumbnail.url %}

View File

@ -22,13 +22,15 @@
{% block maincontent %} {% block maincontent %}
<article class="LP-PlaceDetail"> <article class="LP-PlaceDetail">
<header class="LP-PlaceDetail__Header"> <header class="LP-PlaceDetail__Header">
<h1 class="LP-Headline">{{ place.name }} {% include 'partials/icons/place_favorite.html' %} {% include 'partials/icons/place_visited.html' %}</h1> <h1 class="LP-Headline">{{ place.name }} {% include 'partials/icons/place_favorite.html' %} {% include 'partials/icons/place_visited.html' %}</h1>
{% if place.placeimages.first.filename.hero.url %} {% if place.get_hero_image %}
<div class="LP-PlaceDetail__Image"> <div class="LP-PlaceDetail__Image">
{% partial image %} {% partial image %}
{% set source_url place.placeimages.first.filename.hero.url %} {% set source_url place.get_hero_image.filename.hero.url %}
{% set link_url %}
{{"#image"|addstr:place.get_hero_index_in_queryset}}
{% endset %}
{% endpartial %} {% endpartial %}
</div> </div>
{% endif %} {% endif %}

View File

@ -1,6 +1,7 @@
{% extends 'global.html'%} {% extends 'global.html'%}
{% load static %} {% load static %}
{% load i18n %} {% load i18n %}
{% load widget_tweaks %}
# {% block title %}{% translate 'Edit place' %}{% endblock %} # {% block title %}{% translate 'Edit place' %}{% endblock %}
@ -38,6 +39,15 @@
{% include 'partials/form/inputField.html' with field=form.filename %} {% include 'partials/form/inputField.html' with field=form.filename %}
</div> </div>
</div> </div>
{% if object.placeimages.all|length > 0 %}
<legend class="LP-Form__Legend">{% translate 'Set Hero Image' %}</legend>
<div class="LP-Form__Composition">
<div class="LP-Form__Field">
{% render_field form.hero container_class='LP-ImageGrid__Container' item_class='LP-ImageGrid__Item LP-Select' current_selected_value=object.hero.id%}
</div>
</div>
{% endif %}
{% translate 'Update' as action %} {% translate 'Update' as action %}
<div class="LP-Form__Composition LP-Form__Composition--buttons"> <div class="LP-Form__Composition LP-Form__Composition--buttons">