Commit 92f591e0 authored by Florent Chehab's avatar Florent Chehab

Fixed oneToOne fields on taggedItems => ForeignKey

Cleared migrations
added LIST_SHOULD_BE_DETAIL in MyModelViewSet
cleaned get_viewset_permissions
parent a49d5f70
Pipeline #27137 passed with stages
in 2 minutes and 38 seconds
......@@ -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
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-10 13:28
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
('backend', '0001_squashed_0006_auto_20180910_1508'),
]
operations = [
migrations.AlterField(
model_name='countryscholarship',
name='country',
field=models.ManyToManyField(related_name='country_scholarhips', to='backend.Country'),
),
migrations.AlterField(
model_name='countrytaggeditem',
name='country',
field=models.OneToOneField(on_delete=django.db.models.deletion.PROTECT, primary_key=True, related_name='country_tagged_items', serialize=False, to='backend.Country'),
),
]
# Generated by Django 2.0.3 on 2018-09-09 13:47
import backend.fields.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.fields.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.fields.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.fields.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.fields.jSONField.JSONField(default=list),
),
]
# Generated by Django 2.0.3 on 2018-09-10 13:29
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('backend', '0002_auto_20180910_1528'),
]
operations = [
migrations.AlterField(
model_name='countryscholarship',
name='country',
field=models.ManyToManyField(related_name='country_scholarships', to='backend.Country'),
),
]
# Generated by Django 2.0.3 on 2018-09-10 08:16
from django.db import migrations
class Migration(migrations.Migration):
dependencies = [
('backend', '0003_auto_20180909_1547'),
]
operations = [
migrations.AlterUniqueTogether(
name='campustaggeditem',
unique_together={('campus', 'tag', 'importance_level')},
),
migrations.AlterUniqueTogether(
name='citytaggeditem',
unique_together={('city', 'tag', 'importance_level')},
),
migrations.AlterUniqueTogether(
name='countrytaggeditem',
unique_together={('country', 'tag', 'importance_level')},
),
migrations.AlterUniqueTogether(
name='universitytaggeditem',
unique_together={('university', 'tag', 'importance_level')},
),
]
# Generated by Django 2.0.3 on 2018-09-10 13:37
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
('backend', '0003_auto_20180910_1529'),
]
operations = [
migrations.AlterField(
model_name='campustaggeditem',
name='campus',
field=models.OneToOneField(on_delete=django.db.models.deletion.PROTECT, primary_key=True, related_name='campus_tagged_items', serialize=False, to='backend.Campus'),
),
migrations.AlterField(
model_name='citytaggeditem',
name='city',
field=models.OneToOneField(on_delete=django.db.models.deletion.PROTECT, primary_key=True, related_name='city_tagged_items', serialize=False, to='backend.City'),
),
]
# Generated by Django 2.0.3 on 2018-09-10 13:04
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
('backend', '0004_auto_20180910_1016'),
]
operations = [
migrations.AlterField(
model_name='campus',
name='university',
field=models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, related_name='university_campuses', to='backend.University'),
),
migrations.RemoveField(
model_name='universityscholarship',
name='university',
),
migrations.AddField(
model_name='universityscholarship',
name='university',
field=models.ManyToManyField(related_name='university_scholarships', to='backend.University'),
),
]
# Generated by Django 2.0.3 on 2018-09-10 13:08
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
('backend', '0005_auto_20180910_1504'),
]
operations = [
migrations.AlterField(
model_name='universitytaggeditem',
name='university',
field=models.OneToOneField(on_delete=django.db.models.deletion.PROTECT, primary_key=True, related_name='university_tagged_items', serialize=False, to='backend.University'),
),
]
......@@ -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
......
......@@ -19,6 +19,7 @@ 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']
......
......@@ -6,8 +6,8 @@ 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='campus_tagged_items', primary_key=True)
campus = models.ForeignKey(
Campus, on_delete=models.PROTECT, related_name='campus_tagged_items')
@classmethod
def get_serializer(cls):
......
......@@ -19,6 +19,7 @@ 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']
......
......@@ -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_tagged_items', primary_key=True)
city = models.ForeignKey(
City, on_delete=models.PROTECT, related_name='city_tagged_items')
@classmethod
def get_serializer(cls):
......
......@@ -24,6 +24,7 @@ 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']
......
......@@ -6,8 +6,8 @@ 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_tagged_items', primary_key=True)
country = models.ForeignKey(
Country, on_delete=models.PROTECT, related_name='country_tagged_items')
@classmethod
def get_serializer(cls):
......
......@@ -31,6 +31,7 @@ class UniversityModulesViewSet(MyModelViewSet):
permission_classes = get_viewset_permissions("UniversityModulesViewSet")
serializer_class = UniversityModulesSerializer
queryset = University.objects.all()
LIST_SHOULD_BE_DETAIL = True
def extend_queryset(self):
univ_id = self.kwargs['univ_id']
......
......@@ -6,8 +6,8 @@ from backend.utils import get_model_config, get_viewset_permissions
class UniversityTaggedItem(TaggedItem):
model_config = get_model_config("UniversityTaggedItem")
university = models.OneToOneField(
University, on_delete=models.PROTECT, related_name='university_tagged_items', primary_key=True)
university = models.ForeignKey(
University, on_delete=models.PROTECT, related_name='university_tagged_items')
@classmethod
def get_serializer(cls):
......
......@@ -2,6 +2,7 @@ from .noDeleteIfNotStaff import NoDeleteIfNotStaff # noqa: F401
from .isOwner import IsOwner # noqa: F401
from .noDelete import NoDelete # noqa: F401
from .isStaffOrReadOnly import IsStaffOrReadOnly # noqa: F401
from .readOnly import ReadOnly # noqa: F401
from .isDriOrReadOnly import IsDriOrReadOnly # noqa: F401
from .default_viewset_permissions import DEFAULT_VIEWSET_PERMISSIONS # noqa: F401
from .obj_moderation_permission import OBJ_MODERATION_PERMISSIONS # noqa: F401
......
from rest_framework import permissions
class ReadOnly(permissions.BasePermission):
def has_permission(self, request, view):
if request.method in permissions.SAFE_METHODS:
return True
else:
return False
import yaml
from os.path import join, realpath, dirname
from backend.permissions import IsOwner, IsStaffOrReadOnly, IsDriOrReadOnly
from backend.permissions import IsOwner, IsStaffOrReadOnly, IsDriOrReadOnly, ReadOnly
from rest_framework.permissions import IsAdminUser
from backend.permissions import DEFAULT_VIEWSET_PERMISSIONS
from general.api import get_api_config
def get_viewset_permissions(viewset):
current_dir = dirname(realpath(__file__))
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:
if obj['viewset'] == viewset:
try:
custom_permission = obj["viewset_permission"]
if custom_permission == "IsOwner":
permission = (IsOwner,)
elif custom_permission == "IsStaffOrReadOnly":
permission = (IsStaffOrReadOnly,)
elif custom_permission == "IsDriOrReadOnly":
permission = (IsDriOrReadOnly,)
elif custom_permission == "IsStaff":
permission = (IsAdminUser,)
else:
raise Exception(
"Permission not supported ! Dev what did you do ?")
return DEFAULT_VIEWSET_PERMISSIONS + permission
except KeyError:
return DEFAULT_VIEWSET_PERMISSIONS
api_config = get_api_config()
for obj in api_config:
if obj['viewset'] == viewset:
custom_permission = obj["viewset_permission"]
if custom_permission == "IsOwner":
permission = (IsOwner,)
elif custom_permission == "IsStaffOrReadOnly":
permission = (IsStaffOrReadOnly,)
elif custom_permission == "IsDriOrReadOnly":
permission = (IsDriOrReadOnly,)
elif custom_permission == "IsStaff":
permission = (IsAdminUser,)
elif custom_permission == 'default':
permission = tuple()
else:
raise Exception(
"Permission not supported ! Dev what did you do ?")
if obj["read_only"]:
permission += (ReadOnly,)
return DEFAULT_VIEWSET_PERMISSIONS + permission
raise Exception("Viewset not found in API configuraiton, cannot process !")
......@@ -13,7 +13,8 @@ def get_api_config():
"requires_testing": False,
"moderation_level": 2,
"versionned": False,
"read_only": False
"read_only": False,
"viewset_permission": 'default'
}
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