Commit 10ef6018 authored by Florent Chehab's avatar Florent Chehab

Moved to knew JSONField

parent 61493e5a
Pipeline #26922 passed with stages
in 2 minutes and 19 seconds
This diff is collapsed.
# Generated by Django 2.0.3 on 2018-09-02 15:16
import backend.models.my_model.myModel
import django.core.validators
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('backend', '0001_initial'),
]
operations = [
migrations.AlterField(
model_name='campus',
name='obj_moderation_level',
field=models.SmallIntegerField(default=0, validators=[django.core.validators.MinValueValidator(0), backend.models.my_model.myModel.validate_obj_model_lv]),
),
migrations.AlterField(
model_name='campustaggeditem',
name='obj_moderation_level',
field=models.SmallIntegerField(default=0, validators=[django.core.validators.MinValueValidator(0), backend.models.my_model.myModel.validate_obj_model_lv]),
),
migrations.AlterField(
model_name='city',
name='obj_moderation_level',
field=models.SmallIntegerField(default=0, validators=[django.core.validators.MinValueValidator(0), backend.models.my_model.myModel.validate_obj_model_lv]),
),
migrations.AlterField(
model_name='citytaggeditem',
name='obj_moderation_level',
field=models.SmallIntegerField(default=0, validators=[django.core.validators.MinValueValidator(0), backend.models.my_model.myModel.validate_obj_model_lv]),
),
migrations.AlterField(
model_name='country',
name='obj_moderation_level',
field=models.SmallIntegerField(default=0, validators=[django.core.validators.MinValueValidator(0), backend.models.my_model.myModel.validate_obj_model_lv]),
),
migrations.AlterField(
model_name='countrydri',
name='obj_moderation_level',
field=models.SmallIntegerField(default=0, validators=[django.core.validators.MinValueValidator(0), backend.models.my_model.myModel.validate_obj_model_lv]),
),
migrations.AlterField(
model_name='countryscholarship',
name='obj_moderation_level',
field=models.SmallIntegerField(default=0, validators=[django.core.validators.MinValueValidator(0), backend.models.my_model.myModel.validate_obj_model_lv]),
),
migrations.AlterField(
model_name='countrytaggeditem',
name='obj_moderation_level',
field=models.SmallIntegerField(default=0, validators=[django.core.validators.MinValueValidator(0), backend.models.my_model.myModel.validate_obj_model_lv]),
),
migrations.AlterField(
model_name='currency',
name='obj_moderation_level',
field=models.SmallIntegerField(default=0, validators=[django.core.validators.MinValueValidator(0), backend.models.my_model.myModel.validate_obj_model_lv]),
),
migrations.AlterField(
model_name='department',
name='obj_moderation_level',
field=models.SmallIntegerField(default=0, validators=[django.core.validators.MinValueValidator(0), backend.models.my_model.myModel.validate_obj_model_lv]),
),
migrations.AlterField(
model_name='fortestingmoderation',
name='obj_moderation_level',
field=models.SmallIntegerField(default=0, validators=[django.core.validators.MinValueValidator(0), backend.models.my_model.myModel.validate_obj_model_lv]),
),
migrations.AlterField(
model_name='fortestingversioning',
name='obj_moderation_level',
field=models.SmallIntegerField(default=0, validators=[django.core.validators.MinValueValidator(0), backend.models.my_model.myModel.validate_obj_model_lv]),
),
migrations.AlterField(
model_name='offer',
name='obj_moderation_level',
field=models.SmallIntegerField(default=0, validators=[django.core.validators.MinValueValidator(0), backend.models.my_model.myModel.validate_obj_model_lv]),
),
migrations.AlterField(
model_name='previousdeparture',
name='obj_moderation_level',
field=models.SmallIntegerField(default=0, validators=[django.core.validators.MinValueValidator(0), backend.models.my_model.myModel.validate_obj_model_lv]),
),
migrations.AlterField(
model_name='previousdeparturefeedback',
name='obj_moderation_level',
field=models.SmallIntegerField(default=0, validators=[django.core.validators.MinValueValidator(0), backend.models.my_model.myModel.validate_obj_model_lv]),
),
migrations.AlterField(
model_name='recommendation',
name='obj_moderation_level',
field=models.SmallIntegerField(default=0, validators=[django.core.validators.MinValueValidator(0), backend.models.my_model.myModel.validate_obj_model_lv]),
),
migrations.AlterField(
model_name='recommendationlist',
name='obj_moderation_level',
field=models.SmallIntegerField(default=0, validators=[django.core.validators.MinValueValidator(0), backend.models.my_model.myModel.validate_obj_model_lv]),
),
migrations.AlterField(
model_name='semester',
name='obj_moderation_level',
field=models.SmallIntegerField(default=0, validators=[django.core.validators.MinValueValidator(0), backend.models.my_model.myModel.validate_obj_model_lv]),
),
migrations.AlterField(
model_name='specialty',
name='obj_moderation_level',
field=models.SmallIntegerField(default=0, validators=[django.core.validators.MinValueValidator(0), backend.models.my_model.myModel.validate_obj_model_lv]),
),
migrations.AlterField(
model_name='tag',
name='obj_moderation_level',
field=models.SmallIntegerField(default=0, validators=[django.core.validators.MinValueValidator(0), backend.models.my_model.myModel.validate_obj_model_lv]),
),
migrations.AlterField(
model_name='university',
name='obj_moderation_level',
field=models.SmallIntegerField(default=0, validators=[django.core.validators.MinValueValidator(0), backend.models.my_model.myModel.validate_obj_model_lv]),
),
migrations.AlterField(
model_name='universitydri',
name='obj_moderation_level',
field=models.SmallIntegerField(default=0, validators=[django.core.validators.MinValueValidator(0), backend.models.my_model.myModel.validate_obj_model_lv]),
),
migrations.AlterField(
model_name='universityinfo',
name='obj_moderation_level',
field=models.SmallIntegerField(default=0, validators=[django.core.validators.MinValueValidator(0), backend.models.my_model.myModel.validate_obj_model_lv]),
),
migrations.AlterField(
model_name='universityscholarship',
name='obj_moderation_level',
field=models.SmallIntegerField(default=0, validators=[django.core.validators.MinValueValidator(0), backend.models.my_model.myModel.validate_obj_model_lv]),
),
migrations.AlterField(
model_name='universitysemestersdates',
name='obj_moderation_level',
field=models.SmallIntegerField(default=0, validators=[django.core.validators.MinValueValidator(0), backend.models.my_model.myModel.validate_obj_model_lv]),
),
migrations.AlterField(
model_name='universitytaggeditem',
name='obj_moderation_level',
field=models.SmallIntegerField(default=0, validators=[django.core.validators.MinValueValidator(0), backend.models.my_model.myModel.validate_obj_model_lv]),
),
migrations.AlterField(
model_name='userdata',
name='obj_moderation_level',
field=models.SmallIntegerField(default=0, validators=[django.core.validators.MinValueValidator(0), backend.models.my_model.myModel.validate_obj_model_lv]),
),
]
# Generated by Django 2.0.3 on 2018-09-03 07:53
import backend.models.university.university
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('backend', '0002_auto_20180902_1716'),
]
operations = [
migrations.AddField(
model_name='university',
name='utc_id',
field=models.IntegerField(default=1, unique=True),
preserve_default=False,
),
migrations.AlterField(
model_name='university',
name='logo',
field=models.URLField(blank=True, null=True, validators=[backend.models.university.university.validate_extension_django]),
),
migrations.AlterField(
model_name='university',
name='website',
field=models.URLField(blank=True, null=True),
),
]
from django.db import models
from backend.models.my_model import MyModelVersionned, MyModelVersionnedSerializer, MyModelVersionnedViewSet
from django.contrib.postgres.fields import JSONField
from backend.models.tools import JSONField
from backend.models.tag.tagged_item_validation import validate_content_against_config
from backend.models.tag.tags_config import USEFULL_LINKS_CONFIG
......@@ -8,7 +8,7 @@ from backend.models.tag.tags_config import USEFULL_LINKS_CONFIG
class BasicModule(MyModelVersionned):
comment = models.TextField(blank=True)
usefull_links = JSONField(blank=True, default=list)
usefull_links = JSONField(default=list)
class Meta:
abstract = True
......
......@@ -7,6 +7,7 @@ from .myModel import MyModel
from .pendingModeration import PendingModerationSerializer
from backend.utils import get_user_level
from backend.permissions import is_moderation_required
from backend.models.tools import MySerializerWithJSON
CLEANED_MY_MODEL_DATA = {
'moderated_by': None,
......@@ -22,7 +23,7 @@ def override_data(old_data, new_data):
return old_data
class MyModelSerializer(serializers.ModelSerializer):
class MyModelSerializer(MySerializerWithJSON):
moderated_on = serializers.DateTimeField(
format="%Y-%m-%d %H:%M:%S", read_only=True)
updated_on = serializers.DateTimeField(
......
......@@ -7,6 +7,7 @@ from django.contrib.contenttypes.models import ContentType
from django.core.serializers.base import DeserializationError
from django.core import serializers as djangoSerializers
from backend.utils import get_viewset_permissions
from backend.models.tools import MySerializerWithJSON
class MyModelVersionned(MyModel):
......@@ -51,7 +52,7 @@ class MyModelVersionnedViewSet(MyModelViewSet):
serializer_class = MyModelVersionnedSerializer
class VersionSerializer(serializers.ModelSerializer):
class VersionSerializer(MySerializerWithJSON):
data = serializers.SerializerMethodField()
def get_data(self, obj):
......
......@@ -2,9 +2,10 @@ from django.db import models
from rest_framework import serializers, viewsets
from django.contrib.contenttypes.models import ContentType
from django.contrib.contenttypes.fields import GenericForeignKey
from django.contrib.postgres.fields import JSONField
from backend.models.tools import JSONField
from django.contrib.auth.models import User
from backend.utils import get_viewset_permissions, get_model_config
from backend.models.tools import MySerializerWithJSON
class PendingModeration(models.Model):
......@@ -23,7 +24,7 @@ class PendingModeration(models.Model):
unique_together = ('content_type', 'object_id')
class PendingModerationSerializer(serializers.ModelSerializer):
class PendingModerationSerializer(MySerializerWithJSON):
content_type = serializers.CharField(read_only=True)
object_id = serializers.CharField(read_only=True)
......
from django.db import models
from backend.models.my_model import MyModel, MyModelSerializer, MyModelViewSet
from django.contrib.postgres.fields import JSONField
from backend.models.tools import JSONField
from backend.utils import get_viewset_permissions, get_model_config
......@@ -11,7 +11,7 @@ class Tag(MyModel):
model_config = get_model_config("Tag")
name = models.CharField(max_length=100, unique=True)
config = JSONField(blank=True, default=dict)
config = JSONField(default=dict)
class TagSerializer(MyModelSerializer):
......
......@@ -2,7 +2,7 @@ from django.db import models
from backend.models.my_model import MyModelVersionned, MyModelVersionnedSerializer, MyModelVersionnedViewSet
from .tag import Tag
from .tagged_item_validation import tagged_item_validation
from django.contrib.postgres.fields import JSONField
from backend.models.tools import JSONField
class TaggedItem(MyModelVersionned):
......
from .DictModeViewSet import DictModeViewSet # noqa: F401
from .jSONField import JSONField # noqa: F401
from .mySerializerWithJSON import MySerializerWithJSON # noqa: F401
# taken from : https://github.com/devkral/django-simple-jsonfield
from django.core.exceptions import ValidationError
from django.db import models
import json
class JSONField(models.TextField):
__qualname__ = "JSONField"
__name__ = "JSONField"
""" JSON field implementation on top of django textfield """
def to_dict(self, value):
""" convert json string to python dictionary """
try:
return json.loads(value)
except json.decoder.JSONDecodeError as e:
raise ValidationError(
message=str(e),
code="json_error"
)
def to_json(self, value):
""" convert python dictionary to json string """
return json.dumps(value)
def from_db_value(self, value, expression, connection):
""" convert string from db to python dictionary """
if value is None:
return value
return self.to_dict(value)
def to_python(self, value):
""" convert model input value to python dictionary """
if isinstance(value, (dict, list)):
return value
if value is None:
return value
return self.to_dict(value)
def get_prep_value(self, value):
"""convert python dictionary to string before writing to db"""
return self.to_json(value)
from .jSONField import JSONField
from rest_framework import serializers
field_mapping = serializers.ModelSerializer.serializer_field_mapping
field_mapping[JSONField] = serializers.JSONField
class MySerializerWithJSON(serializers.ModelSerializer):
"""
Simple class to add support for custom JSONField support
"""
serializer_field_mapping = field_mapping
......@@ -3,7 +3,7 @@ from backend.models.other_core.specialty import Specialty
from backend.models.other_core.semester import Semester
from backend.models.university import University
from django.contrib.auth.models import User
from django.contrib.postgres.fields import JSONField
from backend.models.tools import JSONField
from backend.models.my_model import MyModel, MyModelSerializer, MyModelViewSet
from backend.utils import get_model_config, get_viewset_permissions
......
......@@ -3,7 +3,7 @@ from django.core.validators import MaxValueValidator
from backend.models.user import UserRestrictedModule, UserRestrictedModuleSerializer, UserRestrictedModuleViewSet
from backend.models.user import PreviousDeparture
from django.contrib.postgres.fields import JSONField
from backend.models.tools import JSONField
from backend.utils import get_model_config, get_viewset_permissions
......
from django.db import models
from rest_framework import serializers
from backend.models.university import University
from django.contrib.postgres.fields import JSONField
from backend.models.tools import JSONField
from backend.models.my_model import MyModel, MyModelSerializer, MyModelViewSet
from django.contrib.auth.models import User
from backend.utils import get_viewset_permissions, get_model_config
......
......@@ -222,7 +222,6 @@ class ModerationTestCase(WithUserTestCase):
data = {
"comment": "",
"usefull_links": [],
"custom_content": {},
"university": [c.pk]
}
api_end_point = "/api/" + \
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment