-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
281 additions
and
44 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 |
---|---|---|
@@ -1,3 +1,4 @@ | ||
from horizon_contrib.api.base import ClientBase | ||
from horizon_contrib.api.managers import Manager | ||
from horizon_contrib.api.pagination import PaginationClient | ||
from horizon_contrib.api.managers import Manager, PaginatedManager | ||
from horizon_contrib.api.models import APIModel |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,4 @@ | ||
|
||
|
||
|
||
import copy | ||
|
||
import six | ||
|
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,67 @@ | ||
|
||
|
||
import logging | ||
from math import ceil | ||
|
||
from horizon import exceptions | ||
from django.utils import six | ||
from django.conf import settings | ||
from .base import ClientBase | ||
from .response import ListResponse | ||
|
||
LOG = logging.getLogger("client.base") | ||
|
||
API_RESULT_PAGE_SIZE = getattr(settings, 'API_RESULT_PAGE_SIZE', 25) | ||
|
||
|
||
class PaginatedListResponse(ListResponse): | ||
|
||
'''Extend response with pagination helpers''' | ||
|
||
def __init__(self, data, *args, **kwargs): | ||
self._data = data | ||
super(PaginatedListResponse, self).__init__( | ||
data.get('results'), *args, **kwargs) | ||
|
||
@property | ||
def count(self): | ||
return self._data.get('count') | ||
|
||
@property | ||
def previous(self): | ||
return self._data.get('previous', None) | ||
|
||
@property | ||
def next(self): | ||
return self._data.get('next', None) | ||
|
||
@property | ||
def page_range(self): | ||
'''little ugly piece of pagination''' | ||
page_size = float(API_RESULT_PAGE_SIZE) | ||
count = float(self.count) | ||
if ceil(count / page_size) > 0: | ||
return six.moves.range(1, int(ceil(count / page_size)) + 1) | ||
return six.moves.range(1, int(ceil(count / page_size))) | ||
|
||
|
||
class PaginationClient(ClientBase): | ||
'''Use pagination list reponse''' | ||
|
||
list_response_class = PaginatedListResponse | ||
|
||
def process_data(self, data, request): | ||
'''optionaly extract results from response''' | ||
|
||
# check if data are paginated | ||
if 'results' in data: | ||
response = self.list_response_class(data) | ||
else: | ||
response = data | ||
|
||
if 'errors' in data: | ||
if settings.DEBUG: | ||
raise Exception(data['errors']) | ||
exceptions.handle(request, ', '.join(data['errors'])) | ||
|
||
return response |
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 @@ | ||
|
||
|
||
import logging | ||
|
||
from horizon_contrib.utils.dotdict import dotdict | ||
|
||
LOG = logging.getLogger("client.base") | ||
|
||
|
||
class ListResponse(list): | ||
|
||
pass | ||
|
||
|
||
class DictResponse(dotdict): | ||
|
||
pass |
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.