How to access the request.user in a Piston classme

2019-02-18 22:58发布

问题:

I have a model which contains a ManyToMany to User to keep track of which users have 'favorited' a particular model instance.

In my API for this model, when requested by an authenticated user, I'd like to include an 'is_favorite' boolean. However, it seems that any api fields that aren't straight model attributes must be implemented as a class method, which when called in Piston does not get a reference to the request object, and therefore I have no way to know who the current user is.

From the Piston docs:

In addition to these, you may define any other methods you want. You can use these by including their names in the fields directive, and by doing so, the function will be called with a single argument: The instance of the model. It can then return anything, and the return value will be used as the value for that key.

So, if only the Piston CRUD methods get an instance of the request, how can my classmethod fields generate output which is relevant to the current authenticated user?

回答1:

I am not aware of the piston API, but how about using the thread locals middleware to access the request

add this to middleware

try:                                                                    
    from threading import local                                         
except ImportError:                                                     
    from django.utils._threading_local import local                     

_thread_locals = local()                                                
def get_request():                                                
    return getattr(_thread_locals, 'request', None)                       

class ThreadLocals(object):                                             
    def process_request(self, request):                                 
        _thread_locals.request = request

and update the settings with the ThreadLocals middleware

and wherever you want to access the request import get_request from middleware

if you want to just get the current user, modify the middleware to set only request.user in thread locals



回答2:

From the piston wiki page it says that you may specify the contents of foreign keys and many to many fields by nesting attributes. In your case

class FriendHandler(BaseHandler):
    allowed_methods = ('GET',)
    model = User
    fields = ('userfield_1', 'userfield_2', ('friends', ('is_friended')))

    def read(self, request):
        # Anything else you might want to do, but return an object of type User
        # Or whatever your model happens to be called

EDIT: Another slightly hacky way to do it (if you don't want the friend to get passed at all if the is_friended is false) would be to manually create a dict object structured how you like, and then return it. piston processes the dict a works with the built in emitters (the JSON one for sure, haven't tried the others)