def post(self, request, *args, **kwargs):
        try:
            # get the email inputted
            email_inputted = request.POST.get("email")

            # query the database if that email exists
            user = User.objects.get(email=email_inputted)

            # generate a recovery hash for that user
            user_hash = UserHasher.gen_hash(user)
            user_hash_url = request.build_absolute_uri(
                reverse('reset_password', kwargs={'user_hash': user_hash}))
            hash_email_context = RequestContext(
                request, {'user_hash_url': user_hash_url})

            # compose the email
            email_compose = SendGrid.compose(
                sender='Codango <{}>'.format(CODANGO_EMAIL),
                recipient=user.email,
                subject='Codango: Password Recovery',
                text=loader.get_template(
                    'account/forgot-password-email.txt'
                ).render(hash_email_context),
                html=loader.get_template(
                    'account/forgot-password-email.html'
                ).render(hash_email_context),
            )

            # send email
            email_response = SendGrid.send(email_compose)

            # inform the user if mail sent was successful
            context = {
                "email_status": email_response
            }
            return render(
                request,
                'account/forgot-password-status.html',
                context
            )

        except ObjectDoesNotExist:
            messages.add_message(
                request, messages.ERROR,
                'The email specified does not belong to any valid user.')
            return render(request, 'account/forgot-password.html')
    def post(self, request, *args, **kwargs):
        # get email data from form
        form = self.form_class(request.POST)
        if form.is_valid():
            name = request.POST['name']
            email = request.POST['email']
            subject = request.POST['subject']
            message = request.POST['message']

            # compose the email
            email_compose = SendGrid.compose(
                sender='{0} <{1}>'.format(name, email),
                recipient=ADMIN_EMAIL,
                subject=subject,
                text=message,
                html=None
            )

            # send email
            response = SendGrid.send(email_compose)

            # inform the user if mail sent was successful or not
            if response == 200:
                messages.add_message(
                    request, messages.SUCCESS, 'Message sent successfully!')
                return redirect(
                    '/contact-us',
                    context_instance=RequestContext(request)
                )
            else:
                messages.add_message(
                    request, messages.ERROR,
                    'Message failed to send, please try again later')
                return redirect(
                    '/contact-us',
                    context_instance=RequestContext(request)
                )
        else:
            context = super(ContactUsView, self).get_context_data(**kwargs)
            context['contactusform'] = form
            return render(request, self.template_name, context)
Exemple #3
0
    def post(self, request):
        """Handles POST requests to 'register' named route.

        Raw data posted from form is received here,bound to form
        as dictionary and sent to unrendered django form for validation.

        Returns:
            A HttpResponse with a register template, otherwise, redirects to the
            login page.
        """
        usersignupform = UserSignupForm(request.POST)
        # get the user email address
        email = request.POST.get('email')
        signup_new_user = User.objects.filter(email__exact=email)
        if signup_new_user:
            args = {}
            args.update(csrf(request))
            mssg = "Email already taken please signup with another email"
            messages.add_message(request, messages.INFO, mssg)
            return render(request, 'authentication/register.html', args)

        if usersignupform.is_valid():
            usersignupform.save()
            new_user = User.objects.get(email__exact=email)

            # generate an activation hash url for new user account
            activation_hash = Hasher.gen_hash(new_user)
            activation_hash_url = request.build_absolute_uri(
                reverse(
                    'activate_account',
                    kwargs={'activation_hash': activation_hash},
                )
            )
            # compose the email
            activation_email_context = RequestContext(
                request,
                {'activation_hash_url': activation_hash_url,
                 'username': new_user.username,
                },
            )
            activation_email = SendGrid.compose(
                sender='Troupon <*****@*****.**>',
                recipient=new_user.email,
                subject='Troupon: ACTIVATE ACCOUNT',
                html=loader.get_template(
                    'authentication/activate_account_email.html'
                ).render(activation_email_context),
                text=loader.get_template(
                    'authentication/activate_account_email.txt'
                ).render(activation_email_context),
            )
            # send mail to new_user
            activation_status = SendGrid.send(activation_email)
            # inform the user of activation mail sent
            if activation_status == 200:
                new_user_email = new_user.email
                messages.add_message(
                    request, messages.INFO, new_user_email)
            return redirect(reverse('confirm_registration'))

        else:
            args = {}
            args.update(csrf(request))
            return render(request, 'authentication/register.html', {'form': usersignupform})
Exemple #4
0
    def post(self, request, *args, **kwargs):
        """Handles the POST request to the 'account_forgot_password' named route.

        Args: request.
        Returns: A HttpResponse with a forgot_password_recovery_status template
                 otherwise, return forgot_password template.
        """
        email_form = EmailForm(request.POST, auto_id=True)
        if email_form.is_valid():
            try:
                # get the account for that email if it exists:
                input_email = email_form.cleaned_data.get('email')
                registered_user = User.objects.get(email__exact=input_email)

                # generate a recovery hash url for that account:
                recovery_hash = Hasher.gen_hash(registered_user)
                recovery_hash_url = request.build_absolute_uri(
                    reverse(
                        'account_reset_password',
                        kwargs={'recovery_hash': recovery_hash}
                    ))

                # compose the email:
                recovery_email_context = RequestContext(
                    request,
                    {'recovery_hash_url': recovery_hash_url})
                recovery_email = SendGrid.compose(
                    sender='Troupon <*****@*****.**>',
                    recipient=registered_user.email,
                    subject='Troupon: Password Recovery',
                    html=loader.get_template(
                        'authentication/forgot_password_recovery_email.html'
                    ).render(recovery_email_context),
                    text=loader.get_template(
                        'authentication/forgot_password_recovery_email.txt'
                    ).render(recovery_email_context),
                )
                # send it and get the request status:
                email_status = SendGrid.send(recovery_email)

                # inform the user of the status of the recovery mail:
                context = {
                    'page_title': 'Forgot Password',
                    'registered_user': registered_user,
                    'recovery_mail_status': email_status,
                }
                return render(
                    request,
                    'authentication/forgot_password_recovery_status.html',
                    context)

            except ObjectDoesNotExist:
                # set an error message:
                messages.add_message(
                    request, messages.ERROR,
                    'The email you entered does not \
                    belong to a registered user!')

        context = {
            'page_title': 'Forgot Password',
            'email_form': email_form,
        }
        context.update(csrf(request))
        return render(request, 'authentication/forgot_password.html', context)