Login

Tastypie v0.9.11 LoginRequiredAuthorization

Author:
cotton
Posted:
August 16, 2012
Language:
Python
Version:
1.4
Tags:
login_required tastypie
Score:
0 (after 0 ratings)

This is an Authorization class for Tastypie v0.9.11 (v0.9.12 changes how Authorization works).

DjangoAuthorization checks specific permissions — add_model, change_model, delete_model, etc. If you don't need that level of permissions checking, this might be useful. It just makes sure the User is logged in. It's equivalent to the login_required decorator.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
from tastypie.authorization import Authorization


class LoginRequiredAuthorization(Authorization):
    """
    Equivalent to Django's login_required decorator
    Be careful with this; it will allow DELETE and everything else.

    """
    def is_authorized(self, request, object=None):
        # GET is always allowed
        if request.method == 'GET':
            return True
        return request.user.is_authenticated()


# Use like this in a ModelResource
class MyResource(ModelResource):
    class Meta:
        authorization = LoginRequiredAuthorization()
        allowed_methods = ['get', 'post', ...]  # pay attention to what you allow

More like this

  1. Tastypie MongoDB Resource by fatiherikli 2 years, 4 months ago
  2. View Permission Decorator Helper by jgeewax 6 years, 7 months ago
  3. Generic Permissions by muhuk 5 years, 9 months ago
  4. Class based generic views that automatically check permissions by humphreymurray 4 years, 1 month ago
  5. logging tastypie middleware by lalutium 3 months, 3 weeks ago

Comments

Please login first before commenting.