#42 Place Voting / Level

This commit is contained in:
reverend 2021-12-30 23:20:05 +01:00
parent 4c43f87560
commit 9980a2d190
7 changed files with 180 additions and 25 deletions

View File

@ -35,3 +35,4 @@ admin.site.register(Voucher, VoucherAdmin)
admin.site.register(Place, PlacesAdmin)
admin.site.register(PlaceImage, PlaceImagesAdmin)
admin.site.register(PhotoAlbum, PhotoAlbumsAdmin)
admin.site.register(PlaceVoting)

View File

@ -22,7 +22,7 @@ class SignupVoucherForm(UserCreationForm):
)
def is_valid(self):
super().is_valid()
super_result = super().is_valid()
submitted_voucher = self.cleaned_data.get('voucher')
try:
fetched_voucher = Voucher.objects.get(code=submitted_voucher)
@ -35,7 +35,7 @@ class SignupVoucherForm(UserCreationForm):
return False
fetched_voucher.delete()
return True
return super_result
class ExplorerUserChangeForm(UserChangeForm):
class Meta:

View File

@ -1,4 +1,5 @@
import os
from math import floor
from django.db import models
from django.urls import reverse
@ -6,7 +7,7 @@ from django.dispatch import receiver
from django.db.models.signals import post_delete, pre_save
from django.utils.translation import ugettext_lazy as _
from lostplaces.models.abstract_models import Submittable, Taggable, Mapable
from lostplaces.models.abstract_models import Submittable, Taggable, Mapable, Expireable
from easy_thumbnails.fields import ThumbnailerImageField
from easy_thumbnails.files import get_thumbnailer
@ -15,7 +16,7 @@ PLACE_LEVELS = (
(1, 'Ruin'),
(2, 'Vandalized'),
(3, 'Natures Treasure'),
(4, 'Long Time no See'),
(4, 'Lost in History'),
(5, 'Time Capsule')
)
@ -58,10 +59,15 @@ class Place(Submittable, Taggable, Mapable):
return reverse('place_detail', kwargs={'pk': self.pk})
def get_hero_index_in_queryset(self):
'''
Calculates the index of the hero image within
the list / queryset of images. Necessary for
the lightbox.
'''
for i in range(0, len(self.placeimages.all())):
image = self.placeimages.all()[i]
if image == self.hero:
return i
return i
return None
@ -84,6 +90,24 @@ class Place(Submittable, Taggable, Mapable):
return {'latitude': latitude, 'longitude': longitude}
def calculate_place_level(self):
self.remove_expired_votes()
if self.placevotings.count() == 0:
return 5
level = 0
for vote in self.placevotings.all():
level += vote.vote
self.level = floor(level / self.placevotings.count())
def remove_expired_votes(self):
for vote in self.placevotings.all():
if vote.is_expired:
vote.delete()
def __str__(self):
return self.name
@ -104,7 +128,7 @@ class PlaceAsset(Submittable):
"""
class Meta:
abstract = True
abstract = True
place = models.ForeignKey(
Place,
@ -180,3 +204,13 @@ def auto_delete_file_on_change(sender, instance, **kwargs):
new_file = instance.filename
if not old_file == new_file:
old_file.delete(save=False)
class PlaceVoting(PlaceAsset, Expireable):
vote = models.IntegerField(choices=PLACE_LEVELS)
def get_human_readable_level(self):
return PLACE_LEVELS[self.vote - 1][1]
def get_all_choices(self):
return reversed(PLACE_LEVELS)

View File

@ -0,0 +1,39 @@
{% load i18n %}
<div class="LP-Voting">
<div class="LP-Voting__Left">
<h2 class="LP-Headline">
Place level
</h2>
<div class="LP-Voting__Choices">
{% for choice in voting.get_all_choices %}
<a href="{% url 'place_vote' place_id=place.id vote=choice.0 %}" class="LP-Voting__Vote {% if choice.0 <= place.level %} LP-Voting__Vote--overall{% endif %}" title="Vote place as &quot;{{choice.1}}&quot;">
<i class="mdi mdi-shield-home"></i>
<span class="LP-Voting__Label">
{{choice.1}}
</span>
</a>
{% endfor %}
</div>
<div class="LP-Voting__CurrentVote">
{{place.get_level_display}}
</div>
</div>
<div class="LP-Voting__Right">
<div class="LP-Voting__Info">
<div class="LP-Voting__UserVote">
<span class="LP-Voting__InfoLabel">You voted this place as</span>
<span class="LP-Voting__Vote">{{voting.get_human_readable_level}}</span>
</div>
<div class="LP-Voting__Expiration">
<span class="LP-Voting__InfoLabel">Your vote expires on</span>
<span class="LP-Voting__Date">
<time datetime="{{voting.expires_when|date:'Y-m-d'}}">
{{voting.expires_when|date:'d.m.Y'}}
</time>
</span>
</div>
</div>
</div>
</div>

View File

@ -40,25 +40,64 @@
<p class="LP-Paragraph">{{ place.description }}</p>
</div>
<section class="LP-Section">
{% url 'place_tag_submit' place_id=place.id as tag_submit_url%}
{% partial tagging %}
{% set config=tagging_config %}
{% endpartial %}
</section>
<div class="LP-Quickinfo">
<section class="LP-Section">
{% url 'place_tag_submit' place_id=place.id as tag_submit_url %}
{% partial tagging %}
{% set config=tagging_config %}
{% endpartial %}
</section>
{{votingplace.vote}}
<section class="LP-Section">
{% partial voting %}
{% set place=place %}
{% set voting=placevoting %}
{% endpartial %}
</section>
</div>
<section class="LP-Section">
<h1 class="LP-Headline">{% translate 'Map links' %}</h1>
{% partial osm_map config=mapping_config %}
<div class="LP-LinkList">
<ul class="LP-LinkList__Container">
<li class="LP-LinkList__Item"><a target="_blank" href="https://www.google.com/maps?q={{place.latitude|safe}},{{place.longitude|safe}}" class="LP-Link"><span class="LP-Text">Google Maps</span></a></li>
<li class="LP-LinkList__Item"><a target="_blank" href="https://www.tim-online.nrw.de/tim-online2/?center={{place.latitude|safe}},{{place.longitude|safe}}&icon=true&bg=dop" class="LP-Link"><span class="LP-Text">TIM Online</span></a></li>
<li class="LP-LinkList__Item"><a target="_blank" href="http://www.openstreetmap.org/?mlat={{place.latitude|safe}}&mlon={{place.longitude|safe}}&zoom=16" class="LP-Link"><span class="LP-Text">OSM</span></a></li>
</ul>
</div>
<ul class="LP-LinkList">
<li class="LP-LinkList__Item">
<a target="_blank" href="https://www.google.com/maps?q={{place.latitude|safe}},{{place.longitude|safe}}" class="LP-Link">
<span class="LP-Text RV-Iconized">
<span class="RV-Iconized__Text">
Google Maps
</span>
<span class="RV-Iconized__Icon RV-Iconized__Icon--left">
<i class="mdi mdi-map-outline"></i>
</span>
</span>
</a>
</li>
<li class="LP-LinkList__Item">
<a target="_blank" href="https://www.tim-online.nrw.de/tim-online2/?center={{place.latitude|safe}},{{place.longitude|safe}}&icon=true&bg=dop" class="LP-Link">
<span class="LP-Text RV-Iconized">
<span class="RV-Iconized__Text">
TIM Online
</span>
<span class="RV-Iconized__Icon RV-Iconized__Icon--left">
<i class="mdi mdi-map-outline"></i>
</span>
</span>
</a>
</li>
<li class="LP-LinkList__Item">
<a target="_blank" href="http://www.openstreetmap.org/?mlat={{place.latitude|safe}}&mlon={{place.longitude|safe}}&zoom=16" class="LP-Link">
<span class="LP-Text RV-Iconized">
<span class="RV-Iconized__Text">
OSM
</span>
<span class="RV-Iconized__Icon RV-Iconized__Icon--left">
<i class="mdi mdi-map-outline"></i>
</span>
</span>
</a>
</li>
</ul>
</section>
<section class=" LP-Section">

View File

@ -22,7 +22,8 @@ from lostplaces.views import (
PhotoAlbumCreateView,
PhotoAlbumDeleteView,
ExplorerProfileView,
ExplorerProfileUpdateView
ExplorerProfileUpdateView,
PlaceVoteView
)
urlpatterns = [
@ -47,6 +48,8 @@ urlpatterns = [
path('place_image/create/<int:place_id>/', PlaceImageCreateView.as_view(), name='place_image_create'),
path('place_image/delete/<int:pk>/', PlaceImageDeleteView.as_view(), name='place_image_delete'),
path('place/vote/<int:place_id>/<int:vote>', PlaceVoteView.as_view(), name='place_vote'),
path('photo_album/create/<int:place_id>/', PhotoAlbumCreateView.as_view(), name='photo_album_create'),
path('photo_album/delete/<int:pk>/', PhotoAlbumDeleteView.as_view(), name='photo_album_delete')

View File

@ -1,5 +1,6 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from datetime import timedelta
from django.db.models.functions import Lower
@ -10,11 +11,17 @@ from django.views.generic.detail import SingleObjectMixin
from django.contrib import messages
from django.contrib.messages.views import SuccessMessageMixin
from django.utils.translation import ugettext_lazy as _
from django.utils import timezone
from django.shortcuts import render, redirect, get_object_or_404
from django.urls import reverse_lazy, reverse
from lostplaces.models import Place, PlaceImage
from lostplaces.models import (
Place,
PlaceImage,
PlaceVoting
)
from lostplaces.views.base_views import (
IsAuthenticatedMixin,
IsPlaceSubmitterMixin,
@ -46,8 +53,10 @@ class PlaceDetailView(IsAuthenticatedMixin, IsEligibleToSeePlaceMixin, View):
def get_place(self):
return get_object_or_404(Place, pk=self.kwargs['pk'])
def get(self, request, pk):
def get(self, request, pk):
place = self.get_place()
place.calculate_place_level()
explorer = request.user.explorer
context = {
'place': place,
@ -61,7 +70,8 @@ class PlaceDetailView(IsAuthenticatedMixin, IsEligibleToSeePlaceMixin, View):
'tagged_item': place,
'submit_url': reverse('place_tag_submit', kwargs={'tagged_id': place.id}),
'delete_url_name': 'place_tag_delete'
}
},
'placevoting': PlaceVoting.objects.filter(place=place, submitted_by=explorer).first()
}
return render(request, 'place/place_detail.html', context)
@ -185,3 +195,32 @@ class PlaceVisitDeleteView(IsAuthenticatedMixin, View):
request.user.explorer.save()
return redirect_referer_or(request, reverse('place_detail', kwargs={'pk': place.pk}))
class PlaceVoteView(IsEligibleToSeePlaceMixin, View):
delta = timedelta(weeks=24)
def get(self, request, place_id, vote):
place = get_object_or_404(Place, id=place_id)
explorer = request.user.explorer
voting = PlaceVoting.objects.filter(
submitted_by=explorer,
place=place
).first()
if voting is None:
voting = PlaceVoting.objects.create(
submitted_by=explorer,
place=place,
vote=vote,
expires_when=timezone.now()+self.delta
)
messages.success(self.request, _('Vote submitted'))
else:
voting.expires_when=timezone.now()+self.delta
voting.vote = vote
messages.success(self.request, _('Your vote has been update'))
voting.save()
return redirect_referer_or(request, reverse('place_detail', kwargs={'pk': place.pk}))