|
1 | 1 | from django.http import HttpRequest
|
2 |
| -from rest_framework import serializers |
3 |
| -from rest_framework.exceptions import ValidationError |
| 2 | +from django.utils.translation import gettext_lazy as _ |
| 3 | +from rest_framework import serializers, status |
| 4 | +from rest_framework.exceptions import APIException, ValidationError |
4 | 5 | from rest_framework.permissions import IsAuthenticated
|
5 | 6 | from rest_framework.response import Response
|
6 | 7 | from rest_framework.views import APIView
|
7 | 8 |
|
8 | 9 | from thunderstore.api.utils import conditional_swagger_auto_schema
|
9 |
| -from thunderstore.social.views import DeleteAccountForm |
| 10 | +from thunderstore.social.views import ( |
| 11 | + DeleteAccountForm, |
| 12 | + LinkedAccountDisconnectExecption, |
| 13 | + LinkedAccountDisconnectForm, |
| 14 | +) |
| 15 | + |
| 16 | + |
| 17 | +class CyberstormException(APIException): |
| 18 | + status_code = status.HTTP_400_BAD_REQUEST |
| 19 | + default_detail = _("Issue occured when trying to process action") |
| 20 | + default_code = "error" |
10 | 21 |
|
11 | 22 |
|
12 | 23 | class CyberstormUserDeleteRequestSerialiazer(serializers.Serializer):
|
@@ -38,3 +49,47 @@ def post(self, request: HttpRequest):
|
38 | 49 | return Response()
|
39 | 50 | else:
|
40 | 51 | raise ValidationError(form.errors)
|
| 52 | + |
| 53 | + |
| 54 | +class CyberstormUserDisconnectProviderRequestSerialiazer(serializers.Serializer): |
| 55 | + provider = serializers.CharField() |
| 56 | + |
| 57 | + |
| 58 | +class CyberstormUserDisconnectProviderResponseSerialiazer(serializers.Serializer): |
| 59 | + username = serializers.CharField() |
| 60 | + provider = serializers.CharField() |
| 61 | + |
| 62 | + |
| 63 | +class UserLinkedAccountDisconnectAPIView(APIView): |
| 64 | + permission_classes = [IsAuthenticated] |
| 65 | + |
| 66 | + @conditional_swagger_auto_schema( |
| 67 | + request_body=CyberstormUserDisconnectProviderRequestSerialiazer, |
| 68 | + responses={200: CyberstormUserDisconnectProviderResponseSerialiazer}, |
| 69 | + operation_id="cyberstorm.current-user.linked-account-disconnect", |
| 70 | + tags=["cyberstorm"], |
| 71 | + ) |
| 72 | + def post(self, request: HttpRequest): |
| 73 | + serializer = CyberstormUserDisconnectProviderRequestSerialiazer( |
| 74 | + data=request.data |
| 75 | + ) |
| 76 | + serializer.is_valid(raise_exception=True) |
| 77 | + form = LinkedAccountDisconnectForm( |
| 78 | + user=request.user, |
| 79 | + data=serializer.validated_data, |
| 80 | + ) |
| 81 | + if form.is_valid(): |
| 82 | + try: |
| 83 | + form.disconnect_account(with_raise=True) |
| 84 | + except LinkedAccountDisconnectExecption as e: |
| 85 | + raise CyberstormException(detail=e) |
| 86 | + return Response( |
| 87 | + CyberstormUserDisconnectProviderResponseSerialiazer( |
| 88 | + { |
| 89 | + "username": request.user.username, |
| 90 | + "provider": serializer.validated_data["provider"], |
| 91 | + } |
| 92 | + ).data |
| 93 | + ) |
| 94 | + else: |
| 95 | + raise ValidationError(form.errors) |
0 commit comments