This repository has been archived by the owner on Jun 29, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #54 from kawazrepos/new_api_interface
Refactoring for adding new API interface
- Loading branch information
Showing
48 changed files
with
572 additions
and
420 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# coding=utf-8 | ||
""" | ||
""" | ||
__author__ = 'Alisue <lambdalisue@hashnote.net>' | ||
from rest_framework import filters | ||
from kawaz.core.utils.permission import filter_with_perm | ||
|
||
|
||
class KawazObjectPermissionFilter(filters.BaseFilterBackend): | ||
""" | ||
A filter backend that limits results to those where the requesting user | ||
has read object level permissions. | ||
""" | ||
def filter_queryset(self, request, queryset, view): | ||
user = request.user | ||
queryset = filter_with_perm(user, queryset, 'view') | ||
return queryset |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# coding=utf-8 | ||
""" | ||
""" | ||
__author__ = 'Alisue <lambdalisue@hashnote.net>' | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
# coding=utf-8 | ||
""" | ||
""" | ||
__author__ = 'Alisue <lambdalisue@hashnote.net>' | ||
from rest_framework import viewsets | ||
from .filters import KawazObjectPermissionFilter | ||
|
||
|
||
class KawazModelViewSet(viewsets.ModelViewSet): | ||
author_field_name = None | ||
|
||
def pre_save(self, obj): | ||
if self.author_field_name and self.request.user.is_authenticated(): | ||
obj[self.author_field_name] = self.request.user | ||
|
||
def get_queryset(self): | ||
manager = self.model.objects | ||
if self.request.method == 'GET': | ||
return self.get_queryset_for_read(manager) | ||
return self.get_queryset_for_write(manager) | ||
|
||
def get_queryset_for_read(self, manager): | ||
if hasattr(manager, 'published'): | ||
return manager.published(self.request.user) | ||
return manager.all() | ||
|
||
def get_queryset_for_write(self, manager): | ||
if hasattr(manager, 'related'): | ||
return manager.related(self.request.user) | ||
return manager.all() | ||
|
||
def get_filter_backends(self): | ||
return (KawazObjectPermissionFilter,) |
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
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
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
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
Oops, something went wrong.