Created
February 1, 2012 10:10
-
-
Save namsral/1716333 to your computer and use it in GitHub Desktop.
Mixin for the Djando Rest Framework (DFR) to filter objects by authenticated user.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class AuthenticatedUserResourceMixin(ResourceMixin): | |
permissions = (IsAuthenticated,) | |
def build_query(self, *args, **kwargs): | |
tmp = dict(kwargs) | |
if BaseRenderer._FORMAT_QUERY_PARAM in tmp: | |
del tmp[BaseRenderer._FORMAT_QUERY_PARAM] | |
if args: | |
tmp.update({'pk': args[-1]}) | |
# Return objects from authenticated users only. | |
user = self.request.user if self.request.user.is_authenticated() else 0 | |
tmp.update({'user':user}) | |
return Q(**tmp) | |
class InstanceUserMessageView(AuthenticatedUserResourceMixin, InstanceModelView): | |
resource = UserMessageResource |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment