diff options
Diffstat (limited to 'shopdb/__main__.py')
-rw-r--r-- | shopdb/__main__.py | 123 |
1 files changed, 123 insertions, 0 deletions
diff --git a/shopdb/__main__.py b/shopdb/__main__.py new file mode 100644 index 0000000..059aad2 --- /dev/null +++ b/shopdb/__main__.py @@ -0,0 +1,123 @@ +from datetime import datetime +import click +import prettytable +from ._base import Session +from ._customer import Customer +from ._group import Group +from ._job import Job, JobPriorities +from ._staff import Staff +from ._status import Status, StatusValues + + +session = Session() + + +@click.group() +def main(): + pass + + +@main.command(name="customer") +@click.option("--add", is_flag=True, default=False) +def customer(add): + if add: + click.echo("adding new customer") + name = click.prompt("name") + email = click.prompt("email") + room = click.prompt("room") + groups = click.prompt("group id(s)").split(',') + groups = [session.query(Group).filter(Group.id == int(g))[0] for g in groups] + new = Customer(name=name, email=email, room=room, groups=groups) + session.add(new) + session.commit() + click.echo("...done!") + else: + out = prettytable.PrettyTable() + out.field_names = ["id", "name", "email", "room", "group(s)"] + out.align = "l" + for instance in session.query(Customer).order_by(Customer.id): + groups = ",".join([g.name for g in instance.groups]) + out.add_row([instance.id, instance.name, instance.email, instance.room, groups]) + print(out) + + +@main.command(name="group") +@click.option("--add", is_flag=True, default=False) +def group(add): + if add: + click.echo("adding new group") + name = click.prompt("name") + new = Group(name=name) + session.add(new) + session.commit() + click.echo("...done!") + else: + out = prettytable.PrettyTable() + out.field_names = ["id", "name"] + out.align = "l" + for instance in session.query(Group).order_by(Group.id): + out.add_row([instance.id, instance.name]) + print(out) + + +@main.command(name="job") +@click.option("--add", is_flag=True, default=False) +def job(add): + if add: + click.echo("adding new job") + name = click.prompt("name") + customers = click.prompt("customer id(s)").split(',') + customers = [session.query(Customer).filter(Customer.id == int(g))[0] for g in customers] + priority = click.prompt("priority", type=click.Choice([a.name for a in (JobPriorities)])) + new = Job(name=name, customers=customers, priority=priority) + session.add(new) + session.commit() + click.echo("...done!") + else: + out = prettytable.PrettyTable() + out.field_names = ["id", "priority", "name", "customer(s)"] + out.align = "l" + for instance in session.query(Job).order_by(Job.id): + customers = ",".join([c.name for c in instance.customers]) + out.add_row([instance.id, instance.priority.name, instance.name, customers]) + print(out) + + +@main.command(name="staff") +@click.option("--add", is_flag=True, default=False) +def staff(add): + if add: + click.echo("adding new staff member") + name = click.prompt("name") + new = Staff(name=name) + session.add(new) + session.commit() + click.echo("...done!") + else: + out = prettytable.PrettyTable() + out.field_names = ["id", "name"] + out.align = "l" + for instance in session.query(Staff).order_by(Staff.id): + out.add_row([instance.id, instance.name]) + print(out) + + +@main.command(name="status") +@click.option("--add", is_flag=True, default=False) +def staff(add): + if add: + click.echo("recording status change") + job = session.query(Job).filter(Job.id == click.prompt("job id", int))[0] + value = click.prompt("new status", type=click.Choice([a.name for a in (StatusValues)])) + comment = click.prompt("comment") + new = Status(job=job, comment=comment, value=str(value), timestamp=datetime.utcnow()) + session.add(new) + session.commit() + click.echo("...done!") + else: + out = prettytable.PrettyTable() + out.field_names = ["id", "timestamp (UTC)", "job", "value", "comment"] + out.align = "l" + for instance in session.query(Status).order_by(Status.id): + out.add_row([instance.id,instance.timestamp, instance.job.name, instance.value.name, instance.comment]) + print(out) |