def user_management(request):

    msg = None
    success = False

    if request.method == "POST":
        form = SignUpForm(request.POST)
        if form.is_valid():
            form.save()
            username = form.cleaned_data.get("username")
            raw_password = form.cleaned_data.get("password1")
            user = authenticate(username=username, password=raw_password)

            msg = 'User created'
            success = True

            #return redirect("/login/")

        else:
            msg = 'Form is not valid'
    else:
        form = SignUpForm()

    context = {}
    context['segment'] = 'user-management.html'
    context['page_name'] = 'User Management'
    context['users'] = User.objects.all()
    context['form'] = form
    context['msg'] = msg
    context['success'] = success

    html_template = loader.get_template('user-management.html')
    return HttpResponse(html_template.render(context, request))
Ejemplo n.º 2
0
def registerView(request):
    if request.method == 'POST':
        form = SignUpForm(request.POST)
        if form.is_valid():
            form.save()
            username = form.cleaned_data.get('username')
            raw_password = form.cleaned_data.get('password1')
            user = authenticate(username=username, password=raw_password)
            login(request, user)
            return redirect('/')
    else:
        form = SignUpForm()

    if (request.user.is_authenticated):
        user = User.objects.get(username=request.user.username)
        context = {
            'form': form,
            'user': user,
        }
    else:
        context = {
            'form': form,
        }

    return render(request, 'register.html', context=context)
Ejemplo n.º 3
0
def register(request):

    if request.method == 'POST':
        form = SignUpForm(request.POST)
        if form.is_valid():
            form.save()
            return redirect('authentication:login')
    else:
        form = SignUpForm()

    return render(request, 'register.html', {'form': form})
Ejemplo n.º 4
0
def signup(request):
    if request.method == 'POST':
        form = SignUpForm(request.POST)
        if form.is_valid():
            form.save()
            username = form.cleaned_data.get('username')
            raw_password = form.cleaned_data.get('password1')
            user = authenticate(username=username, password=raw_password)
            login(request, user)
            return redirect('/books/')
    else:
        form = SignUpForm()
    return render(request, 'signup.html', {'form': form})
Ejemplo n.º 5
0
    def post(self, request):
        form = SignUpForm(request.POST)
        if form.is_valid():
            form.save()
            username = form.cleaned_data['username']
            password = form.cleaned_data['password1']

            user = authenticate(request, username=username, password=password)
            if user:
                login(request, user)
            return redirect('index')
        context = {'form': form}
        return render(request, 'authentication/signupform.html', context)
Ejemplo n.º 6
0
def signup(request):
    if request.method == 'POST':
        form = SignUpForm(request.POST)
        if not form.is_valid():
            return render(request, 'authentication/signup.html',
                          {'form': form})

        else:
            user = form.save(commit=False)
            email = form.cleaned_data.get('email')
            password = form.cleaned_data.get('password')
            user.set_password(password)
            user.is_active = False
            user.save()

            current_site = get_current_site(request)
            subject = 'Activate Your Onlinebookshare Account'
            message = render_to_string('authentication/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),
            })
            send_mail(subject, message, '*****@*****.**', [email])

            return redirect('account_activation_sent')

    else:
        return render(request, 'authentication/signup.html',
                      {'form': SignUpForm()})
Ejemplo n.º 7
0
def signup(request):
    if request.method == 'POST':
        form = SignUpForm(data=request.POST)
        if form.is_valid():
            user = form.save(commit=False)
            # Setting as False to prevent login without confirming email
            user.is_active = False
            user.save()
            current_site = get_current_site(request)
            subject = 'Verify your DjangoLogin Account'
            message = render_to_string(
                'authentication/account_activation_email.html', {
                    'user': user,
                    'domain': current_site.domain,
                    'uid': urlsafe_base64_encode(force_bytes(
                        user.pk)).decode(),
                    'token': account_activation_token.make_token(user),
                })
            user.email_user(subject, message)
            print("Mail Done!")
            return redirect(to='authentication:account_activation_sent')
        else:
            return render(request,
                          template_name='authentication/signup.html',
                          context={'form': form})
    else:
        form = SignUpForm()
        return render(request,
                      template_name='authentication/signup.html',
                      context={'form': form})
Ejemplo n.º 8
0
def signup(request):
    """Render sign up view."""
    if request.method == 'POST':
        form = SignUpForm(request.POST)

        if form.is_valid():
            user = form.save(commit=False)
            user.is_active = False
            user.save()
            current_site = get_current_site(request)
            subject = _('Activá tu cuenta de NFL prode')
            message = render_to_string('authentication/account_activation_email.html', {
                'user': user,
                'domain': current_site.domain,
                'uid': urlsafe_base64_encode(force_bytes(user.pk)).decode(),
                'token': account_activation_token.make_token(user),
                })
            user.email_user(subject, message)

            return redirect('account_activation_sent')

    else:
        form = SignUpForm()

    return render(request, 'authentication/signup.html', {'form': form, 'view': 'signup'})
Ejemplo n.º 9
0
def sign_up(request):
    if request.method == "GET":
        form = SignUpForm()
        return render(request, 'authentication/sign_up.html', {'form': form})
    elif request.method == "POST":
        form = SignUpForm(request.POST)
        if form.is_valid():
            user = form.save()
            return redirect('authentication:login')
    return render(request, 'authentication/sign_up.html', {'form': form})
Ejemplo n.º 10
0
def signup(request):
    if request.method == 'POST':
        form = SignUpForm(request.POST)
        if form.is_valid():
            user = form.save()
            user.profile.brith_day = form.cleaned_data.get('birth_date')
            user.save()
            raw_password = form.cleaned_data.get('password1')
            user = authenticate(username=user.username, password=raw_password)
            login(request, user)
            return redirect('/home')
        else:
            return render(request, 'registration/signup.html', {'form': form})
    else:
        form = SignUpForm()
        return render(request, 'registration/signup.html', {'form': form})
Ejemplo n.º 11
0
def signup(request, **kwargs):
    if request.method == 'POST':
        form = SignUpForm(request.POST)
        if form.is_valid():
            user = form.save()
            user.refresh_from_db()
            user.profile.birth_date = form.cleaned_data.get('birth_date')
            user.save()
            username = user.username
            raw_password = form.cleaned_data.get('password1')
            user = authenticate(username=username, password=raw_password)
            login(request, user)
            return redirect('home')
    else:
        form = SignUpForm()
    return render(request, 'signup.html', {'form': form})
Ejemplo n.º 12
0
def login(request):
    if request.method == 'POST':
        form = SignUpForm(request.POST)
        if form.is_valid():
            user = form.save(commit=False)
            try:
                user.refresh_from_db()
                if user.profile.authenticates_by_mail:
                    send_activation(request, user)
                    return redirect('authentication:account_activation_sent',
                                    is_new=False)
            except ObjectDoesNotExist:
                print('ObjectDoesNotExist')
                pass  # I'm not sure this can ever happen.

            # There should be precisely one or zero existing user with the
            # given email, but since the django user model doesn't impose
            # a unique constraint on user emails, I'm stuck verifying this.
            existing_users = User.objects.filter(
                email=form.cleaned_data['email'])
            if len(existing_users) > 1:
                return HttpResponseServerError(
                    "Data error: Multiple email addresses found")
            if len(existing_users) == 1:
                existing_user = existing_users[0]
                if existing_user.profile.authenticates_by_mail:
                    send_activation(request, existing_user)
                    return redirect('authentication:account_activation_sent',
                                    is_new=False)
            user.email = form.cleaned_data['email']
            user.username = get_random_string(20)
            user.is_active = False
            user.save()
            send_activation(request, user)
            return redirect('authentication:account_activation_sent',
                            is_new=True)
        else:
            # Form is not valid.
            pass
    else:
        form = SignUpForm()
    return render(request, 'authentication/login.html', {'form': form})
Ejemplo n.º 13
0
def email_signup(request):
    if request.method == 'POST':
        form = SignUpForm(request.POST)
        if form.is_valid():
            user = form.save(commit=False)
            user.is_active = False
            user.save()
            # user.refresh_from_db()

            current_site = get_current_site(request)
            subject = 'Activate Your Fleet Manager Account'
            message = render_to_string('account_activation_email.html', {
                'user': user,
                'domain': current_site.domain,
                # 'uid': urlsafe_base64_encode(force_bytes(user.pk)),
                'uid': str(user.pk),
                'token': account_activation_token.make_token(user)
            })
            user.email_user(subject, message)
            return redirect('account_activation_sent')
    else:
        form = SignUpForm()
    return render(request, 'signup.html', {'form': form})
