Ejemplo n.º 1
0
def edit(request, pk, template_name='home_data_user_edit.html'):
    data = get_object_or_404(User, pk=pk)
    form = SignUpForm(request.POST or None, instance=data)

    if form.is_valid():
        if form.cleaned_data.get('password1') == form.cleaned_data.get(
                'password2'):
            user = form.save()
            user.refresh_from_db()
            user.username = form.cleaned_data.get('username')
            user.first_name = form.cleaned_data.get('first_name')
            user.last_name = form.cleaned_data.get('last_name')
            user.email = form.cleaned_data.get('email')

            if form.cleaned_data.get('is_staff') == 'True':
                user.is_superuser = True
                user.is_staff = False
            else:
                user.is_superuser = False
                user.is_staff = True

            user.save()

        return redirect('home:data-user')

    return render(request, template_name, {'form': form})
Ejemplo n.º 2
0
def register(request):
    if request.method == 'POST':
        form = SignUpForm(request.POST)
        if form.is_valid():
            #form.setdefault('is_staff', True)
            form.is_superuser = True
            form.is_active = True
            form.is_staff = True
            form.save()
            username = form.cleaned_data.get('username')
            raw_password = form.cleaned_data.get('password1')
            user = authenticate(username=username, password=raw_password)
            if user is not None:

                if user.is_active:
                    login(request,user)
                    return redirect('/home')
                    return HttpResponse("login successful")
                else:
                    return HttpResponse("disabled account")
            else:
                return HttpResponse("invalid login")

            #login(request, user)
            
    else:
        form = SignUpForm()

    return render(request, 'account/index3.html', {'form': form})	
Ejemplo n.º 3
0
def register(request):
    # If the user is already logged in, redirect to the dashboard.
    # TODO: add user object to request.
    if request.method == 'POST':
        form = SignUpForm(request.POST)
        if form.is_valid():
            return send_confirmation_and_redirect(request, 
                                                  form.cleaned_data['email'], 
                                                  utils.CREATE_TASK)
    else:
        form = SignUpForm()
    return fm_render(request, 'account/register_form.html', {'form': form})
Ejemplo n.º 4
0
def test_sign_up_form_wrong_password2():
    form = SignUpForm()
    form.cleaned_data = {
        'password': '******',
        'password2': 'password2',
        'phone': ''.join(i for i in FAKE.phone_number() if i.isdigit())[:10]
    }
    try:
        form.clean()
    except ValidationError:
        pass
    else:
        assert 2 == 1
Ejemplo n.º 5
0
def test_sign_up_form_wrong_phone():
    form = SignUpForm()
    form.cleaned_data = {
        'password': '******',
        'password2': 'password',
        'phone': FAKE.phone_number()
    }
    try:
        form.clean()
    except ValidationError:
        pass
    else:
        assert 2 == 1
Ejemplo n.º 6
0
 def test_signup_for_valid(self):
     form_signup = {
         'username': '******',
         'email': '*****@*****.**',
         'first_name': 'john',
         'last_name': 'test',
         'password1': 'pass12345',
         'password2': 'pass12345',
         'birth_date': '23-07-2000'
     }
     form = SignUpForm(data=form_signup)
     print(form.errors)
     self.assertTrue(form.is_valid())
Ejemplo n.º 7
0
def test_sign_up_form_to_short_phone():
    form = SignUpForm()
    form.cleaned_data = {
        'password': '******',
        'password2': 'password',
        'phone': '2'
    }
    try:
        form.clean()
    except ValidationError:
        pass
    else:
        assert 2 == 1
Ejemplo n.º 8
0
def register_user(request):
    print("Here")
    if request.method == "POST":
        print("HERE2")
        user_form = UserCreationForm(request.POST)
        profile_form = SignUpForm(request.POST)

        print(user_form)
        print(profile_form)

        if user_form.is_valid() and profile_form.is_valid():
            print("HERE3")
            user = user_form.save()
            user.refresh_from_db()
            user.first_name = profile_form.cleaned_data.get("first_name")
            user.last_name = profile_form.cleaned_data.get("last_name")
            user.email = profile_form.cleaned_data.get("email")
            # user.employee_id = profile_form.cleaned_data.get("employee_id")
            user.pto_balance = profile_form.cleaned_data.get("pto_balance")

            user.profile.first_name = profile_form.cleaned_data.get(
                "first_name")
            user.profile.last_name = profile_form.cleaned_data.get("last_name")
            user.profile.email = profile_form.cleaned_data.get("email")
            # user.profile.employee_id = profile_form.cleaned_data.get("employee_id")
            user.profile.pto_balance = profile_form.cleaned_data.get(
                "pto_balance")

            group = Group.objects.get(name="Editors")
            user.groups.add(group)

            name_upper = user.username.upper()

            user.save()
            username = user_form.cleaned_data.get("username")
            raw_password = user_form.cleaned_data.get("password1")
            user = authenticate(username=username, password=raw_password)
            login(request, user)
            print("Redirecting")
            return redirect("/")

    else:
        user_form = UserCreationForm()
        profile_form = SignUpForm()

        return render(
            request, "registration/register.html", {
                "user_form": user_form,
                "profile_form": profile_form,
                "path": request.path_info
            })
