Skip to content

Commit

Permalink
refactor(api): streamline authenticated action views
Browse files Browse the repository at this point in the history
  • Loading branch information
peterthomassen committed Jan 19, 2022
1 parent bc6c5c9 commit 9022c0e
Showing 1 changed file with 25 additions and 21 deletions.
46 changes: 25 additions & 21 deletions api/desecapi/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -633,6 +633,21 @@ class AuthenticatedActionView(generics.GenericAPIView):
html_url = None # Redirect GET requests to this webapp GUI URL
http_method_names = ['get', 'post'] # GET is for redirect only
renderer_classes = [JSONRenderer, StaticHTMLRenderer]
_authenticated_action = None

@property
def authenticated_action(self):
if self._authenticated_action is None:
serializer = self.get_serializer(data=self.request.data)
serializer.is_valid(raise_exception=True)
try:
self._authenticated_action = serializer.Meta.model(**serializer.validated_data)
except ValueError: # this happens when state cannot be verified
ex = ValidationError('This action cannot be carried out because another operation has been performed, '
'invalidating this one. (Are you trying to perform this action twice?)')
ex.status_code = status.HTTP_409_CONFLICT
raise ex
return self._authenticated_action

@property
def authentication_classes(self):
Expand Down Expand Up @@ -665,29 +680,17 @@ def get(self, request, *args, **kwargs):
raise NotAcceptable

def post(self, request, *args, **kwargs):
serializer = self.get_serializer(data=request.data)
serializer.is_valid(raise_exception=True)
try:
self.authenticated_action = serializer.Meta.model(**serializer.validated_data)
except ValueError: # this happens when state cannot be verified
ex = ValidationError('This action cannot be carried out because another operation has been performed, '
'invalidating this one. (Are you trying to perform this action twice?)')
ex.status_code = status.HTTP_409_CONFLICT
raise ex

self.authenticated_action.act()
return self.finalize()

def finalize(self):
raise NotImplementedError
return Response(status=status.HTTP_202_ACCEPTED)


class AuthenticatedActivateUserActionView(AuthenticatedActionView):
html_url = '/confirm/activate-account/{code}/'
permission_classes = () # don't require that user is activated already
serializer_class = serializers.AuthenticatedActivateUserActionSerializer

def finalize(self):
def post(self, request, *args, **kwargs):
super().post(request, *args, **kwargs)
if not self.authenticated_action.domain:
return self._finalize_without_domain()
else:
Expand Down Expand Up @@ -740,7 +743,8 @@ class AuthenticatedChangeEmailUserActionView(AuthenticatedActionView):
html_url = '/confirm/change-email/{code}/'
serializer_class = serializers.AuthenticatedChangeEmailUserActionSerializer

def finalize(self):
def post(self, request, *args, **kwargs):
super().post(request, *args, **kwargs)
return Response({
'detail': f'Success! Your email address has been changed to {self.authenticated_action.user.email}.'
})
Expand All @@ -750,7 +754,8 @@ class AuthenticatedResetPasswordUserActionView(AuthenticatedActionView):
html_url = '/confirm/reset-password/{code}/'
serializer_class = serializers.AuthenticatedResetPasswordUserActionSerializer

def finalize(self):
def post(self, request, *args, **kwargs):
super().post(request, *args, **kwargs)
return Response({'detail': 'Success! Your password has been changed.'})


Expand All @@ -761,17 +766,16 @@ class AuthenticatedDeleteUserActionView(AuthenticatedActionView):
def post(self, request, *args, **kwargs):
if self.request.user.domains.exists():
return AccountDeleteView.response_still_has_domains
return super().post(request, *args, **kwargs)

def finalize(self):
super().post(request, *args, **kwargs)
return Response({'detail': 'All your data has been deleted. Bye bye, see you soon! <3'})


class AuthenticatedRenewDomainBasicUserActionView(AuthenticatedActionView):
html_url = '/confirm/renew-domain/{code}/'
serializer_class = serializers.AuthenticatedRenewDomainBasicUserActionSerializer

def finalize(self):
def post(self, request, *args, **kwargs):
super().post(request, *args, **kwargs)
return Response({'detail': f'We recorded that your domain {self.authenticated_action.domain} is still in use.'})


Expand Down

0 comments on commit 9022c0e

Please sign in to comment.