Commit 39210901 authored by Florent Chehab's avatar Florent Chehab

UserData updated

parent 54cd053f
Pipeline #26553 passed with stages
in 3 minutes and 1 second
# Generated by Django 2.0.3 on 2018-08-27 07:27
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('backend', '0016_auto_20180826_1556'),
]
operations = [
migrations.AddField(
model_name='userdata',
name='black_list',
field=models.ManyToManyField(related_name='_userdata_black_list_+', to='backend.University'),
),
]
# Generated by Django 2.0.3 on 2018-08-27 08:08
import django.contrib.postgres.fields.jsonb
from django.db import migrations
class Migration(migrations.Migration):
dependencies = [
('backend', '0017_userdata_black_list'),
]
operations = [
migrations.RemoveField(
model_name='userdata',
name='is_anonymous',
),
migrations.RemoveField(
model_name='userdata',
name='is_public',
),
migrations.AlterField(
model_name='userdata',
name='contact_info',
field=django.contrib.postgres.fields.jsonb.JSONField(default={}, null=True),
),
]
# Generated by Django 2.0.3 on 2018-08-27 08:10
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('backend', '0018_auto_20180827_1008'),
]
operations = [
migrations.AlterField(
model_name='userdata',
name='black_list',
field=models.ManyToManyField(blank=True, related_name='_userdata_black_list_+', to='backend.University'),
),
]
# Generated by Django 2.0.3 on 2018-08-27 08:26
from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
('backend', '0019_auto_20180827_1010'),
]
operations = [
migrations.AlterField(
model_name='userdata',
name='owner',
field=models.OneToOneField(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL),
),
]
......@@ -28,6 +28,9 @@ class MyModelSerializer(serializers.ModelSerializer):
model = MyModel
def moderation_required(self):
if hasattr(self.Meta, 'NEVER_MODERATE') and self.Meta.NEVER_MODERATE:
return False
user = self.get_user_in_request()
request = self.context['request']
......@@ -46,27 +49,44 @@ class MyModelSerializer(serializers.ModelSerializer):
def set_model_attr_no_moder(self, moderated_and_updated):
user = self.get_user_in_request()
now = timezone.now()
self.validated_data['moderated_by'] = user
self.validated_data['moderated_on'] = now
self.override_validated_data({
'moderated_by': user,
'moderated_on': now,
})
if moderated_and_updated:
self.validated_data['updated_by'] = user
self.validated_data['updated_on'] = now
self.override_validated_data({
'updated_by': user,
'updated_on': now,
})
def clean_validated_data(self):
self.validated_data['moderated_by'] = None
self.validated_data['moderated_on'] = None
self.validated_data['updated_by'] = None
self.validated_data['updated_on'] = None
self.override_validated_data({
'moderated_by': None,
'moderated_on': None,
'updated_by': None,
'updated_on': None,
})
def get_pending_models(self):
ct = ContentType.objects.get_for_model(self.Meta.model)
return PendingModeration.objects.filter(
content_type=ct, object_id=self.instance.pk)
def override_validated_data(self, new_data):
"""
Method used to force specific attributes when saving a model
"""
for key in new_data:
self.validated_data[key] = new_data[key]
def my_pre_save(self):
pass
def save(self, **kwargs):
user = self.get_user_in_request()
self.clean_validated_data()
self.my_pre_save()
if self.moderation_required():
if self.instance is None: # we need to create the main model
......
......@@ -2,3 +2,5 @@ from .DictModeViewSet import DictModeViewSet # noqa: F401
from .usefullLinksField import UsefullLinksField # noqa: F401
from .validateWithRestFramework import validate_with_rest_framework # noqa: F401
from .noDeleteIfNotAdmin import NoDeleteIfNotAdmin # noqa: F401
from .isOwner import IsOwner # noqa: F401
from .noDelete import NoDelete # noqa: F401
from rest_framework.permissions import BasePermission
class IsOwner(BasePermission):
def has_object_permission(self, request, view, obj):
if request.user == obj.owner:
return True
else:
return False
from rest_framework.permissions import BasePermission
class NoDelete(BasePermission):
def has_permission(self, request, view):
if request.method == 'DELETE':
return False
return True
from django.db import models
from rest_framework import permissions
from backend.models.tools import UsefullLinksField
from rest_framework import permissions, serializers
from backend.models.university import University
from django.contrib.postgres.fields import JSONField
from backend.models.user import UserRestrictedModule, UserRestrictedModuleSerializer, UserRestrictedModuleViewSet
from backend.models.my_model import MyModel, MyModelSerializer, MyModelViewSet
from django.contrib.auth.models import User
from backend.models.tools import NoDelete, IsOwner
class UserData(UserRestrictedModule):
contact_info = UsefullLinksField(null=True)
class UserData(MyModel):
owner = models.OneToOneField(User, on_delete=models.CASCADE)
contact_info = JSONField(null=True, default={})
contact_info_is_public = models.BooleanField(default=False)
config = JSONField(null=True, default={})
black_list = models.ManyToManyField(
University, related_name='+', blank=True)
other_data = JSONField(null=True, default={})
class UserDataSerializer(UserRestrictedModuleSerializer):
class UserDataSerializer(MyModelSerializer):
owner = serializers.CharField(read_only=True)
def my_pre_save(self):
user = self.get_user_in_request()
self.override_validated_data({'owner': user})
class Meta:
model = UserData
fields = '__all__'
NEVER_MODERATE = True
class UserDataViewSet(UserRestrictedModuleViewSet):
permission_classes = (permissions.IsAdminUser,) # TODO change
queryset = UserData.objects.all() # pylint: disable=E1101
class UserDataViewSet(MyModelViewSet):
permission_classes = (
permissions.IsAuthenticated,
NoDelete,
IsOwner
)
serializer_class = UserDataSerializer
# TODO change to require a login not display all
def get_queryset(self):
return UserData.objects.filter(owner=self.request.user) # pylint: disable=E1101
......@@ -130,7 +130,7 @@ router.register(r'university_more/previous_departure_feedback',
PreviousDepartureFeedbackViewSet)
router.register(r'user/recommendation', RecommendationViewSet)
router.register(r'user/recommendations_list', RecommendationListViewSet)
router.register(r'user/data', UserDataViewSet)
router.register(r'user/data', UserDataViewSet, "user_data-detail")
router.register(r'pending_moderation', PendingModerationViewSet)
......
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