ojuso-map/apps/map/views.py
Carl van Tonder dce53630f7 Autocomplete from GIS list of CRSs..
..using django-autocomplete-light, in both the admin area and the
end-user form

Closes #32
2018-04-04 15:28:08 -04:00

92 lines
2.3 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

from django.core.mail import send_mail
from django.conf import settings
from django.db.models import Q
from django.views.generic import DetailView
from django.views.generic.base import TemplateView
from django.views.generic.edit import CreateView
from django.contrib.auth.mixins import LoginRequiredMixin
from django.urls import reverse
from dal import autocomplete
from .models import CaseStudy, SpatialRefSys
from .forms import ShortCaseStudyForm, LongCaseStudyForm
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
"""
class Map(TemplateView):
template_name = "map/index.html"
class Create(LoginRequiredMixin, TemplateView):
template_name = "map/how_much_time.html"
class BaseForm(LoginRequiredMixin, CreateView):
"""View for base case study form."""
template_name = 'map/form.html'
success_url = '/case-study/create/success/'
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()
return super().form_valid(form)
class ShortForm(BaseForm):
"""View for short version of case study form."""
form_class = ShortCaseStudyForm
class LongForm(BaseForm):
"""View for long version of case study form."""
form_class = LongCaseStudyForm
class FormSuccess(TemplateView):
template_name = 'map/form-success.html'
class CaseStudyDetail(DetailView):
template_name = "map/detail.html"
model = CaseStudy
context_object_name = "case_study"
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