Example #1
0
 def post(self, request):
     form = SignUpForm(request.POST)
     if form.is_valid():
         user = form.save(commit=False)
         user.is_active = False
         user.save()
         current_site = get_current_site(request)
         mail_subject = 'Activate your blog account.'
         message = render_to_string(
             'registration/acc_active_email.html', {
                 'user': user,
                 'domain': current_site.domain,
                 'uid': urlsafe_base64_encode(force_bytes(
                     user.pk)).decode(),
                 'token': account_activation_token.make_token(user),
             })
         to_email = form.cleaned_data.get('email')
         email = EmailMessage(mail_subject, message, to=[to_email])
         email.send()
         # TODO Redirect or make template
         return HttpResponse(
             'Please confirm your email address to complete the registration'
         )
     else:
         return render(request, 'registration/signup.html', {'form': form})
Example #2
0
 def perform_create(self, serializer):
     if CONFIG['security']['email_verification']:
         token = account_activation_token.make_token()
         expiration_date = datetime.date.today() + datetime.timedelta(
             days=CONFIG['security']['verification_key_expire'])
         instance = serializer.save(is_active=False)
         main_models.UserVerification.objects.create(
             user=instance, token=token, expiration_date=expiration_date)
     else:
         serializer.save(is_active=True)
Example #3
0
    def create(self, request, *args, **kwargs):
        data = self.request.data

        try:
            user = User.objects.get(email=data['email'])
        except User.DoesNotExist:
            return Response({'message': 'User with this email does not exist'}, status=status.HTTP_404_NOT_FOUND)

        if user.is_active:
            return Response({'message': 'User already active'}, status=status.HTTP_400_BAD_REQUEST)

        token = account_activation_token.make_token()
        expiration_date = datetime.date.today() + datetime.timedelta(days=CONFIG['security']['verification_key_expire'])
        user.verification.create(
            token=token,
            expiration_date=expiration_date,
            created_date=datetime.date.today()
        )
        return Response({'message': 'Verification was sent'}, status=status.HTTP_200_OK)