add EnsureWritableCampMixin to villages

This commit is contained in:
Thomas Steen Rasmussen 2017-03-18 16:54:23 +01:00
parent 66f7b7ec2e
commit e95cc5ea93
3 changed files with 23 additions and 4 deletions

17
src/villages/mixins.py Normal file
View file

@ -0,0 +1,17 @@
from django.views.generic.detail import SingleObjectMixin
from django.contrib import messages
from django.shortcuts import redirect
from django.urls import reverse
class EnsureWritableCampMixin(SingleObjectMixin):
def dispatch(self, request, *args, **kwargs):
# do not permit view if camp is in readonly mode
if self.camp.read_only:
messages.error(request, "No thanks")
return redirect(reverse('village_list', kwargs={'camp_slug': self.camp.slug}))
# alright, continue with the request
return super().dispatch(request, *args, **kwargs)

View file

@ -16,7 +16,7 @@ Villages | {{ block.super }}
tent, chairs and tables in the shop!
</p>
{% if user.is_authenticated %}
{% if user.is_authenticated and not camp.read_only %}
<a href="{% url 'village_create' camp_slug=camp.slug %}" class="btn btn-primary">Create a new {{ camp.title }} village</a>
{% endif %}

View file

@ -7,6 +7,8 @@ from django.views.generic.detail import SingleObjectMixin
from .models import Village
from camps.models import Camp
from camps.mixins import CampViewMixin
from .mixins import EnsureWritableCampMixin
from django.contrib import messages
class VillageListView(CampViewMixin, ListView):
@ -21,7 +23,7 @@ class VillageDetailView(CampViewMixin, DetailView):
context_object_name = 'village'
class VillageCreateView(LoginRequiredMixin, CreateView):
class VillageCreateView(CampViewMixin, LoginRequiredMixin, EnsureWritableCampMixin, CreateView):
model = Village
template_name = 'village_form.html'
fields = ['name', 'description', 'private']
@ -51,7 +53,7 @@ class EnsureUserOwnsVillageMixin(SingleObjectMixin):
)
class VillageUpdateView(EnsureUserOwnsVillageMixin, LoginRequiredMixin, UpdateView):
class VillageUpdateView(CampViewMixin, EnsureUserOwnsVillageMixin, LoginRequiredMixin, EnsureWritableCampMixin, UpdateView):
model = Village
queryset = Village.objects.not_deleted()
template_name = 'village_form.html'
@ -61,7 +63,7 @@ class VillageUpdateView(EnsureUserOwnsVillageMixin, LoginRequiredMixin, UpdateVi
return self.get_object().get_absolute_url()
class VillageDeleteView(EnsureUserOwnsVillageMixin, LoginRequiredMixin, DeleteView):
class VillageDeleteView(CampViewMixin, EnsureUserOwnsVillageMixin, LoginRequiredMixin, EnsureWritableCampMixin, DeleteView):
model = Village
template_name = 'village_confirm_delete.html'
context_object_name = 'village'