#42 Place Voting / Level
This commit is contained in:
@@ -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}))
|
Reference in New Issue
Block a user