forked from AdaGold/task-list-api
-
Notifications
You must be signed in to change notification settings - Fork 71
/
Copy pathtask.py
41 lines (35 loc) · 1.3 KB
/
task.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
from flask import current_app
from sqlalchemy.orm import backref
from app import db
class Task(db.Model):
task_id = db.Column(db.Integer, primary_key=True,autoincrement=True)
title= db.Column(db.String)
description= db.Column(db.String)
completed_at=db.Column(db.DateTime, nullable=True, default=None)
#establishing one to many relationship with Goal
goal_id = db.Column(db.Integer,db.ForeignKey('goal.goal_id'), nullable=True)
goal = db.relationship("Goal",backref=db.backref('tasks'),lazy=True)
#returns True when there is a task completed
def is_complete(self):
if self.completed_at==None:
return False
else:
return True
#runs to determine which body to return based on goal bool status
def to_json(self):
if self.goal_id == None:
task_dict={
"id": self.task_id,
"title": self.title,
"description": self.description,
"is_complete": self.is_complete()
}
else:
task_dict={
"id": self.task_id,
"goal_id": self.goal_id,
"title": self.title,
"description": self.description,
"is_complete": self.is_complete()
}
return task_dict