Add basic editing functionality

This commit is contained in:
Anna Sidwell 2019-03-04 19:48:29 +00:00
parent eb7eabb7c7
commit c41141fea6
4 changed files with 60 additions and 31 deletions

View File

@ -88,7 +88,6 @@ class BaseCaseStudyForm(forms.models.ModelForm):
self.helper.form_id = 'case-study-form' self.helper.form_id = 'case-study-form'
self.helper.form_class = 'form-horizontal' self.helper.form_class = 'form-horizontal'
self.helper.form_method = 'post' self.helper.form_method = 'post'
self.helper.form_action = 'add'
self.helper.label_class = 'col-md-3' self.helper.label_class = 'col-md-3'
self.helper.field_class = 'col-md-9' self.helper.field_class = 'col-md-9'
self.helper.include_media = False self.helper.include_media = False
@ -110,7 +109,6 @@ class ShortCaseStudyForm(BaseCaseStudyForm):
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
super(ShortCaseStudyForm, self).__init__(*args, **kwargs) super(ShortCaseStudyForm, self).__init__(*args, **kwargs)
self.helper.form_action = reverse('short-form')
self.helper.add_input(Submit('submit', _('Submit'), css_class='btn-success center-block')) self.helper.add_input(Submit('submit', _('Submit'), css_class='btn-success center-block'))
# Duplicate code from long form, below... # Duplicate code from long form, below...

View File

