Пример #1
0
def check_with_token(request):
    if request.method == "GET":
        return render(
            request=request,
            template_name="token_reg.html",
            context={"data": ""},
        )
    elif request.method == "POST":
        request_data = parse.parse_qs(request.body.decode())
        # make user
        try:
            request_data["token"][0]
        except:
            return JsonResponse({"status": "empty token"})
        for i in CustomUser.objects.all():
            if (tokens.PasswordResetTokenGenerator().check_token(
                    user=i, token=request_data["token"][0])):
                the_user = i
                break
        else:
            return JsonResponse({"status": "wrong token"})
        return render(
            request=request,
            template_name="token_reg_form.html",
            context={
                "the_role": the_user.get_role_display(),
                "token": request_data["token"][0],
                "show_clinic": True if the_user.role == "CM" else False,
                "clinics": Clinic.objects.all(),
            },
        )
Пример #2
0
def get_reset_pw_token(request):
    if request.method == "GET":
        return render(
            request=request,
            template_name="reset_password.html",
            context={},
        )
    elif request.method == "POST":
        request_data = parse.parse_qs(request.body.decode())
        # make user
        try:
            theuser = CustomUser.objects.get(email=request_data["email"][0])

            send_mail(
                'Password reset link:',
                'thelink: http://127.0.0.1:8000/acc/reset_password/' +
                tokens.PasswordResetTokenGenerator().make_token(theuser),
                '*****@*****.**',
                [theuser.email],
                fail_silently=False,
            )
            return render(
                request=request,
                template_name="all_done.html",
                context={},
            )
        except:
            return JsonResponse({"status": "wrong token"})
Пример #3
0
def pre_register(request):
    if request.method == "GET":
        return render(
            request=request,
            template_name="pre_reg.html",
            context={"data": ""},
        )
    elif request.method == "POST":
        request_data = parse.parse_qs(request.body.decode())
        # make user
        new_user = CustomUser()
        new_user.email = request_data["email"][0]
        new_user.is_active = False
        new_user.username = str(random.random())
        new_user.role = request_data["role"][0]
        new_user.save()
        new_token = tokens.PasswordResetTokenGenerator().make_token(new_user)
        send_mail(
            'Welcome',
            'Your token:' + new_token,
            '*****@*****.**',
            [request_data["email"][0]],
            fail_silently=False,
        )
        return render(
            request=request,
            template_name="check_email.html",
            context={},
        )
Пример #4
0
def reset_password(request, token=""):
    if token == "":
        return JsonResponse({"status": "wrong token"})
    if request.method == "GET":
        return render(
            request=request,
            template_name="reset_password_form.html",
            context={},
        )
    elif request.method == "POST":
        request_data = parse.parse_qs(request.body.decode())
        # make user
        print(request.path_info)
        for i in CustomUser.objects.all():
            if (tokens.PasswordResetTokenGenerator().check_token(
                    user=i,
                    token=os.path.basename(os.path.normpath(
                        request.path_info)))):
                the_user = i
                the_user.set_password(request_data["password"][0])
                the_user.save()
                break
        else:
            return JsonResponse({"status": "wrong token"})
        return render(
            request=request,
            template_name="all_done.html",
            context={},
        )
def ChangePass(request, uidb64, token):
    email = urlsafe_base64_decode(uidb64).decode()
    user = CustomUser.objects.get(pk=email)
    tokenChecker = tokens.PasswordResetTokenGenerator()
    if tokenChecker.check_token(user, token):
        login(request, user)
    else:
        raise Http404('Password reset link is no longer valid. Please get another email.')

    return render(request, 'pages/passResetConfirm.html')
Пример #6
0
def agregar_usuario(request):
    mensaje = ""
    if request.method == 'POST':
        form = UsuarioForm(request.POST)
        if form.is_valid():
            try:
                # user = User(**form.cleaned_data)
                username = form.cleaned_data['username']
                first_name = form.cleaned_data['first_name']
                last_name = form.cleaned_data['last_name']
                email = form.cleaned_data['email']
                group = form.cleaned_data['groups']
                group = Group.objects.get(pk=int(group))
                user = User(username=username,
                            first_name=first_name,
                            last_name=last_name,
                            email=email)
                user.set_unusable_password()
                user.save()
                user.groups.add(group)
                template = get_template('auth/email_password.html')
                token = tokens.PasswordResetTokenGenerator()
                content = template.render({
                    'user':
                    user,
                    'uid':
                    urlsafe_base64_encode(force_bytes(user.pk)),
                    'token':
                    token.make_token(user),
                    'request':
                    request,
                })
                email = EmailMessage('Creación de password',
                                     content,
                                     to={
                                         user.email,
                                     })
                email.send()
                mensaje = "Usuario creado con éxito"
                messages.success(request, mensaje)
                # Limpiando campos después de guardar (Reset Forms)
                form = UsuarioForm()
            except Exception as e:
                mensaje = "Error al crear el usuario, {}".format(e)
                messages.error(request, mensaje)

    else:
        form = UsuarioForm()

    return render(request, 'auth/add_user.html', {
        'form': form,
        'mensaje': mensaje
    })
Пример #7
0
def ChangePass(request, uidb64, token):
    # Decode the password reset link
    email = urlsafe_base64_decode(uidb64).decode()
    # Get the user object from the database
    user = CustomUser.objects.get(pk=email)
    # Check to see if the password reset link is still valid
    tokenChecker = tokens.PasswordResetTokenGenerator()
    # If the token is still valid, allow the user to change their password
    if tokenChecker.check_token(user, token):
        login(request, user)
    # Otherwise, raise an HTTP 404 error message to inform the user that the link is still invalid
    else:
        raise Http404(
            'Password reset link is no longer valid. Please get another email.'
        )

    return render(request, 'pages/passResetConfirm.html')
Пример #8
0
def reg_with_token(request):
    if request.method == "POST":
        request_data = parse.parse_qs(request.body.decode())
        # make user
        for i in CustomUser.objects.all():
            if (tokens.PasswordResetTokenGenerator().check_token(
                    user=i, token=request_data["token"][0])):
                the_user = i
                the_user.first_name = request_data["first"][0]
                the_user.last_name = request_data["last"][0]
                the_user.username = request_data["username"][0]
                the_user.set_password(request_data["password"][0])
                the_user.is_active = True
                if the_user.role == "CM":
                    the_user.clinic = Clinic.objects.get(
                        id=int(request_data["clinic_id"][0]))
                the_user.save()
                break

        return render(
            request=request,
            template_name="all_done.html",
            context={},
        )