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
This commit is contained in:
3
django_lostplaces/lostplaces/views/__init__.py
Normal file
3
django_lostplaces/lostplaces/views/__init__.py
Normal file
@@ -0,0 +1,3 @@
|
||||
from lostplaces.views.base_views import *
|
||||
from lostplaces.views.views import *
|
||||
from lostplaces.views.place_views import *
|
108
django_lostplaces/lostplaces/views/base_views.py
Normal file
108
django_lostplaces/lostplaces/views/base_views.py
Normal file
@@ -0,0 +1,108 @@
|
||||
from django.views import View
|
||||
from django.views.generic.edit import CreateView
|
||||
from django.views.generic.detail import SingleObjectMixin
|
||||
|
||||
from django.contrib import messages
|
||||
from django.contrib.auth.mixins import UserPassesTestMixin, LoginRequiredMixin
|
||||
from django.contrib.messages.views import SuccessMessageMixin
|
||||
|
||||
from django.shortcuts import redirect
|
||||
from django.urls import reverse_lazy
|
||||
|
||||
from lostplaces.models import Place
|
||||
|
||||
class IsAuthenticatedMixin(LoginRequiredMixin, View):
|
||||
'''
|
||||
A view mixin that checks wether a user is loged in or not.
|
||||
If the user is not logged in, he gets redirected to
|
||||
the login page.
|
||||
'''
|
||||
login_url = reverse_lazy('login')
|
||||
permission_denied_message = 'Please login to proceed'
|
||||
|
||||
def handle_no_permission(self):
|
||||
messages.error(self.request, self.permission_denied_message)
|
||||
return super().handle_no_permission()
|
||||
|
||||
class IsPlaceSubmitterMixin(UserPassesTestMixin, View):
|
||||
'''
|
||||
A view mixin that checks wethe a user is the submitter
|
||||
of a place Throws 403 if the user is not. The subclass
|
||||
has to provide a get_place method, wich returns the
|
||||
place to check.
|
||||
'''
|
||||
place_submitter_error_message = None
|
||||
|
||||
def get_place(self):
|
||||
pass
|
||||
|
||||
def test_func(self):
|
||||
""" Check if user is eligible to modify place. """
|
||||
|
||||
if not hasattr(self.request, 'user'):
|
||||
return False
|
||||
|
||||
if self.request.user.is_superuser:
|
||||
return True
|
||||
|
||||
# Check if currently logged in user was the submitter
|
||||
place_obj = self.get_place()
|
||||
|
||||
if place_obj and hasattr(place_obj, 'submitted_by') and self.request.user.explorer == place_obj.submitted_by:
|
||||
return True
|
||||
|
||||
if self.place_submitter_error_message:
|
||||
messages.error(self.request, self.place_submitter_error_message)
|
||||
return False
|
||||
|
||||
class PlaceAssetCreateView(IsAuthenticatedMixin, SuccessMessageMixin, CreateView):
|
||||
model = None
|
||||
fields = []
|
||||
template_name = ''
|
||||
success_message = ''
|
||||
|
||||
def get(self, request, place_id, *args, **kwargs):
|
||||
self.place = Place.objects.get(pk=place_id)
|
||||
return super().get(request, *args, **kwargs)
|
||||
|
||||
def post(self, request, place_id, *args, **kwargs):
|
||||
self.place = Place.objects.get(pk=place_id)
|
||||
response = super().post(request, *args, **kwargs)
|
||||
self.object.place = self.place
|
||||
self.object.submitted_by = request.user.explorer
|
||||
self.object.save()
|
||||
return response
|
||||
|
||||
def get_context_data(self, **kwargs):
|
||||
context = super().get_context_data(**kwargs)
|
||||
context['place'] = self.place
|
||||
return context
|
||||
|
||||
def get_success_url(self):
|
||||
return reverse_lazy('place_detail', kwargs={'pk': self.place.id})
|
||||
|
||||
class PlaceAssetDeleteView(IsAuthenticatedMixin, IsPlaceSubmitterMixin, SingleObjectMixin, View):
|
||||
model = None
|
||||
success_message = ''
|
||||
permission_denied_message = ''
|
||||
|
||||
def get_place(self):
|
||||
place_id = self.get_object().place.id
|
||||
return Place.objects.get(pk=place_id)
|
||||
|
||||
def test_func(self):
|
||||
can_edit_place = super().test_func()
|
||||
if can_edit_place:
|
||||
return True
|
||||
|
||||
if self.get_object().submitted_by == self.request.user.explorer:
|
||||
return True
|
||||
|
||||
messages.error(self.request, self.permission_denied_message)
|
||||
return False
|
||||
|
||||
def get(self, request, *args, **kwargs):
|
||||
place_id = self.get_object().place.id
|
||||
self.get_object().delete()
|
||||
messages.success(self.request, self.success_message)
|
||||
return redirect(reverse_lazy('place_detail', kwargs={'pk': place_id}))
|
129
django_lostplaces/lostplaces/views/place_views.py
Normal file
129
django_lostplaces/lostplaces/views/place_views.py
Normal file
@@ -0,0 +1,129 @@
|
||||
from django.views import View
|
||||
from django.views.generic.edit import CreateView, UpdateView, DeleteView
|
||||
from django.views.generic.detail import SingleObjectMixin
|
||||
from django.views.generic import ListView
|
||||
|
||||
from django.contrib import messages
|
||||
from django.contrib.messages.views import SuccessMessageMixin
|
||||
|
||||
from django.shortcuts import render, redirect
|
||||
from django.urls import reverse_lazy
|
||||
|
||||
from lostplaces.models import Place, PlaceImage
|
||||
from lostplaces.views import IsAuthenticatedMixin, IsPlaceSubmitterMixin
|
||||
from lostplaces.forms import PlaceForm, PlaceImageCreateForm, TagSubmitForm
|
||||
|
||||
from taggit.models import Tag
|
||||
|
||||
class PlaceListView(IsAuthenticatedMixin, ListView):
|
||||
paginate_by = 5
|
||||
model = Place
|
||||
template_name = 'place/place_list.html'
|
||||
ordering = ['name']
|
||||
|
||||
def get_context_data(self, **kwargs):
|
||||
context = super().get_context_data(**kwargs)
|
||||
context['mapping_config'] = {
|
||||
'all_points': context['place_list'],
|
||||
'map_center': Place.average_latlon(context['place_list'])
|
||||
}
|
||||
return context
|
||||
|
||||
class PlaceDetailView(IsAuthenticatedMixin, View):
|
||||
def get(self, request, pk):
|
||||
place = Place.objects.get(pk=pk)
|
||||
context = {
|
||||
'place': place,
|
||||
'mapping_config': {
|
||||
'all_points': [ place ],
|
||||
'map_center': {'latitude': place.latitude, 'longitude': place.longitude},
|
||||
},
|
||||
'tagging_config': {
|
||||
'all_tags': Tag.objects.all(),
|
||||
'submit_form': TagSubmitForm(),
|
||||
'tagged_item': place,
|
||||
'submit_url_name': 'place_tag_submit',
|
||||
'delete_url_name': 'place_tag_delete'
|
||||
}
|
||||
}
|
||||
return render(request, 'place/place_detail.html', context)
|
||||
|
||||
class PlaceUpdateView(IsAuthenticatedMixin, IsPlaceSubmitterMixin, SuccessMessageMixin, UpdateView):
|
||||
template_name = 'place/place_update.html'
|
||||
model = Place
|
||||
form_class = PlaceForm
|
||||
success_message = 'Successfully updated place.'
|
||||
place_submitter_error_message = 'You do no have permissions to alter this place'
|
||||
|
||||
def get_success_url(self):
|
||||
return reverse_lazy('place_detail', kwargs={'pk':self.get_object().pk})
|
||||
|
||||
def get_place(self):
|
||||
return self.get_object()
|
||||
|
||||
class PlaceCreateView(IsAuthenticatedMixin, View):
|
||||
|
||||
def get(self, request, *args, **kwargs):
|
||||
place_image_form = PlaceImageCreateForm()
|
||||
place_form = PlaceForm()
|
||||
|
||||
context = {
|
||||
'place_form': place_form,
|
||||
'place_image_form': place_image_form
|
||||
}
|
||||
return render(request, 'place/place_create.html', context)
|
||||
|
||||
def post(self, request, *args, **kwargs):
|
||||
place_form = PlaceForm(request.POST)
|
||||
|
||||
if place_form.is_valid():
|
||||
submitter = request.user.explorer
|
||||
place = place_form.save(commit=False)
|
||||
# Save logged in user as "submitted_by"
|
||||
place.submitted_by = submitter
|
||||
place.save()
|
||||
|
||||
if request.FILES:
|
||||
self._apply_multipart_image_upload(
|
||||
files=request.FILES.getlist('filename'),
|
||||
place=place,
|
||||
submitter=submitter
|
||||
)
|
||||
|
||||
messages.success(
|
||||
self.request,
|
||||
'Successfully created place.'
|
||||
)
|
||||
return redirect(reverse_lazy('place_detail', kwargs={'pk': place.pk}))
|
||||
|
||||
else:
|
||||
# Usually the browser should have checked the form before sending.
|
||||
messages.error(
|
||||
self.request,
|
||||
'Please fill in all required fields.'
|
||||
)
|
||||
return render(request, 'place/place_create.html', context={'form': form_place})
|
||||
|
||||
def _apply_multipart_image_upload(self, files, place, submitter):
|
||||
for image in files:
|
||||
place_image = PlaceImage.objects.create(
|
||||
filename=image,
|
||||
place=place,
|
||||
submitted_by=submitter
|
||||
)
|
||||
place_image.save()
|
||||
|
||||
class PlaceDeleteView(IsAuthenticatedMixin, IsPlaceSubmitterMixin, DeleteView):
|
||||
template_name = 'place/place_delete.html'
|
||||
model = Place
|
||||
success_message = 'Successfully deleted place.'
|
||||
success_url = reverse_lazy('place_list')
|
||||
success_message = 'Place deleted'
|
||||
place_submitter_error_message = 'You do no have permission to delete this place'
|
||||
|
||||
def delete(self, request, *args, **kwargs):
|
||||
messages.success(self.request, self.success_message)
|
||||
return super().delete(request, *args, **kwargs)
|
||||
|
||||
def get_place(self):
|
||||
return self.get_object()
|
81
django_lostplaces/lostplaces/views/views.py
Normal file
81
django_lostplaces/lostplaces/views/views.py
Normal file
@@ -0,0 +1,81 @@
|
||||
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')
|
Reference in New Issue
Block a user