@ -16,6 +16,7 @@ urlpatterns = [
url(r'^case-study/create/success/?$', views.FormSuccess.as_view(), name='form-success'), url(r'^case-study/create/success/?$', views.FormSuccess.as_view(), name='form-success'),
url(r'^case-study/draft/?$', views.Drafts.as_view(), name='drafts'), url(r'^case-study/draft/?$', views.Drafts.as_view(), name='drafts'),
url(r'^case-study/(?P<slug>[-\w]+)/?$', views.CaseStudyDetail.as_view(), name='detail'), url(r'^case-study/(?P<slug>[-\w]+)/?$', views.CaseStudyDetail.as_view(), name='detail'),
url(r'^case-study/edit/(?P<pk>[\d]+)/?$', views.EditCaseStudy.as_view(), name='edit'),
url(r'^map/?$', views.Map.as_view(), name='map'), url(r'^map/?$', views.Map.as_view(), name='map'),
# API # API

View File

@ -6,10 +6,11 @@ from django.core.mail import send_mail
from django.db.models import Q from django.db.models import Q
from django.http import Http404, HttpResponse from django.http import Http404, HttpResponse
from django.urls import reverse from django.urls import reverse
from django.urls import reverse_lazy
from django.views import View from django.views import View
from django.views.generic import DetailView from django.views.generic import DetailView
from django.views.generic.base import TemplateView from django.views.generic.base import TemplateView
from django.views.generic.edit import CreateView from django.views.generic.edit import CreateView, UpdateView
from dal import autocomplete from dal import autocomplete
@ -47,34 +48,45 @@ class PointOfInterest(LoginRequiredMixin, CreateView):
form_class = PointOfInterest form_class = PointOfInterest
class BaseForm(LoginRequiredMixin, CreateView): def send_email(study_id):
"""View for base case study form."""
template_name = 'map/form-case_study.html'
success_url = '/case-study/create/success/'
model = CaseStudy
def send_email(self):
"""Sends email to moderator to approve case study.""" """Sends email to moderator to approve case study."""
try:
send_mail( send_mail(
'New case study submitted', 'New case study submitted',
NOTIFY_MESSAGE % ( NOTIFY_MESSAGE % (
settings.SITE_URL, settings.SITE_URL,
reverse('admin:map_casestudy_change', args=[self.object.id]) reverse('admin:map_casestudy_change', args=[study_id])
), ),
'noreply@ojuso.org', 'noreply@ojuso.org',
[settings.DATABASE_EMAIL], [settings.DATABASE_EMAIL],
fail_silently=False, fail_silently=False,
) )
def form_valid(self, form): except:
# XXX This is bad. We should do something more useful with the error
# than this.
pass
def delete_user_draft(user_id):
""" Delete the draft of a given user """
try:
draft = CaseStudyDraft.objects.get(author=user_id)
draft.delete()
except CaseStudyDraft.DoesNotExist:
pass
class FilesHandlerMixin:
def remove_bad_fields(self, form):
form.cleaned_data.pop('official_project_documents', None) form.cleaned_data.pop('official_project_documents', None)
form.cleaned_data.pop('other_documents', None) form.cleaned_data.pop('other_documents', None)
form.cleaned_data.pop('shapefiles', None) form.cleaned_data.pop('shapefiles', None)
form.cleaned_data.pop('images', None) form.cleaned_data.pop('images', None)
self.object = form.save() def add_file_refs(self, form):
self.object.official_project_documents.set(form.cleaned_data.get( self.object.official_project_documents.set(form.cleaned_data.get(
'official_project_documents_files', [] 'official_project_documents_files', []
)) ))
@ -88,31 +100,35 @@ class BaseForm(LoginRequiredMixin, CreateView):
'images_files', [] 'images_files', []
)) ))
def form_valid(self, form):
self.remove_bad_fields(form)
self.object.author = self.request.user self.object.author = self.request.user
self.object = form.save()
self.add_file_refs(form)
self.object.save() self.object.save()
# Don't error if the email can't be sent
try:
self.send_email()
except:
pass
# Delete the corresponding draft
try:
draft = CaseStudyDraft.objects.get(author=self.request.user)
draft.delete()
except CaseStudyDraft.DoesNotExist:
pass
return super().form_valid(form) return super().form_valid(form)
class ShortForm(BaseForm): class BaseCreateForm(LoginRequiredMixin, FilesHandlerMixin, CreateView):
"""View for base case study form."""
template_name = 'map/form-case_study.html'
success_url = reverse_lazy('form-success')
model = CaseStudy
def form_valid(self, form):
super().form_valid(form)
send_email(self.object.id)
delete_user_draft(self.request.user)
class ShortForm(BaseCreateForm):
"""View for short version of case study form.""" """View for short version of case study form."""
form_class = ShortCaseStudyForm form_class = ShortCaseStudyForm
class LongForm(BaseForm): class LongForm(BaseCreateForm):
"""View for long version of case study form.""" """View for long version of case study form."""
form_class = LongCaseStudyForm form_class = LongCaseStudyForm
@ -127,6 +143,18 @@ class CaseStudyDetail(DetailView):
context_object_name = "case_study" context_object_name = "case_study"
class BaseEditForm(LoginRequiredMixin, FilesHandlerMixin, UpdateView):
"""View for base case study form."""
template_name = 'map/form-case_study.html'
success_url = reverse_lazy('form-success')
model = CaseStudy
class EditCaseStudy(BaseEditForm):
form_class = ShortCaseStudyForm
class SpatialRefSysAutocomplete(autocomplete.Select2QuerySetView): class SpatialRefSysAutocomplete(autocomplete.Select2QuerySetView):
def get_queryset(self): def get_queryset(self):
qs = SpatialRefSys.objects.all() qs = SpatialRefSys.objects.all()

View File

@ -37,7 +37,9 @@
<tr> <tr>
<td>{{ case.entry_name }} <td>{{ case.entry_name }}
<td>{% if case.approved %}Yes{% else %}No{% endif %} <td>{% if case.approved %}Yes{% else %}No{% endif %}
<td><a href="{% url 'detail' case.slug %}">View</a> | Edit <td>
<a href="{% url 'detail' case.slug %}">View</a> |
<a href="{% url 'edit' case.id %}">Edit</a>
{% endfor %} {% endfor %}
</tbody> </tbody>
</table> </table>