Skip to content

Commit

Permalink
Merge pull request #346 from livMatS/2024-10-24-pagination-buttons
Browse files Browse the repository at this point in the history
2024-10-24 pagination buttons
  • Loading branch information
jotelha authored Oct 31, 2024
2 parents 18043d8 + 11f0173 commit d8008aa
Show file tree
Hide file tree
Showing 3 changed files with 383 additions and 213 deletions.
8 changes: 4 additions & 4 deletions dtool_lookup_gui/models/datasets.py
Original file line number Diff line number Diff line change
Expand Up @@ -272,9 +272,9 @@ def __init__(self, uri=None, dataset_info=None):
raise ValueError('Please provide either `uri` or `dateset_info`.')

@classmethod
async def get_datasets(cls, keyword, page_number, page_size, pagination={}):
async def get_datasets(cls, keyword, page_number, page_size,sort_fields,sort_order, pagination={} ,sorting={}):
async with ConfigurationBasedLookupClient() as lookup:
datasets = await lookup.get_datasets(keyword, page_number=page_number, page_size=page_size, pagination=pagination)
datasets = await lookup.get_datasets(keyword, page_number=page_number, page_size=page_size,sort_fields=sort_fields,sort_order=sort_order, pagination=pagination,sorting=sorting)
return [await cls.from_lookup(lookup_dict) for lookup_dict in datasets]

@classmethod
Expand All @@ -284,10 +284,10 @@ async def query(cls, query_text, *args, **kwargs):
return [await cls.from_lookup(lookup_dict) for lookup_dict in datasets]

@classmethod
async def query_all(cls, page_number=1, page_size=10, pagination={}):
async def query_all(cls,sort_fields,sort_order, page_number=1, page_size=10, pagination={},sorting={}):
"""Query all datasets from the lookup server."""
async with ConfigurationBasedLookupClient() as lookup:
datasets = await lookup.get_datasets(page_number=page_number, page_size=page_size, pagination=pagination)
datasets = await lookup.get_datasets(page_number=page_number, page_size=page_size,sort_fields=sort_fields,sort_order=sort_order, pagination=pagination,sorting=sorting)
return [await cls.from_lookup(lookup_dict) for lookup_dict in datasets]

@classmethod
Expand Down
68 changes: 61 additions & 7 deletions dtool_lookup_gui/views/main_window.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,12 +176,14 @@ class MainWindow(Gtk.ApplicationWindow):

main_statusbar = Gtk.Template.Child()
contents_per_page = Gtk.Template.Child()
linting_errors_button = Gtk.Template.Child()

sort_field_combo_box = Gtk.Template.Child()

linting_errors_button = Gtk.Template.Child()

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)

self.sort_order = 1 # default ascending sort order

self.application = self.get_application()

Expand Down Expand Up @@ -273,7 +275,6 @@ def __init__(self, *args, **kwargs):

_logger.debug(f"Constructed main window for app '{self.application.get_application_id()}'")


# Initialize pagination parameters, hide page advancer button by default, set default items per page, and highlight the current page button.
self.pagination = {}
self.page_advancer_button.set_visible(False)
Expand Down Expand Up @@ -350,13 +351,46 @@ def _update_main_statusbar(self):
def contents_per_page_changed(self, widget):
self.contents_per_page_value = widget.get_active_text()
self.on_first_page_button_clicked(self.first_page_button) # Directly call the method

def on_sort_field_combo_box_changed(self, widget):
transformed_fields = {
"Uri": "uri",
"Name": "name",
"Base Uri": "base_uri",
"Created At": "created_at",
"Frozen At": "frozen_at",
"Uuid": "uuid",
"Creator Username": "creator_username",
}

selected_text = widget.get_active_text()

transformed_text = transformed_fields.get(selected_text)

sort_order = self.sort_order

asyncio.create_task(self._fetch_search_results(keyword=None, sort_fields=[transformed_text], sort_order=[sort_order]))

async def _fetch_search_results(self, keyword, on_show=None, page_number=1, page_size=10, widget=None):
@Gtk.Template.Callback()
def on_sort_order_switch_state_set(self, widget,state):
# Toggle sort order based on the switch state
if state:
self.sort_order = -1 # Switch is on, use ascending order
else:
self.sort_order = 1 # Switch is off, use descending order

self.on_sort_field_combo_box_changed(self.sort_field_combo_box)



async def _fetch_search_results(self, keyword, on_show=None, page_number=1, page_size=10,sort_fields=["uri"],sort_order=[1] , widget=None):
# Here sort order 1 implies ascending
row = self.base_uri_list_box.search_results_row
row.start_spinner()
self.main_spinner.start()

self.pagination = {} # Add pagination dictionary
self.sorting = {} # Add sorting dictionary
try:
if keyword:
if is_valid_query(keyword):
Expand All @@ -365,22 +399,33 @@ async def _fetch_search_results(self, keyword, on_show=None, page_number=1, page
keyword,
page_number=page_number,
page_size=page_size,
pagination=self.pagination
sort_fields = sort_fields,
sort_order = sort_order,
pagination=self.pagination,
sorting = self.sorting

)
else:
_logger.debug("Specified search text is not a valid query, just perform free text search.")
datasets = await DatasetModel.get_datasets(
keyword,
page_number=page_number,
page_size=page_size,
pagination=self.pagination
sort_fields = sort_fields,
sort_order = sort_order,
pagination=self.pagination,
sorting = self.sorting
)
else:
_logger.debug("No keyword specified, list all datasets.")
datasets = await DatasetModel.query_all(
page_number=page_number,
page_size=page_size,
pagination=self.pagination
sort_fields = sort_fields,
sort_order = sort_order,
pagination=self.pagination,
sorting = self.sorting

)

if len(datasets) > self._max_nb_datasets:
Expand All @@ -394,6 +439,7 @@ async def _fetch_search_results(self, keyword, on_show=None, page_number=1, page
self._update_search_summary(datasets)
self._update_main_statusbar()
self.contents_per_page.connect("changed", self.contents_per_page_changed)
self.sort_field_combo_box.connect("changed", self.on_sort_field_combo_box_changed)
if self.base_uri_list_box.get_selected_row() == row:
# Only update if the row is still selected
self.dataset_list_box.fill(datasets, on_show=on_show)
Expand Down Expand Up @@ -877,6 +923,14 @@ def on_error_bar_response(self, widget, response_id):
if response_id == Gtk.ResponseType.CLOSE:
self.error_bar.set_revealed(False)

# sort signal handlers
# @Gtk.Template.Callback()
# def on_sort_field_combo_box_changed(self, widget):
# sort_field = widget.get_active_text()
# _logger.debug("sort field changed to %s", sort_field)

# pagination signal handlers

@Gtk.Template.Callback()
def on_first_page_button_clicked(self, widget):
# Navigate to the first page if results are not currently being fetched
Expand Down
Loading

0 comments on commit d8008aa

Please sign in to comment.