Difference between revisions of "Extensible Calendar"
(Adding REST) |
(→REST) |
||
Line 12: | Line 12: | ||
You recognize ''GET'' and ''POST'' from Module 2. All along, there were actually more than just these two request methods. REST simply takes advantage of all of them. | You recognize ''GET'' and ''POST'' from Module 2. All along, there were actually more than just these two request methods. REST simply takes advantage of all of them. | ||
− | ''' | + | '''Note:''' In practice, since older browsers do not support the additional HTTP request methods required by REST, the actual implementation of ''PUT'' and ''DELETE'' will most likely be ''POST'' with an additional header defining the request type. The frameworks takes care of this difference under the hood. |
== The Back End == | == The Back End == |
Revision as of 21:55, 19 October 2013
This page documents an example of using the Extensible Calendar Pro API with Django as the back end.
Contents
REST
The Extensible Calendar is going to connect to your server via AJAX calls. These calls conform to a web application paradigm known as REST, or REpresentational State Transfer. In particular, the application will automatically send requests with the following HTTP methods:
- GET when it wants to read something
- POST when it wants to create something
- PUT when it wants to change something
- DELETE when it wants to remove something
You recognize GET and POST from Module 2. All along, there were actually more than just these two request methods. REST simply takes advantage of all of them.
Note: In practice, since older browsers do not support the additional HTTP request methods required by REST, the actual implementation of PUT and DELETE will most likely be POST with an additional header defining the request type. The frameworks takes care of this difference under the hood.
The Back End
First, we will set up the back end.
Creating the App with Source Control
Start by making a new Django project and app.
$ django-admin.py startproject cse330calendar
$ cd cse330calendar
$ python manage.py startapp cal
Right now would be a good time to set up your IDE and source control. Create a new Komodo project in the "cse330calendar" directory, and set up a repo on the "cse330calendar" in SourceTree. Make frequent commits after each step of the process.
Initial Configuration
Go into settings.py and configure your database.
Enable the admin panel, which requires editing both settings.py and urls.py. Add the cal application to the admin panel by creating cal/admin.py with the following content:
from django.contrib import admin
# We will create these models in the next step
from cal.models import Cal, Event
admin.site.register(Cal)
admin.site.register(Event)
Finally, add "cal" to the INSTALLED_APPS list in settings.py.
Creating the Models
Set up the models required for the Extensible Calendar. We have already done this work for you. Copy the following code into your cal/models.py file.
from django.db import models
# Extensible wants two models: a "calendar" model and an "event" model.
# The "calendar" model represents a collection of events. Note that the end
# application supports multiple calendars in the same view, with the events
# in each calendar having a different background color.
#
# First we will define our calendar model, `Cal`, and then we will define
# our event model, `Event`.
class Cal(models.Model):
# Title of the calendar
title = models.CharField(max_length=50)
# Text description of the calendar
description = models.CharField(max_length=200)
# Color ID (1-32)
color = models.IntegerField(default=1)
# Boolean for whether the calendar is hidden by default
hidden = models.BooleanField(default=False)
class Event(models.Model):
# Declare a one-to-many relationship with Cal
cal = models.ForeignKey(Cal)
# Title of the event
title = models.CharField(max_length=50)
# DateTime of event start
start = models.DateTimeField()
# DateTime of event end
end = models.DateTimeField()
# Additional information that can be associated with an event:
loc = models.CharField(max_length=50) # Location
notes = models.CharField(max_length=200) # Notes
url = models.CharField(max_length=100) # URL
ad = models.BooleanField(default=False) # Is this an all-day event
rem = models.CharField(max_length=200) # Reminder
RESTful Routes
Our back end is going to display our models in only one way: through RESTful JSON. REST is a convention in web application development that involves