lostplaces-backend/lostplaces/lostplaces_app/views/views.py

69 lines
2.4 KiB
Python
Raw Normal View History

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
2020-09-02 00:08:00 +02:00
from django.http import HttpResponseForbidden
2020-08-30 17:11:24 +02:00
2020-08-30 18:39:45 +02:00
from lostplaces_app.forms import ExplorerCreationForm, TagSubmitForm
2020-08-30 17:11:24 +02:00
from lostplaces_app.models import Place, PhotoAlbum
2020-08-30 18:39:45 +02:00
from lostplaces_app.views.base_views import IsAuthenticated
2020-08-30 17:11:24 +02:00
2020-08-30 17:26:43 +02:00
from lostplaces_app.views.base_views import (
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.'
class HomeView(View):
def get(self, request, *args, **kwargs):
place_list = Place.objects.all().order_by('-submitted_when')[:10]
place_map_center = Place.average_latlon(place_list)
context = {
'place_list': place_list,
'place_map_center': place_map_center
}
return render(request, 'home.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: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'
2020-08-30 18:39:45 +02:00
permission_denied_messsage = 'You do not have permissions to alter this photo album'
class PlaceTagSubmitView(IsAuthenticated, View):
def post(self, request, place_id, *args, **kwargs):
place = Place.objects.get(pk=place_id)
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
class PlaceTagDeleteView(IsAuthenticated, View):
def get(self, request, tagged_id, tag_id, *args, **kwargs):
place = Place.objects.get(pk=tagged_id)
tag = Tag.objects.get(pk=tag_id)
place.tags.remove(tag)
return redirect(reverse_lazy('place_detail', kwargs={'pk': tagged_id}))