Commit 8cca4d48 authored by Jason Yeomans's avatar Jason Yeomans
Browse files

Added room and neighbourhood api query, other api improvements

parent be0c2af4
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import models, migrations
class Migration(migrations.Migration):
dependencies = [
('api', '0002_auto_20141009_2325'),
]
operations = [
migrations.AddField(
model_name='building',
name='neighbourhood',
field=models.CharField(default=None, max_length=100, blank=True),
preserve_default=False,
),
migrations.AlterField(
model_name='room',
name='bedB',
field=models.CharField(max_length=80, blank=True),
),
migrations.AlterField(
model_name='room',
name='bedC',
field=models.CharField(max_length=80, blank=True),
),
migrations.AlterField(
model_name='room',
name='bedD',
field=models.CharField(max_length=80, blank=True),
),
]
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import models, migrations
class Migration(migrations.Migration):
dependencies = [
('api', '0003_auto_20141015_0308'),
]
operations = [
migrations.RemoveField(
model_name='building',
name='neighbourhood',
),
]
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import models, migrations
class Migration(migrations.Migration):
dependencies = [
('api', '0004_remove_building_neighbourhood'),
]
operations = [
migrations.AddField(
model_name='building',
name='neighbourhood',
field=models.CharField(default=b'None', max_length=100, blank=True),
preserve_default=True,
),
]
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import models, migrations
class Migration(migrations.Migration):
dependencies = [
('api', '0005_building_neighbourhood'),
]
operations = [
migrations.AlterField(
model_name='building',
name='neighbourhood',
field=models.CharField(default=b'NA', max_length=100, choices=[(b'NA', b'None'), (b'AQ', b'Aquia'), (b'RA', b'Rappahannock'), (b'SH', b'Shenandoah')]),
),
]
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import models, migrations
class Migration(migrations.Migration):
dependencies = [
('api', '0006_auto_20141015_0319'),
]
operations = [
migrations.AlterField(
model_name='building',
name='neighbourhood',
field=models.CharField(default=b'na', max_length=100, choices=[(b'na', b'None'), (b'aq', b'Aquia'), (b'ra', b'Rappahannock'), (b'sh', b'Shenandoah')]),
),
]
......@@ -23,6 +23,20 @@ class Class(models.Model):
class Building(models.Model):
name = models.CharField(max_length=100)
NONE = 'na'
AQUIA = 'aq'
RAPPAHANNOCK = 'ra'
SHENANDOAH = 'sh'
NEIGHBOURHOOD_CHOICES = (
(NONE, 'None'),
(AQUIA, 'Aquia'),
(RAPPAHANNOCK, 'Rappahannock'),
(SHENANDOAH, 'Shenandoah'),
)
neighbourhood = models.CharField(max_length=100, choices=NEIGHBOURHOOD_CHOICES,
default=NONE)
address = models.ForeignKey('Address')
def __str__(self): # __unicode__ on Python 2
......
......@@ -7,4 +7,6 @@ urlpatterns = patterns('',
url(r'^building/(?P<building>.+)/(?P<room_number>\d+)$', views.room, name='room'),
url(r'^building/(?P<buildingName>[a-zA-Z]+)$', views.building, name='building'),
url(r'^buildings/$', views.buildings_list, name='buildings_list'),
url(r'^neighbourhood/(?P<nhood>[a-zA-Z]+)$', views.neighbourhood, name='neighbourhood'),
url(r'^room/(?P<building>[a-zA-Z]+)/(?P<room_number>\d+)$', views.room, name='room'),
)
from django.http import HttpResponse
import json
from api.models import Building, Room
# Create your views here.
......@@ -7,28 +9,70 @@ def index(request):
return HttpResponse("Hello, world. You're at the RoomList index.")
def buildings_list(request):
building_list = Building.objects.order_by('-name')[:5]
json = '{"buildings":['
building_list = Building.objects.order_by('name')[:5]
jsons = '{"buildings":['
for p in building_list:
json += '"'+p.__str__()+'",'
json = json[:-1]+']}'
return HttpResponse(json)
jsons += '"'+p.__str__()+'":"'+p.address.__str__()+'",'
jsons = jsons[:-1]+']}'
return HttpResponse(jsons)
def building(request, buildingName):
room_list = Room.objects.filter(building__name=""+buildingName)
json = '{"'+buildingName+'":['
for p in room_list:
json += '"'+p.number.__str__()+'":['
json += '"floor":'+p.floor.__str__()+',"bedA":"'+p.bedA.__str__()+'"'
if p.bedB.__str__() is not '':
json += ',"bedB":"'+p.bedB.__str__()+'"'
if p.bedC.__str__() is not '':
json += ',"bedC":"'+p.bedC.__str__()+'"'
if p.bedD.__str__() is not '':
json += ',"bedD":"'+p.bedD.__str__()+'"'
json += '],'
json = json[:-1]+']}'
return HttpResponse(json)
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)
def room(request, building, room_number):
return HttpResponse("You are looking up room number %s in %s" % (room_number, building))
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+'","residents":['
for p in room_obj:
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 += ']}'
return HttpResponse(jsons)
###################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))
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)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment