forked from AdaGold/task-list-api
-
Notifications
You must be signed in to change notification settings - Fork 111
/
Copy pathtask.py
73 lines (59 loc) · 2.1 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
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
from app import db
from flask import jsonify, abort, make_response
# from requests import requests
class Task(db.Model):
task_id = db.Column(db.Integer, primary_key=True, autoincrement=True)
title = db.Column(db.String, nullable=False)
description = db.Column(db.String, nullable=False)
completed_at = db.Column(db.DateTime)
goal_id = db.Column(db.Integer, db.ForeignKey('goal.goal_id'), nullable=True)
goals = db.relationship("Goal")
def to_json(self):
is_complete = self.completed_at != None
hash_map = {
"id" : self.task_id,
"title" : self.title,
"description" : self.description,
"is_complete" : is_complete
}
if self.goal_id:
hash_map["goal_id"] = self.goal_id
return hash_map
@classmethod
def create_task(cls, request_body):
try:
try:
new_task = cls(
title=request_body['title'],
description=request_body['description'],
completed_at=request_body['completed_at']
)
except KeyError:
new_task = cls(
title=request_body['title'],
description=request_body['description'],
)
return new_task
except:
return abort(make_response({"details": "Invalid data"}, 400))
# def to_dict(self):
# return {
# "task": {
# "id": self.id,
# "task_id": self.id,
# "title": self.title,
# "description": self.description,
# "is_complete": self.completed_at
# @classmethod
# def from_dict(cls, data_dict):
# return cls(
# title=data_dict["title"],
# description=data_dict["description"],
# completed_at=data_dict["completed_at"],
# )
#return dict(
# id=self.task_id,
# title = self.title,
# description=self.description,
# is_complete = True if self.completed_at else False
# )