2016-07-16 09:15:26 +00:00
|
|
|
from django.http import Http404
|
|
|
|
from django.contrib.auth.mixins import LoginRequiredMixin
|
2016-07-05 21:43:18 +00:00
|
|
|
from django.core.urlresolvers import reverse_lazy
|
|
|
|
from django.http import HttpResponseRedirect
|
2016-07-10 17:19:41 +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-07-05 21:43:18 +00:00
|
|
|
|
|
|
|
|
|
|
|
class VillageListView(ListView):
|
2016-07-10 17:19:41 +00:00
|
|
|
queryset = Village.objects.not_deleted()
|
2016-07-05 21:43:18 +00:00
|
|
|
template_name = 'village_list.html'
|
|
|
|
context_object_name = 'villages'
|
|
|
|
|
|
|
|
|
|
|
|
class VillageDetailView(DetailView):
|
2016-07-10 17:19:41 +00:00
|
|
|
queryset = Village.objects.not_deleted()
|
2016-07-05 21:43:18 +00:00
|
|
|
template_name = 'village_detail.html'
|
|
|
|
context_object_name = 'village'
|
|
|
|
|
|
|
|
|
2016-07-16 09:15:26 +00:00
|
|
|
class VillageCreateView(LoginRequiredMixin, CreateView):
|
2016-07-05 21:43:18 +00:00
|
|
|
model = Village
|
|
|
|
template_name = 'village_form.html'
|
2016-07-05 21:57:08 +00:00
|
|
|
fields = ['name', 'description', 'private']
|
2016-07-05 21:43:18 +00:00
|
|
|
success_url = reverse_lazy('villages:list')
|
|
|
|
|
|
|
|
def form_valid(self, form):
|
|
|
|
village = form.save(commit=False)
|
|
|
|
village.contact = self.request.user
|
|
|
|
village.save()
|
|
|
|
return HttpResponseRedirect(village.get_absolute_url())
|
|
|
|
|
|
|
|
|
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
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
class VillageUpdateView(EnsureUserOwnsVillageMixin, LoginRequiredMixin, UpdateView):
|
2016-07-05 21:43:18 +00:00
|
|
|
model = Village
|
2016-07-10 17:19:41 +00:00
|
|
|
queryset = Village.objects.not_deleted()
|
2016-07-05 21:43:18 +00:00
|
|
|
template_name = 'village_form.html'
|
2016-07-05 21:57:08 +00:00
|
|
|
fields = ['name', 'description', 'private']
|
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
|
|
|
|
|
|
|
|
2016-07-16 09:15:26 +00:00
|
|
|
class VillageDeleteView(EnsureUserOwnsVillageMixin, LoginRequiredMixin, DeleteView):
|
2016-07-10 17:19:41 +00:00
|
|
|
model = Village
|
|
|
|
success_url = reverse_lazy('villages:list')
|
|
|
|
template_name = 'village_confirm_delete.html'
|
|
|
|
context_object_name = 'village'
|