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

Merge branch 'connect_back_front' into 'master'

Connect back front

See merge request chehabfl/outgoing_rex!36
parents a938e15d 6ff038f3
Pipeline #27364 passed with stages
in 3 minutes and 1 second
# Generated by Django 2.0.3 on 2018-09-16 14:24
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('backend', '0002_auto_20180916_1555'),
]
operations = [
migrations.AlterField(
model_name='countryscholarship',
name='frequency',
field=models.CharField(blank=True, choices=[('w', 'week'), ('m', 'month'), ('s', 'semester'), ('y', 'year'), ('o', 'one_shot')], default='m', max_length=1, null=True),
),
migrations.AlterField(
model_name='universityscholarship',
name='frequency',
field=models.CharField(blank=True, choices=[('w', 'week'), ('m', 'month'), ('s', 'semester'), ('y', 'year'), ('o', 'one_shot')], default='m', max_length=1, null=True),
),
]
......@@ -12,9 +12,9 @@ IMPORTANCE_LEVEL = (
class BasicModule(MyModelVersionned):
comment = models.TextField(blank=True)
usefull_links = JSONField(default=list)
title = models.CharField(default='', blank=True, max_length=150)
comment = models.CharField(default='', blank=True, max_length=5000)
useful_links = JSONField(default=list)
importance_level = models.CharField(
max_length=2,
choices=IMPORTANCE_LEVEL,
......@@ -28,8 +28,8 @@ class BasicModule(MyModelVersionned):
class BasicModuleSerializer(MyModelVersionnedSerializer):
def my_validate(self, attrs):
content = {'usefull_links': attrs['usefull_links']}
config = {'usefull_links': USEFULL_LINKS_CONFIG}
content = {'useful_links': attrs['useful_links']}
config = {'useful_links': USEFULL_LINKS_CONFIG}
validate_content_against_config(config, content)
return attrs
......
......@@ -19,7 +19,8 @@ CLEANED_MY_MODEL_DATA = {
def override_data(old_data, new_data):
for key in new_data:
old_data[key] = new_data[key]
if key in old_data:
old_data[key] = new_data[key]
return old_data
......@@ -34,19 +35,22 @@ class MyModelSerializer(MySerializerWithJSON):
model_config = serializers.SerializerMethodField()
# For easier handling on the client side, we force an id field
# this is usefull when a model has a dedicated primary key
# this is useful when a model has a dedicated primary key
id = serializers.SerializerMethodField()
def get_model_config(self, obj=None):
return self.Meta.model.model_config
FORCE_FULL_DISPLAY = False
def get_pending_moderation(self, obj):
pm = PendingModerationSerializer(
obj.pending_moderation, many=True, read_only=True, context=self.context).data
if self.context['view'].action == 'list':
return len(pm)
if not self.FORCE_FULL_DISPLAY and self.context['view'].action == 'list':
return None
else:
return pm
ct = ContentType.objects.get_for_model(self.Meta.model)
pending = PendingModeration.objects.filter(
content_type=ct, object_id=obj.pk)
return PendingModerationSerializer(pending, many=True, read_only=True, context=self.context).data
def get_id(self, obj):
return obj.pk
......@@ -107,8 +111,10 @@ class MyModelSerializer(MySerializerWithJSON):
self.my_pre_save()
ct = ContentType.objects.get_for_model(self.Meta.model)
if is_moderation_required(self.get_model_config()['moderation_level'], self.instance, self.user, self.user_level):
if is_moderation_required(self.get_model_config(), self.instance, self.user, self.user_level):
if self.instance is None: # we need to create the main model
# Store the user for squashing data in versions models
self.validated_data.updated_by = self.user
self.instance = super(
MyModelSerializer, self).save(*args, **kwargs)
......@@ -146,7 +152,15 @@ class MyModelSerializer(MySerializerWithJSON):
object_id=self.instance.pk,
)
self.clean_validated_data() # Make that it is done...
if pending_instance.new_object == self.validated_data:
# We have to compare the serialized data
# So we make sure to compare the same elements
key_to_remove = []
for key in self.initial_data:
if key not in self.validated_data:
key_to_remove.append(key)
for key in key_to_remove:
self.initial_data.pop(key, None)
if pending_instance.new_object == self.initial_data:
moderated_and_updated = False
self.validated_data['updated_by'] = pending_instance.updated_by
self.validated_data['updated_on'] = pending_instance.updated_on
......
from backend.models.abstract.my_model import MyModel, MyModelSerializer, MyModelViewSet
from backend.signals import new_revision_saved
from backend.signals.__squash_revision_by_user import new_revision_saved
from rest_framework import serializers, mixins, viewsets
import reversion
from reversion.models import Version
......@@ -30,7 +30,7 @@ class MyModelVersionnedSerializer(MyModelSerializer):
content_type_id = serializers.SerializerMethodField()
def get_nb_versions(self, obj):
if self.context['view'].action != 'list':
if self.FORCE_FULL_DISPLAY or self.context['view'].action != 'list':
versions = Version.objects.get_for_object(obj)
return len(versions)
return None
......@@ -39,11 +39,9 @@ class MyModelVersionnedSerializer(MyModelSerializer):
return ContentType.objects.get_for_model(self.Meta.model).id
def save(self, *args, **kwargs):
user = self.context['request'].user
res = None
with reversion.create_revision():
res = self.my_save(*args, **kwargs)
reversion.set_user(user)
reversion.set_user(res.updated_by)
new_revision_saved.send(sender=self.__class__, obj=self.instance)
return res
......@@ -64,7 +62,6 @@ class VersionSerializer(MySerializerWithJSON):
try:
tmp = list(djangoSerializers.deserialize(
obj.format, data, ignorenonexistent=True))[0]
print(tmp.object)
# Version is valid,
obj_serializer = tmp.object.get_serializer()
new_context = dict(self.context)
......
......@@ -4,18 +4,39 @@ from backend.models.currency import Currency
from rest_framework import serializers
from django.core.validators import MinValueValidator
SCHOLARSHIP_FREQUENCIES = (
('w', 'week'),
('m', 'month'),
('s', 'semester'),
('y', 'year'),
('o', 'one_shot')
)
class Scholarship(BasicModule):
type = models.CharField(max_length=200)
currency = models.ForeignKey(Currency, on_delete=models.PROTECT)
currency = models.ForeignKey(Currency, null=True, on_delete=models.PROTECT)
other_advantages = models.CharField(
default='', blank=True, max_length=5000)
frequency = models.CharField(
max_length=1,
choices=SCHOLARSHIP_FREQUENCIES,
default='m',
null=True,
blank=True,
)
amount_min = models.DecimalField(
null=True,
max_digits=20,
decimal_places=2,
validators=[MinValueValidator(0)]
)
amount_max = models.DecimalField(
null=True,
max_digits=20,
decimal_places=2,
validators=[MinValueValidator(0)]
......@@ -26,12 +47,18 @@ class Scholarship(BasicModule):
class ScholarshipSerializer(BasicModuleSerializer):
FORCE_FULL_DISPLAY = True
def my_validate(self, attrs):
attrs = super(ScholarshipSerializer, self).my_validate(attrs)
if attrs['amount_max'] < attrs['amount_min']:
raise serializers.ValidationError(
"Amount_max should be greater or equal than amount_min")
if attrs['amount_min'] is not None:
if attrs['currency'] is None:
raise serializers.ValidationError(
"A currency must be specified when there is a value")
if attrs['amount_max'] is not None:
if attrs['amount_max'] < attrs['amount_min']:
raise serializers.ValidationError(
"amount_max should be greater or equal than amount_min")
return attrs
......
......@@ -14,6 +14,7 @@ class TaggedItem(BasicModule):
class TaggedItemSerializer(BasicModuleSerializer):
FORCE_FULL_DISPLAY = True
def my_validate(self, attrs):
tagged_item_validation(attrs)
......
from .campus import Campus, CampusViewSet, CampusSerializer, MainCampusViewSet # noqa: F401
from .campusTaggedItem import CampusTaggedItem, CampusTaggedItemSerializer, CampusTaggedItemViewSet # noqa: F401
from .campusModulesAPI import CampusModulesViewSet # noqa: F401
......@@ -10,8 +10,7 @@ class Campus(BasicModule):
model_config = get_model_config("Campus")
is_main_campus = models.BooleanField(null=False)
name = models.CharField(max_length=200, null=True)
comment = models.TextField(null=True, blank=True)
name = models.CharField(max_length=200, default='', blank=True)
city = models.ForeignKey(City, on_delete=models.PROTECT, null=False)
university = models.ForeignKey(
University, on_delete=models.PROTECT, null=False, related_name="university_campuses")
......
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
......@@ -27,3 +27,9 @@ class CampusTaggedItemViewSet(TaggedItemViewSet):
permission_classes = get_viewset_permissions("CampusTaggedItemViewSet")
queryset = CampusTaggedItem.objects.all() # pylint: disable=E1101
serializer_class = CampusTaggedItemSerializer
BYPASS_DICT_MODE = True
def extend_queryset(self):
campus_id = self.kwargs['campus_id']
return self.my_model_queryset.filter(campus=campus_id).distinct()
from .city import City, CitySerializer, CityViewSet # noqa: F401
from .cityTaggedItem import CityTaggedItem, CityTaggedItemSerializer, CityTaggedItemViewSet # noqa: F401
from .cityModulesAPI import CityModulesViewSet # noqa: F401
......@@ -8,10 +8,10 @@ class City(MyModel):
model_config = get_model_config("City")
name = models.CharField(max_length=200)
local_name = models.CharField(max_length=200, null=True, blank=True)
local_name = models.CharField(max_length=200, default='', blank=True)
# We add an area to distinguish similarly named cities
# in a country
area = models.CharField(max_length=200, null=True, blank=True)
area = models.CharField(max_length=200, default='', blank=True)
country = models.ForeignKey(Country, on_delete=models.PROTECT)
......
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
......@@ -28,3 +28,9 @@ class CityTaggedItemViewSet(TaggedItemViewSet):
permission_classes = get_viewset_permissions("CityTaggedItemViewSet")
queryset = CityTaggedItem.objects.all() # pylint: disable=E1101
serializer_class = CityTaggedItemSerializer
BYPASS_DICT_MODE = True
def extend_queryset(self):
city_id = self.kwargs['city_id']
return self.my_model_queryset.filter(city=city_id).distinct()
......@@ -2,4 +2,3 @@ from .country import Country, CountrySerializer, CountryViewSet # 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
......@@ -10,15 +10,15 @@ class Country(MyModel):
name = models.CharField(max_length=200)
iso_alpha2_code = models.CharField(primary_key=True, max_length=2)
iso_alpha3_code = models.CharField(
unique=True, max_length=3, blank=False, null=False)
unique=True, max_length=3, default='', blank=True)
region_name = models.CharField(max_length=200)
region_un_code = models.CharField(max_length=3)
sub_region_name = models.CharField(max_length=200, null=True, blank=True)
sub_region_un_code = models.CharField(max_length=3, null=True, blank=True)
sub_region_name = models.CharField(max_length=200, default='', blank=True)
sub_region_un_code = models.CharField(max_length=3, default='', blank=True)
intermediate_region_name = models.CharField(
max_length=200, null=True, blank=True)
max_length=200, default='', blank=True)
intermediate_region_un_code = models.CharField(
max_length=3, null=True, blank=True)
max_length=3, default='', blank=True)
class CountrySerializer(MyModelSerializer):
......
......@@ -6,7 +6,7 @@ from backend.utils import get_model_config, get_viewset_permissions
class CountryDri(BasicModule):
model_config = get_model_config("CountryDri")
country = models.ManyToManyField(
countries = models.ManyToManyField(
Country, related_name="country_dri")
@classmethod
......@@ -15,6 +15,7 @@ class CountryDri(BasicModule):
class CountryDriSerializer(BasicModuleSerializer):
FORCE_FULL_DISPLAY = True
class Meta:
model = CountryDri
......@@ -25,3 +26,9 @@ class CountryDriViewSet(BasicModuleViewSet):
permission_classes = get_viewset_permissions("CountryDriViewSet")
queryset = CountryDri.objects.all() # pylint: disable=E1101
serializer_class = CountryDriSerializer
BYPASS_DICT_MODE = True
def extend_queryset(self):
country_id = self.kwargs['country_id']
return self.my_model_queryset.filter(countries__pk=country_id).distinct()
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
......@@ -6,7 +6,7 @@ from backend.utils import get_model_config, get_viewset_permissions
class CountryScholarship(Scholarship):
model_config = get_model_config("CountryScholarship")
country = models.ManyToManyField(
countries = models.ManyToManyField(
Country, related_name="country_scholarships")
@classmethod
......@@ -25,3 +25,9 @@ class CountryScholarshipViewSet(ScholarshipViewSet):
permission_classes = get_viewset_permissions("CountryScholarshipViewSet")
queryset = CountryScholarship.objects.all() # pylint: disable=E1101
serializer_class = CountryScholarshipSerializer
BYPASS_DICT_MODE = True
def extend_queryset(self):
country_id = self.kwargs['country_id']
return self.my_model_queryset.filter(countries__pk=country_id).distinct()
......@@ -27,3 +27,9 @@ class CountryTaggedItemViewSet(TaggedItemViewSet):
permission_classes = get_viewset_permissions("CountryTaggedItemViewSet")
queryset = CountryTaggedItem.objects.all() # pylint: disable=E1101
serializer_class = CountryTaggedItemSerializer
BYPASS_DICT_MODE = True
def extend_queryset(self):
country_id = self.kwargs['country_id']
return self.my_model_queryset.filter(country=country_id).distinct()
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