diff options
author | luxagraf <sng@luxagraf.net> | 2018-12-29 08:37:39 -0600 |
---|---|---|
committer | luxagraf <sng@luxagraf.net> | 2018-12-29 08:37:39 -0600 |
commit | 4f7b84194b056b5d6d9acca4cceb2cabc04fd8a5 (patch) | |
tree | 7fe109e7aeaddab7aa5e7f46f99414064a248e52 /apps/utils | |
parent | 02f520038e3c6d5a01c9545e9b1c3eb91e4e016c (diff) |
cleaned up JS and made modal handler.
Diffstat (limited to 'apps/utils')
-rw-r--r-- | apps/utils/views.py | 27 | ||||
-rw-r--r-- | apps/utils/widgets.py | 33 |
2 files changed, 46 insertions, 14 deletions
diff --git a/apps/utils/views.py b/apps/utils/views.py index 595e102..ec3a902 100644 --- a/apps/utils/views.py +++ b/apps/utils/views.py @@ -1,6 +1,6 @@ from itertools import chain import json -from django.http import Http404, HttpResponse +from django.http import Http404, HttpResponse, JsonResponse from django.apps import apps from django.views.generic import ListView from django.views.generic.base import View, RedirectView @@ -42,6 +42,31 @@ class LoggedInViewWithUser(View): return kwargs + +class AjaxableResponseMixin: + """ + Mixin to add AJAX support to a form. + Must be used with an object-based FormView (e.g. CreateView) + """ + def form_invalid(self, form): + response = super().form_invalid(form) + if self.request.is_ajax(): + return JsonResponse(form.errors, status=400) + else: + return response + + def form_valid(self, form): + # We make sure to call the parent's form_valid() method because + # it might do some processing (in the case of CreateView, it will + # call form.save() for example). + response = super().form_valid(form) + if self.request.is_ajax(): + data = { + 'pk': self.object.pk, + } + return JsonResponse(data) + else: + return response ''' class TagAutocomplete(autocomplete.Select2QuerySetView): def get_queryset(self): diff --git a/apps/utils/widgets.py b/apps/utils/widgets.py index f4a7a4a..2745932 100644 --- a/apps/utils/widgets.py +++ b/apps/utils/widgets.py @@ -2,8 +2,9 @@ import os from django import forms from django.contrib import admin from django.contrib.admin.widgets import AdminFileWidget -from django.contrib.gis.admin import OSMGeoAdmin from django.utils.safestring import mark_safe +from django.forms import widgets +from django.urls import reverse from django.utils.translation import ugettext_lazy as _ from django.template.loader import render_to_string from django.template import Context @@ -130,15 +131,21 @@ class LGEntryFormSmall(forms.ModelForm): } -class OLAdminBase(OSMGeoAdmin): - default_lon = -9285175 - default_lat = 4025046 - default_zoom = 15 - units = True - scrollable = False - map_width = 700 - map_height = 425 - map_template = 'gis/admin/osm.html' - openlayers_url = '/static/admin/js/OpenLayers.js' - - +class RelatedFieldWidgetCanAdd(widgets.Select): + """ + Modifies standard django Select widget to add link after to add new instance + of related model (doesn't check permissions, that's for the form instance) + """ + def __init__(self, related_model, related_url=None, *args, **kw): + super(RelatedFieldWidgetCanAdd, self).__init__(*args, **kw) + if not related_url: + rel_to = related_model + info = (rel_to._meta.app_label, rel_to._meta.object_name.lower()) + related_url = 'admin:%s_%s_add' % info + self.related_url = related_url + + def render(self, name, value, *args, **kwargs): + self.related_url = reverse(self.related_url) + output = [super(RelatedFieldWidgetCanAdd, self).render(name, value, *args, **kwargs)] + output.append('<a class="circle plus small-circle modal-open" href="%s" id="add_id_%s" data-modal-hed-class="%s" data-modal-hed="Add a New %s">New</a>' % (self.related_url, name, name, name.capitalize())) + return mark_safe(u''.join(output)) |