Ejemplo n.º 9
0
    def post(self, request):
        email = request.POST['email']
        password = request.POST['password']
        form = SignUpForm(request.POST or None)
        if not form.is_valid():
            logger.debug(f'validate error: {form}')
            return Response({'form': form},
                            template_name='account/create.html')
        # if form.is_duplicate_email():
        #     return Response({'form': form}, template_name='account/create.html')

        user = User.objects.create_user(email=email, password=password)

        login(request, user)
        return redirect('/')
Ejemplo n.º 10
0
def signup(request):
    if request.method == 'POST':
        form = SignUpForm(request.POST)
        if form.is_valid():
            user = form.save()
            user.refresh_from_db(
            )  # load the profile instance created by the signal
            user.save()
            raw_password = form.cleaned_data.get('password1')
            user = authenticate(username=user.username, password=raw_password)
            login(request, user)
            return redirect('/books')
    else:
        form = SignUpForm
    return render(request, 'account/signup.html', {'form': form})
Ejemplo n.º 11
0
def signup_view(request):
    context = {}
    if request.POST:
        form = SignUpForm(request.POST)
        if form.is_valid():
            user = form.save()
            login(request,
                  user,
                  backend='django.contrib.auth.backends.ModelBackend')
            return redirect('home')
        else:
            context['form'] = form
    else:
        context['form'] = SignUpForm()
    return render(request, 'signup.html', context)
Ejemplo n.º 12
0
 def test_email_validation(self):
     user = self.create_user()
     form_signup = {
         'username': '******',
         'email': user.email,
         'first_name': 'john',
         'last_name': 'test',
         'password1': 'pass12345',
         'password2': 'pass12345',
         'birth_date': '23-07-2000'
     }
     form = SignUpForm(data=form_signup)
     self.assertFalse(form.is_valid())
     email_error = form.errors['email'].as_json()
     self.assertEqual("Email addresses must be unique.",
                      json.loads(email_error)[0]['message'])
Ejemplo n.º 13
0
 def signup(request):
     form = None
     if request.method == 'POST':
         form = SignUpForm(request.POST)
         if form.is_valid():
             form_user = form.save(commit=False)
             user = User.objects.create_user(
                 username=form_user.username,
                 email=form_user.email,
                 password=form_user.password,
             )
             user.first_name = form_user.first_name
             user.last_name = form_user.last_name
             user.save()
             account = Account(user=user)
             account.save()
     return render(request, 'index.html', {'formSignUp': form})
Ejemplo n.º 14
0
def register(request):
    if(request.method == "POST"):
        form = SignUpForm(request.POST)
        
        if form.is_valid():
            form.save()
            print('SAVE already')
            u = User.objects.get(username=request.POST['username'])
            u.is_staff = True
            perms = get_permission(request.POST['perm'])
            print(perms)
            for perm in perms:
                permission = Permission.objects.get(codename=perm)
                u.user_permissions.add(permission)
            print('update permission already')

        else:
            print('testetewtew')
            return render(request, 'account/failformRegis.html')
    elif(request.method == "GET"):
        print("regisForm")
        form = SignUpForm()
        permission = ['add_blog','change_blog','delete_blog','add_comment','change_comment','delete_comment']
        return render(request, 'account/regisForm.html', {'form':form ,'permission': permission})

    return HttpResponseRedirect(reverse('blog:index'))
Ejemplo n.º 15
0
def signup(request):
    if request.method == 'POST':
        form1 = SignUpForm(request.POST)
        form2 = UserProfileForm(request.POST)
        if form1.is_valid() and form2.is_valid():
            form1.save()
            username = form1.cleaned_data.get('username')
            raw_password = form1.cleaned_data.get('password1')
            user = authenticate(username=username, password=raw_password)
            login(request, user)
            profile = form2.save(commit=False)
            profile.user = user
            profile.save()
            return redirect('account:profile')
    else:
        form1 = SignUpForm()
        form2 = UserProfileForm()
    return render(request, 'account/signup.html', {'form1': form1, 'form2':form2})
