views.py 2.36 KB
Newer Older
1 2 3 4 5 6
from django.http import HttpResponse, HttpResponseRedirect
from django.shortcuts import render
from django.template import RequestContext, loader
from django.contrib.auth.forms import AuthenticationForm
from django.contrib.auth import authenticate, logout

7
from housing.models import Building, Room
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 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63

# Create your views here.
def index(request):
    template = loader.get_template('index.html')
    context = RequestContext(request, {
    })
    return HttpResponse(template.render(context))

def buildings(request):
    building_list = Building.objects.order_by('name')[:5]
    template = loader.get_template('buildings.html')
    context = RequestContext(request, {
        'building_list' : building_list,
    })
    return HttpResponse(template.render(context))

def building(request, buildingName):
    building = Building.objects.get(name__iexact=''+buildingName)
    room_list = Room.objects.filter(building__name=''+building.name).order_by('number')
    template = loader.get_template('building.html')
    context = RequestContext(request, {
        'building' : building,
        'room_list' : room_list,
    })
    return HttpResponse(template.render(context))

def login(request):
     # if this is a POST request we need to process the form data
    if request.method == 'POST':
        # create a form instance and populate it with data from the request:
        # check whether it's valid:
        username = request.POST['username']
        password = request.POST['password']
        user = authenticate(username=username, password=password)
        if user is not None:
            # the password verified for the user
            if user.is_active:
                print("User is valid, active and authenticated")
            else:
                print("The password is valid, but the account has been disabled!")
        else:
            # the authentication system was unable to verify the username and password
            print("The username and password were incorrect.")
        return HttpResponseRedirect('/')
    # if a GET (or any other method) we'll create a blank form
    else:
        form = AuthenticationForm()
        template = loader.get_template('login.html')
        context = RequestContext(request, {
            'form' : form,
        })
        return HttpResponse(template.render(context))

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