Commit b92d223e authored by Florent Chehab's avatar Florent Chehab

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
......@@ -6,3 +6,4 @@ exclude =
node_modules/*
backend/urls.py
backend/admin.py
backend/permissions/__list_user_post_permission.py
\ No newline at end of file
......@@ -13,4 +13,5 @@ htmlcov
/backend/urls.py
/backend/admin.py
database.db
database.db-journal
\ No newline at end of file
database.db-journal
backend/permissions/__list_user_post_permission.py
......@@ -5,4 +5,6 @@ class BackendConfig(AppConfig):
name = 'backend'
def ready(self):
import backend.signals # noqa: F401
import backend.signals.__create_user_modules_post_create # noqa: F401
import backend.signals.__squash_revision_by_user # noqa: F401
import backend.signals.__create_univ_modules_post_save # noqa: F401
from backend.fields.jSONField import JSONField
from backend.fields import JSONField
from rest_framework import serializers
field_mapping = serializers.ModelSerializer.serializer_field_mapping
......
from .jSONField import JSONField # noqa: F401
......@@ -43,6 +43,11 @@ template_path = join(templates_dir, 'urls.tpl')
output_path = join(saving_dir, 'urls.py')
render_and_save(template_path, api_config, output_path)
# render list_user_post_permission.py
template_path = join(templates_dir, 'list_user_post_permission.tpl')
output_path = join(saving_dir, './permissions/__list_user_post_permission.py')
render_and_save(template_path, api_config, output_path)
# Render admin.py
data = []
for obj in api_config:
......
{% autoescape off %}
# WARNING
# THIS FILE HAS BEEN AUTOMATICALLY GENERATED
# WITH /backend/generate/generate_backend_files.py
# MODIFY THE FILE ABOVE IF YOUR NOT SATISFIED
# THIS WARNING DOESN'T APPLY TO .tpl FILES...
from django.conf import settings
ALL_VIEWSETS = {}
{% for model in data %}{% if not model.requires_testing %}{% if model.viewset != 'UserDataViewSet' %}
from backend.models.{{model.import_location}} import {{model.viewset}}
ALL_VIEWSETS["{{model.viewset}}"] = {{model.viewset}}
{% endif %}{% endif %}{% endfor %}
if settings.TESTING:
{% for model in data %}{% if model.requires_testing %}
from backend.models.{{model.import_location}} import {{model.viewset}}
ALL_VIEWSETS["{{model.viewset}}"] = {{model.viewset}}
{% endif %}{% endfor %}
class Request(object):
def __init__(self, user, method):
self.user = user
self.method = method
def list_user_post_permission(user):
viewsets_user_can_post = []
request = Request(user, 'POST')
for viewset_name in ALL_VIEWSETS:
viewset = ALL_VIEWSETS[viewset_name]
user_can_post = True
for permission_class in viewset.permission_classes:
if not permission_class.has_permission(None, request, None):
user_can_post = False
break
if user_can_post:
name = viewset_name.split('ViewSet')[0]
name = name[0].lower() + name[1:]
viewsets_user_can_post.append(name)
return viewsets_user_can_post
{% endautoescape %}
......@@ -8,6 +8,8 @@
from django.conf.urls import url, include
from django.conf import settings
from rest_framework import routers
from django.urls import path
from . import views
ALL_MODELS = []
ALL_VIEWSETS = []
......@@ -51,6 +53,7 @@ router.register(
){% endif %}{% endfor %}
urlpatterns += [url(r'^api/', include(router.urls))]
urlpatterns.append(path('api/serverModerationStatus/', views.app_moderation_status))
for model in ALL_MODELS:
for key in model.model_config:
......
......@@ -7,5 +7,49 @@
"required": true
}
}
},
{
"name": "insurance",
"config": {}
},
{
"name": "transport",
"config": {}
},
{
"name": "accommodation",
"config": {}
},
{
"name": "student_life",
"config": {}
},
{
"name": "everyday_life",
"config": {}
},
{
"name": "other",
"config": {}
},
{
"name": "administrative",
"config": {}
},
{
"name": "culture",
"config": {}
},
{
"name": "tourism",
"config": {}
},
{
"name": "shared_comment",
"config": {}
},
{
"name": "specific_partnership",
"config": {}
}
]
\ No newline at end of file
......@@ -3,11 +3,15 @@ from .loading_scripts import LoadCurrencies
from .loading_scripts import LoadCountries
from .loading_scripts import LoadUniversities
from .loading_scripts import LoadTags
from .loading_scripts import LoadUniversityEx
import reversion
def load_all():
admin = LoadAdminUser().get()
LoadCurrencies(admin).load()
LoadCountries(admin).load()
LoadUniversities(admin).load()
LoadTags(admin).load()
with reversion.create_revision():
admin = LoadAdminUser().get()
LoadCurrencies(admin).load()
LoadCountries(admin).load()
LoadUniversities(admin).load()
LoadTags(admin).load()
LoadUniversityEx(admin).load()
......@@ -3,3 +3,4 @@ from .loadCountries import LoadCountries # noqa: F401
from .loadUniversities import LoadUniversities # noqa: F401
from .loadTags import LoadTags # noqa: F401
from .loadCurrencies import LoadCurrencies # noqa: F401
from .loadUniversityEx import LoadUniversityEx # noqa: F401
......@@ -49,4 +49,4 @@ class LoadCountries(LoadGeneric):
intermediate_region_un_code=r["Intermediate Region Code"],
)
country.save()
self.add_info(country, self.admin)
self.add_info_and_save(country, self.admin)
......@@ -26,4 +26,4 @@ class LoadCurrencies(LoadGeneric):
one_EUR_in_this_currency=Decimal(r[2])
)
currency.save()
self.add_info(currency, self.admin)
self.add_info_and_save(currency, self.admin)
......@@ -5,7 +5,7 @@ import reversion
class LoadGeneric(object):
@classmethod
def add_info(cls, obj, admin):
def add_info_and_save(cls, obj, admin):
with reversion.create_revision():
obj.moderated_by = admin
obj.updated_by = admin
......
......@@ -20,4 +20,4 @@ class LoadTags(LoadGeneric):
for tag in tags:
t = Tag(name=tag['name'], config=tag['config'])
t.save()
self.add_info(t, self.admin)
self.add_info_and_save(t, self.admin)
......@@ -33,7 +33,7 @@ class LoadUniversities(LoadGeneric):
country=country
)
city.save()
self.add_info(city, self.admin)
self.add_info_and_save(city, self.admin)
univ = University.objects.update_or_create(
utc_id=utc_id,
......@@ -44,7 +44,7 @@ class LoadUniversities(LoadGeneric):
"logo": logo
}
)[0]
self.add_info(univ, self.admin)
self.add_info_and_save(univ, self.admin)
main_campus = Campus(
is_main_campus=True,
......@@ -55,4 +55,4 @@ class LoadUniversities(LoadGeneric):
lon=lon
)
main_campus.save()
self.add_info(main_campus, self.admin)
self.add_info_and_save(main_campus, self.admin)
from .loadGeneric import LoadGeneric
from backend.models.university import University
from backend.models.university import UniversityDri
from backend.models.university import UniversityInfo
from backend.models.university import UniversitySemestersDates
from backend.models.university import UniversityScholarship
from backend.models.university import UniversityTaggedItem
from backend.models.currency import Currency
from backend.models.tag import Tag
from datetime import datetime
class LoadUniversityEx(LoadGeneric):
def __init__(self, admin):
self.admin = admin
def load(self):
EPFL = University.objects.get(acronym='EPFL')
CHF = Currency.objects.get(pk='CHF')
ACCOMMODATION_TAG = Tag.objects.get(name='accommodation')
univ_dri_1 = UniversityDri(
comment="Attention les cours de master sont 100% en anglais")
univ_dri_1.save()
univ_dri_1.universities.add(EPFL)
self.add_info_and_save(univ_dri_1, self.admin)
univ_info = UniversityInfo.objects.get(university=EPFL)
univ_info.cost_exchange = 0
univ_info.costs_currency = CHF
self.add_info_and_save(univ_info, self.admin)
usd = UniversitySemestersDates.objects.get(
university=EPFL)
usd.autumn_begin = datetime.strptime("17/09/2018", '%d/%m/%Y')
usd.autumn_end = datetime.strptime("29/01/2019", '%d/%m/%Y')
self.add_info_and_save(usd, self.admin)
univ_scholarship_1 = UniversityScholarship(
type="Swiss European...",
currency=CHF,
frequency='s',
amount_min=2200,
amount_max=2200,
comment="Il n'y a rien à faire pour l'obtenir"
)
univ_scholarship_1.save()
univ_scholarship_1.universities.add(EPFL)
self.add_info_and_save(univ_scholarship_1, self.admin)
univ_tag_1 = UniversityTaggedItem(
university=EPFL,
tag=ACCOMMODATION_TAG,
title="C'est compliqué de trouver un logement",
comment="Mon commentaire.",
importance_level='++'
)
self.add_info_and_save(univ_tag_1, self.admin)
This diff is collapsed.
# Generated by Django 2.0.3 on 2018-09-16 13:55
import backend.models.university.university
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('backend', '0001_initial'),
]
operations = [
migrations.AlterField(
model_name='campus',
name='comment',
field=models.CharField(blank=True, default='', max_length=5000),
),
migrations.AlterField(
model_name='campus',
name='name',
field=models.CharField(blank=True, default='', max_length=200),
),
migrations.AlterField(
model_name='campus',
name='title',
field=models.CharField(blank=True, default='', max_length=150),
),
migrations.AlterField(
model_name='campustaggeditem',
name='comment',
field=models.CharField(blank=True, default='', max_length=5000),
),
migrations.AlterField(
model_name='campustaggeditem',
name='title',
field=models.CharField(blank=True, default='', max_length=150),
),
migrations.AlterField(
model_name='city',
name='area',
field=models.CharField(blank=True, default='', max_length=200),
),
migrations.AlterField(
model_name='city',
name='local_name',
field=models.CharField(blank=True, default='', max_length=200),
),
migrations.AlterField(
model_name='citytaggeditem',
name='comment',
field=models.CharField(blank=True, default='', max_length=5000),
),
migrations.AlterField(
model_name='citytaggeditem',
name='title',
field=models.CharField(blank=True, default='', max_length=150),
),
migrations.AlterField(
model_name='country',
name='intermediate_region_name',
field=models.CharField(blank=True, default='', max_length=200),
),
migrations.AlterField(
model_name='country',
name='intermediate_region_un_code',
field=models.CharField(blank=True, default='', max_length=3),
),
migrations.AlterField(
model_name='country',
name='iso_alpha3_code',
field=models.CharField(blank=True, default='', max_length=3, unique=True),
),
migrations.AlterField(
model_name='country',
name='sub_region_name',
field=models.CharField(blank=True, default='', max_length=200),
),
migrations.AlterField(
model_name='country',
name='sub_region_un_code',
field=models.CharField(blank=True, default='', max_length=3),
),
migrations.AlterField(
model_name='countrydri',
name='comment',
field=models.CharField(blank=True, default='', max_length=5000),
),
migrations.AlterField(
model_name='countrydri',
name='title',
field=models.CharField(blank=True, default='', max_length=150),
),
migrations.AlterField(
model_name='countryscholarship',
name='comment',
field=models.CharField(blank=True, default='', max_length=5000),
),
migrations.AlterField(
model_name='countryscholarship',
name='other_advantages',
field=models.CharField(blank=True, default='', max_length=5000),
),
migrations.AlterField(
model_name='countryscholarship',
name='title',
field=models.CharField(blank=True, default='', max_length=150),
),
migrations.AlterField(
model_name='countrytaggeditem',
name='comment',
field=models.CharField(blank=True, default='', max_length=5000),
),
migrations.AlterField(
model_name='countrytaggeditem',
name='title',
field=models.CharField(blank=True, default='', max_length=150),
),
migrations.AlterField(
model_name='currency',
name='symbol',
field=models.CharField(blank=True, default='', max_length=30),
),
migrations.AlterField(
model_name='previousdeparturefeedback',
name='adequation_comment',
field=models.CharField(blank=True, default='', max_length=5000),
),
migrations.AlterField(
model_name='previousdeparturefeedback',
name='integration_comment',
field=models.CharField(blank=True, default='', max_length=5000),
),
migrations.AlterField(
model_name='recommendation',
name='comment',
field=models.CharField(blank=True, default='', max_length=5000),
),
migrations.AlterField(
model_name='university',
name='acronym',
field=models.CharField(blank=True, default='', max_length=20),
),
migrations.AlterField(
model_name='university',
name='logo',
field=models.URLField(blank=True, default='', validators=[backend.models.university.university.validate_extension_django]),
),
migrations.AlterField(
model_name='university',
name='website',
field=models.URLField(blank=True, default='', max_length=300),
),
migrations.AlterField(
model_name='universitydri',
name='comment',
field=models.CharField(blank=True, default='', max_length=5000),
),
migrations.AlterField(
model_name='universitydri',
name='title',
field=models.CharField(blank=True, default='', max_length=150),
),
migrations.AlterField(
model_name='universityinfo',
name='comment',
field=models.CharField(blank=True, default='', max_length=5000),
),
migrations.AlterField(
model_name='universityinfo',
name='title',
field=models.CharField(blank=True, default='', max_length=150),
),
migrations.AlterField(
model_name='universityscholarship',
name='comment',
field=models.CharField(blank=True, default='', max_length=5000),
),
migrations.AlterField(
model_name='universityscholarship',
name='other_advantages',
field=models.CharField(blank=True, default='', max_length=5000),
),
migrations.AlterField(
model_name='universityscholarship',
name='title',
field=models.CharField(blank=True, default='', max_length=150),
),
migrations.AlterField(
model_name='universitysemestersdates',
name='comment',
field=models.CharField(blank=True, default='', max_length=5000),
),
migrations.AlterField(
model_name='universitysemestersdates',
name='title',
field=models.CharField(blank=True, default='', max_length=150),
),
migrations.AlterField(
model_name='universitytaggeditem',
name='comment',
field=models.CharField(blank=True, default='', max_length=5000),
),
migrations.AlterField(
model_name='universitytaggeditem',
name='title',
field=models.CharField(blank=True, default='', max_length=150),
),
]
# 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,
)