Ejemplo n.º 14
0
def register_user(request):
    msg = None
    success = False

    if request.method == "GET":
        form = SignUpForm()
    elif request.method == "POST":
        form = SignUpForm(request.POST)
        if form.is_valid():
            try:
                form.save()
            except Exception as e:
                print(e)
                return render(request, ERROR_404)

            first_name = form.cleaned_data.get("first_name")
            last_name = form.cleaned_data.get("last_name")
            try:
                usr_obj = User.objects.get(first_name=first_name,
                                           last_name=last_name,
                                           username="")
            except Exception as e:
                msg = 'User cannot be created. Exception : {}'.format(str(e))
                print(msg)
                return render(request, ERROR_500)
            else:
                username = ""
                if DEBUG:
                    # decide on username as auser for Axel User
                    username = form.cleaned_data.get("first_name").lower(
                    )[0] + form.cleaned_data.get("last_name").lower()
                else:
                    # this means we have to setup a temporary username
                    username = str(randint(1, 1000))

                usr_obj.username = username
                try:
                    usr_obj.save()
                    User_Profile.objects.create(username=username)
                except Exception as e:
                    print(e)
                    return render(request, ERROR_500)

                if DEBUG:
                    user = authenticate(
                        username=username,
                        password=form.cleaned_data.get("password1"))
                    login(request, user)
                    return redirect("login")

                msg = 'User created {}- please provide documentation for verification'.format(
                    usr_obj.username)
                success = True
        else:
            msg = 'Form is not valid'

    return render(request, USER_REGISTER, {
        "form": form,
        "msg": msg,
        "success": success
    })
Ejemplo n.º 15
0
def login(request, base_template=None):
    
    if request.method == 'POST':
        form = SignUpForm(request.POST)
        if form.is_valid():
            user = form.save(commit=False)
            remember_me = form.cleaned_data["remember_me"]
            try:
                user.refresh_from_db()
                if user.profile.authenticates_by_mail:
                    send_activation(request, user, False, remember_me)
                    return render(request, 'authentication/account_activation_sent.html', {'is_new': False})
            except ObjectDoesNotExist:
                print('ObjectDoesNotExist')
                pass            # I'm not sure this can ever happen.

            # There should be precisely one or zero existing user with the
            # given email, but since the django user model doesn't impose
            # a unique constraint on user emails, I'm stuck verifying this.
            existing_users = User.objects.filter(email=form.cleaned_data['email'])
            if len(existing_users) > 1:
                return HttpResponseServerError(
                    "Data error: Multiple email addresses found")
            
            # If password given by user connect with it else connect by mail
            password1 = form.cleaned_data["password1"]
            password2 = form.cleaned_data["password2"]
            if password1:
                # If user exists and gives password, connect him
                if len(existing_users) == 1:
                    existing_user = existing_users[0].username
                    user = auth.authenticate(username= existing_user, password= password1)

                    if user:
                        auth.login(request, user)
                        # Set session cookie expiration to session duration if "False" otherwise for 30 days
                        if remember_me == False:
                            request.session.set_expiry(0)
                        else:
                            request.session.set_expiry(60*60*24*30)
                        return redirect("index")
                # If user doesn't exist and give password, create account and send a confirmation mail
                if len(existing_users) == 0:
                    if password2 and password1 == password2:
                        user.email = form.cleaned_data['email']
                        user.username = get_random_string(20)
                        user.is_active = False
                        user.set_password(password1)
                        user.save()
                        send_activation(request, user, True, remember_me)
                        return render(request, 'authentication/account_activation_sent.html', {'is_new': True})
                    elif not password2 or password1 != password2:
                        # return to login page with password error message
                        context = {}
                        context["form"] = form
                        context["pwd_error"] = "Veuillez entrer à nouveau le mot de passe."
                        return render(request, 'authentication/login.html', context)
            
            # If no password given
            else:
                # If user exisis, send authentication mail
                if len(existing_users) == 1:
                    existing_user = existing_users[0]
                    if existing_user.profile.authenticates_by_mail:
                        send_activation(request, existing_user, False, remember_me)
                        return render(request, 'authentication/account_activation_sent.html', {'is_new': False})
                # If user doesn't exist, create account and send confirmation mail
                if len(existing_users) == 0:
                    user.email = form.cleaned_data['email']
                    user.username = get_random_string(20)
                    user.is_active = False
                    user.save()
                    send_activation(request, user, True, remember_me)
                    return render(request, 'authentication/account_activation_sent.html', {'is_new': True})
        else:
            # # Form is not valid.
            context = {}
            context["form"] = form
            return render(request, 'authentication/login.html', context)
    else:
        form = SignUpForm()
    return render(request, 'authentication/login.html', {'form': form})