Beispiel #1
0
    def post(self, request, *args, **kwargs):
        name = request.POST.get('slack_username', '')
        name = name.decode('base64', 'strict')
        message = request.POST.get('slack_message', '')

        user_id = request.user.id
        user = User.objects.get(id=user_id)
        reply_hash = Hasher.gen_hash(user)
        reply_hash_url = request.build_absolute_uri(
            reverse(
                'reply_feedback',
                kwargs={'reply_hash': reply_hash},
            ))

        slack_api_url = envvars.get('SLACK_API_URL')
        slack = slackweb.Slack(url=slack_api_url)
        response = slack.notify(text=message + ' reply to ' + reply_hash_url,
                                channel='@' + name,
                                username="******",
                                icon_emoji=":ghost:")
        if response == 'ok':
            sent_feedback = SentFeedback(
                sender=user,
                receiver=name,
                message=message,
            )
            sent_feedback.save()
        return HttpResponse("success", content_type="text/plain")
Beispiel #2
0
    def post(self, request, *args, **kwargs):
        name = request.POST.get('slack_username', '')
        name = name.decode('base64', 'strict')
        message = request.POST.get('slack_message', '')

        user_id = request.user.id
        user = User.objects.get(id=user_id)
        reply_hash = Hasher.gen_hash(user)
        reply_hash_url = request.build_absolute_uri(
                            reverse(
                                'reply_feedback',
                                kwargs={'reply_hash': reply_hash},
                            )
                        )

        slack_api_url = envvars.get('SLACK_API_URL')
        slack = slackweb.Slack(url=slack_api_url)
        response = slack.notify(text=message + ' reply to ' + reply_hash_url, channel='@' + name, username="******", icon_emoji=":ghost:")
        if response == 'ok':
            sent_feedback = SentFeedback(
                                sender=user,
                                receiver=name,
                                message=message,
                            )
            sent_feedback.save()
        return HttpResponse("success", content_type="text/plain")
Beispiel #3
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)
                url_str = str(
                    reverse_lazy('account_reset_password',
                                 kwargs={'recovery_hash': recovery_hash}))
                recovery_hash_url = request.build_absolute_uri(url_str)

                # compose the email:
                recovery_email_context = RequestContext(
                    request, {'recovery_hash_url': recovery_hash_url})
                subject, from_email, to = 'TheEventDiary: Password Recovery', 'Theeventdiary <*****@*****.**>', registered_user.email
                html = loader.get_template(
                    'forgot_password_recovery_email.html').render(
                        recovery_email_context)
                text = loader.get_template('forgot_password_recovery_email.txt'
                                           ).render(recovery_email_context)

                msg = EmailMultiAlternatives(subject, text, from_email, [to])
                msg.attach_alternative(html, "text/html")
                email_status = msg.send()

                # 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, '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,
        }
        return render(request, 'forgot_password.html', context)
Beispiel #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)
                url_str = str(reverse_lazy('account_reset_password',kwargs={'recovery_hash': recovery_hash}))
                recovery_hash_url = request.build_absolute_uri(url_str)

                # compose the email:
                recovery_email_context = RequestContext(request, {'recovery_hash_url': recovery_hash_url})
                subject, from_email, to = 'TheEventDiary: Password Recovery', 'Theeventdiary <*****@*****.**>', registered_user.email
                html=loader.get_template('forgot_password_recovery_email.html').render(recovery_email_context)
                text=loader.get_template('forgot_password_recovery_email.txt').render(recovery_email_context)

                msg = EmailMultiAlternatives(subject, text, from_email, [to])
                msg.attach_alternative(html, "text/html")
                email_status = msg.send()

                # 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, '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,
        }
        return render(request, 'forgot_password.html', context)
