Working on API

master
Nick Sergeant 2011-06-20 23:53:54 -04:00
parent d4dc37e430
commit 37fedf10f2
6 changed files with 54 additions and 0 deletions

0
api/__init__.py Normal file
View File

31
api/handlers.py Normal file
View File

@ -0,0 +1,31 @@
from piston.handler import AnonymousBaseHandler, BaseHandler
from piston.utils import rc
from snipts.models import Snipt
class AnonSniptHandler(AnonymousBaseHandler):
model = Snipt
fields = ('title',)
def read(self, request, snipt_id):
return super(self.read)
class SniptHandler(BaseHandler):
allowed_methods = ('GET',)
anonymous = AnonSniptHandler
exclude = ('id',)
model = Snipt
def read(self, request, snipt_id):
"""
Returns an individual public or private snipt.
"""
if snipt_id:
try:
return Snipt.objects.get(pk=snipt_id, public=True)
except Snipt.DoesNotExist:
return rc.NOT_FOUND
else:
return rc.BAD_REQUEST

17
api/urls.py Normal file
View File

@ -0,0 +1,17 @@
from django.conf.urls.defaults import *
from piston.emitters import Emitter
from piston.resource import Resource
from api.handlers import SniptHandler
Emitter.unregister('django')
Emitter.unregister('pickle')
Emitter.unregister('yaml')
snipt_handler = Resource(SniptHandler)
urlpatterns = patterns('',
url(r'^snipt/(?P<snipt_id>\d+)/', snipt_handler),
)

View File

@ -13,6 +13,9 @@
-e git+http://github.com/benoitc/gunicorn.git#egg=gunicorn
-e hg+ssh://hg@bitbucket.org/nicksergeant/dwfab#egg=dwfab
# API
-e hg+ssh://hg@bitbucket.org/jespern/django-piston#egg=django-piston
BeautifulSoup
django-taggit
django-postmark

View File

@ -60,6 +60,7 @@ INSTALLED_APPS = (
'compressor',
'django_bcrypt',
'piston',
'south',
'taggit',

View File

@ -14,5 +14,7 @@ urlpatterns = patterns('',
url(r'^404/$', direct_to_template, {'template': '404.html'}),
url(r'^500/$', direct_to_template, {'template': '500.html'}),
url(r'^api/', include('api.urls')),
url(r'^$', home),
)