Commit a4301c4e authored by Daniel W Bond's avatar Daniel W Bond

Merge branch 'api' into 'amherst'

merge api into amherst

hardly to say that the API is done, but that it's functional to an extremely limited extent, enough to replace the previous version

See merge request !24
parents cd4ca351 857536ee
Django==1.7.3
Whoosh==2.6.0
argparse==1.2.1
beautifulsoup4==4.3.2
Django==1.8.9
argparse==1.2.1
django-allauth==0.20.0
django-analytical==0.19.0
django-autoslug==1.7.2
django-braces==1.4.0
git+https://github.com/kstateome/django-cas.git
django-crispy-forms==1.4.0
git+https://github.com/the-ben-waters/django-cas.git
django-filter==0.12.0
django-gravatar2==1.1.4
django-haystack==2.3.1
django-localflavor==1.0
......@@ -16,7 +16,9 @@ django-multiselectfield==0.1.3
django-randomslugfield==0.3.0
django-ratelimit==0.6.0
django-redis-cache==0.13.0
djangorestframework==3.3.2
flake8==2.4.0
Markdown==2.6.5
mccabe==0.3
MySQL-python==1.2.5
oauthlib==0.7.2
......@@ -28,3 +30,5 @@ requests==2.5.1
requests-oauthlib==0.4.2
six==1.8.0
wsgiref==0.1.2
wheel==0.24.0
Whoosh==2.6.0
# standard library imports
from __future__ import absolute_import, print_function
# core django imports
from django.core.serializers import serialize
# third party imports
from rest_framework import serializers
# imports from your apps
from housing.models import Building, Floor, Room
from accounts.models import Major
class BuildingSerializer(serializers.ModelSerializer):
# floors
# rooms
class Meta:
model = Building
fields = ('name', 'neighbourhood', 'campus')
class FloorSerializer(serializers.ModelSerializer):
building = serializers.SerializerMethodField('get_building_name')
#rooms = serializers.SerializerMethodField('get_floor_rooms')
def get_building_name(self, floor):
return floor.building.name
def get_floor_rooms(self, floor):
rooms = serialize('json', Room.objects.filter(floor=floor),
fields=('number'))
return rooms
class Meta:
model = Floor
fields = ('number', 'building', )#'rooms')
class RoomSerializer(serializers.ModelSerializer):
building = serializers.SerializerMethodField('get_building_name')
floor = serializers.SerializerMethodField('get_floor_number')
def get_building_name(self, room):
return room.floor.building.name
def get_floor_number(self, room):
return room.floor.number
class Meta:
model = Room
fields = ('number', 'floor', 'building')
class MajorSerializer(serializers.ModelSerializer):
class Meta:
model = Major
fields = ('name', )
from django.conf.urls import patterns, url
# standard library imports
from __future__ import absolute_import, print_function
# core django imports
from django.conf.urls import patterns, include, url
# third party imports
from rest_framework.routers import DefaultRouter
# imports from your apps
from .views import BuildingAPI, FloorAPI, RoomAPI, MajorAPI
from api import views
urlpatterns = patterns('',
url(r'^$', views.index, name='index'),
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'),
)
router = DefaultRouter()
router.register(r'buildings', BuildingAPI)
router.register(r'floors', FloorAPI)
router.register(r'rooms', RoomAPI)
router.register(r'majors', MajorAPI)
urlpatterns = router.urls
from django.conf.urls import patterns, url
from api import views
urlpatterns = patterns('',
url(r'^$', views.index, name='index'),
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'),
)
# standard library imports
from __future__ import absolute_import, print_function
# core django imports
from django.http import HttpResponse
# third party imports
import json
from rest_framework.viewsets import ReadOnlyModelViewSet
from rest_framework.pagination import PageNumberPagination
# imports from your apps
from housing.models import Building, Room
from housing.models import Building, Floor, Room
from accounts.models import Major
from .serializers import (BuildingSerializer, FloorSerializer, RoomSerializer,
MajorSerializer)
def index(request):
return HttpResponse("Hello, world. You're at the Roomlist index.")
class HousingPagination(PageNumberPagination):
page_size = 25
page_size_query_param = 'page_size'
max_page_size = 1000
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)
class BuildingAPI(ReadOnlyModelViewSet):
queryset = Building.objects.all()
serializer_class = BuildingSerializer
pagination_class = HousingPagination
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)
class FloorAPI(ReadOnlyModelViewSet):
queryset = Floor.objects.all()
serializer_class = FloorSerializer
pagination_class = HousingPagination
def room(request, building, room_number):
room_obj = Room.objects.filter(building__name=''+building, number=room_number)
class RoomAPI(ReadOnlyModelViewSet):
queryset = Room.objects.all()
serializer_class = RoomSerializer
pagination_class = HousingPagination
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)
###################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)
class MajorAPI(ReadOnlyModelViewSet):
queryset = Major.objects.all()
serializer_class = MajorSerializer
# standard library imports
from __future__ import absolute_import, print_function
# core django imports
from django.http import HttpResponse
# third party imports
import json
# imports from your apps
from housing.models import Building, Room
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]
jsons = '{"buildings":['
for p in building_list:
jsons += '"'+p.__str__()+'":"'+p.address.__str__()+'",'
jsons = jsons[:-1]+']}'
return HttpResponse(jsons)
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)
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)
###################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)
......@@ -11,7 +11,7 @@ from autoslug import AutoSlugField
class Building(TimeStampedModel):
name = models.CharField(max_length=100)
name = models.CharField(max_length=100, unique=True)
NONE = 'na'
AQUIA = 'aq'
......@@ -47,7 +47,6 @@ class Building(TimeStampedModel):
def get_absolute_url(self):
return reverse('detail_building', kwargs={
'building': slugify(self.building_name),
'slug': self.slug,
})
def __str__(self): # __unicode__ on Python 2
......@@ -92,7 +91,6 @@ class Floor(TimeStampedModel):
return reverse('detail_floor', kwargs={
'building': slugify(self.building.building_name),
'floor': self.floor_num,
'slug': self.slug,
})
def __str__(self): # __unicode__ on Python 2
......@@ -115,6 +113,7 @@ class Floor(TimeStampedModel):
class Meta:
ordering = ['building', 'number']
unique_together = ('number', 'building')
class Room(TimeStampedModel):
......@@ -130,7 +129,6 @@ class Room(TimeStampedModel):
'floor': self.floor.floor_num,
'building': slugify(self.floor.building.building_name),
'room': self.room_num,
'slug': self.slug,
})
def __str__(self): # __unicode__ on Python 2
......@@ -147,3 +145,4 @@ class Room(TimeStampedModel):
class Meta:
ordering = ['number']
unique_together = ('number', 'floor',)
......@@ -10,13 +10,13 @@ urlpatterns = patterns('',
url(r'^$', ListBuildings.as_view(), name='list_buildings'),
url(r'^(?P<slug>[\w-]+)/(?P<building>[\w-]+)/$',
url(r'^(?P<building>[\w-]+)/$',
DetailBuilding.as_view(), name='detail_building'),
url(r'^(?P<slug>[\w-]+)/(?P<building>[\w-]+)/(?P<floor>[\w-]+)/$',
url(r'^(?P<building>[\w-]+)/(?P<floor>[\w-]+)/$',
DetailFloor.as_view(), name='detail_floor'),
url(r'^(?P<slug>[\w-]+)/(?P<building>[\w-]+)/(?P<floor>[\w-]+)/(?P<room>[\w-]+)/$',
url(r'^(?P<building>[\w-]+)/(?P<floor>[\w-]+)/(?P<room>[\w-]+)/$',
DetailRoom.as_view(), name='detail_room'),
)
......@@ -36,6 +36,13 @@ class DetailBuilding(LoginRequiredMixin, DetailView):
login_url = 'login'
def get_object(self):
url_parts = self.request.get_full_path().split('/')
# [u'', u'housing', u'building',]
building_name = url_parts[2].replace('-', ' ').title()
building = Building.objects.get(name=building_name)
return building
def get_context_data(self, **kwargs):
context = super(DetailBuilding, self).get_context_data(**kwargs)
context['floors'] = Floor.objects.filter(building=self.get_object()).order_by('number')
......@@ -48,6 +55,16 @@ class DetailFloor(LoginRequiredMixin, DetailView):
context_object_name = 'floor'
template_name = 'detail_floor.html'
def get_object(self):
url_parts = self.request.get_full_path().split('/')
# [u'', u'housing', u'building', u'floor', ]
building_name = url_parts[2].replace('-', ' ').title()
floor_number = int(url_parts[3])
building = Building.objects.get(name=building_name)
floor = Floor.objects.get(number=floor_number,
building=building)
return floor
def get_context_data(self, **kwargs):
context = super(DetailFloor, self).get_context_data(**kwargs)
......@@ -64,6 +81,19 @@ class DetailRoom(LoginRequiredMixin, DetailView):
context_object_name = 'room'
template_name = 'detail_room.html'
def get_object(self):
url_parts = self.request.get_full_path().split('/')
# [u'', u'housing', u'building', u'floor', u'room', ]
building_name = url_parts[2].replace('-', ' ').title()
floor_number = int(url_parts[3])
room_number = int(url_parts[4])
building = Building.objects.get(name=building_name)
floor = Floor.objects.get(number=floor_number,
building=building)
room = Room.objects.get(floor=floor,
number=room_number)
return room
def get_context_data(self, **kwargs):
context = super(DetailRoom, self).get_context_data(**kwargs)
......
......@@ -79,6 +79,7 @@ INSTALLED_APPS = (
'randomslugfield',
'haystack',
'multiselectfield',
'rest_framework',
# social media authentication
'allauth',
'allauth.account',
......@@ -100,7 +101,7 @@ MIDDLEWARE_CLASSES = (
'django.contrib.sessions.middleware.SessionMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'cas.middleware.CASMiddleware',
'django.middleware.doc.XViewMiddleware',
'django.contrib.admindocs.middleware.XViewMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
......
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