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

89 lines
3.0 KiB
Python
Raw 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
from django.urls import reverse_lazy
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 ExplorerCreationForm, TagSubmitForm
from lostplaces.models import Place, PhotoAlbum
from lostplaces.views.base_views import IsAuthenticatedMixin
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 = ExplorerCreationForm
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'
success_message = _('Photo Album 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'
success_message = _('Photo Album deleted')
permission_denied_messsage = _('You do not have permissions to alter this photo album')
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)
return redirect(reverse_lazy('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')