Skip to content

Commit

Permalink
Merge pull request #51 from Samweli/reworked_pagination
Browse files Browse the repository at this point in the history
Updates on pagination functionality
  • Loading branch information
Samweli committed Dec 9, 2021
2 parents 394d627 + e763caf commit 3627bdf
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 41 deletions.
29 changes: 11 additions & 18 deletions src/qgis_stac/api/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
from .network import ContentFetcherTask
from ..conf import ConnectionSettings

from ..lib.pystac import ItemCollection


class BaseClient(QtCore.QObject):
""" Base API client, defines main plugin content fetching operations
Expand All @@ -37,6 +39,9 @@ class BaseClient(QtCore.QObject):
list,
ResourcePagination
)
item_collections_received = QtCore.pyqtSignal(
ItemCollection
)
error_received = QtCore.pyqtSignal([str], [str, int, str])

def __init__(
Expand Down Expand Up @@ -107,24 +112,6 @@ def get_collections(

QgsApplication.taskManager().addTask(self.content_task)

def get_pagination(self, item_collection):
""" Generates pagination details from the pystac_client item collection
:param item_collection: Collection of items generator
:type item_collection: ItemCollection
:returns: Pagination instance
:rtype: ResourcePagination
"""
pagination = ResourcePagination()
for link in item_collection.to_dict()['links']:
if link['rel'] == "next":
pagination.next_page = link['href']
elif link['rel'] == "previous":
pagination.previous_page = link['href']

return pagination

def handle_collections(
self,
collections
Expand All @@ -137,6 +124,12 @@ def handle_items(
):
raise NotImplementedError

def handle_item_collections(
self,
items
):
raise NotImplementedError

def handle_error(
self,
message: str
Expand Down
26 changes: 22 additions & 4 deletions src/qgis_stac/api/client.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import datetime
import typing

from .base import BaseClient
from .models import (
Expand All @@ -10,6 +11,7 @@
)

from ..utils import log
from ..lib.pystac import ItemCollection


class Client(BaseClient):
Expand All @@ -18,7 +20,8 @@ class Client(BaseClient):
"""
def handle_items(
self,
items_response
items_response,
pagination
):
"""Emits the search results items, so plugin signal observers
eg. gui can use the data.
Expand All @@ -27,9 +30,8 @@ def handle_items(
:type items_response: List[models.Items]
"""
items = []
pagination = ResourcePagination()
properties = None
for item in items_response.get_items():
for item in items_response.items:
try:
item_datetime = datetime.datetime.strptime(
item.properties.get("datetime"),
Expand Down Expand Up @@ -62,9 +64,25 @@ def handle_items(

self.items_received.emit(items, pagination)

def handle_item_collections(
self,
items_response,
pagination
):
"""Emits the search results item collections generator
:param items_response: Search results items
:type items_response: List[models.Items]
"""
self.item_collections_received.emit(
items_response.get_item_collections(),
pagination
)

def handle_collections(
self,
collections_response
collections_response,
pagination
):
"""Emits the search results collections.
Expand Down
5 changes: 3 additions & 2 deletions src/qgis_stac/api/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@
@dataclasses.dataclass
class ResourcePagination:
"""The plugin resource pagination for the search results"""
total_records: int = 0
total_items: int = 0
total_pages: int = 0
current_page: int = 1
page_size: int = 10
next_page: str = None
Expand Down Expand Up @@ -228,7 +229,7 @@ def params(self):
parameters = {
"ids": self.ids,
"collections": self.collections or None,
"max_items": self.page_size,
"limit": self.page_size,
"bbox": bbox,
"datetime": datetime_str,
}
Expand Down
20 changes: 17 additions & 3 deletions src/qgis_stac/api/network.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ class ContentFetcherTask(QgsTask):

response = None
error = None
client: Client = None
client = None
pagination = None

def __init__(
self,
Expand Down Expand Up @@ -66,9 +67,22 @@ def run(self):
try:
if self.resource_type == \
ResourceType.FEATURE:
self.response = self.client.search(
response = self.client.search(
**self.search_params.params()
)
pages = 1
items = 0
self.pagination = ResourcePagination()

for i, collection in enumerate(response.get_item_collections()):
pages = pages + i
items += len(collection.items)
if self.search_params.page == (i + 1):
self.response = collection

self.pagination.total_pages = pages
self.pagination.total_items = items

elif self.resource_type == \
ResourceType.COLLECTION:
self.response = self.client.get_collections()
Expand All @@ -89,7 +103,7 @@ def finished(self, result: bool):
:type result: bool
"""
if result:
self.response_handler(self.response)
self.response_handler(self.response, self.pagination)
else:
message = f"Problem in fetching content for {self.url!r}," \
f"Error {self.error}"
Expand Down
49 changes: 40 additions & 9 deletions src/qgis_stac/gui/qgis_stac_widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,13 @@ def __init__(
self.connections_box.activated.connect(self.update_current_connection)

self.search_btn.clicked.connect(
self.search_items
self.search_items_api
)
self.next_btn.clicked.connect(
self.next_items
)
self.prev_btn.clicked.connect(
self.previous_items
)

self.fetch_collections_btn.clicked.connect(
Expand Down Expand Up @@ -113,6 +119,7 @@ def __init__(

# initialize page
self.page = 1
self.total_pages = 0

self.current_collections = []
self.get_filters()
Expand Down Expand Up @@ -183,7 +190,7 @@ def update_current_connection(self, index: int):
current_text = self.connections_box.itemText(index)
if current_text == "":
return
current_connection = settings_manager.\
current_connection = settings_manager. \
find_connection_by_name(current_text)
settings_manager.set_current_connection(current_connection.id)
if current_connection:
Expand All @@ -208,18 +215,31 @@ def update_connections_box(self):
)
current_connection = settings_manager.get_current_connection()
if current_connection is not None:
current_index = self.connections_box.\
current_index = self.connections_box. \
findText(current_connection.name)
self.connections_box.setCurrentIndex(current_index)
else:
self.connections_box.setCurrentIndex(0)

def search_items_api(self):
self.current_progress_message = tr(
f"Searching items ..."
)
self.page = 1
self.search_items()

def previous_items(self):
self.page -= 1
self.current_progress_message = tr(
f"Retrieving data"
)
self.search_items()

def next_items(self):
self.page += 1
self.current_progress_message = tr(
f"Retrieving data"
)
self.search_items()

def search_items(self):
Expand All @@ -229,8 +249,6 @@ def search_items(self):
search operation.
"""
self.search_type = ResourceType.FEATURE
self.current_progress_message = tr("Searching for items...")

start_dte = self.start_dte.dateTime() \
if not self.start_dte.dateTime().isNull() else None
end_dte = self.end_dte.dateTime() \
Expand All @@ -243,6 +261,7 @@ def search_items(self):
ItemSearch(
collections=collections,
page_size=page_size,
page=self.page,
start_datetime=start_dte,
end_datetime=end_dte,
spatial_extent=spatial_extent,
Expand Down Expand Up @@ -312,6 +331,8 @@ def update_search_inputs(self, enabled):
self.extent_box.setEnabled(enabled)
self.metadata_group.setEnabled(enabled)
self.search_btn.setEnabled(enabled)
self.next_btn.setEnabled(self.page < self.total_pages)
self.prev_btn.setEnabled(self.page > 1)

def prepare_message_bar(self):
""" Initializes the widget message bar settings"""
Expand Down Expand Up @@ -343,7 +364,7 @@ def prepare_extent_box(self):
self.extent_box.setOutputExtentFromCurrent()
self.extent_box.setMapCanvas(map_canvas)

def display_results(self, results):
def display_results(self, results, pagination):
""" Shows the found results into their respective view. Emits
the search end signal after completing loading up the results
into the view.
Expand All @@ -361,9 +382,19 @@ def display_results(self, results):
self.save_filters()

elif self.search_type == ResourceType.FEATURE:
self.result_items_la.setText(
tr("Found {} STAC item(s)").format(len(results))
)
if pagination.total_items != 0:
self.result_items_la.setText(
tr(
"Page {} out {} pages, "
"total {} STAC item(s) found.").format(
self.page,
pagination.total_pages,
pagination.total_items
)
)
self.total_pages = pagination.total_pages
self.next_btn.setEnabled(self.page < pagination.total_pages)
self.prev_btn.setEnabled(self.page > 1)

items_model = ItemsModel(items=results)
self.items_proxy_model.setSourceModel(items_model)
Expand Down
10 changes: 5 additions & 5 deletions src/qgis_stac/ui/qgis_stac_widget.ui
Original file line number Diff line number Diff line change
Expand Up @@ -604,9 +604,9 @@
</spacer>
</item>
<item>
<widget class="QPushButton" name="previous_btn">
<widget class="QPushButton" name="prev_btn">
<property name="enabled">
<bool>false</bool>
<bool>true</bool>
</property>
<property name="sizePolicy">
<sizepolicy hsizetype="Minimum" vsizetype="Maximum">
Expand All @@ -615,7 +615,7 @@
</sizepolicy>
</property>
<property name="toolTip">
<string>&lt;html&gt;&lt;head/&gt;&lt;body&gt;&lt;p&gt;Go to previous page&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</string>
<string>Go back to previous page</string>
</property>
<property name="text">
<string>Previous</string>
Expand All @@ -625,7 +625,7 @@
<item>
<widget class="QPushButton" name="next_btn">
<property name="enabled">
<bool>false</bool>
<bool>true</bool>
</property>
<property name="sizePolicy">
<sizepolicy hsizetype="Minimum" vsizetype="Maximum">
Expand All @@ -634,7 +634,7 @@
</sizepolicy>
</property>
<property name="toolTip">
<string>&lt;html&gt;&lt;head/&gt;&lt;body&gt;&lt;p&gt;Go to next page&lt;/p&gt;&lt;p&gt;&lt;br/&gt;&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</string>
<string>Go to next page</string>
</property>
<property name="text">
<string>Next</string>
Expand Down

0 comments on commit 3627bdf

Please sign in to comment.