views.py 3.09 KB
Newer Older
Daniel W Bond's avatar
Daniel W Bond committed
1
# core django imports
2
from django.http import HttpResponse
Daniel W Bond's avatar
Daniel W Bond committed
3
# third party imports
4
import json
Daniel W Bond's avatar
Daniel W Bond committed
5
# imports from your apps
6
from housing.models import Building, Room
Daniel W Bond's avatar
Daniel W Bond committed
7

8
9

def index(request):
10
    return HttpResponse("Hello, world. You're at the Roomlist index.")
11

Daniel W Bond's avatar
Daniel W Bond committed
12

13
14
15
16
17
18
19
20
def buildings_list(request):
    building_list = Building.objects.order_by('name')[:5]
    jsons = '{"buildings":['
    for p in building_list:
        jsons += '"'+p.__str__()+'":"'+p.address.__str__()+'",'
    jsons = jsons[:-1]+']}'
    return HttpResponse(jsons)

Daniel W Bond's avatar
Daniel W Bond committed
21

22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
def building(request, buildingName):
    room_list = Room.objects.filter(building__name=''+buildingName).order_by('number')
    jsons = 'Building does not exist'
    if room_list:
        jsons = '{"name":"'+buildingName+'","rooms":['
        for p in room_list:
            jsons += '"'+p.number.__str__()+'":['
            jsons += '"floor":'+p.floor.__str__()+',"bedA":"'+p.bedA.__str__()+'"'
            if p.bedB.__str__() is not '':
                jsons += ',"bedB":"'+p.bedB.__str__()+'"'
            if p.bedC.__str__() is not '':
                jsons += ',"bedC":"'+p.bedC.__str__()+'"'
            if p.bedD.__str__() is not '':
                jsons += ',"bedD":"'+p.bedD.__str__()+'"'
            jsons += '],'
        jsons = jsons[:-1] + ']}'
    return HttpResponse(jsons)

Daniel W Bond's avatar
Daniel W Bond committed
40

41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
def room(request, building, room_number):
    room_obj = Room.objects.filter(building__name=''+building, number=room_number)

    jsons = "This room does not exist or has not been created"
    if room_obj:
        jsons = '{"building":"'+building+'","number":'+room_number+','
        for p in room_obj:
            jsons += '"floor":'+p.floor.__str__()+',"residents":["bedA":"'+p.bedA.__str__()+'"'
            if p.bedB.__str__() is not '':
                jsons += ',"bedB":"'+p.bedB.__str__()+'"'
            if p.bedC.__str__() is not '':
                jsons += ',"bedC":"'+p.bedC.__str__()+'"'
            if p.bedD.__str__() is not '':
                jsons += ',"bedD":"'+p.bedD.__str__()+'"'
            jsons += ']'
        jsons += ']}'
    return HttpResponse(jsons)

Daniel W Bond's avatar
Daniel W Bond committed
59

60
61
62
63
64
65
66
67
68
69
70
71
72
###################JASON trying to JSON in python, so confuzed:
#    if room_obj:
#        jsons = {'building':building, 'number':room_number, 'residents': []}
#        for p in room_obj:
#            jsons.residents[0] =  'bedA':p.bedA.__str__()
#            if p.bedB.__str__() is not '':
#                jsons.residents[1] = 'bedB':p.bedB.__str__()
#            if p.bedC.__str__() is not '':
#                jsons.residents[2] = 'bedC':p.bedC.__str__()
#            if p.bedD.__str__() is not '':
#                jsons.residents[3] = 'bedD':p.bedD.__str__()
#    return HttpResponse(json.dumps(jsons))

Daniel W Bond's avatar
Daniel W Bond committed
73

74
75
76
77
78
79
80
81
82
83
84
def neighbourhood(request, nhood):
    building_list = Building.objects.filter(neighbourhood=''+nhood).order_by('name')
    jsons = 'That neighbourhood has no buildings or does not exist'
    code = 404
    if building_list:
        code = 200
        jsons = '{"neighbourhood":"'+nhood+'","buildings":['
        for p in building_list:
            jsons += '"'+p.__str__()+'",'
        jsons = jsons[:-1]+']}'
    return HttpResponse(jsons, status=code)