Difference between revisions of "Extensible Calendar"
(→REST) |
(Finishing back end) |
||
Line 97: | Line 97: | ||
</source> | </source> | ||
− | + | Finally, sync the db. After you do this, run the Django server and play around with the admin panel to make sure everything is in working order. | |
− | + | <source lang="bash"> | |
+ | $ python manage.py syncdb | ||
+ | </source> | ||
+ | |||
+ | === Tastypie === | ||
+ | |||
+ | Django does not come with REST support out of the box, so we need to install an additional framework. I have so far had success with '''[http://django-tastypie.readthedocs.org/en/latest/tutorial.html Tastypie]''', and I will be using Tastypie throughout this tutorial. | ||
+ | |||
+ | Start by installing Tastypie. There are instructions on the above link. | ||
+ | |||
+ | Your ''cal/api.py'' file can contain simply: | ||
+ | |||
+ | <source lang="python"> | ||
+ | from tastypie.resources import ModelResource | ||
+ | from tastypie.paginator import Paginator | ||
+ | from cal.models import Cal, Event | ||
+ | from tastypie.fields import IntegerField | ||
+ | |||
+ | class CalResource(ModelResource): | ||
+ | class Meta: | ||
+ | queryset = Cal.objects.all() | ||
+ | paginator_class = Paginator | ||
+ | |||
+ | class EventResource(ModelResource): | ||
+ | cal_id = IntegerField(attribute="cal__id") # load the calendar ID as an integer | ||
+ | class Meta: | ||
+ | queryset = Event.objects.all() | ||
+ | paginator_class = Paginator | ||
+ | </source> | ||
+ | |||
+ | In the above snippet, we also are installing the ''Paginator'' utility. The Extensible Calendar requires that your data have pagination. | ||
+ | |||
+ | By this time, your ''urls.py'' file will look something like this: | ||
+ | |||
+ | <source lang="python"> | ||
+ | from django.conf.urls import patterns, include, url | ||
+ | from cal.api import CalResource, EventResource | ||
+ | |||
+ | # Uncomment the next two lines to enable the admin: | ||
+ | from django.contrib import admin | ||
+ | admin.autodiscover() | ||
+ | |||
+ | # RESTful setup: | ||
+ | cal_resource = CalResource() | ||
+ | event_resource = EventResource() | ||
+ | |||
+ | urlpatterns = patterns('', | ||
+ | # Admin page | ||
+ | url(r'^admin/', include(admin.site.urls)), | ||
+ | |||
+ | # Templates | ||
+ | url(r'^', include('cal.urls')), | ||
+ | |||
+ | # RESTful URLs | ||
+ | url(r'^', include(cal_resource.urls)), | ||
+ | url(r'^', include(event_resource.urls)), | ||
+ | ) | ||
+ | </source> | ||
+ | |||
+ | I also recommend making JSON the default output format for Tastypie. To do this, add the following line to your ''settings.py'' file: | ||
+ | |||
+ | <source lang="python"> | ||
+ | TASTYPIE_DEFAULT_FORMATS = ['json'] | ||
+ | </source> | ||
+ | |||
+ | Try launching [http://localhost:8000/cal/ http://localhost:8000/cal/] in your browser. If everything is working properly, you should see a JSON representation of the data in your calendar. | ||
+ | |||
+ | === Additional Work on the Back End === | ||
+ | |||
+ | In the above example, we have implemented a read-only API for your calendar. You will need to use Django Auth to add users and to only reveal those calendars associated with a user. You will also need to implement the writing site of Tastypie. Almost all of these steps are in the Tastypie documentation. | ||
[[Category:Module 6]] | [[Category:Module 6]] |
Revision as of 22:08, 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
Finally, sync the db. After you do this, run the Django server and play around with the admin panel to make sure everything is in working order.
$ python manage.py syncdb
Tastypie
Django does not come with REST support out of the box, so we need to install an additional framework. I have so far had success with Tastypie, and I will be using Tastypie throughout this tutorial.
Start by installing Tastypie. There are instructions on the above link.
Your cal/api.py file can contain simply:
from tastypie.resources import ModelResource
from tastypie.paginator import Paginator
from cal.models import Cal, Event
from tastypie.fields import IntegerField
class CalResource(ModelResource):
class Meta:
queryset = Cal.objects.all()
paginator_class = Paginator
class EventResource(ModelResource):
cal_id = IntegerField(attribute="cal__id") # load the calendar ID as an integer
class Meta:
queryset = Event.objects.all()
paginator_class = Paginator
In the above snippet, we also are installing the Paginator utility. The Extensible Calendar requires that your data have pagination.
By this time, your urls.py file will look something like this:
from django.conf.urls import patterns, include, url
from cal.api import CalResource, EventResource
# Uncomment the next two lines to enable the admin:
from django.contrib import admin
admin.autodiscover()
# RESTful setup:
cal_resource = CalResource()
event_resource = EventResource()
urlpatterns = patterns('',
# Admin page
url(r'^admin/', include(admin.site.urls)),
# Templates
url(r'^', include('cal.urls')),
# RESTful URLs
url(r'^', include(cal_resource.urls)),
url(r'^', include(event_resource.urls)),
)
I also recommend making JSON the default output format for Tastypie. To do this, add the following line to your settings.py file:
TASTYPIE_DEFAULT_FORMATS = ['json']
Try launching http://localhost:8000/cal/ in your browser. If everything is working properly, you should see a JSON representation of the data in your calendar.
Additional Work on the Back End
In the above example, we have implemented a read-only API for your calendar. You will need to use Django Auth to add users and to only reveal those calendars associated with a user. You will also need to implement the writing site of Tastypie. Almost all of these steps are in the Tastypie documentation.