forked from ivanderk/que_hacer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata.py
54 lines (36 loc) · 1.51 KB
/
data.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
from models import db, User, Project, Task
# https://flask-sqlalchemy.palletsprojects.com/en/3.0.x/queries/
# https://docs.sqlalchemy.org/en/20/orm/queryguide/index.html
def get_user_by_name(user_name):
"Searched for User from db by name. Must exist or generates exception"
return db.session.execute(db.select(User).filter_by(name=user_name)).scalar_one()
def find_user_by_name(user_name):
"Searched for User from db by name. Needs not to exist"
return db.session.execute(db.select(User).filter_by(name=user_name)).scalar_one_or_none()
def get_project_by_id(project_id):
return db.session.execute(db.select(Project).filter_by(id=project_id)).scalar_one()
def get_projects_by_user(user_name):
"Get user (must exist) associated projects"
user = get_user_by_name(user_name)
return user.projects
def get_tasks_by_project(project_id):
project = get_project_by_id(project_id)
if project:
return project.tasks
else:
return []
def create_task(project_id, task_name, description):
task = Task(name=task_name, description=description, project_id=project_id, complete=False)
db.session.add(task)
db.session.commit()
return task
def find_task_by_id(task_id):
task = db.session.execute(db.select(Task).filter_by(id=task_id)).scalar_one()
return task
def change_task(task, **props):
for key in props.keys():
setattr(task, key, props[key])
db.session.commit()
def delete_task(task):
db.session.delete(task)
db.session.commit()