Login

simple app using RestViews

Author:
kuba
Posted:
January 16, 2009
Language:
Python
Version:
1.0
Tags:
rest
Score:
0 (after 0 ratings)

Inspired by Eric Florenzano's post about writing simple and very fast pure-WSGI applications. Using RestView approach. More about it on my blog

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
### in urls.py
from django.conf.urls.defaults import *
from bench.songs.views import Song, All

urlpatterns = patterns('',
    (r'^$', All()),
    (r'^song/(?P<id>\d+)/', Song()),
)

### in songs/views.py
from django.http import HttpResponse
from collections import defaultdict
## RestView from http://www.djangosnippets.org/snippets/1071/
from bench.rest import RestView

counts = defaultdict(int)

class All(RestView):
    
    def GET(self, request, *args):
        res = ','.join(['%s=%s' % (k, v) for k, v in counts.iteritems()])
        return HttpResponse(res,  mimetype='text/plain')
    
    def DELETE(self, request):
        counts = defaultdict(int)
        return HttpResponse('OK',  mimetype='text/plain')

class Song(RestView):
    
    def GET(self, request, id):
        return HttpResponse(str(counts[id]),  mimetype='text/plain')
    
    def POST(self, request, id):
        counts[id] += 1
        return HttpResponse(str(counts[id]), mimetype='text/plain')

More like this

  1. Integrate Tenjin using a decorator by mwdiers 7 years ago
  2. Example usage of SimpleCouchPaginator by toke 6 years, 6 months ago
  3. Database migration and dump/load script by akaihola 8 years, 1 month ago
  4. Mathematical Captcha by dogada 7 years, 5 months ago
  5. Simple login script from a nub by alandelevie 6 years, 5 months ago

Comments

Please login first before commenting.