lostplaces-backend/django_lostplaces/lostplaces/views/views.py

102 lines
3.5 KiB
Python
Raw Permalink Normal View History

2020-09-19 22:50:07 +02:00
#!/usr/bin/env python
# -*- coding: utf-8 -*-
2020-08-30 17:11:24 +02:00
from django.views import View
from django.views.generic.edit import CreateView
from django.contrib.messages.views import SuccessMessageMixin
from django.contrib import messages
2020-12-25 13:00:30 +01:00
from django.urls import reverse_lazy, reverse
from django.shortcuts import render, redirect, get_object_or_404
2020-09-02 00:08:00 +02:00
from django.http import HttpResponseForbidden
from django.utils.translation import ugettext_lazy as _
2020-08-30 17:11:24 +02:00
from lostplaces.forms import SignupVoucherForm, TagSubmitForm
from lostplaces.models import Place, PhotoAlbum
from lostplaces.views.base_views import IsAuthenticatedMixin
2020-12-25 12:57:05 +01:00
from lostplaces.common import redirect_referer_or
2020-08-30 17:11:24 +02:00
from lostplaces.views.base_views import (
2020-08-30 17:26:43 +02:00
PlaceAssetCreateView,
2020-09-02 00:08:00 +02:00
PlaceAssetDeleteView,
2020-08-30 17:26:43 +02:00
)
2020-09-02 00:08:00 +02:00
from taggit.models import Tag
2020-08-30 17:11:24 +02:00
class SignUpView(SuccessMessageMixin, CreateView):
form_class = SignupVoucherForm
2020-08-30 17:11:24 +02:00
success_url = reverse_lazy('login')
template_name = 'signup.html'
success_message = _('User created')
2020-08-30 17:11:24 +02:00
2020-09-13 10:56:18 +02:00
class HomeView(IsAuthenticatedMixin, View):
2020-08-30 17:11:24 +02:00
def get(self, request, *args, **kwargs):
place_list = Place.objects.all().order_by('-submitted_when')[:10]
context = {
'place_list': place_list,
2020-09-13 20:15:49 +02:00
'mapping_config': {
'all_points': place_list,
2020-09-12 11:47:42 +02:00
'map_center': Place.average_latlon(place_list)
}
2020-08-30 17:11:24 +02:00
}
return render(request, 'home.html', context)
def handle_no_permission(self):
place_list = Place.objects.all().order_by('-submitted_when')[:5]
context = {
'place_list': place_list
}
return render(self.request, 'home_unauth.html', context)
2020-08-30 17:26:43 +02:00
class PhotoAlbumCreateView(PlaceAssetCreateView):
2020-08-30 17:11:24 +02:00
model = PhotoAlbum
fields = ['url', 'label']
template_name = 'photo_album/photo_album_create.html'
2020-10-11 21:27:27 +02:00
success_message = _('Photo album link submitted')
2020-08-30 17:11:24 +02:00
2020-08-30 17:26:43 +02:00
class PhotoAlbumDeleteView(PlaceAssetDeleteView):
2020-08-30 17:11:24 +02:00
model = PhotoAlbum
pk_url_kwarg = 'pk'
2020-10-11 21:27:27 +02:00
success_message = _('Photo album link deleted')
permission_denied_messsage = _('You are not allowed to edit this photo album link')
2020-09-03 00:11:08 +02:00
2020-09-13 10:56:18 +02:00
class PlaceTagSubmitView(IsAuthenticatedMixin, View):
2020-09-12 11:24:16 +02:00
def post(self, request, tagged_id, *args, **kwargs):
2020-09-18 23:32:52 +02:00
place = get_object_or_404(Place, pk=tagged_id)
2020-08-30 18:39:45 +02:00
form = TagSubmitForm(request.POST)
if form.is_valid():
tag_list_raw = form.cleaned_data['tag_list']
tag_list_raw = tag_list_raw.strip().split(',')
tag_list = []
for tag in tag_list_raw:
tag_list.append(tag.strip())
place.tags.add(*tag_list)
place.save()
return redirect(reverse_lazy('place_detail', kwargs={'pk': place.id}))
2020-09-02 00:08:00 +02:00
2020-09-13 10:56:18 +02:00
class PlaceTagDeleteView(IsAuthenticatedMixin, View):
2020-09-02 00:08:00 +02:00
def get(self, request, tagged_id, tag_id, *args, **kwargs):
2020-09-18 23:32:52 +02:00
place = get_object_or_404(Place, pk=tagged_id)
tag = get_object_or_404(Tag, pk=tag_id)
2020-09-02 00:08:00 +02:00
place.tags.remove(tag)
2020-12-25 12:57:05 +01:00
return redirect_referer_or(request, reverse('place_detail', kwargs={'pk': tagged_id}))
def FlatView(request, slug):
if request.LANGUAGE_CODE == 'de':
return render(request, 'flat/' + slug + '-de' + '.html')
else:
return render(request, 'flat/' + slug + '.html')
2020-12-25 13:29:34 +01:00
class OSMMapView(IsAuthenticatedMixin, View):
def get(self, request):
place_list = Place.objects.all()
context = {
'mapping_config': {
'all_points': place_list,
'map_center': Place.average_latlon(place_list)
}
}
return render(request, 'osm_map_full.html', context)