Add categories tidy command
This commit is contained in:
parent
900fb9f0c0
commit
5080e61abb
@ -106,7 +106,7 @@ def delete_categories(ids):
|
||||
results = get_categories(ids)
|
||||
|
||||
for r in results:
|
||||
sql = 'SELECT COUNT(id) FROM activities WHERE category_id = ?'
|
||||
sql = 'select count(id) from activities where category_id = ?'
|
||||
count = c.execute(sql, (r[0],)).fetchone()[0]
|
||||
click.echo('@{0[0]}: {0[1]} ({1} activities)'.format(r, count))
|
||||
|
||||
@ -169,6 +169,30 @@ def list_category_activities(ids):
|
||||
click.echo('@{0[0]}: {0[2]} » {0[1]}'.format(r))
|
||||
|
||||
|
||||
@categories.command('tidy')
|
||||
def tidy_categories():
|
||||
""" Remove categories with no activities """
|
||||
|
||||
sql = 'SELECT categories.id, categories.name FROM categories LEFT JOIN activities ON categories.id = activities.category_id WHERE activities.id IS NULL'
|
||||
categories = c.execute(sql).fetchall()
|
||||
|
||||
click.echo('Found {0} empty categories:'.format(len(categories)))
|
||||
|
||||
for cat in categories:
|
||||
click.echo('@{0[0]}: {0[1]}'.format(cat))
|
||||
|
||||
click.confirm('Do you want to continue?', abort=True)
|
||||
|
||||
sql = 'DELETE FROM categories '
|
||||
|
||||
sql = sql + 'WHERE id IN ({seq})'.format(
|
||||
seq=','.join(['?'] * len(categories))
|
||||
)
|
||||
|
||||
c.execute(sql, [cat[0] for cat in categories])
|
||||
conn.commit()
|
||||
|
||||
|
||||
@cli.group()
|
||||
def activities():
|
||||
pass
|
||||
|
Loading…
Reference in New Issue
Block a user