Beispiel #1
0
def team_members(request, user_uuid=None):
    team_member_data = User.objects.all()
    user = None

    if user_uuid:
        user = team_member_data.get(user_uuid=user_uuid)

    if request.method == 'POST':
        if user:
            form = CustomUserForm(request.POST, instance=user)
        else:
            form = CustomUserForm(request.POST)

        if form.is_valid():
            team_member = form.save(commit=False)
            team_member.email = team_member.email.lower()
            team_member.save()

            if not user:
                new_password = make_uuid()
                team_member.set_password(new_password)
                url_pattern = '{}{}'.format(settings.APPLICATION_URL, reverse('security:create_new_password',
                                                                              args=(team_member.user_uuid, new_password)))

                # the employee is new so send them an email with their password.
                email_subject = 'NSI Employee Portal Account Created'

                email_body = """
<p>{first_name}</p>,
<p>An account in the NSI Employee Portal has been created for you.  Please <a href="{url_pattern}">click here</a>
to login.</p>
<p>Please keep in mind this temporary password will expire in {expires} hours. Upon clicking the link you will be
prompted to create a new password.</p>""".format(first_name=team_member.first_name, url_pattern=url_pattern,
                                                 expires=settings.TEMP_PASSWORD_EXPIRES)

                email = EmailMultiAlternatives(email_subject, '', settings.APPLICATION_EMAIL, [team_member.email])
                email.attach_alternative(email_body, "text/html")
                email.send(fail_silently=False)

            messages.success(request, 'The team member data was saved successfully.')
            return redirect('security:team_members')
        else:
            messages.error(request, settings.GENERIC_ERROR)
    else:
        if user:
            form = CustomUserForm(instance=user)
        else:
            form = CustomUserForm()

    return render(request, 'team_members.html', {'form': form, 'team_member_data': team_member_data,
                                                 'user_uuid': user_uuid})
Beispiel #2
0
def reset_password(request, template_name='resetPassword.html'):
    temp_expire = settings.TEMP_PASSWORD_EXPIRES

    if request.method == 'POST':
        try:
            form = ResetPasswordForm(request.POST)

            if form.is_valid():
                # Random string 10 characters long based on upper, lower, digits and special characters
                new_password = make_uuid()

                email = request.POST['email'].lower()

                user = User.objects.get(email=email)
                user.set_password(new_password)
                user.password_reset = True
                user.dt_password_reset = datetime.now()
                user.save()

                url_pattern = '{}{}'.format(settings.APPLICATION_URL, reverse('security:create_new_password',
                                                                              args=(user.user_uuid, new_password)))

                email_subject = 'NSI Employee Portal Password Reset'
                email_body = """
<p>{first_name}</p>
<p>You have requested a password reset through the NSI Employee Portal. The following link will reset your password.
<a href="{url_pattern}">Please click here to continue.</a></p>
<p>Please keep in mind this temporary password will expire in {password_expire} hours. Upon clicking the link you will
be prompted to create a new password.</p>""".format(first_name=user.first_name, url_pattern=url_pattern,
                                                    password_expire=settings.TEMP_PASSWORD_EXPIRES)

                email = EmailMultiAlternatives(email_subject, '', settings.APPLICATION_EMAIL, [email])
                email.attach_alternative(email_body, "text/html")

                email.send(fail_silently=False)

                messages.success(request, """Your password reset has been processed. You will receive an email
                                          containing a temporary password.""")

                return redirect('security:login')
            else:
                # form is not valid
                messages.error(request, settings.GENERIC_ERROR)
        except User.DoesNotExist:
            messages.error(request, 'A password reset request could not be processed for this email address.')
            return redirect('security:reset_password')
    else:
        form = ResetPasswordForm()

    return render(request, template_name, {'form': form, 'temp_expire': temp_expire})
Beispiel #3
0
def team_members(request, user_uuid=None):
    team_member_data = User.objects.all()
    user = None

    if user_uuid:
        user = team_member_data.get(user_uuid=user_uuid)

    if request.method == 'POST':
        if user:
            form = CustomUserForm(request.POST, instance=user)
        else:
            form = CustomUserForm(request.POST)

        if form.is_valid():
            team_member = form.save(commit=False)
            team_member.email = team_member.email.lower()
            team_member.save()

            if not user:
                new_password = make_uuid()
                team_member.set_password(new_password)
                url_pattern = '{}{}'.format(
                    settings.APPLICATION_URL,
                    reverse('security:create_new_password',
                            args=(team_member.user_uuid, new_password)))

                # the employee is new so send them an email with their password.
                email_subject = 'NSI Employee Portal Account Created'

                email_body = """
<p>{first_name}</p>,
<p>An account in the NSI Employee Portal has been created for you.  Please <a href="{url_pattern}">click here</a>
to login.</p>
<p>Please keep in mind this temporary password will expire in {expires} hours. Upon clicking the link you will be
prompted to create a new password.</p>""".format(
                    first_name=team_member.first_name,
                    url_pattern=url_pattern,
                    expires=settings.TEMP_PASSWORD_EXPIRES)

                email = EmailMultiAlternatives(email_subject, '',
                                               settings.APPLICATION_EMAIL,
                                               [team_member.email])
                email.attach_alternative(email_body, "text/html")
                email.send(fail_silently=False)

            messages.success(request,
                             'The team member data was saved successfully.')
            return redirect('security:team_members')
        else:
            messages.error(request, settings.GENERIC_ERROR)
    else:
        if user:
            form = CustomUserForm(instance=user)
        else:
            form = CustomUserForm()

    return render(request, 'team_members.html', {
        'form': form,
        'team_member_data': team_member_data,
        'user_uuid': user_uuid
    })
Beispiel #4
0
def reset_password(request, template_name='resetPassword.html'):
    temp_expire = settings.TEMP_PASSWORD_EXPIRES

    if request.method == 'POST':
        try:
            form = ResetPasswordForm(request.POST)

            if form.is_valid():
                # Random string 10 characters long based on upper, lower, digits and special characters
                new_password = make_uuid()

                email = request.POST['email'].lower()

                user = User.objects.get(email=email)
                user.set_password(new_password)
                user.password_reset = True
                user.dt_password_reset = datetime.now()
                user.save()

                url_pattern = '{}{}'.format(
                    settings.APPLICATION_URL,
                    reverse('security:create_new_password',
                            args=(user.user_uuid, new_password)))

                email_subject = 'NSI Employee Portal Password Reset'
                email_body = """
<p>{first_name}</p>
<p>You have requested a password reset through the NSI Employee Portal. The following link will reset your password.
<a href="{url_pattern}">Please click here to continue.</a></p>
<p>Please keep in mind this temporary password will expire in {password_expire} hours. Upon clicking the link you will
be prompted to create a new password.</p>""".format(
                    first_name=user.first_name,
                    url_pattern=url_pattern,
                    password_expire=settings.TEMP_PASSWORD_EXPIRES)

                email = EmailMultiAlternatives(email_subject, '',
                                               settings.APPLICATION_EMAIL,
                                               [email])
                email.attach_alternative(email_body, "text/html")

                email.send(fail_silently=False)

                messages.success(
                    request,
                    """Your password reset has been processed. You will receive an email
                                          containing a temporary password.""")

                return redirect('security:login')
            else:
                # form is not valid
                messages.error(request, settings.GENERIC_ERROR)
        except User.DoesNotExist:
            messages.error(
                request,
                'A password reset request could not be processed for this email address.'
            )
            return redirect('security:reset_password')
    else:
        form = ResetPasswordForm()

    return render(request, template_name, {
        'form': form,
        'temp_expire': temp_expire
    })