From 90f9313faa4bcfdd9e6708d0f34b123a8805d5b7 Mon Sep 17 00:00:00 2001 From: Oskar Hahn Date: Sat, 11 Jan 2014 21:59:28 +0100 Subject: [PATCH] Use the jsonfield as requirement --- README.rst | 3 + openslides/config/models.py | 4 +- openslides/motion/models.py | 2 +- openslides/utils/jsonfield/LICENSE | 20 ------- openslides/utils/jsonfield/README | 13 ----- openslides/utils/jsonfield/__init__.py | 3 - openslides/utils/jsonfield/fields.py | 79 -------------------------- openslides/utils/jsonfield/models.py | 1 - requirements_production.txt | 1 + tests/config/test_config.py | 2 +- 10 files changed, 8 insertions(+), 120 deletions(-) delete mode 100644 openslides/utils/jsonfield/LICENSE delete mode 100644 openslides/utils/jsonfield/README delete mode 100644 openslides/utils/jsonfield/__init__.py delete mode 100644 openslides/utils/jsonfield/fields.py delete mode 100644 openslides/utils/jsonfield/models.py diff --git a/README.rst b/README.rst index 66da95e06..efb00890f 100644 --- a/README.rst +++ b/README.rst @@ -256,6 +256,9 @@ OpenSlides uses the following projects or parts of them: * `Django haystack `_, License: BSD +* `django-jsonfield _, + License: MIT + * `pdf.js `_, License: Apache License v2.0 * `Pillow `_, License: Standard diff --git a/openslides/config/models.py b/openslides/config/models.py index 6fdc8f803..31f7b62cf 100644 --- a/openslides/config/models.py +++ b/openslides/config/models.py @@ -3,7 +3,7 @@ from django.db import models from django.utils.translation import ugettext_noop -from openslides.utils.jsonfield import JSONField +from jsonfield import JSONField class ConfigStore(models.Model): @@ -11,7 +11,7 @@ class ConfigStore(models.Model): A model class to store all config variables in the database. """ - key = models.CharField(max_length=255, primary_key=True) + key = models.CharField(max_length=255, unique=True, db_index=True) """A string, the key of the config variable.""" value = JSONField() diff --git a/openslides/motion/models.py b/openslides/motion/models.py index 64c9f0a2e..6516b3f12 100644 --- a/openslides/motion/models.py +++ b/openslides/motion/models.py @@ -12,7 +12,7 @@ from openslides.mediafile.models import Mediafile from openslides.poll.models import (BaseOption, BasePoll, BaseVote, CollectInvalid, CollectVotesCast) from openslides.projector.models import RelatedModelMixin, SlideMixin -from openslides.utils.jsonfield import JSONField +from jsonfield import JSONField from openslides.utils.person import PersonField from .exceptions import WorkflowError diff --git a/openslides/utils/jsonfield/LICENSE b/openslides/utils/jsonfield/LICENSE deleted file mode 100644 index 46bbd542a..000000000 --- a/openslides/utils/jsonfield/LICENSE +++ /dev/null @@ -1,20 +0,0 @@ -Copyright (c) 2012 Brad Jasper - -Permission is hereby granted, free of charge, to any person obtaining -a copy of this software and associated documentation files (the -"Software"), to deal in the Software without restriction, including -without limitation the rights to use, copy, modify, merge, publish, -distribute, sublicense, and/or sell copies of the Software, and to -permit persons to whom the Software is furnished to do so, subject to -the following conditions: - -The above copyright notice and this permission notice shall be -included in all copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND -NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE -LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION -OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION -WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/openslides/utils/jsonfield/README b/openslides/utils/jsonfield/README deleted file mode 100644 index 74113188c..000000000 --- a/openslides/utils/jsonfield/README +++ /dev/null @@ -1,13 +0,0 @@ -https://github.com/bradjasper/django-jsonfield - -django-jsonfield is a reusable django field that allows you to store validated JSON in your model. - -It silently takes care of serialization. To use, simple add the field to one of your models. - -=== - -from django.db import models -from jsonfield import JSONField - -class MyModel(models.Model): -json = JSONField() diff --git a/openslides/utils/jsonfield/__init__.py b/openslides/utils/jsonfield/__init__.py deleted file mode 100644 index 6dde401ce..000000000 --- a/openslides/utils/jsonfield/__init__.py +++ /dev/null @@ -1,3 +0,0 @@ -from fields import JSONField - -__all__ = ['JSONField'] diff --git a/openslides/utils/jsonfield/fields.py b/openslides/utils/jsonfield/fields.py deleted file mode 100644 index f3432af11..000000000 --- a/openslides/utils/jsonfield/fields.py +++ /dev/null @@ -1,79 +0,0 @@ -# -*- coding: utf-8 -*- - -import json - -from django.core.serializers.json import DjangoJSONEncoder -from django.db import models -from django.forms.fields import Field -from django.forms.util import ValidationError as FormValidationError -from django.utils.translation import ugettext as _ - - -class JSONFormField(Field): - def clean(self, value): - - if not value and not self.required: - return None - - value = super(JSONFormField, self).clean(value) - - if isinstance(value, basestring): - try: - json.loads(value) - except ValueError: - raise FormValidationError(_("Enter valid JSON")) - return value - - -class JSONField(models.TextField): - """JSONField is a generic textfield that serializes/unserializes JSON objects""" - - # Used so to_python() is called - __metaclass__ = models.SubfieldBase - - def __init__(self, *args, **kwargs): - self.dump_kwargs = kwargs.pop('dump_kwargs', {'cls': DjangoJSONEncoder}) - self.load_kwargs = kwargs.pop('load_kwargs', {}) - - super(JSONField, self).__init__(*args, **kwargs) - - def to_python(self, value): - """Convert string value to JSON""" - if isinstance(value, basestring): - try: - return json.loads(value, **self.load_kwargs) - except ValueError: - pass - return value - - def get_db_prep_value(self, value, connection, prepared=False): - """Convert JSON object to a string""" - - if isinstance(value, basestring): - return value - return json.dumps(value, **self.dump_kwargs) - - def value_to_string(self, obj): - value = self._get_val_from_obj(obj) - return self.get_prep_value(value) - - def value_from_object(self, obj): - return json.dumps(super(JSONField, self).value_from_object(obj)) - - def formfield(self, **kwargs): - - if "form_class" not in kwargs: - kwargs["form_class"] = JSONFormField - - field = super(JSONField, self).formfield(**kwargs) - - if not field.help_text: - field.help_text = "Enter valid JSON" - - return field - -try: - from south.modelsinspector import add_introspection_rules - add_introspection_rules([], ["^jsonfield\.fields\.JSONField"]) -except ImportError: - pass diff --git a/openslides/utils/jsonfield/models.py b/openslides/utils/jsonfield/models.py deleted file mode 100644 index e5faf1b16..000000000 --- a/openslides/utils/jsonfield/models.py +++ /dev/null @@ -1 +0,0 @@ -# Django needs this to see it as a project diff --git a/requirements_production.txt b/requirements_production.txt index 061138f38..7bda20411 100644 --- a/requirements_production.txt +++ b/requirements_production.txt @@ -4,6 +4,7 @@ beautifulsoup4>=4.3,<4.4 bleach>=1.2,<1.3 django-haystack>=2.1,<2.2 django-mptt>=0.6,<0.7 +jsonfield>=0.9,<0.10 pillow>=2.2,<2.3 reportlab>=2.7,<2.8 sockjs-tornado>=1.0,<1.1 diff --git a/tests/config/test_config.py b/tests/config/test_config.py index 7f3d1565c..672fd84b5 100644 --- a/tests/config/test_config.py +++ b/tests/config/test_config.py @@ -179,7 +179,7 @@ class ConfigFormTest(TestCase): self.assertEqual(config['string_var'], 'other_special_unique_string faiPaid4utie6eeL') self.assertFalse(config['bool_var']) self.assertEqual(config['integer_var'], 3) - self.assertEqual(config['choices_var'], 2) + self.assertEqual(config['choices_var'], '2') def test_post_config_form_error(self): response = self.client_manager.post(