Add a 'due' field to tasks

This commit is contained in:
Danny Robson 2020-05-21 10:58:10 +10:00
parent 31b5832fe1
commit c17a67df72
3 changed files with 43 additions and 4 deletions

View File

@ -0,0 +1,32 @@
"""add due date to tasks
Revision ID: 17946e7038d7
Revises: 719f2d1539c8
Create Date: 2020-05-21 10:49:26.110397
"""
from alembic import op
import sqlalchemy as sa
# revision identifiers, used by Alembic.
revision = '17946e7038d7'
down_revision = '719f2d1539c8'
branch_labels = None
depends_on = None
def upgrade():
op.add_column(
'tasks',
sa.Column(
'due_at',
sa.DateTime,
nullable=True
)
)
def downgrade():
with op.batch_alter_table('tasks') as tasks:
tasks.drop_column('due_at')

View File

@ -1,2 +1,3 @@
alembic
sqlalchemy
dateparser

View File

@ -3,14 +3,19 @@
import sqlalchemy as sa
import sqlalchemy.ext.automap
import dateparser
###############################################################################
Base = sa.ext.automap.automap_base()
Task = None
###############################################################################
def add(session: sa.orm.Session, title: str):
obj = Task(title=title)
def add(session: sa.orm.Session, title: str, due: str = None):
if due is not None:
due = dateparser.parse(due)
obj = Task(title=title, due_at=due)
session.add(obj)
session.commit()
show_by_obj(session, obj)
@ -32,7 +37,7 @@ def rm(session: sa.orm.Session, index: int):
# -----------------------------------------------------------------------------
def show_by_obj(session: sa.orm.Session, obj: Task):
print(obj.id, obj.created_at, obj.active, obj.title)
print(obj.id, obj.created_at, obj.due_at, obj.active, obj.title)
# -----------------------------------------------------------------------------
@ -76,8 +81,9 @@ if __name__ == '__main__':
subparsers = parser.add_subparsers()
add_parser = subparsers.add_parser('add')
add_parser.set_defaults(func=add)
add_parser.set_defaults(func=add, due=None)
add_parser.add_argument("title", type=str)
add_parser.add_argument("--due", type=str)
done_parser = subparsers.add_parser('done')
done_parser.set_defaults(func=done)