Repository: liqd/a4-opin

Issue:
Remove clash between adhocracy4 library names
 - prevents override of global var  if same library name is used from
   different bundles

fixes #687 


Discussion:

Resolving patch:
diff --git a/euth/maps/fields.py b/euth/maps/fields.py
--- a/euth/maps/fields.py
+++ b/euth/maps/fields.py
@@ -1,17 +1,22 @@
+from django.core import validators as django_validators
 from django.utils.translation import ugettext_lazy as _
 from jsonfield.fields import JSONField, JSONFormField
 
-from .validators import GeoJSONFormFieldValidator
-
 
 class GeoJSONFormField(JSONFormField):
 
     def __init__(self, *args, **kwargs):
-        geom_type = kwargs.pop('geom_type')
-        required = kwargs.pop('required')
-        kwargs.setdefault(
-            'validators', [GeoJSONFormFieldValidator(geom_type, required)])
+        required_message = kwargs.pop('required_message')
         super().__init__(*args, **kwargs)
+        self.default_error_messages['required'] = required_message
+
+    def to_python(self, value):
+        empty_featureset = '{"type":"FeatureCollection","features":[]}'
+        if (value not in django_validators.EMPTY_VALUES and not
+                value == empty_featureset):
+            return super().to_python(value)
+        else:
+            return None
 
 
 class GeoJSONField(JSONField):
@@ -20,17 +25,8 @@ class GeoJSONField(JSONField):
     dim = 2
     geom_type = 'GEOMETRY'
 
-    '''
-    def __init__(self, *args, **kwargs):
-        required = kwargs.pop('required', True)
-        kwargs.setdefault(
-            'validators', [GeoJSONFormFieldValidator(
-                self.geom_type, required)])
-        super().__init__(*args, **kwargs)
-    '''
-
     def formfield(self, **kwargs):
-        kwargs.setdefault('geom_type', self.geom_type)
+        kwargs.setdefault('required_message', self.required_message)
         return super(GeoJSONField, self).formfield(**kwargs)
 
 
@@ -40,7 +36,9 @@ class GeometryField(GeoJSONField):
 
 class PointField(GeometryField):
     geom_type = 'POINT'
+    required_message = _('Please add a Marker on the map')
 
 
 class MultiPolygonField(GeoJSONField):
     geom_type = 'MULTIPOLYGON'
+    required_message = _('Please draw a Polygon on the map')
diff --git a/euth/maps/forms.py b/euth/maps/forms.py
--- a/euth/maps/forms.py
+++ b/euth/maps/forms.py
@@ -1,4 +1,5 @@
 from django import forms
+from django.utils.translation import ugettext as _
 
 from . import models
 from .widgets import MapChoosePointWidget
@@ -11,6 +12,8 @@ def __init__(self, *args, **kwargs):
         super().__init__(*args, **kwargs)
         self.fields['point'].widget = MapChoosePointWidget(
             polygon=self.settings.polygon)
+        self.fields['point'].error_messages['required'] = _(
+            'Please locate your proposal on the map.')
 
     class Meta:
         model = models.MapIdea
diff --git a/euth/maps/validators.py b/euth/maps/validators.py
deleted file mode 100644
--- a/euth/maps/validators.py
+++ /dev/null
@@ -1,31 +0,0 @@
-import json
-
-from django.core.exceptions import ValidationError
-from django.utils.translation import ugettext_lazy as _
-
-
-class GeoJSONFormFieldValidator(object):
-
-    def __init__(self, geom_type, required):
-        self.geom_type = geom_type
-        self.required = required
-
-    def __call__(self, value):
-        if isinstance(value, str):
-            value = json.loads(value)
-        err_msg = None
-        json_type = value.get('type')
-        if json_type == 'Feature' and self.geom_type == 'GEOMETRY':
-            geom_type = value.get('geometry').get('type')
-            is_geometry = geom_type in (
-                "Point", "MultiPoint", "LineString", "MultiLineString",
-                "Polygon", "MultiPolygon", "GeometryCollection"
-            )
-            if not is_geometry:
-                err_msg = '%s is not a valid GeoJSON geometry type' % geom_type
-        elif json_type == 'FeatureCollection':
-            if len(value.get('features')) == 0 and self.required:
-                err_msg = _('Field can not be empty')
-
-        if err_msg:
-            raise ValidationError(err_msg)


Test patch: