2016-07-16 09:15:26 +00:00
|
|
|
from django.http import Http404
|
|
|
|
from django.contrib.auth.mixins import LoginRequiredMixin
|
2018-04-03 16:44:10 +00:00
|
|
|
from django.urls import reverse_lazy
|
2016-07-05 21:43:18 +00:00
|
|
|
from django.http import HttpResponseRedirect
|
2019-06-16 12:32:24 +00:00
|
|
|
from django.views.generic import (
|
|
|
|
ListView,
|
|
|
|
DetailView,
|
|
|
|
CreateView,
|
|
|
|
UpdateView,
|
|
|
|
DeleteView,
|
|
|
|
)
|
2016-07-16 09:15:26 +00:00
|
|
|
from django.views.generic.detail import SingleObjectMixin
|
|
|
|
from .models import Village
|
2016-12-28 23:15:13 +00:00
|
|
|
from camps.models import Camp
|
|
|
|
from camps.mixins import CampViewMixin
|
2017-03-18 15:54:23 +00:00
|
|
|
from .mixins import EnsureWritableCampMixin
|
2016-07-05 21:43:18 +00:00
|
|
|
|
|
|
|
|
2016-12-28 23:15:13 +00:00
|
|
|
class VillageListView(CampViewMixin, ListView):
|
2018-06-21 10:32:02 +00:00
|
|
|
model = Village
|
2019-06-16 12:32:24 +00:00
|
|
|
template_name = "village_list.html"
|
|
|
|
context_object_name = "villages"
|
2016-07-05 21:43:18 +00:00
|
|
|
|
2018-06-21 10:32:02 +00:00
|
|
|
def get_queryset(self):
|
|
|
|
return super().get_queryset().filter(deleted=False)
|
2016-07-05 21:43:18 +00:00
|
|
|
|
2018-06-23 22:41:58 +00:00
|
|
|
|
2016-12-28 23:15:13 +00:00
|
|
|
class VillageDetailView(CampViewMixin, DetailView):
|
2018-06-21 10:32:02 +00:00
|
|
|
model = Village
|
2019-06-16 12:32:24 +00:00
|
|
|
template_name = "village_detail.html"
|
|
|
|
context_object_name = "village"
|
2016-07-05 21:43:18 +00:00
|
|
|
|
2018-06-21 10:32:02 +00:00
|
|
|
def get_queryset(self):
|
|
|
|
return super().get_queryset().filter(deleted=False)
|
|
|
|
|
2016-07-05 21:43:18 +00:00
|
|
|
|
2019-06-16 12:32:24 +00:00
|
|
|
class VillageCreateView(
|
|
|
|
CampViewMixin, LoginRequiredMixin, EnsureWritableCampMixin, CreateView
|
|
|
|
):
|
2016-07-05 21:43:18 +00:00
|
|
|
model = Village
|
2019-06-16 12:32:24 +00:00
|
|
|
template_name = "village_form.html"
|
|
|
|
fields = ["name", "description", "private"]
|
2016-07-05 21:43:18 +00:00
|
|
|
|
|
|
|
def form_valid(self, form):
|
|
|
|
village = form.save(commit=False)
|
|
|
|
village.contact = self.request.user
|
2019-06-16 12:32:24 +00:00
|
|
|
village.camp = Camp.objects.get(slug=self.request.session["campslug"])
|
2017-08-23 01:04:33 +00:00
|
|
|
if not village.name:
|
|
|
|
village.name = "noname"
|
2016-07-05 21:43:18 +00:00
|
|
|
village.save()
|
|
|
|
return HttpResponseRedirect(village.get_absolute_url())
|
|
|
|
|
2017-03-18 14:38:12 +00:00
|
|
|
def get_success_url(self):
|
2019-06-16 12:32:24 +00:00
|
|
|
return reverse_lazy("village_list", kwargs={"camp_slug": self.object.camp.slug})
|
2017-03-18 14:38:12 +00:00
|
|
|
|
2016-07-05 21:43:18 +00:00
|
|
|
|
2016-07-16 09:15:26 +00:00
|
|
|
class EnsureUserOwnsVillageMixin(SingleObjectMixin):
|
|
|
|
model = Village
|
|
|
|
|
|
|
|
def dispatch(self, request, *args, **kwargs):
|
|
|
|
# If the user is not contact for this village OR is not staff
|
|
|
|
if not request.user.is_staff:
|
|
|
|
if self.get_object().contact != request.user:
|
|
|
|
raise Http404("Village not found")
|
|
|
|
|
|
|
|
return super(EnsureUserOwnsVillageMixin, self).dispatch(
|
|
|
|
request, *args, **kwargs
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2019-06-16 12:32:24 +00:00
|
|
|
class VillageUpdateView(
|
|
|
|
CampViewMixin,
|
|
|
|
EnsureUserOwnsVillageMixin,
|
|
|
|
LoginRequiredMixin,
|
|
|
|
EnsureWritableCampMixin,
|
|
|
|
UpdateView,
|
|
|
|
):
|
2016-07-05 21:43:18 +00:00
|
|
|
model = Village
|
2019-06-16 12:32:24 +00:00
|
|
|
template_name = "village_form.html"
|
|
|
|
fields = ["name", "description", "private"]
|
2016-07-05 21:43:18 +00:00
|
|
|
|
2017-08-23 01:09:41 +00:00
|
|
|
def form_valid(self, form):
|
2017-08-23 01:04:33 +00:00
|
|
|
village = form.save(commit=False)
|
|
|
|
if not village.name:
|
|
|
|
village.name = "noname"
|
2018-06-23 22:41:58 +00:00
|
|
|
return super().form_valid(form)
|
2017-08-23 01:04:33 +00:00
|
|
|
|
2016-07-05 21:43:18 +00:00
|
|
|
def get_success_url(self):
|
|
|
|
return self.get_object().get_absolute_url()
|
2016-07-10 17:19:41 +00:00
|
|
|
|
2018-06-21 10:32:02 +00:00
|
|
|
def get_queryset(self):
|
|
|
|
return super().get_queryset().filter(deleted=False)
|
|
|
|
|
2016-07-10 17:19:41 +00:00
|
|
|
|
2019-06-16 12:32:24 +00:00
|
|
|
class VillageDeleteView(
|
|
|
|
CampViewMixin,
|
|
|
|
EnsureUserOwnsVillageMixin,
|
|
|
|
LoginRequiredMixin,
|
|
|
|
EnsureWritableCampMixin,
|
|
|
|
DeleteView,
|
|
|
|
):
|
2016-07-10 17:19:41 +00:00
|
|
|
model = Village
|
2019-06-16 12:32:24 +00:00
|
|
|
template_name = "village_confirm_delete.html"
|
|
|
|
context_object_name = "village"
|
2016-12-28 23:15:13 +00:00
|
|
|
|
2017-03-18 14:38:12 +00:00
|
|
|
def get_success_url(self):
|
2019-06-16 12:32:24 +00:00
|
|
|
return reverse_lazy("village_list", kwargs={"camp_slug": self.object.camp.slug})
|