Changeset 10159

Show
Ignore:
Timestamp:
12/04/08 17:56:05 (5 weeks ago)
Author:
gmaps
Message:

json decorators

Location:
trunk/src/astrometry/net/portal
Files:
2 modified

Legend:

Unmodified
Added
Removed
  • trunk/src/astrometry/net/portal/api.py

    r10156 r10159  
    11from django.contrib import auth 
    22from django.contrib.auth import authenticate 
    3 from django.contrib.auth.decorators import login_required 
     3#from django.contrib.auth.decorators import login_required 
    44from django.contrib.auth.models import User 
    55 
     
    4747        super(HttpResponseJson, self).__init__(doc, content_type=json_type) 
    4848 
    49 #def json_auth_token_decorator(func): 
    50  
    5149def create_session(key): 
    5250    from django.conf import settings 
     
    5654    return sess 
    5755 
     56# decorator for extracting JSON arguments from a POST. 
     57def requires_json_args(handler): 
     58    def handle_request(request, *pargs, **kwargs): 
     59        print 'requires_json_args decorator running.' 
     60        json = request.POST.get('request-json') 
     61        if not json: 
     62            return HttpResponseErrorJson('no json') 
     63        args = json2python(json) 
     64        if args is None: 
     65            return HttpResponseErrorJson('failed to parse JSON: ', json) 
     66        request.json = args 
     67        print 'json:', request.json 
     68        return handler(request, *pargs, **kwargs) 
     69    return handle_request 
     70 
     71# decorator for retrieving the user's session based on a session key in JSON. 
     72# requires "request.json" to exist: you probably need to precede this decorator 
     73# by the "requires_json_args" decorator. 
     74def requires_json_session(handler): 
     75    def handle_request(request, *args, **kwargs): 
     76        print 'requires_json_session decorator running.' 
     77        if not 'session' in request.json: 
     78            return HttpResponseErrorJson('no "session" in JSON.') 
     79        key = request.json['session'] 
     80        session = create_session(key) 
     81        if not session.exists(key): 
     82            return HttpResponseErrorJson('no session with key "%s"' % key) 
     83        request.session = session 
     84        print 'session:', request.session 
     85        resp = handler(request, *args, **kwargs) 
     86        session.save() 
     87        # remove the session from the request so that SessionMiddleware 
     88        # doesn't try to set cookies. 
     89        del request.session 
     90        return resp 
     91    return handle_request 
     92 
     93def requires_json_login(handler): 
     94    def handle_request(request, *args, **kwargs): 
     95        print 'requires_json_login decorator running.' 
     96        user = auth.get_user(request) 
     97        print 'user:', request.session 
     98        if not user.is_authenticated(): 
     99            return HttpResponseErrorJson('user is not authenticated.') 
     100        return handler(request, *args, **kwargs) 
     101    return handle_request 
     102     
    58103def login(request): 
    59104    json = request.POST.get('request-json') 
     
    112157                              }) 
    113158 
     159@requires_json_args 
    114160def amiloggedin(request): 
    115     json = request.POST.get('request-json') 
    116     if not json: 
    117         return HttpResponseErrorJson('no json') 
    118     args = json2python(json) 
    119     if args is None: 
    120         return HttpResponseErrorJson('failed to parse JSON: ', json) 
    121     if not 'session' in args: 
     161    if not 'session' in request.json: 
    122162        return HttpResponseJson({ 'loggedin': False, 
    123163                                  'reason': 'no session in args'}) 
    124     key = args['session'] 
     164    key = request.json['session'] 
    125165    session = create_session(key) 
    126166    if not session.exists(key): 
     
    137177                              'username': user.username}) 
    138178 
     179 
     180@requires_json_args 
     181@requires_json_session 
     182@requires_json_login 
    139183def logout(request): 
    140184    return HttpResponse('Not implemented.') 
  • trunk/src/astrometry/net/portal/test_api.py

    r10155 r10159  
    4545        print 'response is', r2 
    4646 
     47    def test_logout(self): 
     48        print 
     49        print 'test_logout: logging in.' 
     50        print 
     51        resp = self.login_with(self.u1, self.p1) 
     52        py = json2python(resp.content) 
     53        print 'response args: ', py 
     54        print 'cookies:', self.client.cookies 
     55 
     56        self.assert_('session' in py) 
     57        key = py['session'] 
     58        args = {'session': key} 
     59        json = python2json(args) 
     60        print 
     61        print 'test_logout: logging out.' 
     62        print 
     63        r2 = self.client.post(reverse('astrometry.net.portal.api.logout'), 
     64                              { 'request-json': json}) 
     65        print 'response is', r2 
     66        print 'cookies:', self.client.cookies