2018-05-01 02:17:18 +00:00
|
|
|
|
import json
|
|
|
|
|
|
2018-04-02 13:02:49 +00:00
|
|
|
|
from django.conf import settings
|
2018-04-16 04:27:13 +00:00
|
|
|
|
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
|
2017-10-08 20:21:51 +00:00
|
|
|
|
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
|
2018-04-16 04:27:13 +00:00
|
|
|
|
|
|
|
|
|
from dal import autocomplete
|
|
|
|
|
|
2018-05-01 02:17:18 +00:00
|
|
|
|
from apps.files.models import File
|
|
|
|
|
|
2018-04-16 04:27:13 +00:00
|
|
|
|
from .models import CaseStudy, CaseStudyDraft, SpatialRefSys
|
2017-10-08 20:21:51 +00:00
|
|
|
|
from .forms import ShortCaseStudyForm, LongCaseStudyForm
|
2017-05-20 23:47:14 +00:00
|
|
|
|
|
2018-04-14 02:40:42 +00:00
|
|
|
|
|
2018-04-02 13:02:49 +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
|
|
|
|
|
2017-10-08 20:21:51 +00:00
|
|
|
|
class Map(TemplateView):
|
|
|
|
|
template_name = "map/index.html"
|
2017-05-20 23:47:14 +00:00
|
|
|
|
|
2017-06-16 16:06:22 +00:00
|
|
|
|
|
2017-10-08 20:21:51 +00:00
|
|
|
|
class Create(LoginRequiredMixin, TemplateView):
|
|
|
|
|
template_name = "map/how_much_time.html"
|
2017-06-16 16:06:22 +00:00
|
|
|
|
|
2017-10-08 20:21:51 +00:00
|
|
|
|
|
2017-10-09 23:58:36 +00:00
|
|
|
|
class BaseForm(LoginRequiredMixin, CreateView):
|
|
|
|
|
"""View for base case study form."""
|
2017-10-08 20:21:51 +00:00
|
|
|
|
template_name = 'map/form.html'
|
2018-03-28 07:16:13 +00:00
|
|
|
|
success_url = '/case-study/create/success/'
|
2017-10-09 23:58:36 +00:00
|
|
|
|
model = CaseStudy
|
2017-10-08 20:21:51 +00:00
|
|
|
|
|
2018-04-02 13:02:49 +00:00
|
|
|
|
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',
|
2018-04-04 17:36:27 +00:00
|
|
|
|
[settings.DATABASE_EMAIL],
|
2018-04-02 13:02:49 +00:00
|
|
|
|
fail_silently=False,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
def form_valid(self, form):
|
2018-05-01 02:17:18 +00:00
|
|
|
|
form.cleaned_data.pop('official_project_documents', None)
|
|
|
|
|
form.cleaned_data.pop('other_documents', None)
|
|
|
|
|
form.cleaned_data.pop('shapefiles', None)
|
2018-04-23 05:15:33 +00:00
|
|
|
|
|
2018-04-02 13:02:49 +00:00
|
|
|
|
self.object = form.save()
|
2018-04-23 05:15:33 +00:00
|
|
|
|
|
|
|
|
|
self.object.official_project_documents = form.cleaned_data.get(
|
|
|
|
|
'official_project_document_files', []
|
|
|
|
|
)
|
|
|
|
|
self.object.other_documents = form.cleaned_data.get(
|
|
|
|
|
'other_documents_files', []
|
|
|
|
|
)
|
|
|
|
|
self.object.shapefiles = form.cleaned_data.get(
|
|
|
|
|
'shapefiles_files', []
|
|
|
|
|
)
|
|
|
|
|
|
2018-04-02 13:02:49 +00:00
|
|
|
|
self.send_email()
|
2018-04-16 00:57:05 +00:00
|
|
|
|
|
|
|
|
|
# Delete the corresponding draft
|
2018-05-01 02:17:18 +00:00
|
|
|
|
draft = CaseStudyDraft.objects.get(author=self.request.user)
|
2018-04-16 00:57:05 +00:00
|
|
|
|
if draft:
|
|
|
|
|
draft.delete()
|
|
|
|
|
|
2018-04-02 13:02:49 +00:00
|
|
|
|
return super().form_valid(form)
|
|
|
|
|
|
2017-10-08 20:21:51 +00:00
|
|
|
|
|
|
|
|
|
class ShortForm(BaseForm):
|
2017-10-09 23:58:36 +00:00
|
|
|
|
"""View for short version of case study form."""
|
2017-10-08 20:21:51 +00:00
|
|
|
|
form_class = ShortCaseStudyForm
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class LongForm(BaseForm):
|
2017-10-09 23:58:36 +00:00
|
|
|
|
"""View for long version of case study form."""
|
2017-10-08 20:21:51 +00:00
|
|
|
|
form_class = LongCaseStudyForm
|
|
|
|
|
|
|
|
|
|
|
2017-10-09 23:58:36 +00:00
|
|
|
|
class FormSuccess(TemplateView):
|
|
|
|
|
template_name = 'map/form-success.html'
|
|
|
|
|
|
|
|
|
|
|
2017-10-08 20:21:51 +00:00
|
|
|
|
class CaseStudyDetail(DetailView):
|
|
|
|
|
template_name = "map/detail.html"
|
|
|
|
|
model = CaseStudy
|
|
|
|
|
context_object_name = "case_study"
|
2018-04-14 02:40:42 +00:00
|
|
|
|
|
|
|
|
|
|
2018-04-16 04:27:13 +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-05-01 02:23:36 +00:00
|
|
|
|
class Drafts(LoginRequiredMixin, View):
|
2018-04-14 02:40:42 +00:00
|
|
|
|
"""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):
|
|
|
|
|
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):
|
|
|
|
|
# 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):
|
|
|
|
|
draft = self.get_object(request)
|
2018-04-23 05:15:33 +00:00
|
|
|
|
|
2018-04-14 02:40:42 +00:00
|
|
|
|
if draft != None:
|
2018-05-01 02:17:18 +00:00
|
|
|
|
data = json.loads(draft.data)
|
|
|
|
|
|
|
|
|
|
for k in ['official_project_documents', 'other_documents',
|
|
|
|
|
'shapefiles']:
|
|
|
|
|
|
2018-05-01 05:08:40 +00:00
|
|
|
|
try:
|
|
|
|
|
items = list(filter(
|
|
|
|
|
lambda x: (
|
|
|
|
|
x['name'] == '{0}_files'.format(k)
|
|
|
|
|
and x['value'] != ''
|
|
|
|
|
), data['data']['form']
|
|
|
|
|
))
|
|
|
|
|
except KeyError:
|
|
|
|
|
continue
|
2018-05-01 02:17:18 +00:00
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
items = items[0]['value'].split(',')
|
|
|
|
|
except IndexError:
|
|
|
|
|
continue
|
|
|
|
|
|
|
|
|
|
for item in items:
|
|
|
|
|
try:
|
|
|
|
|
f = File.objects.get(id=item)
|
|
|
|
|
if f.user != self.request.user:
|
|
|
|
|
continue
|
|
|
|
|
f.delete()
|
|
|
|
|
except File.DoesNotExist:
|
|
|
|
|
continue
|
|
|
|
|
|
2018-04-14 02:40:42 +00:00
|
|
|
|
draft.delete()
|
2018-04-23 05:15:33 +00:00
|
|
|
|
|
2018-04-14 02:40:42 +00:00
|
|
|
|
return HttpResponse(status=204)
|