Beispiel #5
0
    def get(self, request, *args, **kwargs):
        """Handles GET requests to 'account_reset_password' named route.

        Resets user password.

        Returns:
            HttpResponse with reset_password template if user is active
            otherwise, flashes 'Account not activated' error to the session.
        """
        # get the recovery_hash captured in url
        recovery_hash = kwargs['recovery_hash']

        # reverse the hash to get the user (auto-authentication)
        user = Hasher.reverse_hash(recovery_hash)

        if user is not None:
            if user.is_active:
                # save the user in session:
                request.session['recovery_user_pk'] = user.pk

                # render the reset password view template.
                context = {
                    'page_title': 'Reset Password',
                    'reset_password_form': ResetPasswordForm(auto_id=True),
                }
                context.update(csrf(request))
                return render(
                    request,
                    'authentication/reset_password.html',
                    context
                )
            else:
                # set an 'account not activated' error message
                # and return forbidden response:
                messages.add_message(
                    request, messages.ERROR,
                    'Account not activated!')
                return HttpResponse(
                    'Account not activated!',
                    status_code=403,
                    reason_phrase='You are not allowed to view this content \
                    because your account is not activated!'
                )
        else:
            # raise 404 when the hash doesn't return a user:
            raise Http404("/User does not exist")
Beispiel #6
0
    def get(self, request, *args, **kwargs):
        """Handles GET requests to 'activate_account' named route.
        Returns: A template displaying that activation was successful
        Raises: A Http404 error.
        """
        # get the activation_hash captured in url
        activation_hash = kwargs['activation_hash']

        # reverse the hash to get the user (auto-authentication)
        user = Hasher.reverse_hash(activation_hash)

        if user is not None:
            if not user.is_active:
                user.is_active = True
                user.save()
                if user.is_active:
                    return render(request, 'activation_successful.html')

        else:
            raise Http404("User does not exist")
Beispiel #7
0
    def get(self, request, *args, **kwargs):
        """Handles GET requests to 'activate_account' named route.
        Returns: A template displaying that activation was successful
        Raises: A Http404 error.
        """
        # get the activation_hash captured in url
        activation_hash = kwargs['activation_hash']

        # reverse the hash to get the user (auto-authentication)
        user = Hasher.reverse_hash(activation_hash)

        if user is not None:
            if not user.is_active:
                user.is_active = True
                user.save()
                if user.is_active:
                    return render(request, 'activation_successful.html')

        else:
            raise Http404("User does not exist")
Beispiel #8
0
    def get(self, request, *args, **kwargs):
        """Handles GET requests to 'reply_message' named route.

        Returns: A redirect to the login page.
        Raises: A Http404 error.
        """
        args = {}

        # get the activation_hash captured in url
        reply_hash = kwargs['reply_hash']

        # reverse the hash to get the user (auto-authentication)
        user = Hasher.reverse_hash(reply_hash)

        if user is not None:
            reply_email = user.email
            sender_id = user
            slack_reply_user = get_slack_username(reply_email)
            sender_feedback = SentFeedback.objects.filter(sender=sender_id)[0]

            user_id = request.user.id
            user = User.objects.get(id=user_id)

            r_feedback = ReceivedFeedback(
                receiver=user,
                slack_username=sender_feedback.receiver,
                message=sender_feedback.message
            )

            r_feedback.save()

            slack_user = slack_reply_user.encode('base64', 'strict')
            args['reply'] = slack_user
            args.update(csrf(request))

            return render(request, self.template_name, args)

        else:
            raise Http404("/User does not exist")
Beispiel #9
0
    def get(self, request, *args, **kwargs):
        """Handles GET requests to 'account_reset_password' named route.
        Resets user password.
        Returns:
            HttpResponse with reset_password template if user is active
            otherwise, flashes 'Account not activated' error to the session.
        """
        # get the recovery_hash captured in url
        recovery_hash = kwargs['recovery_hash']

        # reverse the hash to get the user (auto-authentication)
        user = Hasher.reverse_hash(recovery_hash)

        if user is not None:
            if user.is_active:
                # save the user in session:
                request.session['recovery_user_pk'] = user.pk

                # render the reset password view template.
                context = {
                    'page_title': 'Reset Password',
                    'reset_password_form': ResetPasswordForm(auto_id=True),
                }
                return render(request,'reset_password.html',context)
            else:
                # set an 'account not activated' error message
                # and return forbidden response:
                messages.add_message(
                    request, messages.ERROR,
                    'Account not activated!')
                return HttpResponse(
                    'Account not activated!',
                    status_code=403,
                    reason_phrase='You are not allowed to view this content \
                    because your account is not activated!'
                )
        else:
            # raise 404 when the hash doesn't return a user:
            raise Http404("/User does not exist")
