Commit a938e15d authored by Florent Chehab's avatar Florent Chehab
Browse files

Merge branch 'clean_backend' into 'master'

Clean backend

See merge request chehabfl/outgoing_rex!35
parents 9e62f092 92f591e0
...@@ -7,13 +7,25 @@ class DictModeViewSet(viewsets.ModelViewSet): ...@@ -7,13 +7,25 @@ class DictModeViewSet(viewsets.ModelViewSet):
primary key. Instead of list. primary key. Instead of list.
""" """
BYPASS_DICT_MODE = False BYPASS_DICT_MODE = False
LIST_SHOULD_BE_DETAIL = False
def list(self, request, *args, **kwargs): def list(self, request, *args, **kwargs):
response = super(viewsets.ModelViewSet, self).list( # pylint: disable=E1003 response = super(viewsets.ModelViewSet, self).list( # pylint: disable=E1003
request, *args, **kwargs) # call the original 'list' request, *args, **kwargs) # call the original 'list'
if not self.BYPASS_DICT_MODE:
if self.LIST_SHOULD_BE_DETAIL:
if len(response.data) == 0:
response.data = dict()
elif len(response.data) == 1:
response.data = response.data[0]
else:
raise Exception(
"There should be no more than one element here check your queryset !")
elif not self.BYPASS_DICT_MODE:
pk_attr_name = self.serializer_class.Meta.model._meta.pk.name pk_attr_name = self.serializer_class.Meta.model._meta.pk.name
response.data = { response.data = {
d[pk_attr_name]: d for d in response.data d[pk_attr_name]: d for d in response.data
} }
return response return response
from .DictModeViewSet import DictModeViewSet # noqa: F401 from .DictModeViewSet import DictModeViewSet # noqa: F401
from .jSONField import JSONField # noqa: F401
from .mySerializerWithJSON import MySerializerWithJSON # noqa: F401 from .mySerializerWithJSON import MySerializerWithJSON # noqa: F401
from .jSONField import JSONField from backend.fields.jSONField import JSONField
from rest_framework import serializers from rest_framework import serializers
field_mapping = serializers.ModelSerializer.serializer_field_mapping field_mapping = serializers.ModelSerializer.serializer_field_mapping
......
from .jSONField import JSONField # noqa: F401
# taken from : https://github.com/devkral/django-simple-jsonfield # inspired by : https://github.com/devkral/django-simple-jsonfield
# MIT license
from django.core.exceptions import ValidationError from django.core.exceptions import ValidationError
from django.db import models from django.db import models
......
This diff is collapsed.
# Generated by Django 2.0.3 on 2018-09-09 12:55
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('backend', '0001_initial'),
]
operations = [
migrations.AddField(
model_name='campus',
name='importance_level',
field=models.CharField(choices=[('-', 'normal'), ('+', 'important'), ('++', 'IMPORTANT')], default='-', max_length=2),
),
migrations.AddField(
model_name='countrydri',
name='importance_level',
field=models.CharField(choices=[('-', 'normal'), ('+', 'important'), ('++', 'IMPORTANT')], default='-', max_length=2),
),
migrations.AddField(
model_name='countryscholarship',
name='importance_level',
field=models.CharField(choices=[('-', 'normal'), ('+', 'important'), ('++', 'IMPORTANT')], default='-', max_length=2),
),
migrations.AddField(
model_name='universitydri',
name='importance_level',
field=models.CharField(choices=[('-', 'normal'), ('+', 'important'), ('++', 'IMPORTANT')], default='-', max_length=2),
),
migrations.AddField(
model_name='universityinfo',
name='importance_level',
field=models.CharField(choices=[('-', 'normal'), ('+', 'important'), ('++', 'IMPORTANT')], default='-', max_length=2),
),
migrations.AddField(
model_name='universityscholarship',
name='importance_level',
field=models.CharField(choices=[('-', 'normal'), ('+', 'important'), ('++', 'IMPORTANT')], default='-', max_length=2),
),
migrations.AddField(
model_name='universitysemestersdates',
name='importance_level',
field=models.CharField(choices=[('-', 'normal'), ('+', 'important'), ('++', 'IMPORTANT')], default='-', max_length=2),
),
]
# Generated by Django 2.0.3 on 2018-09-09 13:47
import backend.models.tools.jSONField
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('backend', '0002_auto_20180909_1455'),
]
operations = [
migrations.AddField(
model_name='campustaggeditem',
name='comment',
field=models.TextField(blank=True),
),
migrations.AddField(
model_name='campustaggeditem',
name='importance_level',
field=models.CharField(choices=[('-', 'normal'), ('+', 'important'), ('++', 'IMPORTANT')], default='-', max_length=2),
),
migrations.AddField(
model_name='campustaggeditem',
name='usefull_links',
field=backend.models.tools.jSONField.JSONField(default=list),
),
migrations.AddField(
model_name='citytaggeditem',
name='comment',
field=models.TextField(blank=True),
),
migrations.AddField(
model_name='citytaggeditem',
name='importance_level',
field=models.CharField(choices=[('-', 'normal'), ('+', 'important'), ('++', 'IMPORTANT')], default='-', max_length=2),
),
migrations.AddField(
model_name='citytaggeditem',
name='usefull_links',
field=backend.models.tools.jSONField.JSONField(default=list),
),
migrations.AddField(
model_name='countrytaggeditem',
name='comment',
field=models.TextField(blank=True),
),
migrations.AddField(
model_name='countrytaggeditem',
name='importance_level',
field=models.CharField(choices=[('-', 'normal'), ('+', 'important'), ('++', 'IMPORTANT')], default='-', max_length=2),
),
migrations.AddField(
model_name='countrytaggeditem',
name='usefull_links',
field=backend.models.tools.jSONField.JSONField(default=list),
),
migrations.AddField(
model_name='universitytaggeditem',
name='comment',
field=models.TextField(blank=True),
),
migrations.AddField(
model_name='universitytaggeditem',
name='importance_level',
field=models.CharField(choices=[('-', 'normal'), ('+', 'important'), ('++', 'IMPORTANT')], default='-', max_length=2),
),
migrations.AddField(
model_name='universitytaggeditem',
name='usefull_links',
field=backend.models.tools.jSONField.JSONField(default=list),
),
]
from django.db import models from django.db import models
from backend.models.my_model import MyModelVersionned, MyModelVersionnedSerializer, MyModelVersionnedViewSet from backend.models.abstract.my_model import MyModelVersionned, MyModelVersionnedSerializer, MyModelVersionnedViewSet
from backend.models.tools import JSONField from backend.fields import JSONField
from backend.validators.tag import validate_content_against_config from backend.validators.tag import validate_content_against_config
from backend.validators.tag.tags_config import USEFULL_LINKS_CONFIG from backend.validators.tag.tags_config import USEFULL_LINKS_CONFIG
......
...@@ -7,7 +7,7 @@ from .myModel import MyModel ...@@ -7,7 +7,7 @@ from .myModel import MyModel
from .pendingModeration import PendingModerationSerializer from .pendingModeration import PendingModerationSerializer
from backend.utils import get_user_level from backend.utils import get_user_level
from backend.permissions import is_moderation_required from backend.permissions import is_moderation_required
from backend.models.tools import MySerializerWithJSON from backend.custom import MySerializerWithJSON
CLEANED_MY_MODEL_DATA = { CLEANED_MY_MODEL_DATA = {
'moderated_by': None, 'moderated_by': None,
...@@ -41,9 +41,12 @@ class MyModelSerializer(MySerializerWithJSON): ...@@ -41,9 +41,12 @@ class MyModelSerializer(MySerializerWithJSON):
return self.Meta.model.model_config return self.Meta.model.model_config
def get_pending_moderation(self, obj): def get_pending_moderation(self, obj):
if self.context['view'].action != 'list': pm = PendingModerationSerializer(
return PendingModerationSerializer(obj.pending_moderation, many=True, read_only=True, context=self.context).data obj.pending_moderation, many=True, read_only=True, context=self.context).data
return None if self.context['view'].action == 'list':
return len(pm)
else:
return pm
def get_id(self, obj): def get_id(self, obj):
return obj.pk return obj.pk
......
from backend.models.my_model import MyModel, MyModelSerializer, MyModelViewSet from backend.models.abstract.my_model import MyModel, MyModelSerializer, MyModelViewSet
from backend.signals import new_revision_saved from backend.signals import new_revision_saved
from rest_framework import serializers, mixins, viewsets from rest_framework import serializers, mixins, viewsets
import reversion import reversion
...@@ -7,7 +7,7 @@ from django.contrib.contenttypes.models import ContentType ...@@ -7,7 +7,7 @@ from django.contrib.contenttypes.models import ContentType
from django.core.serializers.base import DeserializationError from django.core.serializers.base import DeserializationError
from django.core import serializers as djangoSerializers from django.core import serializers as djangoSerializers
from backend.utils import get_viewset_permissions from backend.utils import get_viewset_permissions
from backend.models.tools import MySerializerWithJSON from backend.custom import MySerializerWithJSON
class MyModelVersionned(MyModel): class MyModelVersionned(MyModel):
......
from .myModelSerializer import MyModelSerializer from .myModelSerializer import MyModelSerializer
from backend.permissions import DEFAULT_VIEWSET_PERMISSIONS from backend.permissions import DEFAULT_VIEWSET_PERMISSIONS
from backend.models.tools import DictModeViewSet from backend.custom import DictModeViewSet
class MyModelViewSet(DictModeViewSet): class MyModelViewSet(DictModeViewSet):
...@@ -12,7 +12,7 @@ class MyModelViewSet(DictModeViewSet): ...@@ -12,7 +12,7 @@ class MyModelViewSet(DictModeViewSet):
Extended default rest framework behavior Extended default rest framework behavior
to prefetch some table and enhance performances to prefetch some table and enhance performances
""" """
self.mymodel_queryset = self.queryset.prefetch_related( self.my_model_queryset = self.queryset.prefetch_related(
'moderated_by', 'updated_by', 'pending_moderation') 'moderated_by', 'updated_by', 'pending_moderation')
return self.extend_queryset() return self.extend_queryset()
...@@ -20,4 +20,4 @@ class MyModelViewSet(DictModeViewSet): ...@@ -20,4 +20,4 @@ class MyModelViewSet(DictModeViewSet):
""" """
Function to extend get_queryset when subclassing Function to extend get_queryset when subclassing
""" """
return self.mymodel_queryset return self.my_model_queryset
...@@ -2,10 +2,10 @@ from django.db import models ...@@ -2,10 +2,10 @@ from django.db import models
from rest_framework import serializers, viewsets from rest_framework import serializers, viewsets
from django.contrib.contenttypes.models import ContentType from django.contrib.contenttypes.models import ContentType
from django.contrib.contenttypes.fields import GenericForeignKey from django.contrib.contenttypes.fields import GenericForeignKey
from backend.models.tools import JSONField from backend.fields import JSONField
from django.contrib.auth.models import User from django.contrib.auth.models import User
from backend.utils import get_viewset_permissions, get_model_config from backend.utils import get_viewset_permissions, get_model_config
from backend.models.tools import MySerializerWithJSON from backend.custom import MySerializerWithJSON
class PendingModeration(models.Model): class PendingModeration(models.Model):
......
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