From b8fdce378f116d1ed0d89b0e66970d073f632ce7 Mon Sep 17 00:00:00 2001 From: Lance Edgar Date: Thu, 1 Nov 2018 00:34:28 -0500 Subject: [PATCH] Add basic API endpoint for upgrades, partial pagination support latter is still broken, but this much is a starting point i think --- tailbone/api/__init__.py | 1 + tailbone/api/master.py | 11 +++++++++ tailbone/api/upgrades.py | 51 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 63 insertions(+) create mode 100644 tailbone/api/upgrades.py diff --git a/tailbone/api/__init__.py b/tailbone/api/__init__.py index d09c669a..572ff44d 100644 --- a/tailbone/api/__init__.py +++ b/tailbone/api/__init__.py @@ -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') diff --git a/tailbone/api/master.py b/tailbone/api/master.py index ff3261c9..c4ffb2b6 100644 --- a/tailbone/api/master.py +++ b/tailbone/api/master.py @@ -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 diff --git a/tailbone/api/upgrades.py b/tailbone/api/upgrades.py new file mode 100644 index 00000000..34415002 --- /dev/null +++ b/tailbone/api/upgrades.py @@ -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 . +# +################################################################################ +""" +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)