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
from django.test import TestCase from django.test import TestCase
from backend.models.scholarship import ScholarshipSerializer from backend.models.abstract.scholarship import ScholarshipSerializer
import pytest import pytest
from rest_framework.validators import ValidationError from rest_framework.validators import ValidationError
......
from backend.models.my_model import ForTestingVersioning from backend.models.abstract.my_model import ForTestingVersioning
from reversion.models import Version from reversion.models import Version
from backend.signals import squashRevisionByUser from backend.signals import squashRevisionByUser
import reversion import reversion
......
from .withUserTestCase import WithUserTestCase from .withUserTestCase import WithUserTestCase
from backend.models.my_model import ForTestingVersioning from backend.models.abstract.my_model import ForTestingVersioning
from django.conf import settings from django.conf import settings
from reversion.models import Version from reversion.models import Version
from backend.signals import new_revision_saved from backend.signals import new_revision_saved
......
from .is_member import is_member # noqa: F401 from .is_member import is_member # noqa: F401
from .file import read_file, write_file # noqa: F401
from .find_api_end_point_for_viewset import find_api_end_point_for_viewset # 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_model_config import get_model_config # noqa: F401
from .get_viewset_permissions import get_viewset_permissions # noqa: F401 from .get_viewset_permissions import get_viewset_permissions # noqa: F401
......
def read_file(file):
with open(file, "r") as myfile:
return myfile.read()
def write_file(file, data):
with open(file, 'w') as the_file:
the_file.write(data)
import yaml from backend.permissions import IsOwner, IsStaffOrReadOnly, IsDriOrReadOnly, ReadOnly
from os.path import join, realpath, dirname
from backend.permissions import IsOwner, IsStaffOrReadOnly, IsDriOrReadOnly
from rest_framework.permissions import IsAdminUser from rest_framework.permissions import IsAdminUser
from backend.permissions import DEFAULT_VIEWSET_PERMISSIONS from backend.permissions import DEFAULT_VIEWSET_PERMISSIONS
from general.api import get_api_config
def get_viewset_permissions(viewset): def get_viewset_permissions(viewset):
current_dir = dirname(realpath(__file__)) api_config = get_api_config()
api_file_loc = join(current_dir, '../../general/api/api_config.yml')
with open(api_file_loc, "r") as file:
api_config = yaml.load(file.read())
for obj in api_config: for obj in api_config:
if obj['viewset'] == viewset: if obj['viewset'] == viewset:
try:
custom_permission = obj["viewset_permission"] custom_permission = obj["viewset_permission"]
if custom_permission == "IsOwner": if custom_permission == "IsOwner":
permission = (IsOwner,) permission = (IsOwner,)
...@@ -22,11 +17,14 @@ def get_viewset_permissions(viewset): ...@@ -22,11 +17,14 @@ def get_viewset_permissions(viewset):
permission = (IsDriOrReadOnly,) permission = (IsDriOrReadOnly,)
elif custom_permission == "IsStaff": elif custom_permission == "IsStaff":
permission = (IsAdminUser,) permission = (IsAdminUser,)
elif custom_permission == 'default':
permission = tuple()
else: else:
raise Exception( raise Exception(
"Permission not supported ! Dev what did you do ?") "Permission not supported ! Dev what did you do ?")
if obj["read_only"]:
permission += (ReadOnly,)
return DEFAULT_VIEWSET_PERMISSIONS + permission return DEFAULT_VIEWSET_PERMISSIONS + permission
except KeyError:
return DEFAULT_VIEWSET_PERMISSIONS
raise Exception("Viewset not found in API configuraiton, cannot process !") raise Exception("Viewset not found in API configuraiton, cannot process !")
...@@ -177,10 +177,34 @@ ...@@ -177,10 +177,34 @@
- model: null - model: null
viewset: UniversityModulesViewSet viewset: UniversityModulesViewSet
import_location: university import_location: university
api_end_point: universitiesAll api_end_point: universityAll
api_attr: (?P<univ_id>[0-9]+) api_attr: (?P<univ_id>[0-9]+)
api_name: university_details api_name: university_details
read_only: true
- model: null
viewset: CountryModulesViewSet
import_location: country
api_end_point: countryAll
api_attr: (?P<country_id>[a-zA-Z]+)
api_name: country_details
read_only: true
- model: null
viewset: CityModulesViewSet
import_location: city
api_end_point: cityAll
api_attr: (?P<city_id>[0-9]+)
api_name: city_details
read_only: true
- model: null
viewset: CampusModulesViewSet
import_location: campus
api_end_point: campusAll
api_attr: (?P<campus_id>[0-9]+)
api_name: campus_details
read_only: true
- model: CampusTaggedItem - model: CampusTaggedItem
...@@ -224,14 +248,14 @@ ...@@ -224,14 +248,14 @@
- model: PendingModeration - model: PendingModeration
viewset: PendingModerationViewSet viewset: PendingModerationViewSet
import_location: my_model import_location: abstract.my_model
api_end_point: pendingModeration api_end_point: pendingModeration
viewset_permission: IsStaff viewset_permission: IsStaff
- model: Version - model: Version
viewset: VersionViewSet viewset: VersionViewSet
import_location: my_model import_location: abstract.my_model
api_end_point: versions api_end_point: versions
api_attr: (?P<content_type_id>[0-9]+)/(?P<object_pk>[0-9A-Za-z]+) api_attr: (?P<content_type_id>[0-9]+)/(?P<object_pk>[0-9A-Za-z]+)
api_name: versionsList api_name: versionsList
...@@ -239,14 +263,14 @@ ...@@ -239,14 +263,14 @@
- model: ForTestingModeration - model: ForTestingModeration
viewset: ForTestingModerationViewSet viewset: ForTestingModerationViewSet
import_location: my_model import_location: abstract.my_model
api_end_point: test/moderation api_end_point: test/moderation
moderation_level: 1 moderation_level: 1
requires_testing: true requires_testing: true
- model: ForTestingVersioning - model: ForTestingVersioning
viewset: ForTestingVersioningViewSet viewset: ForTestingVersioningViewSet
import_location: my_model import_location: abstract.my_model
api_end_point: test/versioning api_end_point: test/versioning
moderation_level: 1 moderation_level: 1
requires_testing: true requires_testing: true
......
...@@ -13,7 +13,8 @@ def get_api_config(): ...@@ -13,7 +13,8 @@ def get_api_config():
"requires_testing": False, "requires_testing": False,
"moderation_level": 2, "moderation_level": 2,
"versionned": False, "versionned": False,
"read_only": False "read_only": False,
"viewset_permission": 'default'
} }
for obj in api_config: for obj in api_config:
......
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