1. Yohan Boniface
  2. django-leaflet-storage

Commits

Yohan Boniface  committed 4130056

return errors as message in formless views

  • Participants
  • Parent commits 6578dcf
  • Branches master

Comments (0)

Files changed (2)

File leaflet_storage/forms.py

View file
  • Ignore whitespace
+# -*- coding: utf-8 -*-
 
 from django import forms
 from django.contrib.gis.geos import Point
 from django.utils.translation import ugettext_lazy as _
 from django.template.defaultfilters import slugify
 from django.conf import settings
+from django.forms.util import ErrorList
 
 from .models import Map, DataLayer
 
 DEFAULT_CENTER = Point(DEFAULT_LONGITUDE, DEFAULT_LATITUDE)
 
 
+
+class FlatErrorList(ErrorList):
+    def __unicode__(self):
+        return self.flat()
+    def flat(self):
+        if not self: return u''
+        return u' — '.join([e for e in self])
+
 class UpdateMapPermissionsForm(forms.ModelForm):
 
     class Meta:

File leaflet_storage/views.py

View file
  • Ignore whitespace
 from .utils import get_uri_template
 from .forms import (DataLayerForm, UpdateMapPermissionsForm, MapSettingsForm,
                     AnonymousMapPermissionsForm, DEFAULT_LATITUDE,
-                    DEFAULT_LONGITUDE)
+                    DEFAULT_LONGITUDE, FlatErrorList)
 
 
 # ############## #
     http_method_names = [u'post', ]
 
     def form_invalid(self, form):
-        return simple_json_response(errors=form.errors, infos=_("An error occured."))
-
+        return simple_json_response(errors=form.errors, error=unicode(form.errors))
 
+    def get_form(self, form_class):
+        kwargs = self.get_form_kwargs()
+        kwargs['error_class'] = FlatErrorList
+        return form_class(**kwargs)
 
 class MapDetailMixin(object):
 
         return response
 
 
-class DataLayerCreate(CreateView):
+class DataLayerCreate(FormLessEditMixin, CreateView):
     model = DataLayer
     form_class = DataLayerForm
 
         return simple_json_response(**self.object.metadata)
 
 
-class DataLayerUpdate(UpdateView):
+class DataLayerUpdate(FormLessEditMixin, UpdateView):
     model = DataLayer
     form_class = DataLayerForm