From f0006c92f0e741bc7743b2ca8d86851a63f8b3d6 Mon Sep 17 00:00:00 2001 From: Tim-Hinnerk Heuer Date: Fri, 3 Jan 2020 02:50:19 +1300 Subject: [PATCH] Issue 310 (#311) * fix item previous and next links #310 * fix get_next comment #310 * ensure correct formatting for flake8 #310 * change id to id_ * change previous to prev * fix ref * Update item.html Co-authored-by: Tom Kralidis --- pygeoapi/provider/postgresql.py | 40 +++++++++++++++++++++++++++++++++ pygeoapi/templates/item.html | 34 ++++++++++++++++++---------- 2 files changed, 62 insertions(+), 12 deletions(-) diff --git a/pygeoapi/provider/postgresql.py b/pygeoapi/provider/postgresql.py index 08eafd1..d0c0b79 100644 --- a/pygeoapi/provider/postgresql.py +++ b/pygeoapi/provider/postgresql.py @@ -265,6 +265,44 @@ class PostgreSQLProvider(BaseProvider): return feature_collection + def get_previous(self, cursor, identifier): + """ + Query previous ID given current ID + + :param identifier: feature id + + :returns: feature id + """ + sql = 'SELECT {} AS id FROM {} WHERE {}<%s ORDER BY {} DESC LIMIT 1' + cursor.execute(SQL(sql).format( + Identifier(self.id_field), + Identifier(self.table), + Identifier(self.id_field), + Identifier(self.id_field), + ), (identifier,)) + item = cursor.fetchall() + id_ = item[0]['id'] + return id_ + + def get_next(self, cursor, identifier): + """ + Query next ID given current ID + + :param identifier: feature id + + :returns: feature id + """ + sql = 'SELECT {} AS id FROM {} WHERE {}>%s ORDER BY {} LIMIT 1' + cursor.execute(SQL(sql).format( + Identifier(self.id_field), + Identifier(self.table), + Identifier(self.id_field), + Identifier(self.id_field), + ), (identifier,)) + item = cursor.fetchall() + id_ = item[0]['id'] + return id_ + def get(self, identifier): """ Query the provider for a specific @@ -298,6 +336,8 @@ class PostgreSQLProvider(BaseProvider): row_data = cursor.fetchall()[0] feature = self.__response_feature(row_data) + feature['prev'] = self.get_previous(cursor, identifier) + feature['next'] = self.get_next(cursor, identifier) return feature def __response_feature(self, row_data): diff --git a/pygeoapi/templates/item.html b/pygeoapi/templates/item.html index 5b5b171..037a4ec 100644 --- a/pygeoapi/templates/item.html +++ b/pygeoapi/templates/item.html @@ -37,7 +37,28 @@
-
+
+
+
+
+
+
+
+ {% if data['prev'] or data['next'] %} +
+
+ {% for link in data['links'] %} + {% if link['rel'] == 'prev' %} + Previous + {% elif link['rel'] == 'next' %} + Next + {% endif %} + {% endfor %} +
+
+ {% endif %} +
+
@@ -72,17 +93,6 @@
-
-
- {% for link in data['links'] %} - {% if link['rel'] == 'prev' %} - Prev - {% elif link['rel'] == 'next' %} - Next - {% endif %} - {% endfor %} -
-
{% endblock %}