Add basic API endpoint for upgrades, partial pagination support

latter is still broken, but this much is a starting point i think
This commit is contained in:
Lance Edgar 2018-11-01 00:34:28 -05:00
parent 0c41395cfc
commit b8fdce378f
3 changed files with 63 additions and 0 deletions

View file

@ -33,4 +33,5 @@ from .master import APIMasterView
def includeme(config):
config.include('tailbone.api.auth')
config.include('tailbone.api.customers')
config.include('tailbone.api.upgrades')
config.include('tailbone.api.users')

View file

@ -26,6 +26,8 @@ Tailbone Web API - Master View
from __future__ import unicode_literals, absolute_import
from paginate_sqlalchemy import SqlalchemyOrmPage
from tailbone.api import APIView, api
from tailbone.db import Session
@ -83,6 +85,15 @@ class APIMasterView(APIView):
sortkey = getattr(self.model_class, sortkey)
objects = objects.order_by(getattr(sortkey, sortdir)())
# NOTE: we only page results if sorting is in effect, otherwise
# record sequence is "non-determinant" (is that the word?)
page = self.request.params.get('page')
per_page = self.request.params.get('per_page')
if page.isdigit() and per_page.isdigit():
page = int(page)
per_page = int(per_page)
objects = SqlalchemyOrmPage(objects, items_per_page=per_page, page=page)
data = [self.normalize(obj) for obj in objects]
return data

51
tailbone/api/upgrades.py Normal file
View file

@ -0,0 +1,51 @@
# -*- coding: utf-8; -*-
################################################################################
#
# Rattail -- Retail Software Framework
# Copyright © 2010-2018 Lance Edgar
#
# This file is part of Rattail.
#
# Rattail is free software: you can redistribute it and/or modify it under the
# terms of the GNU General Public License as published by the Free Software
# Foundation, either version 3 of the License, or (at your option) any later
# version.
#
# Rattail is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along with
# Rattail. If not, see <http://www.gnu.org/licenses/>.
#
################################################################################
"""
Tailbone Web API - Upgrade Views
"""
from __future__ import unicode_literals, absolute_import
from rattail.db import model
from tailbone.api import APIMasterView
class UpgradeView(APIMasterView):
model_class = model.Upgrade
def normalize(self, upgrade):
return {
'created': upgrade.created.isoformat(),
'description': upgrade.description,
'enabled': upgrade.enabled,
# 'status_code': upgrade.status_code,
'status_code': self.enum.UPGRADE_STATUS[upgrade.status_code],
'executed': upgrade.executed.isoformat() if upgrade.executed else None,
# 'executed_by':
}
def includeme(config):
UpgradeView.defaults(config)