Skip to content

Instantly share code, notes, and snippets.

@iMerica
Last active July 27, 2023 17:20
Show Gist options
  • Save iMerica/a6a7efd80d49d6de82c7928140676957 to your computer and use it in GitHub Desktop.
Save iMerica/a6a7efd80d49d6de82c7928140676957 to your computer and use it in GitHub Desktop.

Revisions

  1. iMerica revised this gist Jul 9, 2018. 1 changed file with 4 additions and 2 deletions.
    6 changes: 4 additions & 2 deletions views.py
    Original file line number Diff line number Diff line change
    @@ -11,7 +11,8 @@ class ConfirmEmailView(APIView):
    def get(self, *args, **kwargs):
    self.object = confirmation = self.get_object()
    confirmation.confirm(self.request)
    return Response({'detail': 'Successfully verified'})
    # A React Router Route will handle the failure scenario
    return HttpResponseRedirect('/login/success/')

    def get_object(self, queryset=None):
    key = self.kwargs['key']
    @@ -22,7 +23,8 @@ def get_object(self, queryset=None):
    try:
    email_confirmation = queryset.get(key=key.lower())
    except EmailConfirmation.DoesNotExist:
    raise NotFound({'detail': 'Failed to verify key'}, code=400)
    # A React Router Route will handle the failure scenario
    return HttpResponseRedirect('/login/failure/')
    return email_confirmation

    def get_queryset(self):
  2. iMerica renamed this gist Jul 9, 2018. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  3. iMerica revised this gist Jul 9, 2018. No changes.
  4. iMerica created this gist Jul 9, 2018.
    4 changes: 4 additions & 0 deletions urls.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,4 @@

    urlpatterns = [
    url(r'^rest-auth/registration/account-confirm-email/(?P<key>[-:\w]+)/$', ConfirmEmailView.as_view(), name='account_confirm_email'),
    ]
    31 changes: 31 additions & 0 deletions view.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,31 @@
    from rest_framework.exceptions import NotFound
    from rest_framework.views import APIView
    from rest_framework.response import Response
    from rest_framework.permissions import AllowAny
    from allauth.account.models import EmailConfirmation, EmailConfirmationHMAC


    class ConfirmEmailView(APIView):
    permission_classes = [AllowAny]

    def get(self, *args, **kwargs):
    self.object = confirmation = self.get_object()
    confirmation.confirm(self.request)
    return Response({'detail': 'Successfully verified'})

    def get_object(self, queryset=None):
    key = self.kwargs['key']
    email_confirmation = EmailConfirmationHMAC.from_key(key)
    if not email_confirmation:
    if queryset is None:
    queryset = self.get_queryset()
    try:
    email_confirmation = queryset.get(key=key.lower())
    except EmailConfirmation.DoesNotExist:
    raise NotFound({'detail': 'Failed to verify key'}, code=400)
    return email_confirmation

    def get_queryset(self):
    qs = EmailConfirmation.objects.all_valid()
    qs = qs.select_related("email_address__user")
    return qs