...
 
......@@ -15,13 +15,22 @@ class Event(models.Model):
start_time = models.DateField(default=(datetime(1949, 10, 1)))
end_time = models.DateField(default=(datetime(1949, 10, 1)))
def make_dict(event):
return {
'uniqueid': event.uniqueid,
'title': event.title,
'locations': event.locations,
'description': event.description,
'starttimeiso': event.starttimeiso,
'dayofweek': event.dayofweek,
'duration': event.duration
}
#def __str__(self):
# return str(self.uniqueid) + ',' + self.title + ', ' + self.locations + ', ' + self.description + ', ' + self.start_time + ', ' + self.end_time
#doesn't insert event if unique id is already in database
def insert_event(self):
if self.uniqueid in [theEvent.uniqueid for theEvent in Event.objects.all()]:
return False
else:
......@@ -30,5 +39,4 @@ class Event(models.Model):
#give ban
def give_ban(self):
return u'༼ つ ◕_ ◕ ༽つ GIVE BAN ༼ つ ◕_ ◕ ༽つ'
return u'༼ つ ◕_ ◕ ༽つ GIVE BAN ༼ つ ◕_ ◕ ༽つ'
\ No newline at end of file
......@@ -3,6 +3,8 @@ from django.urls import path
from . import views
urlpatterns = [
path('', views.apiwelcome, name='apiwelcome'),
path('rawdata/', views.rawdata, name='rawdata')
path('', views.api_welcome, name='apiwelcome'),
path('rawdata/', views.raw_data, name='rawdata'),
path('event/<int:eventid>', views.event_by_id,
name='event by id'),
]
\ No newline at end of file
from django.shortcuts import render
from django.shortcuts import render, get_object_or_404
from django.http import HttpResponse
from .models import Event
......@@ -6,19 +6,18 @@ import json
# Create your views here.
def apiwelcome(request):
def api_welcome(request):
return render(request, 'api/apiwelcome.html')
def rawdata(request):
eventlist = []
for event in Event.objects.all():
eventlist.append({
'uniqueid': event.uniqueid,
'title': event.title,
'locations': event.locations,
'description': event.description,
'starttimeiso': event.start_time.isoformat(),
'endtimeiso': event.end_time.isoformat(),
})
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")
\ No newline at end of file
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