1
0
Fork 0
mirror of https://gitlab.com/SIGBUS/nyaa.git synced 2024-12-22 19:30:00 +00:00

Add a function for getting a id->category name map

Redo _create_upload_category_choices to use new function
Also removes (renames) by_id from models.SubCategory (we use composite key)
This commit is contained in:
TheAMM 2017-05-19 15:21:20 +03:00
parent 6406825790
commit 18ebf201b8
2 changed files with 22 additions and 9 deletions

View file

@ -292,13 +292,9 @@ class SubCategory(db.Model):
def id_as_string(self): def id_as_string(self):
return '_'.join(str(x) for x in self.get_category_ids()) return '_'.join(str(x) for x in self.get_category_ids())
@classmethod
def by_id(cls, id):
return cls.query.get(id)
@classmethod @classmethod
def by_category_ids(cls, main_cat_id, sub_cat_id): def by_category_ids(cls, main_cat_id, sub_cat_id):
return cls.query.filter(cls.id == sub_cat_id, cls.main_category_id == main_cat_id).first() return cls.query.get( (sub_cat_id, main_cat_id) )
class UserLevelType(IntEnum): class UserLevelType(IntEnum):

View file

@ -110,6 +110,19 @@ def get_utc_timestamp(datetime_str):
def get_display_time(datetime_str): def get_display_time(datetime_str):
return datetime.strptime(datetime_str, '%Y-%m-%dT%H:%M:%S').strftime('%Y-%m-%d %H:%M') return datetime.strptime(datetime_str, '%Y-%m-%dT%H:%M:%S').strftime('%Y-%m-%d %H:%M')
@utils.cached_function
def get_category_id_map():
''' Reads database for categories and turns them into a dict with
ids as keys and name list as the value, ala
{'1_0': ['Anime'], '1_2': ['Anime', 'English-translated'], ...} '''
cat_id_map = {}
for main_cat in models.MainCategory.query:
cat_id_map[main_cat.id_as_string] = [main_cat.name]
for sub_cat in main_cat.sub_categories:
cat_id_map[sub_cat.id_as_string] = [main_cat.name, sub_cat.name]
return cat_id_map
# Routes start here # # Routes start here #
app.register_blueprint(api_handler.api_blueprint, url_prefix='/api') app.register_blueprint(api_handler.api_blueprint, url_prefix='/api')
@ -510,10 +523,14 @@ def activate_user(payload):
def _create_upload_category_choices(): def _create_upload_category_choices():
''' Turns categories in the database into a list of (id, name)s ''' ''' Turns categories in the database into a list of (id, name)s '''
choices = [('', '[Select a category]')] choices = [('', '[Select a category]')]
for main_cat in models.MainCategory.query.order_by(models.MainCategory.id): id_map = get_category_id_map()
choices.append((main_cat.id_as_string, main_cat.name, True))
for sub_cat in main_cat.sub_categories: for key in sorted(id_map.keys()):
choices.append((sub_cat.id_as_string, ' - ' + sub_cat.name)) cat_names = id_map[key]
is_main_cat = key.endswith('_0')
cat_name = is_main_cat and cat_names[0] or (' - ' + cat_names[1])
choices.append( (key, cat_name, is_main_cat) )
return choices return choices