Created
December 2, 2013 19:50
-
-
Save erickgnavar/7757008 to your computer and use it in GitHub Desktop.
Django login required mixins
This file contains hidden or 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
| import json | |
| from django.contrib.auth.decorators import login_required | |
| from django.http import HttpResponse | |
| from django.utils.decorators import method_decorator | |
| class LoginRequiredMixin(object): | |
| @method_decorator(login_required) | |
| def dispatch(self, request, *args, **kwargs): | |
| return super(LoginRequiredMixin, self).dispatch(request, *args, **kwargs) | |
| class JsonLoginRequiredMixin(object): | |
| def dispatch(self, request, *args, **kwargs): | |
| data = { | |
| 'status': 'unauthorized' | |
| } | |
| if not request.user.is_authenticated(): | |
| return HttpResponse(json.dumps(data), mimetype='application/json') | |
| return super(JsonLoginRequiredMixin, self).dispatch(request, *args, **kwargs) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks!!