Exemple #1
0
    def post(self, request, *args, **kwargs):

        form = self.form_class(request.POST)

        if form.is_valid():
            user = form.save(commit=False)
            user.is_active = False
            user.save()

            current_site = get_current_site(request)
            site_name = current_site.name
            domain = current_site.domain
            mail_subject = 'Account Activation Token'
            context = {
                'user': user,
                'domain': domain,
                'site_name': site_name,
                'uid': urlsafe_base64_encode(force_bytes(user.pk)),
                'token': account_activation_token.make_token(user),
            }
            message = loader.render_to_string(
                'registration/activation_email.html', context)
            to_email = form.cleaned_data.get('email')
            email = EmailMessage(mail_subject, message, to=[to_email])
            email.send()

            return TemplateResponse(request,
                                    'registration/activate_email_prompt.html',
                                    {'page_title': 'Email Confirmation'})
        else:
            form = self.form_class()
            return TemplateResponse(request, self.template_name,
                                    {'form': form})
Exemple #2
0
def signup(request):
    if request.method == 'POST':
        form = SignUpForm(request.POST)
        if form.is_valid():
            user = form.save()
            user.is_active = False
            user.save()
            # auth_login(request, user)
            # return redirect('home') # после этих двух строк начинается подтверждения мыла
            current_site = get_current_site(request)
            message = render_to_string('acc_active_email.html', {
                'user': user,
                'domain': current_site.domain,
                'uid': urlsafe_base64_encode(force_bytes(user.pk)),
                'token': account_activation_token.make_token(user),
            })
            mail_subject = 'Activate your account.'
            to_email = form.cleaned_data.get('email')
            send_mail(mail_subject, message, settings.EMAIL_HOST_USER, [to_email])
            # email.send()
            return HttpResponse('Activate your account.')

    else:
        form = SignUpForm()

    return render(request, 'signup.html', {'form': form})
Exemple #3
0
    def test_with_invalid_token(self):
        uidb64 = urlsafe_base64_encode(force_bytes(3))  # user with id = 3 DoesNotExist
        token = account_activation_token.make_token(self.user)
        url = reverse('accounts:activate', args=[uidb64, token])

        response = self.client.get(url, follow=True)
        message = list(response.context.get('messages'))[0]
        self.assertRedirects(response, reverse('accounts:login'), 302)
        self.assertEquals(message.tags, 'error')
        self.assertEquals(message.message, 'Activation link is invalid!')
Exemple #4
0
def register(request):
    if request.method == 'POST':
        first_name = request.POST['first_name']
        last_name = request.POST['last_name']
        username = request.POST['username']
        Email = request.POST['email']
        password = request.POST['password']
        password2 = request.POST['password2']

        if password == password2:
            if User.objects.filter(username=username).exists():
                print("username taken")
                messages.info(request, 'username taken')
                return redirect('accounts:register')
            elif User.objects.filter(email=Email).exists():
                print("email taken")
                messages.info(request, 'email taken')
                return redirect('accounts:register')
            else:
                user = User.objects.create_user(first_name=first_name,
                                                last_name=last_name,
                                                username=username,
                                                email=Email,
                                                password=password)

                user.is_active = False  # Deactivate account till it is confirmed
                user.save()
                UserProfile.objects.create(user=user)
                print('user creater')

                current_site = get_current_site(request)
                message = render_to_string(
                    'accounts/acc_active_email.html', {
                        'user': user,
                        'domain': current_site.domain,
                        'uid': urlsafe_base64_encode(force_bytes(user.pk)),
                        'token': account_activation_token.make_token(user),
                    })
                mail_subject = 'Activate your Meddeling Account.'
                to_email = request.POST['email']
                email = EmailMessage(mail_subject, message, to=[to_email])
                email.send()
                messages.info(
                    request,
                    'Please confirm your email address to complete the registration.'
                )
                return render(request, 'accounts/register.html')
            # return render(request, 'acc_active_sent.html')
        else:
            messages.info(request, 'Password not matching')
            return redirect('accounts:register')
    else:
        return render(request, 'accounts/register.html')
Exemple #5
0
def send_verification_email(sender, instance, created, **kwargs):
    if created:
        subject = 'Activate your e-mail'
        message = render_to_string(
            'account/activate_email.html', {
                'user': instance,
                'domain': 'http://localhost:8000',
                'uid': force_text(
                    urlsafe_base64_encode(force_bytes(instance.pk))),
                'token': account_activation_token.make_token(instance)
            })
        instance.email_user(subject, message)
