Skip to content

Commit

Permalink
dsc ls support reverse order and finally also
Browse files Browse the repository at this point in the history
support both orderby and reverse in non-TUI `ls`.
  • Loading branch information
JOJ0 committed Dec 26, 2024
1 parent ca2f7d6 commit d093897
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 8 deletions.
7 changes: 4 additions & 3 deletions discodos/cmd23/ls.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@
)
@click.argument("search_terms", metavar="SEARCH_TERMS", nargs=-1)
@click.option("--order-by", "-o", type=str, help="order by DiscoBASE field")
@click.option("--reverse", "-r", is_flag=True, help="reverse order")
@click.pass_obj
def ls_cmd(helper, search_terms, order_by):
def ls_cmd(helper, search_terms, order_by, reverse):
"""Searches and lists collection items.
Supports key=value search. Available keys can be either full DiscoBASE field names
Expand All @@ -43,6 +44,6 @@ def update_user_interaction_helper(user):
coll_ctrl.cli.p(error)

if user.conf.enable_tui:
coll_ctrl.tui_ls_releases(search_key_value, orderby=order_by)
coll_ctrl.tui_ls_releases(search_key_value, orderby=order_by, reverse_order=reverse)
else:
coll_ctrl.ls_releases(search_key_value)
coll_ctrl.ls_releases(search_key_value, orderby=order_by, reverse_order=reverse)
10 changes: 8 additions & 2 deletions discodos/ctrl/collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -973,14 +973,19 @@ def view_stats(self):

# Sales, ls, ls TUI

def ls_releases(self, search_terms):
def ls_releases(self, search_terms, orderby=None, reverse_order=False):
"""search_terms is a key value dict: eg: d_artist: artistname"""

search_results = []
self.cli.p('Searching database for: {}'.format(search_terms))
# Replace orderby with proper database key
if self.cli.cols_key_value_search.shortcuts_dict().get(orderby):
orderby = self.cli.cols_key_value_search.shortcuts_dict()[orderby]
try:
search_results = self.collection.key_value_search_releases(
search_key_value=search_terms,
orderby=orderby,
reverse_order=reverse_order,
filter_cols=self.cli.cols_key_value_search.shortcuts_dict()
)
except Exception as error:
Expand Down Expand Up @@ -1100,7 +1105,7 @@ def remove_and_delete_sales_listing(self, listing_id):
log.warning("Kept sales listing in DiscoBASE!")
return

def tui_ls_releases(self, search_terms, orderby=None):
def tui_ls_releases(self, search_terms, orderby=None, reverse_order=False):
"""search_terms is a key value dict: eg: d_artist: artistname"""

search_results = None
Expand All @@ -1113,6 +1118,7 @@ def tui_ls_releases(self, search_terms, orderby=None):
search_results = self.collection.key_value_search_releases(
search_key_value=search_terms if search_terms else {},
orderby=orderby,
reverse_order=reverse_order,
filter_cols=self.cli.cols_key_value_search.shortcuts_dict()
)
except Exception as error:
Expand Down
4 changes: 2 additions & 2 deletions discodos/model/collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -681,7 +681,7 @@ def create_sales_entry(self, listing_object):

def key_value_search_releases(
self, search_key_value=None, orderby=None, filter_cols=None,
custom_fields=None
custom_fields=None, reverse_order=False,
):
# filter_cols are defined in ViewCommon and passed via the controller call.
replace_cols = filter_cols
Expand Down Expand Up @@ -740,7 +740,7 @@ def key_value_search_releases(

rows = self._select_simple(
fields, "release", fetchone=False, orderby=orderby, condition=where,
join=join, union=union,
join=join, union=union, reverse_order=reverse_order
)
return rows

Expand Down
4 changes: 3 additions & 1 deletion discodos/model/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,8 @@ def _select_simple(
distinct=False,
join=None,
as_dict=False,
union=None
union=None,
reverse_order=False,
):
"""Wrapper around the _select method. Puts together SELECT as string.
Expand Down Expand Up @@ -130,6 +131,7 @@ def _select_simple(
join_clause += f" {join_type} JOIN {join_table} ON {join_cond}"
where_clause = f"WHERE {condition}" if condition else ""
orderby_clause = f"ORDER BY {orderby}" if orderby else ""
orderby_clause += " DESC" if reverse_order else ""
select = "SELECT DISTINCT" if distinct else "SELECT"
limit = f"LIMIT -1 OFFSET {offset}" if offset else ""

Expand Down

0 comments on commit d093897

Please sign in to comment.