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
from django.db.models.signals import post_save
from backend.models.university import University
from backend.models.university import UniversityInfo
from backend.models.university import UniversitySemestersDates
def create_univ_modules(sender, instance, created, **kwargs):
if created:
UniversityInfo.objects.create(university=instance)
UniversitySemestersDates.objects.create(university=instance)
post_save.connect(create_univ_modules, sender=University)
from django.db.models.signals import post_save
from django.contrib.auth.models import User
from backend.models.user import UserData
def create_user_modules(sender, instance, created, **kwargs):
if created:
UserData.objects.create(owner=instance)
post_save.connect(create_user_modules, sender=User)
......@@ -4,7 +4,7 @@ import django.dispatch
new_revision_saved = django.dispatch.Signal(providing_args=["obj"])
def squashRevisionByUser(sender, obj, **kwargs):
def squash_revision_by_user(sender, obj, **kwargs):
"""
It should also work with moderation as obj will be a versionned object
"""
......@@ -25,6 +25,6 @@ def squashRevisionByUser(sender, obj, **kwargs):
new_revision_saved.connect(
squashRevisionByUser,
squash_revision_by_user,
dispatch_uid="receiver_concat_revisions"
)
from django.test import override_settings
from .withUserTestCase import WithUserTestCase
from backend.permissions import is_moderation_required
model_config = {'moderation_level': 2}
class IsModerationActivatedTestCase(WithUserTestCase):
@override_settings(MODERATION_ACTIVATED=True)
def test_is_moderation_1(self):
self.assertFalse(is_moderation_required(
model_config, None, self.staff_user))
self.assertFalse(is_moderation_required(
model_config, None, self.staff_user, 3))
@override_settings(MODERATION_ACTIVATED=False)
def test_is_moderation_2(self):
self.assertFalse(is_moderation_required(
model_config, None, self.staff_user))
self.assertFalse(is_moderation_required(
model_config, None, self.staff_user, 3))
......@@ -221,10 +221,11 @@ class ModerationTestCase(WithUserTestCase):
data = {
"comment": "",
"usefull_links": [],
"university": [c.pk]
"useful_links": [],
"universities": [c.pk]
}
api_end_point = "/api/" + \
find_api_end_point_for_viewset("UniversityDriViewSet") + "/"
find_api_end_point_for_viewset(
"UniversityDriViewSet") + "/" + str(c.pk) + '/'
self._submit_post_test(self.dri_client,
data, api_end_point)
......@@ -2,16 +2,26 @@ from django.test import TestCase
from backend.models.abstract.scholarship import ScholarshipSerializer
import pytest
from rest_framework.validators import ValidationError
from backend.models.currency import Currency
class ScholarshipTestCase(TestCase):
@classmethod
def setUpTestData(cls):
cls.EUR = Currency.objects.create(
code="EUR",
name="Euro",
one_EUR_in_this_currency=1
)
def test_scholarhip_validation(self):
ser = ScholarshipSerializer()
attrs = {
'usefull_links': [],
'comment': ''
'useful_links': [],
'comment': '',
'currency': self.EUR
}
with pytest.raises(ValidationError):
attrs['amount_min'] = 200
attrs['amount_max'] = 100
......
......@@ -13,7 +13,7 @@ class SemesterDatesTestCase(TestCase):
def build(l):
tmp = {sem: val for sem, val in zip(semesters, l)}
tmp['comment'] = ""
tmp['usefull_links'] = []
tmp['useful_links'] = []
return tmp
def _test_attrs_error(attrs):
......@@ -21,8 +21,8 @@ class SemesterDatesTestCase(TestCase):
self.ser.my_validate(attrs)
self.ser = UniversitySemestersDatesSerializer()
_test_attrs_error(build([None] * 4))
_test_attrs_error(build([None, 3, None, None]))
_test_attrs_error(build([2, 3, 3, 2]))
self.ser.my_validate(build([None] * 4))
self.ser.my_validate(build([2, 3, None, None]))
self.ser.my_validate(build([2, 3, 2, 3]))
from django.test import TestCase
from django.contrib.auth.models import User
from backend.models.user import UserData
from backend.models.university import University
from backend.models.university import UniversityInfo
from backend.models.university import UniversitySemestersDates
class AutomaticCreationPostCreateTestCase(TestCase):
@classmethod
def setUpTestData(cls):
cls.user = User.objects.create_user(
username="toto"
)
cls.univ = University.objects.create(
name="Univ de test",
utc_id=1000
)
def test_user_data_automatically_created(self):
self.assertTrue(
UserData.objects.filter(owner=self.user).exists()
)
def test_univ_data_automatically_created(self):
self.assertTrue(
UniversityInfo.objects.filter(university=self.univ).exists()
)
self.assertTrue(
UniversitySemestersDates.objects.filter(
university=self.univ).exists()
)
from backend.models.abstract.my_model import ForTestingVersioning
from reversion.models import Version
from backend.signals import squashRevisionByUser
from backend.signals.__squash_revision_by_user import squash_revision_by_user
import reversion
from .withUserTestCase import WithUserTestCase
import json
......@@ -33,7 +33,7 @@ class SquashVersionsTestCase(WithUserTestCase):
self.obj.save()
reversion.set_user(self.authenticated_user_2)
squashRevisionByUser(None, self.obj)
squash_revision_by_user(None, self.obj)
versions = self.get_versions(self.obj)
self.assertEqual(len(versions), 2)
......@@ -53,7 +53,7 @@ class SquashVersionsTestCase(WithUserTestCase):
self.obj.save()
reversion.set_user(self.authenticated_user)
squashRevisionByUser(None, self.obj)
squash_revision_by_user(None, self.obj)
versions = self.get_versions(self.obj)
self.assertEqual(len(versions), 1)
......
......@@ -2,7 +2,7 @@ from .withUserTestCase import WithUserTestCase
from backend.models.abstract.my_model import ForTestingVersioning
from django.conf import settings
from reversion.models import Version
from backend.signals import new_revision_saved
from backend.signals.__squash_revision_by_user import new_revision_saved
from django.test import override_settings
......
from .is_member import is_member
from backend.utils import is_member
def does_user_have_moderation_rights(user):
......
......@@ -6,6 +6,14 @@ def get_model_config(model):
for obj in api_config:
if obj['model'] == model:
return {"moderation_level": obj["moderation_level"]}
tmp = {
"moderation_level": obj["moderation_level"],
"model": model,
"read_only": obj["read_only"]
}
key = 'enforce_moderation_user_level'
if key in obj.keys():
tmp[key] = obj[key]
return tmp
raise Exception("Model not found in API configuraiton, cannot process !")
from .is_member import is_member
from .__is_member import is_member
from backend.permissions import OBJ_MODERATION_PERMISSIONS
......
from backend.permissions import IsOwner, IsStaffOrReadOnly, IsDriOrReadOnly, ReadOnly
from backend.permissions import IsOwner, IsStaffOrReadOnly, IsDriOrReadOnly, ReadOnly, IsDriOrNoPost, NoPostIfNotStaff
from rest_framework.permissions import IsAdminUser
from backend.permissions import DEFAULT_VIEWSET_PERMISSIONS
from general.api import get_api_config
......@@ -15,6 +15,10 @@ def get_viewset_permissions(viewset):
permission = (IsStaffOrReadOnly,)
elif custom_permission == "IsDriOrReadOnly":
permission = (IsDriOrReadOnly,)
elif custom_permission == "IsDriOrNoPost":
permission = (IsDriOrNoPost,)
elif custom_permission == "NoPostIfNotStaff":
permission = (NoPostIfNotStaff,)
elif custom_permission == "IsStaff":
permission = (IsAdminUser,)
elif custom_permission == 'default':
......
from .is_member import is_member # noqa: F401
from .find_api_end_point_for_viewset import find_api_end_point_for_viewset # noqa: F401
from .get_model_config import get_model_config # noqa: F401
from .get_viewset_permissions import get_viewset_permissions # noqa: F401
from .does_user_have_moderation_rights import does_user_have_moderation_rights # noqa: F401
from .get_user_level import get_user_level # noqa: F401
from .__is_member import is_member # noqa: F401
from .__get_viewset_permissions import get_viewset_permissions # noqa: F401
from .__find_api_end_point_for_viewset import find_api_end_point_for_viewset # noqa: F401
from .__get_user_level import get_user_level # noqa: F401
from .__get_model_config import get_model_config # noqa: F401
from .__does_user_have_moderation_rights import does_user_have_moderation_rights # noqa: F401
from django.conf import settings
from django.utils.deconstruct import deconstructible
import os
@deconstructible
class friendly_path(object):
"""
class to generate file path with filenames
according to one of the model attribute
"""
def __init__(self, rel_path, attribute_distinction):
self.rel_path = rel_path
self.attr = attribute_distinction
def __call__(self, instance, filename):
ext = filename.split('.')[-1]
i = 0
while True:
filename = '{}_{}.{}'.format(
getattr(instance, self.attr), str(i), ext)
path_and_fn = os.path.join(
settings.MEDIA_ROOT, self.rel_path, filename)
if not os.path.isfile(path_and_fn):
break
i += 1
return os.path.join(self.rel_path, filename)
from .url import validate_url # noqa: F401
from .text import validate_text # noqa: F401
from .tagged_item_validation import validate_content_against_config # noqa: F401
from .tagged_item_validation import validate_content_against_config, tagged_item_validation # noqa: F401
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