extend get_themes_orderedby_cname() in test/database.py

This commit is contained in:
Trent Palmer 2021-11-24 16:59:32 -08:00
parent 2b74eacd3b
commit ca15a18c53
2 changed files with 16 additions and 4 deletions

View File

@ -2,7 +2,8 @@ from rank_hugo_themes import (
engine, Hugothemes, engine, Hugothemes,
Hugothemes_from_gitlab, sessionmaker Hugothemes_from_gitlab, sessionmaker
) )
from sqlalchemy import asc, func from test.theme_compare import cname_compare
from functools import cmp_to_key
def get_theme_count(): def get_theme_count():
@ -36,8 +37,9 @@ def get_newest_update_time_from_gitlab():
def get_themes_orderedby_cname(): def get_themes_orderedby_cname():
session = sessionmaker(bind=engine)() session = sessionmaker(bind=engine)()
return session.query( result = session.query(Hugothemes).all()
Hugothemes).order_by(asc(func.lower(Hugothemes.cname))).all() result.sort(key=cmp_to_key(lambda a, b: cname_compare(a, b)))
return result
def get_themes_as_dicts_of_sortable_columns(): def get_themes_as_dicts_of_sortable_columns():

View File

@ -17,7 +17,10 @@ def semver_split(item):
def compare_theme(x, y, sort_key): def compare_theme(x, y, sort_key):
if sort_key == 'sortByName': if sort_key == 'sortByName':
return compare_jk(y['name'].lower(), x['name'].lower()) if y['name'].lower() == x['name'].lower():
return compare_jk(x['name'], y['name'])
else:
return compare_jk(y['name'].lower(), x['name'].lower())
elif sort_key == 'sortByStars': elif sort_key == 'sortByStars':
return compare_jk(int(x['stars']), int(y['stars'])) return compare_jk(int(x['stars']), int(y['stars']))
@ -41,3 +44,10 @@ def theme_compare(a, b, y):
if len(y) == 0: return -1 if len(y) == 0: return -1
rslt = compare_theme(a, b, y[0]) rslt = compare_theme(a, b, y[0])
return theme_compare(a, b, y[1:]) if rslt == 0 else rslt return theme_compare(a, b, y[1:]) if rslt == 0 else rslt
def cname_compare(a, b):
if a.cname.lower() == b.cname.lower():
return 1 if a.cname < b.cname else -1
else:
return -1 if a.cname.lower() < b.cname.lower() else 1