Ejemplo n.º 16
0
def register(request):
    """Регистрация"""
    if request.method == 'POST':
        form = SignUpForm(request.POST)
        if form.is_valid():
            email = form.cleaned_data['email']
            if User.objects.filter(email=email).exists():
                messages.error(request,
                               'Пользователь с таким адресом уже существует')
            else:
                user = form.save()
                user.is_active = False
                current_site = get_current_site(request)
                mail_subject = 'Подтвердите регистрацию на ' + current_site.domain
                mail_from = base.EMAIL_HOST_USER
                mail_to = form.cleaned_data.get('email')
                template = os.path.join(
                    BASE_DIR, 'templates/registration/account_activate.html')
                html_message = render_to_string(
                    template, {
                        'user': user,
                        'domain': current_site.domain,
                        'uid': urlsafe_base64_encode(force_bytes(user.pk)),
                        'token': account_activation_token.make_token(user),
                    })
                plain_message = strip_tags(html_message)
                send_mail(
                    mail_subject,
                    plain_message,
                    mail_from,
                    [mail_to],
                    html_message=html_message,
                )
                user.save()
                messages.success(
                    request,
                    'Вы успешно зарегестрировались. Вам осталось только подтвердить почту'
                )
                return redirect('/')
    else:
        form = SignUpForm
    template = os.path.join(BASE_DIR, 'templates/registration/signup.html')
    context = {'form': form}
    return render(request, template, context)
Ejemplo n.º 17
0
def signup_view(request):
    if request.method == 'POST':
        form = SignUpForm(request.POST)
        if form.is_valid():
            form.save()
            return redirect('sendText:index')
    else:
        form = SignUpForm()
    return render(request, 'account/signup.html', {'form': form})
Ejemplo n.º 18
0
def test_sign_up_form_clear():
    form = SignUpForm()
    form.cleaned_data = {
        'password': '******',
        'password2': 'password',
        'phone': ''.join(i for i in FAKE.phone_number() if i.isdigit())[:10]
    }
    form.clean()
    form.save()
Ejemplo n.º 19
0
def 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()
            current_site = get_current_site(request)
            mail_subject = 'فعالسازی حساب کاربری'
            message = render_to_string(
                'registration/activate_account.html', {
                    'user': user,
                    'domain': current_site.domain,
                    'uid': urlsafe_base64_encode(force_bytes(user.pk)),
                    'token': account_activation_token.make_token(user),
                })
            to_email = form.cleaned_data.get('email')
            email = EmailMessage(mail_subject, message, to=[to_email])
            email.send()
            return render(request, 'registration/activation_confirm.html')
    else:
        form = SignUpForm()

    if request.user.is_authenticated:
        print(check_password('m1419844322', request.user.password))

    return render(request, 'registration/register.html', {'form': form})
Ejemplo n.º 20
0
def signup(request):
    status = False
    if request.method == 'POST':
        form = SignUpForm(request.POST)
        if form.is_valid():
            user = form.save(commit=False)
            user.is_active = status
            user.save()
            if status == False:  #IF ACTIVE STATUS FALSE IT WILL SEND MAIL TO THE USER TO ACTIVATE

                current_site = get_current_site(request)
                subject = 'Activate Your MySite Account'
                message = render_to_string(
                    'account/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),
                    })
                user.email_user(subject, message)  #MAIL SEND HERE
                return render(request, 'account/account_activation_sent.html',
                              {'Title': 'Activate Account'})
            else:
                messages.success(request,
                                 "Your account is Created sucessfully..!")
                return redirect('login')
    else:  # redirect signup form
        form = SignUpForm()
    return render(request, 'account/signup.html', {
        'form': form,
        'Title': 'Sign up'
    })
Ejemplo n.º 21
0
def create(request):
    if request.method == 'POST':
        form = SignUpForm(request.POST)

        if form.is_valid():
            if form.cleaned_data.get('password1') == form.cleaned_data.get(
                    'password2'):
                user = form.save()
                user.refresh_from_db()
                user.username = form.cleaned_data.get('username')
                user.first_name = form.cleaned_data.get('first_name')
                user.last_name = form.cleaned_data.get('last_name')
                user.email = form.cleaned_data.get('email')

                if form.cleaned_data.get('is_staff') == 'True':
                    user.is_superuser = True
                    user.is_staff = False
                else:
                    user.is_superuser = False
                    user.is_staff = True

                user.save()

                return redirect('home:data-user')

    form = SignUpForm()
    return render(request, 'home_data_user_create.html', {'form': form})
Ejemplo n.º 22
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 = form.save()
            login(request,
                  user,
                  backend='django.contrib.auth.backends.ModelBackend')
            return redirect('main')
    else:
        form = SignUpForm()
    return render(request, 'account/index.html', {'form': form})
Ejemplo n.º 23
0
def signup(request):
    print(request)
    if request.method == 'POST':
        print("passed")
        form = SignUpForm(request.POST)
        if form.is_valid():
            form.save()
            return redirect('/accounts/login')
    else:
        print("failed")
        form = SignUpForm()
    return render(request, 'signup.html', {'form': form})