Beispiel #10
0
    def get(self, request, *args, **kwargs):
        """Handles GET requests to 'reply_message' named route.

        Returns: A redirect to the login page.
        Raises: A Http404 error.
        """
        args = {}

        # get the activation_hash captured in url
        reply_hash = kwargs['reply_hash']

        # reverse the hash to get the user (auto-authentication)
        user = Hasher.reverse_hash(reply_hash)

        if user is not None:
            reply_email = user.email
            sender_id = user
            slack_reply_user = get_slack_username(reply_email)
            sender_feedback = SentFeedback.objects.filter(sender=sender_id)[0]

            user_id = request.user.id
            user = User.objects.get(id=user_id)

            r_feedback = ReceivedFeedback(
                receiver=user,
                slack_username=sender_feedback.receiver,
                message=sender_feedback.message)

            r_feedback.save()

            slack_user = slack_reply_user.encode('base64', 'strict')
            args['reply'] = slack_user
            args.update(csrf(request))

            return render(request, self.template_name, args)

        else:
            raise Http404("/User does not exist")
Beispiel #11
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})
Beispiel #12
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)
Beispiel #13
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 = {}
            mssg = "Email already taken please signup with another email"
            messages.add_message(request, messages.INFO, mssg)
            return render(request, 'register.html', args)

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

            if request.POST.get('is_merchant', False):
                new_user.userprofile.is_merchant = True
                new_user.userprofile.save()

            # generate an activation hash url for new user account
            activation_hash = Hasher.gen_hash(new_user)
            url_str = str(reverse_lazy('activate_account', kwargs={'activation_hash': activation_hash}))
            activation_hash_url = request.build_absolute_uri(url_str)

            # compose the email
            activation_email_context = RequestContext(
                request,
                {'activation_hash_url': activation_hash_url,
                 'username': new_user.username,
                },
            )

            receipient = str(new_user.email)

            subject, from_email, to = 'TheEventDiary: ACTIVATE ACCOUNT', EMAIL_SENDER, receipient
            html_content=loader.get_template('activate_account_email.html').render(activation_email_context)
            text_content=loader.get_template('activate_account_email.txt').render(activation_email_context)

            msg = EmailMultiAlternatives(subject, text_content, from_email, [to])
            msg.attach_alternative(html_content, "text/html")
            response = msg.send()

            # inform the user of activation mail sent
            if response == 1:
                new_user_email = new_user.email
                messages.add_message(request, messages.INFO, new_user_email)

            return redirect(reverse_lazy('confirm_registration'))

        else:
            user_form = UserSignupForm()
            args = {}
            args["signupform"] = user_form
            return render(request, 'register.html', args)
Beispiel #14
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 = {}
            mssg = "Email already taken please signup with another email"
            messages.add_message(request, messages.INFO, mssg)
            return render(request, 'register.html', args)

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

            if request.POST.get('is_merchant', False):
                new_user.userprofile.is_merchant = True
                new_user.userprofile.save()

            # generate an activation hash url for new user account
            activation_hash = Hasher.gen_hash(new_user)
            url_str = str(
                reverse_lazy('activate_account',
                             kwargs={'activation_hash': activation_hash}))
            activation_hash_url = request.build_absolute_uri(url_str)

            # compose the email
            activation_email_context = RequestContext(
                request,
                {
                    'activation_hash_url': activation_hash_url,
                    'username': new_user.username,
                },
            )

            receipient = str(new_user.email)

            subject, from_email, to = 'TheEventDiary: ACTIVATE ACCOUNT', EMAIL_SENDER, receipient
            html_content = loader.get_template(
                'activate_account_email.html').render(activation_email_context)
            text_content = loader.get_template(
                'activate_account_email.txt').render(activation_email_context)

            msg = EmailMultiAlternatives(subject, text_content, from_email,
                                         [to])
            msg.attach_alternative(html_content, "text/html")
            response = msg.send()

            # inform the user of activation mail sent
            if response == 1:
                new_user_email = new_user.email
                messages.add_message(request, messages.INFO, new_user_email)

            return redirect(reverse_lazy('confirm_registration'))

        else:
            user_form = UserSignupForm()
            args = {}
            args["signupform"] = user_form
            return render(request, 'register.html', args)