Exemple #6
0
    def test_test_with_valid_token(self):
        uidb64 = urlsafe_base64_encode(force_bytes(self.user.id))
        token = account_activation_token.make_token(self.user)

        url = reverse('accounts:activate', args=[uidb64, token])

        response = self.client.get(url, follow=True)
        message = list(response.context.get('messages'))[0]
        self.assertRedirects(response, reverse('accounts:login'), 302)
        self.assertEquals(message.tags, 'success')
        self.assertEquals(message.message, 'Thank you for confirming your email. You can now login.')

        user = User.objects.get(id=1)

        self.assertTrue(user.is_active)     # user status updated to active
Exemple #7
0
 def form_valid(self, form):
     user = form.save(commit=False)
     user.set_password(form.cleaned_data['password'])
     user.is_director = True
     user.is_active = False
     user.save()
     current_site = get_current_site(self.request)
     subject = 'Activate your account'
     message = render_to_string(
         'registration/account_activation_email.html', {
             'user': user,
             'domain': current_site.domain,
             'uid': urlsafe_base64_encode(force_bytes(user.pk)),
             'token': account_activation_token.make_token(user),
         })
     user.email_user(subject=subject, message=message)
     return super().form_valid(form)
Exemple #8
0
    def test_post_method_with_valid_data(self):
        data = {'email': '*****@*****.**', 'password1': 'password1', 'password2': 'password1'}
        response = self.client.post(self.url, data, follow=True)
        user = response.context['user']
        uid = urlsafe_base64_encode(force_bytes(user.id))
        token = account_activation_token.make_token(user)

        self.assertEqual(len(mail.outbox), 1)
        self.assertEqual(mail.outbox[0].subject, 'Activate your account')
        self.assertEqual(mail.outbox[0].from_email, '<youremail>')      # change to your email <youremail>
        self.assertEqual(mail.outbox[0].to, ['*****@*****.**']) # self.user.email
        self.assertEqual(mail.outbox[0].body, "\nHi ,\nPlease click on the link to confirm your registration,\n"
                                   "http://testserver/accounts/activate/" + str(uid) + "/" + str(token) + "/\n"
                         )

        message = list(response.context.get('messages'))[0]
        self.assertEquals(message.tags, 'success')
        self.assertEquals(message.message, 'We have sent you an activation link in your email. Please confirm '
                                           'your email to continue')
Exemple #9
0
    def post(self, request):
        context = {'data': request.POST, 'has_error': False}

        first_name = request.POST.get('first_name')
        last_name = request.POST.get('last_name')
        email = request.POST.get('email')
        password = request.POST.get('password')
        password2 = request.POST.get('password2')
        if len(password) < 6:
            messages.add_message(
                request, messages.ERROR,
                'passwords should be atleast 6 characters long')
            context['has_error'] = True
        if password != password2:
            messages.add_message(request, messages.ERROR,
                                 'passwords dont match')
            context['has_error'] = True

        if not validate_email(email):
            messages.add_message(request, messages.ERROR,
                                 'Please provide a valid email')
            context['has_error'] = True

        try:
            if User.objects.get(email=email):
                messages.add_message(
                    request, messages.ERROR,
                    'Email already registered by another user')
                context['has_error'] = True

        except Exception as identifier:
            pass

        if context['has_error']:
            return render(request,
                          'accounts/partials/register.html',
                          context,
                          status=400)

        user = User.objects.create_user(email=email, password=password)
        user.set_password(password)
        user.last_name = last_name
        user.is_active = False
        user.save()

        current_site = get_current_site(request)
        subject = 'Active your Account'
        message = render_to_string(
            'accounts/partials/account_activation_email.html', {
                'user': user,
                'domain': current_site.domain,
                'uid': urlsafe_base64_encode(force_bytes(user.pk)),
                'token': account_activation_token.make_token(user)
            })

        to_email = request.POST.get('email')
        email = EmailMessage(subject, message, to=[to_email])

        if not context['has_error']:
            email.send()
            messages.add_message(
                request, messages.SUCCESS,
                'We sent you an email to verify your account')
            #return render(request, 'accounts/partials/account_activation_sent.html')

        return redirect('accounts:login')