Skip to content

Commit f3f42c3

Browse files
refactor: drop redundant validate for child classes
1 parent 9355de7 commit f3f42c3

File tree

1 file changed

+11
-31
lines changed

1 file changed

+11
-31
lines changed

eox_core/api/support/v1/serializers.py

Lines changed: 11 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@ class WrittableEdxappRemoveUserSerializer(serializers.Serializer):
3131
class WrittableEdxappUserSerializer(serializers.Serializer):
3232
"""
3333
Base serializer for updating username or email of an edxapp user.
34+
35+
When a username/email update is being made the following validations are performed:
36+
- The new username/email is not already taken by another user.
37+
- The user is not staff or superuser.
38+
- The user has just one signup source.
3439
"""
3540

3641
def validate_conflicts(self, attrs):
@@ -58,15 +63,14 @@ def validate_role_restrictions(self, attrs):
5863

5964
return attrs
6065

61-
def validate_required_fields(self, required_fields):
66+
def validate(self, attrs):
6267
"""
63-
Validates that at least one field to update is provided.
68+
Base validate method to be used by child serializers to validate common restrictions.
6469
"""
65-
if not required_fields:
66-
raise serializers.ValidationError(
67-
{"detail": "At least one field to update must be provided."}
68-
)
69-
return required_fields
70+
self.validate_conflicts(attrs)
71+
self.validate_role_restrictions(attrs)
72+
73+
return attrs
7074

7175

7276
class WrittableEdxappUsernameSerializer(WrittableEdxappUserSerializer):
@@ -81,18 +85,6 @@ class WrittableEdxappUsernameSerializer(WrittableEdxappUserSerializer):
8185
allow_null=False,
8286
)
8387

84-
def validate(self, attrs):
85-
"""
86-
Validates that the new username is provided and passes all checks.
87-
"""
88-
if not attrs.get("new_username"):
89-
raise serializers.ValidationError({"detail": "You must provide a new username."})
90-
91-
self.validate_conflicts(attrs)
92-
self.validate_role_restrictions(attrs)
93-
94-
return attrs
95-
9688
def update(self, instance, validated_data):
9789
"""
9890
Updates the username of the edxapp User.
@@ -113,18 +105,6 @@ class WrittableEdxappEmailSerializer(WrittableEdxappUserSerializer):
113105
allow_null=False,
114106
)
115107

116-
def validate(self, attrs):
117-
"""
118-
Validates that the new email is provided and passes all checks.
119-
"""
120-
if not attrs.get("new_email"):
121-
raise serializers.ValidationError({"detail": "You must provide a new email."})
122-
123-
self.validate_conflicts(attrs)
124-
self.validate_role_restrictions(attrs)
125-
126-
return attrs
127-
128108
def update(self, instance, validated_data):
129109
"""
130110
Updates the email of the edxapp User.

0 commit comments

Comments
 (0)