示例#1
0
def sign_in(request):
    error_message = None

    if request.method == "POST":
        form = SignInForm(request.POST)
        if form.is_valid():
            user = authenticate(username=form.cleaned_data["user_name"], password=form.cleaned_data["password"])
            if user:
                if user.is_active:
                    login(request, user)
                    next_url = form.cleaned_data["next"]
                    if not next_url.startswith("/"):
                        raise PermissionDenied
                    return HttpResponseRedirect(next_url)
                else:
                    error_message = "Account disabled"
            else:
                error_message = "Bad username or password"
    else:
        next_url = request.GET.get("next") or "/users"
        form = SignInForm(initial={"next": next_url})

    env = common_env()
    env["form"] = form
    env["error_message"] = error_message
    return render(request, "users/signin.html", env)
示例#2
0
文件: views.py 项目: llennox/users
def sign_page(request):
    disabled_account = False
    incorrect_pass_or_username = False
    if request.user.is_authenticated():
        return render(request, 'sign_in.html')
    sign_in_form = SignInForm()
    if request.method == 'POST':
        form = SignInForm(request.POST)
        if form.is_valid():
            datas = {}
            datas['username'] = form.cleaned_data['username']
            datas['password1'] = form.cleaned_data['password1']
            user = authenticate(username=datas['username'],
                                password=datas['password1'])
            if user is not None:
                # the password verified for the user
                if user.is_active:
                    #print("User is valid, active and authenticated")
                    login(request, user)
                    #profil = Profil.objects.get(user=request.user)
                    return redirect('/user-view/')
                else:
                    disabled_account = True
                    #print("The password is valid, but the account has been disabled! you may need to verify email")
            else:
                # the authentication system was unable to verify the username and password
                #print("The username or password were incorrect.")
                incorrect_pass_or_username = True
    return render(
        request, 'sign_in.html', {
            'form': sign_in_form,
            'disabled_account': disabled_account,
            'incorrect': incorrect_pass_or_username
        })  #add error messages here!!!!
示例#3
0
def sign_up(request):
    if request.method == 'GET':
        if request.user.is_authenticated:
            return redirect('views:home')
        form = SignUpForm()
        fields = list(form)
        context = {
            "required": fields[:4],
            "genre": fields[4],
            "instrument": fields[5],
            "sign_in": SignInForm(),
        }
        return render(request, 'sign/signup.html', context)

    elif request.method == 'POST':
        form = SignUpForm(request.POST)

        if form.is_valid():
            user = form.save(commit=False)
            user.set_password(form.cleaned_data.get('password1'))
            user.save()
            # m2m 관계 필드 폼으로 받아서 저장
            form.save_m2m()
            login(request, user, backend='django.contrib.auth.backends.ModelBackend')
            return redirect('views:home')
        else:
            form = SignUpForm(request.POST)
            fields = list(form)
            context = {
                "required": fields[:4],
                "genre": fields[4],
                "instrument": fields[5],
                "sign_in": SignInForm(),
            }
            return render(request, 'sign/signup.html', context)
示例#4
0
def sign_in(request):
    error_message = None

    if request.method == 'POST':
        form = SignInForm(request.POST)
        if form.is_valid():
            user = authenticate(username=form.cleaned_data['user_name'],
                                password=form.cleaned_data['password'])
            if user:
                if user.is_active:
                    login(request, user)
                    next_url = form.cleaned_data['next']
                    if not next_url.startswith('/'):
                        raise PermissionDenied
                    return HttpResponseRedirect(next_url)
                else:
                    error_message = "Account disabled"
            else:
                error_message = "Bad username or password"
    else:
        next_url = request.GET.get('next') or reverse('blog-home')
        form = SignInForm(initial={'next': next_url})

    env = common_env()
    env['form'] = form
    env['error_message'] = error_message
    return render(request, 'users/signin.html', env)
