2626 OauthApplicationSerializer ,
2727 WrittableEdxappRemoveUserSerializer ,
2828 WrittableEdxappUsernameSerializer ,
29+ WrittableEdxappEmailSerializer ,
2930)
3031from eox_core .api .v1 .serializers import EdxappUserReadOnlySerializer
3132from eox_core .api .v1 .views import UserQueryMixin
@@ -95,42 +96,35 @@ def delete(self, request, *args, **kwargs): # pylint: disable=too-many-locals
9596 return Response (message , status = status )
9697
9798
98- class EdxappReplaceUsername (UserQueryMixin , APIView ):
99+ class EdxappUserUpdateBase (UserQueryMixin , APIView ):
99100 """
100- Handles the replacement of the username.
101+ Base view for updating edxapp user attributes (username, email, etc.).
102+ Provides common functionality for user updates with forum synchronization.
101103 """
102104
103105 authentication_classes = (BearerAuthentication , SessionAuthentication , JwtAuthentication )
104106 permission_classes = (EoxCoreSupportAPIPermission ,)
105107 renderer_classes = (JSONRenderer , BrowsableAPIRenderer )
106108
107- @audit_drf_api (action = "Update an Edxapp user's Username." , method_name = 'eox_core_api_method' )
108- def patch (self , request , * args , ** kwargs ):
109+ def get_serializer_class (self ):
109110 """
110- Allows to safely update an Edxapp user's Username along with the
111- forum associated User.
112-
113- For now users that have different signup sources cannot be updated.
114-
115- For example:
116-
117- **Requests**:
118- PATCH <domain>/eox-core/support-api/v1/replace-username/
111+ Returns the serializer class to use.
112+ Must be overridden by subclasses.
113+ """
114+ raise NotImplementedError ("Subclasses must implement get_serializer_class()" )
119115
120- **Request body**
121- {
122- "new_username": "new username"
123- }
116+ def patch (self , request , * args , ** kwargs ):
117+ """
118+ Updates an edxapp user's attribute and synchronizes with the forum.
124119
125- **Response values**
126- User serialized.
120+ For users with different signup sources, updates are not allowed.
127121 """
128122 query = self .get_user_query (request )
129123 user = get_edxapp_user (** query )
130124 data = request .data
131125
132126 with transaction .atomic ():
133- serializer = WrittableEdxappUsernameSerializer (user , data = data )
127+ serializer = self . get_serializer_class () (user , data = data )
134128 serializer .is_valid (raise_exception = True )
135129 serializer .save ()
136130
@@ -149,6 +143,74 @@ def patch(self, request, *args, **kwargs):
149143 return Response (serialized_user .data )
150144
151145
146+ class EdxappReplaceUsername (EdxappUserUpdateBase ):
147+ """
148+ Handles the replacement of the username.
149+ """
150+
151+ def get_serializer_class (self ):
152+ """
153+ Returns the serializer class to use.
154+ """
155+ return WrittableEdxappUsernameSerializer
156+
157+ @audit_drf_api (action = "Update an Edxapp user's Username." , method_name = 'eox_core_api_method' )
158+ def patch (self , request , * args , ** kwargs ):
159+ """
160+ Allows to safely update an Edxapp user's Username along with the
161+ forum associated User.
162+
163+ For now users that have different signup sources cannot be updated.
164+
165+ For example:
166+
167+ **Requests**:
168+ PATCH <domain>/eox-core/support-api/v1/replace-username/?username=old_username
169+
170+ **Request body**
171+ {
172+ "new_username": "new username"
173+ }
174+
175+ **Response values**
176+ User serialized.
177+ """
178+ return super ().patch (request , * args , ** kwargs )
179+
180+
181+ class EdxappReplaceEmail (EdxappUserUpdateBase ):
182+ """
183+ Handles the replacement of the email.
184+ """
185+
186+ def get_serializer_class (self ):
187+ """Returns the serializer class to use."""
188+ return WrittableEdxappEmailSerializer
189+
190+ @audit_drf_api (action = "Update an Edxapp user's Email." , method_name = 'eox_core_api_method' )
191+ def patch (self , request , * args , ** kwargs ):
192+ """
193+ Allows to safely update an Edxapp user's Email along with the
194+ forum associated User.
195+
196+ For now users that have different signup sources cannot be updated.
197+
198+ For example:
199+
200+ **Requests**:
201+ PATCH <domain>/eox-core/support-api/v1/replace-email/?email=old_email
202+
203+ **Request body**
204+ {
205+ "new_email": "new email"
206+ }
207+
208+ **Response values**
209+ User serialized.
210+ """
211+ return super ().patch (request , * args , ** kwargs )
212+
213+
152214class OauthApplicationAPIView (UserQueryMixin , APIView ):
153215 """
154216 Handles requests related to the
0 commit comments