Commit a938e15d authored by Florent Chehab's avatar Florent Chehab

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):
primary key. Instead of list.
"""
BYPASS_DICT_MODE = False
LIST_SHOULD_BE_DETAIL = False
def list(self, request, *args, **kwargs):
response = super(viewsets.ModelViewSet, self).list( # pylint: disable=E1003
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
response.data = {
d[pk_attr_name]: d for d in response.data
}
return response
from .DictModeViewSet import DictModeViewSet # noqa: F401
from .jSONField import JSONField # noqa: F401
from .mySerializerWithJSON import MySerializerWithJSON # noqa: F401
from .jSONField import JSONField
from backend.fields.jSONField import JSONField
from rest_framework import serializers
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.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 backend.models.my_model import MyModelVersionned, MyModelVersionnedSerializer, MyModelVersionnedViewSet
from backend.models.tools import JSONField
from backend.models.abstract.my_model import MyModelVersionned, MyModelVersionnedSerializer, MyModelVersionnedViewSet
from backend.fields import JSONField
from backend.validators.tag import validate_content_against_config
from backend.validators.tag.tags_config import USEFULL_LINKS_CONFIG
......
......@@ -7,7 +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
from backend.custom import MySerializerWithJSON
CLEANED_MY_MODEL_DATA = {
'moderated_by': None,
......@@ -41,9 +41,12 @@ class MyModelSerializer(MySerializerWithJSON):
return self.Meta.model.model_config
def get_pending_moderation(self, obj):
if self.context['view'].action != 'list':
return PendingModerationSerializer(obj.pending_moderation, many=True, read_only=True, context=self.context).data
return None
pm = PendingModerationSerializer(
obj.pending_moderation, many=True, read_only=True, context=self.context).data
if self.context['view'].action == 'list':
return len(pm)
else:
return pm
def get_id(self, obj):
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 rest_framework import serializers, mixins, viewsets
import reversion
......@@ -7,7 +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
from backend.custom import MySerializerWithJSON
class MyModelVersionned(MyModel):
......
from .myModelSerializer import MyModelSerializer
from backend.permissions import DEFAULT_VIEWSET_PERMISSIONS
from backend.models.tools import DictModeViewSet
from backend.custom import DictModeViewSet
class MyModelViewSet(DictModeViewSet):
......@@ -12,7 +12,7 @@ class MyModelViewSet(DictModeViewSet):
Extended default rest framework behavior
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')
return self.extend_queryset()
......@@ -20,4 +20,4 @@ class MyModelViewSet(DictModeViewSet):
"""
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
from rest_framework import serializers, viewsets
from django.contrib.contenttypes.models import ContentType
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 backend.utils import get_viewset_permissions, get_model_config
from backend.models.tools import MySerializerWithJSON
from backend.custom import MySerializerWithJSON
class PendingModeration(models.Model):
......
from django.db import models
from backend.models.basic_module import BasicModule, BasicModuleSerializer, BasicModuleViewSet
from backend.models.abstract.basic_module import BasicModule, BasicModuleSerializer, BasicModuleViewSet
from backend.models.currency import Currency
from rest_framework import serializers
from django.core.validators import MinValueValidator
......
from .taggedItem import TaggedItem, TaggedItemSerializer, TaggedItemViewSet # noqa: F401
from django.db import models
from backend.models.basic_module import BasicModule, BasicModuleSerializer, BasicModuleViewSet
from .tag import Tag
from backend.models.abstract.basic_module import BasicModule, BasicModuleSerializer, BasicModuleViewSet
from backend.models.tag import Tag
from backend.validators.tag import tagged_item_validation
from backend.models.tools import JSONField
from backend.fields import JSONField
class TaggedItem(BasicModule):
......@@ -22,4 +22,4 @@ class TaggedItemSerializer(BasicModuleSerializer):
class TaggedItemViewSet(BasicModuleViewSet):
def extend_queryset(self):
return self.mymodel_queryset.prefetch_related('tag')
return self.my_model_queryset.prefetch_related('tag')
from .campus import Campus, CampusViewSet, CampusSerializer, MainCampusViewSet # noqa: F401
from .campusTaggedItem import CampusTaggedItem, CampusTaggedItemSerializer, CampusTaggedItemViewSet # noqa: F401
from .campusModulesAPI import CampusModulesViewSet # noqa: F401
from django.db import models
from backend.models.basic_module import BasicModule, BasicModuleSerializer, BasicModuleViewSet
from backend.models.abstract.basic_module import BasicModule, BasicModuleSerializer, BasicModuleViewSet
from backend.models.city import City
from backend.models.university import University
from django.core.validators import MinValueValidator, MaxValueValidator
......@@ -14,7 +14,7 @@ class Campus(BasicModule):
comment = models.TextField(null=True, blank=True)
city = models.ForeignKey(City, on_delete=models.PROTECT, null=False)
university = models.ForeignKey(
University, on_delete=models.PROTECT, null=False, related_name="univ_campus")
University, on_delete=models.PROTECT, null=False, related_name="university_campuses")
lat = models.DecimalField(
max_digits=10,
......
from backend.models.campus import Campus
from backend.models.campus import CampusTaggedItemSerializer
from backend.models.abstract.my_model import MyModelSerializer, MyModelViewSet
from backend.utils import get_viewset_permissions
class CampusModulesSerializer(MyModelSerializer):
campus_tagged_items = CampusTaggedItemSerializer(
many=True, read_only=True)
class Meta:
model = Campus
fields = '__all__'
class CampusModulesViewSet(MyModelViewSet):
permission_classes = get_viewset_permissions("CampusModulesViewSet")
serializer_class = CampusModulesSerializer
queryset = Campus.objects.all()
LIST_SHOULD_BE_DETAIL = True
def extend_queryset(self):
campus_id = self.kwargs['campus_id']
return self.my_model_queryset.filter(pk=campus_id).prefetch_related(
'campus_tagged_items'
) # pylint: disable=E1101
from django.db import models
from backend.models.campus import Campus
from backend.models.tag import TaggedItem, TaggedItemSerializer, TaggedItemViewSet
from backend.models.abstract.taggedItem import TaggedItem, TaggedItemSerializer, TaggedItemViewSet
from backend.utils import get_model_config, get_viewset_permissions
class CampusTaggedItem(TaggedItem):
model_config = get_model_config("CampusTaggedItem")
campus = models.OneToOneField(
Campus, on_delete=models.PROTECT, related_name='city_items', primary_key=True)
campus = models.ForeignKey(
Campus, on_delete=models.PROTECT, related_name='campus_tagged_items')
@classmethod
def get_serializer(cls):
......
from .city import City, CitySerializer, CityViewSet # noqa: F401
from .cityTaggedItem import CityTaggedItem, CityTaggedItemSerializer, CityTaggedItemViewSet # noqa: F401
from .cityModulesAPI import CityModulesViewSet # noqa: F401
from django.db import models
from backend.models.country import Country
from backend.models.my_model import MyModel, MyModelSerializer, MyModelVersionnedViewSet
from backend.models.abstract.my_model import MyModel, MyModelSerializer, MyModelVersionnedViewSet
from backend.utils import get_model_config, get_viewset_permissions
......
from backend.models.city import City
from backend.models.city import CityTaggedItemSerializer
from backend.models.abstract.my_model import MyModelSerializer, MyModelViewSet
from backend.utils import get_viewset_permissions
class CityModulesSerializer(MyModelSerializer):
city_tagged_items = CityTaggedItemSerializer(
many=True, read_only=True)
class Meta:
model = City
fields = '__all__'
class CityModulesViewSet(MyModelViewSet):
permission_classes = get_viewset_permissions("CityModulesViewSet")
serializer_class = CityModulesSerializer
queryset = City.objects.all()
LIST_SHOULD_BE_DETAIL = True
def extend_queryset(self):
city_id = self.kwargs['city_id']
return self.my_model_queryset.filter(pk=city_id).prefetch_related(
'city_tagged_items'
) # pylint: disable=E1101
from django.db import models
from backend.models.tag import TaggedItem, TaggedItemSerializer, TaggedItemViewSet
from backend.models.abstract.taggedItem import TaggedItem, TaggedItemSerializer, TaggedItemViewSet
from backend.models.city import City
from backend.utils import get_model_config, get_viewset_permissions
......@@ -7,8 +7,8 @@ from backend.utils import get_model_config, get_viewset_permissions
class CityTaggedItem(TaggedItem):
model_config = get_model_config("CityTaggedItem")
city = models.OneToOneField(
City, on_delete=models.PROTECT, related_name='city_items', primary_key=True)
city = models.ForeignKey(
City, on_delete=models.PROTECT, related_name='city_tagged_items')
@classmethod
def get_serializer(cls):
......
from .country import Country, CountrySerializer, CountryViewSet # noqa: F401
from .countryScholarship import CountryScholarship, CountryScholarshipViewSet # noqa: F401
from .countryDri import CountryDri, CountryDriViewSet # noqa: F401
from .countryScholarship import CountryScholarship, CountryScholarshipSerializer, CountryScholarshipViewSet # noqa: F401
from .countryDri import CountryDri, CountryDriSerializer, CountryDriViewSet # noqa: F401
from .countryTaggedItem import CountryTaggedItem, CountryTaggedItemSerializer, CountryTaggedItemViewSet # noqa: F401
from .countryModulesAPI import CountryModulesViewSet # noqa: F401
from django.db import models
from backend.models.my_model import MyModel, MyModelSerializer, MyModelVersionnedViewSet
from backend.models.abstract.my_model import MyModel, MyModelSerializer, MyModelVersionnedViewSet
from backend.utils import get_model_config, get_viewset_permissions
# Data model based on : https://unstats.un.org/unsd/methodology/m49/overview/
......
from django.db import models
from backend.models.country import Country
from backend.models.basic_module import BasicModule, BasicModuleSerializer, BasicModuleViewSet
from backend.models.abstract.basic_module import BasicModule, BasicModuleSerializer, BasicModuleViewSet
from backend.utils import get_model_config, get_viewset_permissions
......
from backend.models.country import Country
from backend.models.country import CountryDriSerializer
from backend.models.country import CountryScholarshipSerializer
from backend.models.country import CountryTaggedItemSerializer
from backend.models.abstract.my_model import MyModelSerializer, MyModelViewSet
from backend.utils import get_viewset_permissions
class CountryModulesSerializer(MyModelSerializer):
country_scholarships = CountryScholarshipSerializer(
many=True, read_only=True)
country_tagged_items = CountryTaggedItemSerializer(
many=True, read_only=True)
country_dri = CountryDriSerializer(many=True, read_only=True)
class Meta:
model = Country
fields = '__all__'
class CountryModulesViewSet(MyModelViewSet):
permission_classes = get_viewset_permissions("CountryModulesViewSet")
serializer_class = CountryModulesSerializer
queryset = Country.objects.all()
LIST_SHOULD_BE_DETAIL = True
def extend_queryset(self):
country_id = self.kwargs['country_id']
return self.my_model_queryset.filter(pk=country_id).prefetch_related(
'country_dri',
'country_scholarships',
'country_tagged_items'
) # pylint: disable=E1101
from django.db import models
from backend.models.country import Country
from backend.models.scholarship import Scholarship, ScholarshipSerializer, ScholarshipViewSet
from backend.models.abstract.scholarship import Scholarship, ScholarshipSerializer, ScholarshipViewSet
from backend.utils import get_model_config, get_viewset_permissions
class CountryScholarship(Scholarship):
model_config = get_model_config("CountryScholarship")
country = models.ManyToManyField(
Country, related_name="country_scholarhip")
Country, related_name="country_scholarships")
@classmethod
def get_serializer(cls):
......
from django.db import models
from backend.models.tag import TaggedItem, TaggedItemSerializer, TaggedItemViewSet
from backend.models.abstract.taggedItem import TaggedItem, TaggedItemSerializer, TaggedItemViewSet
from backend.models.country import Country
from backend.utils import get_model_config, get_viewset_permissions
class CountryTaggedItem(TaggedItem):
model_config = get_model_config("CountryTaggedItem")
country = models.OneToOneField(
Country, on_delete=models.PROTECT, related_name='country_items', primary_key=True)
country = models.ForeignKey(
Country, on_delete=models.PROTECT, related_name='country_tagged_items')
@classmethod
def get_serializer(cls):
......
from django.db import models
from django.core.validators import MinValueValidator
from backend.models.my_model import MyModel, MyModelSerializer, MyModelViewSet
from backend.models.abstract.my_model import MyModel, MyModelSerializer, MyModelViewSet
from backend.utils import get_model_config, get_viewset_permissions
......
from django.db import models
from backend.models.my_model import MyModel, MyModelSerializer, MyModelViewSet
from backend.models.abstract.my_model import MyModel, MyModelSerializer, MyModelViewSet
from backend.utils import get_model_config, get_viewset_permissions
......
from django.db import models
from backend.models.university import University
from backend.models.other_core import Semester, Specialty
from backend.models.my_model import MyModel, MyModelSerializer, MyModelViewSet
from backend.models.abstract.my_model import MyModel, MyModelSerializer, MyModelViewSet
from backend.utils import get_model_config, get_viewset_permissions
......
from django.db import models
from backend.models.my_model import MyModel, MyModelSerializer, MyModelViewSet
from backend.models.abstract.my_model import MyModel, MyModelSerializer, MyModelViewSet
from backend.utils import get_model_config, get_viewset_permissions
......
from django.db import models
from backend.models.other_core import Department
from backend.models.my_model import MyModel, MyModelSerializer, MyModelViewSet
from backend.models.abstract.my_model import MyModel, MyModelSerializer, MyModelViewSet
from backend.utils import get_model_config, get_viewset_permissions
......
from .tag import Tag, TagSerializer, TagViewSet # noqa: F401
from .taggedItem import TaggedItem, TaggedItemSerializer, TaggedItemViewSet # noqa: F401
from django.db import models
from backend.models.my_model import MyModel, MyModelSerializer, MyModelViewSet
from backend.models.tools import JSONField
from backend.models.abstract.my_model import MyModel, MyModelSerializer, MyModelViewSet
from backend.fields import JSONField
from backend.utils import get_viewset_permissions, get_model_config
......
......@@ -4,5 +4,5 @@ from .universityScholarship import UniversityScholarship, UniversityScholarshipS
from .universityDri import UniversityDri, UniversityDriSerializer, UniversityDriViewSet # noqa: F401
from .universityInfo import UniversityInfo, UniversityInfoSerializer, UniversityInfoViewSet # noqa: F401
from .universitySemestersDates import UniversitySemestersDates, UniversitySemestersDatesSerializer, UniversitySemestersDatesViewSet # noqa: F401
from .universityModulesAPI import UniversityModulesViewSet # noqa: F401
from .universityTaggedItem import UniversityTaggedItem, UniversityTaggedItemSerializer, UniversityTaggedItemViewSet # noqa: F401
from .universityModulesAPI import UniversityModulesViewSet # noqa: F401
from django.db import models
from backend.models.my_model import MyModel
from backend.models.abstract.my_model import MyModel
from backend.utils import get_model_config
from django.core.exceptions import ValidationError
from rest_framework.validators import ValidationError as RFValidationError
from backend.validators.tag.url import validate_extension
from django.conf import settings
from backend.models.my_model import MyModelSerializer, MyModelViewSet
from backend.models.abstract.my_model import MyModelSerializer, MyModelViewSet
from backend.utils import get_viewset_permissions
......
from django.db import models
from backend.models.university