示例#5
0
def sign_in(request):
    error_message = None

    if request.method == 'POST':
        form = SignInForm(request.POST)
        if form.is_valid():
            user = authenticate(username=form.cleaned_data['user_name'],
                    password=form.cleaned_data['password'])
            if user:
                if user.is_active:
                    login(request, user)
                    next_url = form.cleaned_data['next']
                    if not next_url.startswith('/'):
                        raise PermissionDenied
                    return HttpResponseRedirect(next_url)
                else:
                    error_message = "Account disabled"
            else:
                error_message = "Bad username or password"
    else:
        next_url = request.GET.get('next') or reverse('blog-home')
        form = SignInForm(initial={'next': next_url})

    env = common_env()
    env['form'] = form
    env['error_message'] = error_message
    return render(request, 'users/signin.html', env)
示例#6
0
 def post(self):
     form = SignInForm()
     if form.validate_on_submit():
         user = User.get_logged(request.form['email'],
                                request.form['password'])
         if user is not None:
             login_user(user)
             return redirect(url_for('users.profile'))
     return self.get()
示例#7
0
 def post(self, request):
     next_page = self.get_next_page(request=request)
     form = SignInForm(request.POST)
     if form.is_valid():
         user = authenticate(username=form.cleaned_data.get('username'),
                             password=form.cleaned_data.get('password'),
                             request=request)
         if user is not None:
             login(request, user)
             return redirect(next_page)
     data = {"form": form, "next": next_page}
     return render(request, self.template_name, data)
示例#8
0
def signin(request):
    if request.method == "POST":
        form = SignInForm(request.POST)
        if form.is_valid():
            u = authenticate(username=request.POST['username'],
                             password=request.POST['password'])
            if u is not None:
                login(request, u)
                messages.add_message(request, messages.INFO,
                                     'Welcome {}'.format(u))
            else:
                messages.add_message(request, messages.ERROR,
                                     "Invalid credentials!")
    return redirect('index')
示例#9
0
def users(request):
    u = User.objects.all()
    form2 = SignInForm()
    return render(request, 'users.html', {
        'users': u,
        'form2': form2,
        'title': 'Users'
    })
示例#10
0
def sign_up_index(request):
    if request.method == 'GET':
        if request.user.is_authenticated:
            return redirect('views:home')
        context = {
            "google_client_id": settings.GOOGLE_CLIENT_ID,
            "sign_in": SignInForm(),
        }
        return render(request, 'sign/signup-index.html', context)
示例#11
0
文件: views.py 项目: joway/DjangoSeed
def login(request):
    signin_form = SignInForm()
    from_path = request.GET.get('next', '')
    if request.method != 'POST':
        return render(request, 'login.html' + '?next=' + from_path, locals())

    from_path = request.POST.get('next', '')
    signin_form = SignInForm(request.POST)
    if not signin_form.is_valid():
        return render(request, 'login.html', locals())

    email = signin_form.cleaned_data['email']
    password = signin_form.cleaned_data['password']

    try:
        user = authenticate(email=email, password=password)
        if not user:
            return render(request, 'login.html', locals())
    except User.DoesNotExist:
        return render(request, 'register.html', locals())

    django_login(request, user)
    return render(request, 'success.html', locals())
示例#12
0
def index(request):
    title = 'Home'
    p = Post.objects.filter(deleted=False).order_by('created_at').reverse()
    if request.method == 'POST':
        form = SignUpForm(request.POST)
        if form.is_valid():
            user = form.save()
            login(request, user)
            messages.add_message(request, messages.INFO, 'Welcome to our Blog!')
            return redirect('index')
    else:
        form = SignUpForm()
    form2 = SignInForm()
    return render(request, 'index.html', {'title': title, 'posts': p,
                                          'form': form, 'form2': form2})
示例#13
0
def sign_in(request):
    if request.method == 'POST':
        form = SignInForm(request.POST)
        if form.is_valid():
            form.login(request)
            return redirect('views:home')

    else:
        form = SignInForm()

    context = {
        "sign_in": form,
        "google_client_id": settings.GOOGLE_CLIENT_ID,
    }
    return render(request, 'sign/signin.html', context)
