-
Notifications
You must be signed in to change notification settings - Fork 718
Expand file tree
/
Copy pathtask.py
More file actions
49 lines (35 loc) · 1.56 KB
/
task.py
File metadata and controls
49 lines (35 loc) · 1.56 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
from flask_sqlalchemy import SQLAlchemy
from sqlalchemy.orm import Mapped, mapped_column,relationship
from sqlalchemy import ForeignKey
from ..db import db
from sqlalchemy import String, Date
from datetime import datetime
from flask import Blueprint, abort, make_response, request, Response
from typing import Optional
from datetime import date
class Task(db.Model):
id: Mapped[int] = mapped_column(primary_key=True, autoincrement=True)
title: Mapped[str] = mapped_column(String(100), nullable=False)
description: Mapped[str] = mapped_column(String(255))
completed_at:Mapped[date] = mapped_column(Date, nullable=True)
goal_id: Mapped[Optional[int]] = mapped_column(ForeignKey("goal.id"))
goal: Mapped[Optional["Goal"]] = relationship(back_populates="tasks")
def to_dict(self): # json
task_dict = {"id": self.id,
"title":self.title,
"description":self.description,
"is_complete": self.completed_at is not None}
if self.goal_id is not None:
task_dict["goal_id"] = self.goal_id
return task_dict
@classmethod
def from_dict(cls, data_dict):
if "title" not in data_dict:
raise KeyError("title")
if "description" not in data_dict:
raise KeyError("description")
return cls(
title=data_dict["title"],
description=data_dict["description"],
completed_at=None # default to None when creating
)