-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
112 lines (100 loc) · 3.73 KB
/
app.py
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
import click
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from models import Base, User, Category, Expense
engine = create_engine('sqlite:///expense_tracker.db')
Session = sessionmaker(bind=engine)
@click.group()
def cli():
pass
@cli.command()
@click.argument('username')
@click.argument('password')
def create_user(username, password):
session = Session()
user = User(username=username, password=password)
session.add(user)
session.commit()
print(f"User {username} has been created successfully.")
@cli.command()
@click.argument('name')
def add_category(name):
session = Session()
category = Category(name=name)
session.add(category)
session.commit()
print(f"Category '{name}' has been added successfully")
@cli.command()
@click.argument('category_name')
def delete_category(category_name):
session = Session()
category = session.query(Category).filter_by(name=category_name).first()
if category:
session.delete(category)
session.commit()
print(f"Category '{category_name}' has been deleted successfully.")
else:
print(f"Category '{category_name}' does not exist.")
@cli.command()
@click.argument('username')
@click.argument('password')
def login(username, password):
session = Session()
user = session.query(User).filter_by(username=username).first()
if user and user.password == password:
print(f"Logged in as {username}.")
else:
print("Invalid username or password.")
@cli.command()
@click.option('--user', prompt='Username', help='Username of the logged-in user')
@click.argument('amount')
@click.argument('description')
@click.option('--category', prompt='Category', help='Category of the expense')
def add_expense(user, amount, description, category):
session = Session()
user_obj = session.query(User).filter_by(username=user).first()
if user_obj:
category_obj = session.query(Category).filter_by(name=category).first()
if category_obj:
expense = Expense(amount=amount, description=description, user=user_obj, category=category_obj)
session.add(expense)
session.commit()
print("Expense added successfully.")
else:
print(f"Category '{category}' does not exist. Please create the category first.")
else:
print(f"User '{user}' does not exist. Please create an account first.")
@cli.command()
@click.option('--user', prompt='Username')
def view_expenses(user):
session = Session()
user_obj = session.query(User).filter_by(username=user).first()
if user_obj:
expenses = session.query(Expense).filter_by(user=user_obj).all()
if expenses:
print("Expenses:")
for expense in expenses:
print(f"ID: {expense.id}, Amount: {expense.amount}, Description: {expense.description}, Category: {expense.category.name}")
else:
print("No expenses found.")
else:
print(f"User '{user}' does not exist. Please create an account")
@cli.command()
@click.option('--user', prompt='Username')
@click.argument('expense_id')
def delete_expense(user, expense_id):
session = Session()
user_obj = session.query(User).filter_by(username=user).first()
if user_obj:
expense = session.query(Expense).filter_by(id=expense_id, user=user_obj).first()
if expense:
session.delete(expense)
session.commit()
print("Expense deleted successfully")
else:
print("Invalid expense ID or the expense does not belong to the logged-in user.")
else:
print(f"User '{user}' does not exist. Please create an account.")
if __name__ == '__main__':
Base.metadata.create_all(engine)
cli()