forked from uwidcit/INFO2602L2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwsgi.py
More file actions
130 lines (113 loc) · 3.65 KB
/
Copy pathwsgi.py
File metadata and controls
130 lines (113 loc) · 3.65 KB
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
124
125
126
127
128
129
130
import click, sys
from models import db, User, Todo
from app import app
@app.cli.command("init", help="Creates and initializes the database")
def initialize():
db.drop_all()
db.init_app(app)
db.create_all()
bob = User('bob', 'bob@mail.com', 'bobpass')
bob.todos.append(Todo('wash car'))
db.session.add(bob)
db.session.commit()
print(bob)
print('database intialized')
@app.cli.command("get-user", help="Retrieves a User by username or id")
@click.argument('key', default='bob')
def get_user(key):
bob = User.query.filter_by(username=key).first()
if not bob:
bob = User.query.get(int(key))
if not bob:
print(f'{key} not found!')
return
print(bob)
@app.cli.command('get-users')
def get_users():
# gets all objects of a model
users = User.query.all()
print(users)
@app.cli.command("change-email")
@click.argument('username', default='bob')
@click.argument('email', default='bob@mail.com')
def change_email(username, email):
bob = User.query.filter_by(username=username).first()
if not bob:
print(f'{username} not found!')
return
bob.email = email
db.session.add(bob)
db.session.commit()
print(bob)
@app.cli.command('create-user')
@click.argument('username')
@click.argument('email')
@click.argument('password')
def create_user(username, email, password):
newuser = User(username=username, email=email, password=password) # Use keyword arguments
try:
db.session.add(newuser)
db.session.commit()
except IntegrityError:
db.session.rollback() # Undo any previous transaction steps
print("Username or email already taken!") # Show user-friendly error message
else:
print(newuser) # Print the newly created user
@app.cli.command('delete-user')
@click.argument('username', default='bob')
def delete_user(username):
bob = User.query.filter_by(username=username).first()
if not bob:
print(f'{username} not found!')
return
db.session.delete(bob)
db.session.commit()
print(f'{username} deleted')
@app.cli.command('add-todo')
@click.argument('username', default='bob')
@click.argument('text', default='wash car')
def add_task(username, text):
bob = User.query.filter_by(username=username).first()
if not bob:
print(f'{username} not found!')
return
new_todo = Todo(text)
bob.todos.append(new_todo)
db.session.add(bob)
db.session.commit()
print(f'To-Do Added: "{text}" for {username}')
@app.cli.command('get-todos')
@click.argument('username', default='bob')
def get_user_todos(username):
bob = User.query.filter_by(username=username).first()
if not bob:
print(f'{username} not found!')
return
print(bob.todos)
@click.argument('todo_id', default=1)
@click.argument('username', default='bob')
@app.cli.command('toggle-todo')
def toggle_todo_command(todo_id, username):
user = User.query.filter_by(username=username).first()
if not user:
print(f'{username} not found!')
return
todo = Todo.query.filter_by(id=todo_id, user_id=user.id).first()
if not todo:
print(f'{username} has no todo id {todo_id}')
todo.toggle()
print(f'{todo.text} is {"done" if todo.done else "not done"}!')
@click.argument('username', default='bob')
@click.argument('todo_id', default=6)
@click.argument('category', default='chores')
@app.cli.command('add-category', help="Adds a category to a todo")
def add_todo_category_command(username, todo_id, category):
user = User.query.filter_by(username=username).first()
if not user:
print(f'{username} not found!')
return
res = user.add_todo_category(todo_id, category)
if not res:
print(f'{username} has no todo id {todo_id}')
return
print('Category added!')