Finish converting CLI commands to use peewee
This commit is contained in:
parent
ca7cd1aaa3
commit
f8f83ce4d4
@ -11,7 +11,7 @@ from peewee import fn, JOIN
|
|||||||
|
|
||||||
from .db import db, HamsterCategory, HamsterActivity, HamsterFact, KimaiCustomer, KimaiProject, KimaiActivity, HamsterKimaiMapping
|
from .db import db, HamsterCategory, HamsterActivity, HamsterFact, KimaiCustomer, KimaiProject, KimaiActivity, HamsterKimaiMapping
|
||||||
|
|
||||||
# HAMSTER_DIR = Path.home() / '.local/share/hamster'
|
HAMSTER_DIR = Path.home() / '.local/share/hamster'
|
||||||
# HAMSTER_FILE = HAMSTER_DIR / 'hamster.db'
|
# HAMSTER_FILE = HAMSTER_DIR / 'hamster.db'
|
||||||
HAMSTER_FILE = 'hamster-testing.db'
|
HAMSTER_FILE = 'hamster-testing.db'
|
||||||
|
|
||||||
@ -50,7 +50,7 @@ def delete_categories(ids):
|
|||||||
categories = HamsterCategory.select(
|
categories = HamsterCategory.select(
|
||||||
HamsterCategory,
|
HamsterCategory,
|
||||||
fn.Count(HamsterActivity.id).alias("activities_count")
|
fn.Count(HamsterActivity.id).alias("activities_count")
|
||||||
).join(HamsterActivity, JOIN.LEFT_OUTER).group_by(HamsterActivity).where(HamsterCategory.id.in_(ids))
|
).join(HamsterActivity, JOIN.LEFT_OUTER).group_by(HamsterCategory).where(HamsterCategory.id.in_(ids))
|
||||||
|
|
||||||
for c in categories:
|
for c in categories:
|
||||||
click.echo(f'@{c.id}: {c.name} ({c.activities_count} activities)')
|
click.echo(f'@{c.id}: {c.name} ({c.activities_count} activities)')
|
||||||
@ -68,63 +68,58 @@ def delete_categories(ids):
|
|||||||
def rename_category(id_, name):
|
def rename_category(id_, name):
|
||||||
""" Rename a category """
|
""" Rename a category """
|
||||||
|
|
||||||
r = get_categories((id_,))[0]
|
category = HamsterCategory.get(id=id_)
|
||||||
|
|
||||||
click.echo('Renaming @{0[0]}: {0[1]} to "{1}"'.format(r, name))
|
click.echo(f'Renaming @{category.id}: {category.name} to "{name}"')
|
||||||
|
|
||||||
sql = 'UPDATE categories SET name = ? WHERE id = ?'
|
category.name = name
|
||||||
|
category.save()
|
||||||
c.execute(sql, (name, r[0]))
|
|
||||||
conn.commit()
|
|
||||||
|
|
||||||
|
|
||||||
@categories.command('activities')
|
@categories.command('activities')
|
||||||
@click.argument('ids', nargs=-1)
|
@click.argument('ids', nargs=-1)
|
||||||
def list_category_activities(ids):
|
def list_category_activities(ids):
|
||||||
""" Show activities for categories specified by IDS """
|
""" Show activities for categories specified by ids """
|
||||||
sql = '''
|
|
||||||
SELECT
|
|
||||||
activities.id, activities.name, categories.name
|
|
||||||
FROM
|
|
||||||
activities
|
|
||||||
LEFT JOIN
|
|
||||||
categories
|
|
||||||
ON
|
|
||||||
activities.category_id = categories.id
|
|
||||||
WHERE
|
|
||||||
categories.id IN ({seq})
|
|
||||||
'''.format(
|
|
||||||
seq=','.join(['?'] * len(ids))
|
|
||||||
)
|
|
||||||
|
|
||||||
results = c.execute(sql, ids)
|
activities = HamsterActivity.select(
|
||||||
|
HamsterActivity,
|
||||||
|
HamsterCategory.name
|
||||||
|
).join(HamsterCategory, JOIN.LEFT_OUTER).where(HamsterCategory.id.in_(ids))
|
||||||
|
|
||||||
|
for a in activities:
|
||||||
|
click.echo(f'@{a.id}: {a.category.name} » {a.name}')
|
||||||
|
|
||||||
for r in results:
|
|
||||||
click.echo('@{0[0]}: {0[2]} » {0[1]}'.format(r))
|
|
||||||
|
|
||||||
|
|
||||||
@categories.command('tidy')
|
@categories.command('tidy')
|
||||||
def tidy_categories():
|
def tidy_categories():
|
||||||
""" Remove categories with no activities """
|
""" 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'
|
# Create a subquery to calculate the count of activities per category
|
||||||
categories = c.execute(sql).fetchall()
|
subquery = (
|
||||||
|
HamsterCategory
|
||||||
|
.select(HamsterCategory, fn.COUNT(HamsterActivity.id).alias('activities_count'))
|
||||||
|
.join(HamsterActivity, JOIN.LEFT_OUTER)
|
||||||
|
.group_by(HamsterCategory)
|
||||||
|
.alias('subquery')
|
||||||
|
)
|
||||||
|
|
||||||
click.echo('Found {0} empty categories:'.format(len(categories)))
|
# Use the subquery to filter categories where activities_count is 0
|
||||||
|
categories = (
|
||||||
|
HamsterCategory
|
||||||
|
.select()
|
||||||
|
.join(subquery, on=(HamsterCategory.id == subquery.c.id))
|
||||||
|
.where(subquery.c.activities_count == 0)
|
||||||
|
)
|
||||||
|
|
||||||
|
click.echo('Found {0} empty categories:'.format(categories.count()))
|
||||||
|
|
||||||
for cat in categories:
|
for cat in categories:
|
||||||
click.echo('@{0[0]}: {0[1]}'.format(cat))
|
click.echo(f'@{cat.id}: {cat.name}')
|
||||||
|
|
||||||
click.confirm('Do you want to continue?', abort=True)
|
click.confirm('Do you want to continue?', abort=True)
|
||||||
|
|
||||||
sql = 'DELETE FROM categories '
|
[cat.delete_instance() for cat in 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()
|
@cli.group()
|
||||||
@ -138,18 +133,31 @@ def activities():
|
|||||||
def list_activities(search, csv_output):
|
def list_activities(search, csv_output):
|
||||||
""" List / search activities """
|
""" List / search activities """
|
||||||
|
|
||||||
results = get_activities(search=search)
|
activities = HamsterActivity.select(
|
||||||
|
HamsterActivity,
|
||||||
|
HamsterCategory
|
||||||
|
).join(HamsterCategory, JOIN.LEFT_OUTER).order_by(
|
||||||
|
HamsterCategory.name,
|
||||||
|
HamsterActivity.name
|
||||||
|
)
|
||||||
|
|
||||||
results.sort(key=lambda t: (t[2], t[1]))
|
if search is not None:
|
||||||
|
activities = activities.where(HamsterActivity.name.contains(search))
|
||||||
|
|
||||||
if csv_output:
|
if csv_output:
|
||||||
csv_writer = csv.writer(sys.stdout)
|
csv_writer = csv.writer(sys.stdout)
|
||||||
|
|
||||||
for r in results:
|
for a in activities:
|
||||||
|
category_name = a.category.name if a.category_id != -1 else ""
|
||||||
if csv_output:
|
if csv_output:
|
||||||
csv_writer.writerow([r[3], r[2], r[0], r[1]])
|
csv_writer.writerow([
|
||||||
|
a.category_id,
|
||||||
|
category_name,
|
||||||
|
a.id,
|
||||||
|
a.name
|
||||||
|
])
|
||||||
else:
|
else:
|
||||||
click.echo('@{0[3]}: {0[2]} » {0[0]}: {0[1]}'.format(r))
|
click.echo(f'@{a.category_id}: {category_name} » {a.id}: {a.name}')
|
||||||
|
|
||||||
|
|
||||||
@activities.command('delete')
|
@activities.command('delete')
|
||||||
@ -157,25 +165,25 @@ def list_activities(search, csv_output):
|
|||||||
def delete_activities(ids):
|
def delete_activities(ids):
|
||||||
""" Delete activities specified by IDS """
|
""" Delete activities specified by IDS """
|
||||||
|
|
||||||
results = get_activities(ids)
|
activities = HamsterActivity.select(
|
||||||
|
HamsterActivity,
|
||||||
|
HamsterCategory.name,
|
||||||
|
fn.Count(HamsterFact.id).alias("facts_count")
|
||||||
|
).join(HamsterCategory,
|
||||||
|
JOIN.LEFT_OUTER).switch(HamsterActivity).join(HamsterFact,
|
||||||
|
JOIN.LEFT_OUTER).group_by(HamsterActivity).where(
|
||||||
|
HamsterActivity.id.in_(ids)
|
||||||
|
)
|
||||||
|
|
||||||
click.secho('Deleting:', fg='red')
|
click.secho('Deleting:', fg='red')
|
||||||
|
|
||||||
for r in results:
|
for a in activities:
|
||||||
sql = "SELECT COUNT(id) FROM facts WHERE activity_id = ?"
|
category_name = a.category.name if a.category_id != -1 else ""
|
||||||
count = c.execute(sql, (r[0],)).fetchone()[0]
|
click.echo(f'@{a.id}: {category_name} » {a.name} ({a.facts_count} facts)')
|
||||||
click.echo('@{0[0]}: {0[2]} » {0[1]} ({1} facts)'.format(r, count))
|
|
||||||
|
|
||||||
click.confirm('Do you want to continue?', abort=True)
|
click.confirm('Do you want to continue?', abort=True)
|
||||||
|
|
||||||
sql = 'DELETE FROM activities '
|
[a.delete_instance() for a in activities]
|
||||||
|
|
||||||
sql = sql + 'WHERE id IN ({seq})'.format(
|
|
||||||
seq=','.join(['?'] * len(ids))
|
|
||||||
)
|
|
||||||
|
|
||||||
c.execute(sql, ids)
|
|
||||||
conn.commit()
|
|
||||||
|
|
||||||
click.secho('Deleted {0} activities'.format(len(ids)), fg='green')
|
click.secho('Deleted {0} activities'.format(len(ids)), fg='green')
|
||||||
|
|
||||||
@ -185,29 +193,20 @@ def delete_activities(ids):
|
|||||||
@click.argument('ids', nargs=-1)
|
@click.argument('ids', nargs=-1)
|
||||||
def move(category_id, ids):
|
def move(category_id, ids):
|
||||||
""" Move activities to another category """
|
""" Move activities to another category """
|
||||||
category = get_categories((category_id,))[0]
|
category = HamsterCategory.get(id=category_id)
|
||||||
results = get_activities(ids)
|
activities = HamsterActivity.select().where(HamsterActivity.id.in_(ids))
|
||||||
|
|
||||||
click.secho('Moving to "@{0[0]}: {0[1]}":'.format(category), fg='green')
|
click.secho(f'Moving to "@{category.id}: {category.name}":', fg='green')
|
||||||
|
|
||||||
for r in results:
|
for a in activities:
|
||||||
click.secho('@{0[3]}: {0[2]} » @{0[0]}: {0[1]}'.format(r), fg='blue')
|
category_name = a.category.name if a.category_id != -1 else ""
|
||||||
|
click.secho(f'@{a.category_id}: {category_name} » @{a.id}: {a.name}', fg='blue')
|
||||||
|
|
||||||
click.confirm('Do you want to continue?', abort=True)
|
click.confirm('Do you want to continue?', abort=True)
|
||||||
|
|
||||||
sql = '''
|
for a in activities:
|
||||||
UPDATE
|
a.category = category
|
||||||
activities
|
a.save()
|
||||||
SET
|
|
||||||
category_id = ?
|
|
||||||
'''
|
|
||||||
|
|
||||||
sql = sql + 'WHERE id IN ({seq})'.format(
|
|
||||||
seq=','.join(['?'] * len(ids))
|
|
||||||
)
|
|
||||||
|
|
||||||
c.execute(sql, (category[0], *ids))
|
|
||||||
conn.commit()
|
|
||||||
|
|
||||||
click.secho('Moved {0} activities'.format(len(ids)), fg='green')
|
click.secho('Moved {0} activities'.format(len(ids)), fg='green')
|
||||||
|
|
||||||
@ -216,32 +215,15 @@ def move(category_id, ids):
|
|||||||
@click.argument('ids', nargs=-1)
|
@click.argument('ids', nargs=-1)
|
||||||
def list_facts(ids):
|
def list_facts(ids):
|
||||||
""" Show facts for activities """
|
""" Show facts for activities """
|
||||||
|
activities = HamsterActivity.select().where(HamsterActivity.id.in_(ids))
|
||||||
|
|
||||||
results = get_activities(ids)
|
for a in activities:
|
||||||
|
|
||||||
for r in results:
|
|
||||||
click.secho(
|
click.secho(
|
||||||
'@{0[0]}: {0[1]}'.format(r), fg='green'
|
f'@{a.id}: {a.name}', fg='green'
|
||||||
)
|
)
|
||||||
|
|
||||||
sql = '''
|
for f in a.facts:
|
||||||
SELECT
|
click.secho(f'@{f.id}, {f.start_time}', fg='blue')
|
||||||
start_time,
|
|
||||||
activities.name
|
|
||||||
FROM
|
|
||||||
facts
|
|
||||||
LEFT JOIN
|
|
||||||
activities
|
|
||||||
ON
|
|
||||||
facts.activity_id = activities.id
|
|
||||||
WHERE
|
|
||||||
activities.id = ?
|
|
||||||
'''
|
|
||||||
|
|
||||||
results = c.execute(sql, (r[0],))
|
|
||||||
|
|
||||||
for r in results:
|
|
||||||
click.secho('@{0[0]}, {0[1]}'.format(r), fg='blue')
|
|
||||||
|
|
||||||
|
|
||||||
@activities.command()
|
@activities.command()
|
||||||
@ -249,82 +231,52 @@ def list_facts(ids):
|
|||||||
@click.argument('to_id')
|
@click.argument('to_id')
|
||||||
def move_facts(from_id, to_id):
|
def move_facts(from_id, to_id):
|
||||||
""" Move facts from one activity to another """
|
""" Move facts from one activity to another """
|
||||||
from_activity = get_activities((from_id,))[0]
|
from_activity = HamsterActivity.get(id=from_id)
|
||||||
to_activity = get_activities((to_id,))[0]
|
to_activity = HamsterActivity.get(id=to_id)
|
||||||
|
|
||||||
|
from_category_name = from_activity.category.name if from_activity.category_id != -1 else ""
|
||||||
|
to_category_name = to_activity.category.name if to_activity.category_id != -1 else ""
|
||||||
|
|
||||||
click.secho(
|
click.secho(
|
||||||
'Moving facts from "@{0[2]} » @{0[0]}: {0[1]}" to "@{1[2]} » @{1[0]}: {1[1]}"'.format(
|
f'Moving facts from "{from_category_name} » @{from_activity.id}: {from_activity.name}" to "@{to_category_name} » @{to_activity.id}: {to_activity.name}"', fg='green'
|
||||||
from_activity, to_activity
|
|
||||||
), fg='green'
|
|
||||||
)
|
)
|
||||||
|
|
||||||
sql = '''
|
for f in from_activity.facts:
|
||||||
SELECT
|
click.secho(f'@{f.id}, {f.start_time}', fg='blue')
|
||||||
start_time,
|
|
||||||
activities.name
|
|
||||||
FROM
|
|
||||||
facts
|
|
||||||
LEFT JOIN
|
|
||||||
activities
|
|
||||||
ON
|
|
||||||
facts.activity_id = activities.id
|
|
||||||
WHERE
|
|
||||||
activities.id = ?
|
|
||||||
'''
|
|
||||||
|
|
||||||
results = c.execute(sql, (from_id,))
|
|
||||||
|
|
||||||
for r in results:
|
|
||||||
click.secho('@{0[0]}, {0[1]}'.format(r), fg='blue')
|
|
||||||
|
|
||||||
click.confirm('Do you want to continue?', abort=True)
|
click.confirm('Do you want to continue?', abort=True)
|
||||||
|
|
||||||
c.execute(
|
count = HamsterFact.update(activity_id=to_activity.id).where(HamsterFact.activity == from_activity).execute()
|
||||||
'UPDATE facts SET activity_id = ? WHERE activity_id = ?',
|
|
||||||
(to_id, from_id)
|
|
||||||
)
|
|
||||||
|
|
||||||
conn.commit()
|
click.secho('Moved {0} facts'.format(count), fg='green')
|
||||||
|
|
||||||
click.secho('Moved {0} facts'.format(results.rowcount), fg='green')
|
|
||||||
|
|
||||||
click.confirm(
|
click.confirm(
|
||||||
'Would you like to delete @{0[2]} » @{0[0]}: {0[1]}?'.format(
|
f'Would you like to delete "{from_category_name} » @{from_activity.id}: {from_activity.name}?',
|
||||||
from_activity),
|
|
||||||
abort=True
|
abort=True
|
||||||
)
|
)
|
||||||
|
|
||||||
delete_activities((from_id,))
|
from_activity.delete_instance()
|
||||||
|
|
||||||
|
|
||||||
@activities.command()
|
@activities.command()
|
||||||
def find_duplicates():
|
def find_duplicates():
|
||||||
""" Show activities which are not unique in their categories """
|
""" Show activities which are not unique in their categories """
|
||||||
|
|
||||||
sql = '''
|
non_unique_activities = (
|
||||||
SELECT
|
HamsterActivity
|
||||||
categories.id,
|
.select(
|
||||||
categories.name,
|
HamsterActivity,
|
||||||
activities.id,
|
HamsterCategory.id,
|
||||||
activities.name,
|
fn.COALESCE(HamsterCategory.name, 'None').alias("category_name")
|
||||||
COUNT(activities.id) c
|
)
|
||||||
FROM
|
.join(HamsterCategory, JOIN.LEFT_OUTER)
|
||||||
activities
|
.group_by(HamsterActivity.category_id, HamsterActivity.name)
|
||||||
LEFT JOIN
|
.having(fn.COUNT(HamsterActivity.id) > 1)
|
||||||
categories
|
)
|
||||||
ON
|
|
||||||
activities.category_id = categories.id
|
|
||||||
GROUP BY
|
|
||||||
activities.name,
|
|
||||||
activities.category_id
|
|
||||||
HAVING c > 1
|
|
||||||
'''
|
|
||||||
|
|
||||||
results = c.execute(sql)
|
for activity in non_unique_activities:
|
||||||
|
|
||||||
for r in results:
|
|
||||||
click.secho(
|
click.secho(
|
||||||
'@{0[0]}: {0[1]} » @{0[2]}: {0[3]} ({0[4]})'.format(r), fg='blue')
|
f"@{activity.category_id}: {activity.category_name} » @{activity.id}: {activity.name}", fg='blue')
|
||||||
|
|
||||||
|
|
||||||
@cli.group()
|
@cli.group()
|
||||||
@ -353,11 +305,15 @@ def _get_kimai_mapping_file(path, category_search=None):
|
|||||||
'TO Note'
|
'TO Note'
|
||||||
])
|
])
|
||||||
|
|
||||||
results = get_activities(category_search=category_search)
|
activities = HamsterActivity.select(
|
||||||
|
HamsterActivity,
|
||||||
|
HamsterCategory
|
||||||
|
).join(HamsterCategory, JOIN.LEFT_OUTER)
|
||||||
|
|
||||||
for r in results:
|
for a in activities:
|
||||||
mapping_writer.writerow([
|
mapping_writer.writerow([
|
||||||
r[2], r[1]
|
a.category.name if a.category_id != -1 else "",
|
||||||
|
a.name
|
||||||
])
|
])
|
||||||
|
|
||||||
mapping_file.close()
|
mapping_file.close()
|
||||||
@ -534,6 +490,12 @@ def _import(username, mapping_path=None, output=None, category_search=None, afte
|
|||||||
|
|
||||||
args = []
|
args = []
|
||||||
|
|
||||||
|
facts = HamsterFact.select(
|
||||||
|
HamsterFact,
|
||||||
|
HamsterActivity,
|
||||||
|
HamsterCategory
|
||||||
|
).join(HamsterActivity).join(HamsterCategory, JOIN.LEFT_OUTER)
|
||||||
|
|
||||||
sql = '''
|
sql = '''
|
||||||
SELECT
|
SELECT
|
||||||
facts.id, facts.start_time, facts.end_time, facts.description,
|
facts.id, facts.start_time, facts.end_time, facts.description,
|
||||||
@ -547,20 +509,10 @@ def _import(username, mapping_path=None, output=None, category_search=None, afte
|
|||||||
categories ON activities.category_id = categories.id '''
|
categories ON activities.category_id = categories.id '''
|
||||||
|
|
||||||
if category_search is not None:
|
if category_search is not None:
|
||||||
sql = sql + " WHERE categories.name LIKE ?"
|
facts = facts.where(HamsterCategory.name.contains(category_search))
|
||||||
category_search = '%{0}%'.format(category_search)
|
|
||||||
args.append(category_search)
|
|
||||||
|
|
||||||
if after is not None:
|
if after is not None:
|
||||||
if category_search is not None:
|
facts = facts.where(HamsterFact.start_time >= after)
|
||||||
sql = sql + ' AND '
|
|
||||||
else:
|
|
||||||
sql = sql + ' WHERE '
|
|
||||||
sql = sql + f"DATE(facts.start_time) > DATE(?)"
|
|
||||||
args.append(after)
|
|
||||||
|
|
||||||
results = c.execute(sql, args)
|
|
||||||
results = c.fetchall()
|
|
||||||
|
|
||||||
if not show_missing:
|
if not show_missing:
|
||||||
output_writer.writerow([
|
output_writer.writerow([
|
||||||
@ -581,13 +533,13 @@ def _import(username, mapping_path=None, output=None, category_search=None, afte
|
|||||||
"InternalRate"
|
"InternalRate"
|
||||||
])
|
])
|
||||||
|
|
||||||
for fact in results:
|
for fact in facts:
|
||||||
k = '{0}:{1}'.format(fact[6], fact[5])
|
k = f'{fact.activity.category.name}:{fact.activity.name}'
|
||||||
try:
|
try:
|
||||||
mapping_ = mapping[k]
|
mapping_ = mapping[k]
|
||||||
except KeyError:
|
except KeyError:
|
||||||
if show_missing:
|
if show_missing:
|
||||||
output_writer.writerow([fact[6], fact[5]])
|
output_writer.writerow([fact.activity.category.name, fact.activity.name])
|
||||||
click.secho(
|
click.secho(
|
||||||
"Can't find mapping for '{0}', skipping".format(k), fg='yellow')
|
"Can't find mapping for '{0}', skipping".format(k), fg='yellow')
|
||||||
continue
|
continue
|
||||||
@ -595,19 +547,16 @@ def _import(username, mapping_path=None, output=None, category_search=None, afte
|
|||||||
if show_missing:
|
if show_missing:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
if fact[1] is None or fact[2] is None:
|
if fact.start_time is None or fact.end_time is None:
|
||||||
click.secho("Missing duration data '{0}-{1}', skipping".format(
|
click.secho(f"Missing duration data '{fact.start_time}-{fact.end_time}', skipping", fg='yellow')
|
||||||
fact[1],
|
|
||||||
fact[2]
|
|
||||||
), fg='yellow')
|
|
||||||
continue
|
continue
|
||||||
|
|
||||||
if len(mapping_) < 5:
|
if len(mapping_) < 5:
|
||||||
mapping_.append(None)
|
mapping_.append(None)
|
||||||
|
|
||||||
date_start, date_end = (
|
date_start, date_end = (
|
||||||
datetime.strptime(fact[2].split('.')[0], '%Y-%m-%d %H:%M:%S'),
|
datetime.strptime(fact.start_time.split('.')[0], '%Y-%m-%d %H:%M:%S'),
|
||||||
datetime.strptime(fact[1].split('.')[0], '%Y-%m-%d %H:%M:%S')
|
datetime.strptime(fact.end_time.split('.')[0], '%Y-%m-%d %H:%M:%S')
|
||||||
)
|
)
|
||||||
duration = (
|
duration = (
|
||||||
date_start - date_end
|
date_start - date_end
|
||||||
@ -623,7 +572,7 @@ def _import(username, mapping_path=None, output=None, category_search=None, afte
|
|||||||
mapping_[0],
|
mapping_[0],
|
||||||
mapping_[1],
|
mapping_[1],
|
||||||
mapping_[2],
|
mapping_[2],
|
||||||
fact[3] or mapping_[4] or '',
|
fact.description or mapping_[4] or '',
|
||||||
'0', # Exported
|
'0', # Exported
|
||||||
mapping_[3],
|
mapping_[3],
|
||||||
'', # Hourly rate
|
'', # Hourly rate
|
||||||
@ -633,21 +582,26 @@ def _import(username, mapping_path=None, output=None, category_search=None, afte
|
|||||||
output_file.close()
|
output_file.close()
|
||||||
|
|
||||||
|
|
||||||
@kimai.command()
|
@kimai.group('db')
|
||||||
def dbinit():
|
def db_():
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
@db_.command()
|
||||||
|
def init():
|
||||||
db.create_tables([KimaiCustomer, KimaiProject, KimaiActivity,
|
db.create_tables([KimaiCustomer, KimaiProject, KimaiActivity,
|
||||||
HamsterKimaiMapping])
|
HamsterKimaiMapping])
|
||||||
|
|
||||||
|
|
||||||
@kimai.command()
|
@db_.command()
|
||||||
def dbreset():
|
def reset():
|
||||||
HamsterKimaiMapping.delete().execute()
|
HamsterKimaiMapping.delete().execute()
|
||||||
|
|
||||||
|
|
||||||
@kimai.command()
|
@db_.command()
|
||||||
@click.option('-g', '--global', 'global_', help='Does this file contain mappings to global activties', is_flag=True)
|
@click.option('-g', '--global', 'global_', help='Does this file contain mappings to global activties', is_flag=True)
|
||||||
@click.option('--mapping-path', help='Mapping file')
|
@click.option('--mapping-path', help='Mapping file')
|
||||||
def csv2db(mapping_path=None, global_=False):
|
def import_csv(mapping_path=None, global_=False):
|
||||||
mapping_file = _get_kimai_mapping_file(mapping_path, None)
|
mapping_file = _get_kimai_mapping_file(mapping_path, None)
|
||||||
next(mapping_file)
|
next(mapping_file)
|
||||||
mapping_reader = csv.reader(mapping_file)
|
mapping_reader = csv.reader(mapping_file)
|
||||||
@ -677,6 +631,7 @@ def csv2db(mapping_path=None, global_=False):
|
|||||||
kimai_tags=row[5]
|
kimai_tags=row[5]
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@cli.command()
|
@cli.command()
|
||||||
def app():
|
def app():
|
||||||
from .app import HamsterToolsApp
|
from .app import HamsterToolsApp
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
import logging
|
import logging
|
||||||
from peewee import SqliteDatabase, Model, CharField, ForeignKeyField
|
from peewee import SqliteDatabase, Model, CharField, ForeignKeyField, DateTimeField
|
||||||
|
|
||||||
from textual.logging import TextualHandler
|
from textual.logging import TextualHandler
|
||||||
|
|
||||||
@ -29,6 +29,9 @@ class HamsterActivity(Model):
|
|||||||
|
|
||||||
class HamsterFact(Model):
|
class HamsterFact(Model):
|
||||||
activity = ForeignKeyField(HamsterActivity, backref='facts')
|
activity = ForeignKeyField(HamsterActivity, backref='facts')
|
||||||
|
start_time = DateTimeField()
|
||||||
|
end_time = DateTimeField(null=True)
|
||||||
|
description = CharField()
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
database = db
|
database = db
|
||||||
|
Loading…
Reference in New Issue
Block a user