summaryrefslogtreecommitdiff
path: root/forums/views.py
blob: a38d6b78b8308cee8b832a332c67050533fb891b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
from django.http import HttpResponse, HttpResponseRedirect
from django.contrib.auth import login, logout, authenticate
from django.shortcuts import render_to_response
from django.core.context_processors import csrf

from forums.forms import UserLoginForm

def user_login(request):
    if request.user.is_anonymous():
        if request.method == 'POST':
            username = request.POST['username']
            password = request.POST['password']
            user = authenticate(username=username, password=password)
            if user is not None:
                if user.is_active:
                    login(request, user)
                    if 'next' in request.POST:
                        next_url = request.POST.get('next')
                        return HttpResponseRedirect(next_url)
                    return HttpResponseRedirect('/')
                else:
                    return HttpResponse('you are blocked')
            else:
                return HttpResponse('Invalid username or password')
        else:
            form = UserLoginForm()
            next_url = request.GET.get('next')
            context = {
                'form': form,
                'next': next_url
            }
            context.update(csrf(request))
            return render_to_response('forums/templates/user-login.html', context)
    else:
        return HttpResponseRedirect('/')

def user_logout(request):
    logout(request)
    return HttpResponseRedirect('/')