Ejemplo n.º 24
0
def sign_up_user(request):
    if request.method == 'GET':
        context = {
            'form': SignUpForm(),
        }

        return render(request, 'accounts/sign_up.html', context)
    else:
        form = SignUpForm(request.POST)

        if form.is_valid():
            user = form.save()
            profile = UserProfile(user=user, )
            profile.user.groups.add(Group.objects.get(name='UserGroup'))
            profile.save()
            login(request, user)
            return redirect('index')

        context = {
            'form': form,
        }

        return render(request, 'accounts/sign_up.html', context)
Ejemplo n.º 25
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('/account/profile')
    else:
        form = SignUpForm()
    return render(request, 'registration/signup.html', {'form': form})
Ejemplo n.º 26
0
def test_sign_up_form_not_clear():
    form = SignUpForm()
    form.errors['fake_error'] = 'fake'
    form.cleaned_data = {
        'password': '******',
        'password2': 'password',
        'phone': ''.join(i for i in FAKE.phone_number() if i.isdigit())[:10]
    }
    form.clean()
    form.errors.pop('fake_error')
Ejemplo n.º 27
0
def signup_view(request):
    login_form = AuthenticationForm()
    signup_form = SignUpForm()
    _form = request.POST.get('form_name')
    if _form == 'login_form':
        # Loging in user
        login_form = AuthenticationForm(request, request.POST)
        if login_form.is_valid():
            username = login_form.cleaned_data.get('username')
            password = login_form.cleaned_data.get('password')
            user = authenticate(username=username, password=password)
            if user is not None:
                login(request, user)
                messages.success(request, f'User {user} is logged in .')
                return redirect('home')
        else:
            messages.warning(
                request,
                f'Invalid credentials!...Correct the errors on the form')
    if _form == 'signup_form':
        # Signing up user
        signup_form = SignUpForm(request.POST, request.FILES)
        if signup_form.is_valid():
            user = signup_form.save()
            user.category = request.POST.get('Catgy')
            user.student = request.POST.get('Std_cat')
            user.regNo = request.POST.get('Reg')
            user.lecturer = request.POST.get('Lect_cat')
            user.staffId = request.POST.get('Staff')
            user.college_council = request.POST.get('Col_council_cat')
            user.academic_council = request.POST.get('Acad_council_cat')
            user.school_council = request.POST.get('Skl_council_cat')
            user.department_council = request.POST.get('Dep_council_cat')
            user.school = request.POST.get('Skl')
            user.department = request.POST.get('Depart')
            user.level = request.POST.get('Lv')
            user.save()
            username = signup_form.cleaned_data.get('username')
            password = signup_form.cleaned_data.get('password1')
            user = authenticate(username=username, password=password)
            login(request, user)
            messages.success(request,
                             f'Account created succesfully for {username}')
            return redirect('home')
        else:
            messages.warning(
                request, f'User not created!...Correct the errors on the form')

    context = {
        'categories': Category.objects.all(),
        'signup_form': signup_form,
        'login_form': login_form
    }
    return render(request, 'signup.html', context)
Ejemplo n.º 28
0
def signup(request):
    if request.method == 'POST':
        form = SignUpForm(request.POST)
        if form.is_valid():
            user = form.save()
            user.refresh_form_db()
            user.profile.birth_date = 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:
        form = SignUpForm()
    return render(request, 'account/signup.html', {'form': form})
Ejemplo n.º 29
0
def register(request):
    if (request.method == 'POST'):
        form = SignUpForm(request.POST)
        if (form.is_valid()):
            user = form.save()
            login(request, user)
            return redirect('/')
        else:
            return render(request, 'register.html', {'form': form})
    else:
        form = SignUpForm()
        return render(request, 'register.html', {'form': form})
Ejemplo n.º 30
0
def signup(request):
    if (request.user.is_authenticated):
        return redirect('home')
    if request.method == 'POST':
        form = SignUpForm(request.POST)
        if form.is_valid():
            user = form.save()
            auth_login(request, user)
            return redirect('home')
    else:
        form = SignUpForm()
    return render(request, 'signup.html', {'form': form})
Ejemplo n.º 31
0
def signup(request):
    """ simple signup for users """
    if request.method == 'POST':
        form = SignUpForm(request.POST)
        if form.is_valid():
            username, password = form.save()
            novice = authenticate(username=username, password=password)
            login(request, novice)
            return HttpResponseRedirect("/document/")
    else:
        form = SignUpForm()

    return render(request, "account/signup.html", {'form': form})
Ejemplo n.º 32
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)
            #add_blog_permission = Permission.objects.get(name='Can add blog post')
            #change_blog_permission = Permission.objects.get(name='Can change blog post')
            #user.user_permissions.add(add_blog_permission)
            #user.user_permissions.add(change_blog_permission)
            login(request, user)
            return redirect('/')
    else:
        form = SignUpForm()
    return render(request, 'account/signup.html', {'form': form})