lostplaces-backend/django_lostplaces/lostplaces/views/views.py
reverend 547179b0ca Squashed commit of the following:
commit 97b044cafb7f17f23b3b1beedcf70af209a60ddc
Author: reverend <reverend@reverend2048.de>
Date:   Mon Sep 14 17:25:40 2020 +0200

    Updating gitignore

commit 4891d80486e1f95db8ae66385c7c97426a3ca1a4
Author: reverend <reverend@reverend2048.de>
Date:   Mon Sep 14 17:25:20 2020 +0200

    Updating Readme

commit f05c43abbdc7eb30896ad6d10fe80fd6483338d9
Author: reverend <reverend@reverend2048.de>
Date:   Mon Sep 14 17:23:30 2020 +0200

    Renaming Module

commit fd5ad2ee9f8cbacd565da45b257928192ffc651c
Author: reverend <reverend@reverend2048.de>
Date:   Mon Sep 14 17:23:16 2020 +0200

    Renaming module references

commit 828a0dd5dd73723b84b77908497903ed26b6966b
Author: reverend <reverend@reverend2048.de>
Date:   Mon Sep 14 17:21:20 2020 +0200

    Renaming Project
2020-09-14 17:26:17 +02:00

82 lines
2.8 KiB
Python

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
from django.http import HttpResponseForbidden
from lostplaces.forms import ExplorerCreationForm, TagSubmitForm
from lostplaces.models import Place, PhotoAlbum
from lostplaces.views.base_views import IsAuthenticatedMixin
from lostplaces.views.base_views import (
PlaceAssetCreateView,
PlaceAssetDeleteView,
)
from taggit.models import Tag
class SignUpView(SuccessMessageMixin, CreateView):
form_class = ExplorerCreationForm
success_url = reverse_lazy('login')
template_name = 'signup.html'
success_message = 'User created.'
class HomeView(IsAuthenticatedMixin, View):
def get(self, request, *args, **kwargs):
place_list = Place.objects.all().order_by('-submitted_when')[:10]
context = {
'all_points': place_list,
'mapping_config': {
'point_list': place_list,
'map_center': Place.average_latlon(place_list)
}
}
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)
class PhotoAlbumCreateView(PlaceAssetCreateView):
model = PhotoAlbum
fields = ['url', 'label']
template_name = 'photo_album/photo_album_create.html'
success_message = 'Photo Album submitted'
class PhotoAlbumDeleteView(PlaceAssetDeleteView):
model = PhotoAlbum
pk_url_kwarg = 'pk'
success_message = 'Photo Album deleted'
permission_denied_messsage = 'You do not have permissions to alter this photo album'
class PlaceTagSubmitView(IsAuthenticatedMixin, View):
def post(self, request, tagged_id, *args, **kwargs):
place = Place.objects.get(pk=tagged_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}))
class PlaceTagDeleteView(IsAuthenticatedMixin, 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}))
def FlatView(request, slug):
return render(request, 'flat/' + slug + '.html')