summaryrefslogtreecommitdiff
path: root/shopdb/__main__.py
blob: 059aad2ffb0252233ec8ecdcc99d2ae3bd6d7cae (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
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)