...
 
Commits (2)
......@@ -7,4 +7,5 @@ urlpatterns = [
path('rawdata/', views.raw_data, name='rawdata'),
path('event/<int:eventid>', views.event_by_id,
name='event by id'),
path('sort/by-date', views.sort_by_date, name='sort by date'),
]
\ No newline at end of file
......@@ -3,21 +3,51 @@ from django.http import HttpResponse
from .models import Event
import json
from datetime import date
# Create your views here.
def api_welcome(request):
return render(request, 'api/apiwelcome.html')
return render(request, 'api/apiwelcome.html')
def raw_data(request):
eventlist = [
event.make_dict()
for event in Event.objects.all()
]
return HttpResponse(json.dumps(eventlist, ensure_ascii=False),
content_type="application/json")
eventlist = [
event.make_dict()
for event in Event.objects.all()
]
return HttpResponse(json.dumps(eventlist, ensure_ascii=False),
content_type='application/json')
def event_by_id(request, eventid):
event = get_object_or_404(Event, uniqueid=eventid)
return HttpResponse(json.dumps([event.make_dict()], ensure_ascii=False),
content_type="application/json")
\ No newline at end of file
event = get_object_or_404(Event, uniqueid=eventid)
return HttpResponse(json.dumps([event.make_dict()], ensure_ascii=False),
content_type='application/json')
def sort_by_date(request):
sorted_events = Event.objects.all().order_by('start_time')
sorted_json = [] # the full json we'll return
curr_day_dict = {
'date': '',
'events': []
} # a dictionary with just the current day
for event in Event.objects.all().order_by('start_time'):
start_time = event.start_time
curr_date = date(start_time.year, start_time.month, start_time.day)
if curr_day_dict['date'] == str(curr_date):
curr_day_dict['events'].append(event.make_dict())
else:
if curr_day_dict['events'] != []: # is not empty
sorted_json.append(curr_day_dict)
# make the next day and throw this one in
curr_day_dict = {
'date': str(curr_date),
'events': [event.make_dict()]
}
# get the last day
sorted_json.append(curr_day_dict)
return HttpResponse(json.dumps(sorted_json, ensure_ascii=False),
content_type='application/json')
......@@ -19,7 +19,7 @@ from django.urls import path, include
from . import views
urlpatterns = [
path('', views.homepage, name='homepage'),
path('', views.homepage, name='homepage'),
path('admin/', admin.site.urls),
path('api/', include('api.urls')),
]
......@@ -3,4 +3,4 @@ from django.shortcuts import render
# Create some views
def homepage(request):
return render(request, 'homepage.html')
\ No newline at end of file
return render(request, 'homepage.html')
\ No newline at end of file