ojuso-map/apps/map/views.py

146 lines
3.9 KiB
Python
Raw Normal View History

from django.conf import settings
from django.contrib.auth.mixins import LoginRequiredMixin
from django.core.mail import send_mail
from django.db.models import Q
from django.http import Http404, HttpResponse
from django.urls import reverse
2018-04-14 02:40:42 +00:00
from django.views import View
from django.views.generic import DetailView
from django.views.generic.base import TemplateView
2017-10-09 23:58:36 +00:00
from django.views.generic.edit import CreateView
from dal import autocomplete
from .models import CaseStudy, CaseStudyDraft, SpatialRefSys
from .forms import ShortCaseStudyForm, LongCaseStudyForm
2018-04-14 02:40:42 +00:00
NOTIFY_MESSAGE = """
Hello,
Someone has submitted a new case study to the Ojuso website. Please
follow the below link to look over and approve it:
%s%s
Case Study Robot
"""
2017-06-16 16:06:22 +00:00
class Map(TemplateView):
template_name = "map/index.html"
2017-06-16 16:06:22 +00:00
class Create(LoginRequiredMixin, TemplateView):
template_name = "map/how_much_time.html"
2017-06-16 16:06:22 +00:00
2017-10-09 23:58:36 +00:00
class BaseForm(LoginRequiredMixin, CreateView):
"""View for base case study form."""
template_name = 'map/form.html'
success_url = '/case-study/create/success/'
2017-10-09 23:58:36 +00:00
model = CaseStudy
def send_email(self):
"""Sends email to moderator to approve case study."""
send_mail(
'New case study submitted',
NOTIFY_MESSAGE % (
settings.SITE_URL,
reverse('admin:map_casestudy_change', args=[self.object.id])
),
'noreply@ojuso.org',
[settings.DATABASE_EMAIL],
fail_silently=False,
)
def form_valid(self, form):
self.object = form.save()
self.send_email()
# Delete the corresponding draft
draft = CaseStudyDraft.objects.get(author=request.user)
if draft:
draft.delete()
return super().form_valid(form)
class ShortForm(BaseForm):
2017-10-09 23:58:36 +00:00
"""View for short version of case study form."""
form_class = ShortCaseStudyForm
class LongForm(BaseForm):
2017-10-09 23:58:36 +00:00
"""View for long version of case study form."""
form_class = LongCaseStudyForm
2017-10-09 23:58:36 +00:00
class FormSuccess(TemplateView):
template_name = 'map/form-success.html'
class CaseStudyDetail(DetailView):
template_name = "map/detail.html"
model = CaseStudy
context_object_name = "case_study"
2018-04-14 02:40:42 +00:00
class SpatialRefSysAutocomplete(autocomplete.Select2QuerySetView):
def get_queryset(self):
qs = SpatialRefSys.objects.all()
if self.q:
qs = qs.filter(
Q(auth_name__icontains=self.q)
| Q(auth_srid__icontains=self.q)
)
return qs
2018-04-14 02:40:42 +00:00
class Drafts(View):
"""Retrieve or save a draft."""
def get_object(self, request):
try:
return CaseStudyDraft.objects.get(author=request.user)
except:
return None
def get(self, request):
if not request.user.is_authenticated:
return HttpResponse(status=403) # Forbidden
draft = self.get_object(request)
if draft == None:
return HttpResponse(status=404) # Not Found
else:
return HttpResponse(draft.data, content_type="application/json")
def put(self, request):
if not request.user.is_authenticated:
return HttpResponse(status=403) # Forbidden
# Find an existing object is there is one
draft = self.get_object(request)
if draft == None:
# If there isn't, create a new draft...
draft = CaseStudyDraft(author=request.user, data=request.body)
draft.save()
return HttpResponse(status=201) # Created
else:
draft.data = request.body
draft.save()
return HttpResponse(status=200) # OK
def delete(self, request):
if not request.user.is_authenticated:
return HttpResponse(status=403) # Forbidden
draft = self.get_object(request)
if draft != None:
draft.delete()
return HttpResponse(status=204)