示例#14
0
def post(request, pk):
    p = get_object_or_404(Post, pk=pk, deleted=False)
    p.views += 1
    p.save()
    comments = Comment.objects.filter(post_id=pk).order_by('id')
    user = User.objects.get(pk=p.user_id.id)
    flag = False
    try:
        Like.objects.get(user_id=request.user.id, post_id=p.id)
        flag = True
    except:
        pass
    form2 = SignInForm()
    comment_form = CommentForm()
    return render(request, 'post.html', {'title': p.title, 'post': p,
                                         'comments': comments, 'user': user,
                                         'flag': flag, 'form2': form2,
                                         'comment_form': comment_form})
示例#15
0
def index(request):
    if request.user.is_anonymous:
        context = {
            "pop_users":
            User.objects.annotate(
                total=Sum('post__num_liked')).order_by('-total')[:8],
            "pop_posts":
            Post.objects.order_by('-num_liked')[:8],
            "recent_posts":
            Post.objects.order_by('-created_date')[:8],
            "sign_in":
            SignInForm(),
            "google_client_id":
            settings.GOOGLE_CLIENT_ID,
        }
        return render(request, 'index.html', context)

    else:
        return redirect('views:home')
示例#16
0
def user(request, pk):
    user = get_object_or_404(User, pk=pk)
    posts = Post.objects.filter(user_id=pk, deleted=False)
    form2 = SignInForm()

    if request.method == 'POST':
        form = EditProfileForm(request.POST, request.FILES)
        if form.is_valid(
        ) and form.cleaned_data['password'] == form.cleaned_data['confirm']:
            u = User.objects.get(id=request.user.pk)
            u.first_name = form.cleaned_data['first_name']
            u.last_name = form.cleaned_data['last_name']
            u.userdetails.bio = form.cleaned_data['bio']
            if form.cleaned_data['image'] is not None:
                u.userdetails.image = form.cleaned_data['image']
            u.set_password(form.cleaned_data['password'])
            u.save()
            update_session_auth_hash(request, u)
            messages.add_message(request, messages.INFO, "Profile edited!")
            return redirect('user', user.pk)
    else:
        form = EditProfileForm(
            initial={
                'first_name': user.first_name,
                'last_name': user.last_name,
                'bio': user.userdetails.bio
            })

    return render(
        request, 'user.html', {
            'user': user,
            'posts': posts,
            'form': form,
            'form2': form2,
            'title': user.username
        })
示例#17
0
文件: views.py 项目: K-DOT/it_blog
def sign_in(request):
    if request.method == 'GET':
        form = SignInForm()
    else:
        form = SignInForm(request.POST)
        if form.is_valid():
            username_or_email = form.data.get('username_or_email')
            password = form.data.get('password')
            user = authenticate(username=username_or_email, password=password)
            if user:
                if user.is_active:
                    login(request, user)
                    if 'remember' in request.POST:
                        request.session.set_expiry(1209600)  # 2 weeks
                    next = request.GET.get('next', settings.LOGIN_REDIRECT_URL)
                    return HttpResponseRedirect(next)
                else:
                    form.add_error(None, _('User is not active'))
            else:
                form.add_error(None, _('Username or password are incorrect'))

    return render(request, 'sign_in.html', {
        'form': form
    })
示例#18
0
 def get(self):
     form = SignInForm()
     return render_template('users/sign_in.html', form=form)
示例#19
0
def posts(request):
    form2 = SignInForm()
    p = Post.objects.filter(deleted=False).order_by('created_at').reverse()
    return render(request, 'posts.html', {'posts': p, 'form2': form2, 'title': 'Posts'})
示例#20
0
 def test_sign_in_form(self):
     user = self.make_user(username="******")
     data = {"username": user.username, "password": "******"}
     form = SignInForm(data)
     self.assertTrue(form.is_valid())
示例#21
0
 def test_sign_in_bad_username_form(self):
     self.make_user(username="******")
     data = {"username": "******", "password": "******"}
     form = SignInForm(data)
     self.assertFalse(form.is_valid())
示例#22
0
 def get(self, request):
     data = {"form": SignInForm(), "next": request.GET.get('next')}
     return render(request, self.template_name, data)