Skip to content
Snippets Groups Projects
Commit b5cdd424 authored by Bodor Máté's avatar Bodor Máté
Browse files

refactor validators

parent 9bdb3816
No related branches found
No related tags found
No related merge requests found
...@@ -49,12 +49,6 @@ class ProfileSerializer(serializers.ModelSerializer): ...@@ -49,12 +49,6 @@ class ProfileSerializer(serializers.ModelSerializer):
if value is False and modifier.role != "Staff": if value is False and modifier.role != "Staff":
raise serializers.ValidationError("You cannot join without signed") raise serializers.ValidationError("You cannot join without signed")
return value return value
#
# def validate_id(self, value):
# modifier= CurrentUserMiddleware.get_current_user_profile()
# if value != modifier.id and modifier.role != "Staff":
# raise serializers.ValidationError("You don't have permission")
# return value
def update(self, instance, validated_data): def update(self, instance, validated_data):
new_role = validated_data.get('role', instance.role) new_role = validated_data.get('role', instance.role)
......
...@@ -17,11 +17,12 @@ class DocumentSerializer(serializers.ModelSerializer): ...@@ -17,11 +17,12 @@ class DocumentSerializer(serializers.ModelSerializer):
def get_uploaded_by_name(self, obj): def get_uploaded_by_name(self, obj):
return obj.uploaded_by.full_name return obj.uploaded_by.full_name
def validate(self, data): def validate_solution(self, value):
profile = CurrentUserMiddleware.get_current_user_profile() profile = CurrentUserMiddleware.get_current_user_profile()
if data['solution'] not in profile.solution.all(): if value not in profile.solution.all():
raise serializers.ValidationError('You dont have permission!') raise serializers.ValidationError('You dont have permission!')
count = models.Document.objects.filter(uploaded_by=profile, solution=data['solution']).count() count = models.Document.objects.filter(uploaded_by=profile, solution=value).count()
if count >= _max_count: if count >= _max_count:
raise serializers.ValidationError('You cant upload more than ' + str(_max_count) + ' document to one solution!') raise serializers.ValidationError('You cant upload more than ' + str(_max_count) +
return data ' document to one solution!')
return value
...@@ -44,11 +44,23 @@ class SolutionSerializer(serializers.ModelSerializer): ...@@ -44,11 +44,23 @@ class SolutionSerializer(serializers.ModelSerializer):
raise serializers.ValidationError('You late.') raise serializers.ValidationError('You late.')
return value return value
def validate(self, data): def validate_accepted(self, value):
profile = CurrentUserMiddleware.get_current_user_profile() profile = CurrentUserMiddleware.get_current_user_profile()
if profile.role != 'Staff' and (data['accepted'] or data['corrected'] or data['note'] != ''): if profile.role != 'Staff' and value:
raise serializers.ValidationError("You don't have permission!") raise serializers.ValidationError("You don't have permission to modify accepted!")
return data return value
def validate_corrected(self, value):
profile = CurrentUserMiddleware.get_current_user_profile()
if profile.role != 'Staff' and value:
raise serializers.ValidationError("You don't have permission to modify corrected!")
return value
def validate_note(self, value):
profile = CurrentUserMiddleware.get_current_user_profile()
if profile.role != 'Staff' and value != '':
raise serializers.ValidationError("You don't have permission to create note!")
return value
def update(self, instance, validated_data): def update(self, instance, validated_data):
if instance.corrected == False and validated_data.get('corrected', instance.corrected) == True: if instance.corrected == False and validated_data.get('corrected', instance.corrected) == True:
......
...@@ -19,9 +19,10 @@ class StaffEventSerializer(serializers.ModelSerializer): ...@@ -19,9 +19,10 @@ class StaffEventSerializer(serializers.ModelSerializer):
return obj.visitors.all().count() return obj.visitors.all().count()
def validate(self, data): def validate(self, data):
for i in data['absent']: if data['absent'] is not None and data['visitors'] is not None:
if i in data['visitors']: for i in data['absent']:
raise serializers.ValidationError('You cant add a student to absent and visitor in the same time.') if i in data['visitors']:
raise serializers.ValidationError('You cant add a student to absent and visitor in the same time.')
return data return data
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment