Split CLI and DB components apart
This commit is contained in:
parent
b8c3c28bcd
commit
30a7e223c3
@ -1,8 +1,8 @@
|
||||
#!/usr/bin/env python3
|
||||
|
||||
import db
|
||||
|
||||
import sqlalchemy as sa
|
||||
import sqlalchemy.ext.declarative.base
|
||||
import sqlalchemy.orm
|
||||
|
||||
import dateparser
|
||||
|
||||
@ -11,35 +11,6 @@ import logging
|
||||
from typing import List
|
||||
|
||||
|
||||
###############################################################################
|
||||
Base = sqlalchemy.ext.declarative.declarative_base()
|
||||
|
||||
|
||||
depends_association = sa.Table(
|
||||
"depends", Base.metadata,
|
||||
sa.Column('first_id', sa.Integer, sa.ForeignKey('tasks.id'), primary_key=True),
|
||||
sa.Column('second_id', sa.Integer, sa.ForeignKey('tasks.id'), primary_key=True)
|
||||
)
|
||||
|
||||
|
||||
class Task(Base):
|
||||
__tablename__ = "tasks"
|
||||
|
||||
id = sa.Column(sa.Integer, primary_key=True)
|
||||
title = sa.Column(sa.String, nullable=False)
|
||||
created_at = sa.Column(sa.DateTime, server_default=sa.func.current_timestamp(), nullable=False)
|
||||
due_at = sa.Column(sa.DateTime, nullable=True)
|
||||
active = sa.Column(sa.Boolean, server_default=sa.sql.expression.true(), nullable=False)
|
||||
|
||||
depends = sa.orm.relationship(
|
||||
'Task',
|
||||
secondary=depends_association,
|
||||
primaryjoin=id == depends_association.c.first_id,
|
||||
secondaryjoin=id == depends_association.c.second_id,
|
||||
backref="parents"
|
||||
)
|
||||
|
||||
|
||||
###############################################################################
|
||||
def add(session: sa.orm.Session, title: str, due: str = None, depends: List[int] = None):
|
||||
if due is not None:
|
||||
@ -48,8 +19,8 @@ def add(session: sa.orm.Session, title: str, due: str = None, depends: List[int]
|
||||
if depends is None:
|
||||
depends = []
|
||||
|
||||
obj = Task(title=title, due_at=due)
|
||||
obj.depends.extend(session.query(Task).filter(Task.id == idx).one() for idx in depends)
|
||||
obj = db.Task(title=title, due_at=due)
|
||||
obj.depends.extend(session.query(db.Task).filter(db.Task.id == idx).one() for idx in depends)
|
||||
session.add(obj)
|
||||
|
||||
session.commit()
|
||||
@ -58,7 +29,7 @@ def add(session: sa.orm.Session, title: str, due: str = None, depends: List[int]
|
||||
|
||||
# -----------------------------------------------------------------------------
|
||||
def done(session: sa.orm.Session, index: int):
|
||||
obj = session.query(Task).filter(Task.id == index).one()
|
||||
obj = session.query(db.Task).filter(db.Task.id == index).one()
|
||||
assert(obj.active is True)
|
||||
obj.active = False
|
||||
session.commit()
|
||||
@ -66,12 +37,12 @@ def done(session: sa.orm.Session, index: int):
|
||||
|
||||
# -----------------------------------------------------------------------------
|
||||
def rm(session: sa.orm.Session, index: int):
|
||||
session.query(Task).filter(Task.id == index).delete()
|
||||
session.query(db.Task).filter(db.Task.id == index).delete()
|
||||
session.commit()
|
||||
|
||||
|
||||
# -----------------------------------------------------------------------------
|
||||
def show_by_obj(session: sa.orm.Session, obj: Task):
|
||||
def show_by_obj(session: sa.orm.Session, obj: db.Task):
|
||||
for idx in [i.id for i in obj.depends]:
|
||||
show_by_id(session, idx)
|
||||
print(obj.id, obj.created_at, obj.due_at, obj.active, obj.title, [i.id for i in obj.depends])
|
||||
@ -79,19 +50,19 @@ def show_by_obj(session: sa.orm.Session, obj: Task):
|
||||
|
||||
# -----------------------------------------------------------------------------
|
||||
def show_by_id(session: sa.orm.Session, index: int):
|
||||
obj = session.query(Task).filter(Task.id == index).one()
|
||||
obj = session.query(db.Task).filter(db.Task.id == index).one()
|
||||
show_by_obj(session, obj)
|
||||
|
||||
|
||||
# -----------------------------------------------------------------------------
|
||||
def ls(session: sa.orm.Session, _all: False):
|
||||
vals = session.query(Task)
|
||||
vals = session.query(db.Task)
|
||||
# We must be the final task in a series
|
||||
vals = vals.filter(~sa.exists().where(Task.id == depends_association.c.second_id))
|
||||
vals = vals.order_by(Task.created_at)
|
||||
vals = vals.filter(~sa.exists().where(db.Task.id == db.depends_association.c.second_id))
|
||||
vals = vals.order_by(db.Task.created_at)
|
||||
|
||||
if _all is False:
|
||||
vals = vals.filter(Task.active == True) # noqa
|
||||
vals = vals.filter(db.Task.active == True) # noqa
|
||||
|
||||
for i in vals:
|
||||
show_by_obj(session, i)
|
34
src/db.py
Normal file
34
src/db.py
Normal file
@ -0,0 +1,34 @@
|
||||
import sqlalchemy as sa
|
||||
import sqlalchemy.ext.declarative.base
|
||||
import sqlalchemy.orm
|
||||
|
||||
|
||||
###############################################################################
|
||||
Base = sqlalchemy.ext.declarative.declarative_base()
|
||||
|
||||
|
||||
###############################################################################
|
||||
depends_association = sa.Table(
|
||||
"depends", Base.metadata,
|
||||
sa.Column('first_id', sa.Integer, sa.ForeignKey('tasks.id'), primary_key=True),
|
||||
sa.Column('second_id', sa.Integer, sa.ForeignKey('tasks.id'), primary_key=True)
|
||||
)
|
||||
|
||||
|
||||
###############################################################################
|
||||
class Task(Base):
|
||||
__tablename__ = "tasks"
|
||||
|
||||
id = sa.Column(sa.Integer, primary_key=True)
|
||||
title = sa.Column(sa.String, nullable=False)
|
||||
created_at = sa.Column(sa.DateTime, server_default=sa.func.current_timestamp(), nullable=False)
|
||||
due_at = sa.Column(sa.DateTime, nullable=True)
|
||||
active = sa.Column(sa.Boolean, server_default=sa.sql.expression.true(), nullable=False)
|
||||
|
||||
depends = sa.orm.relationship(
|
||||
'Task',
|
||||
secondary=depends_association,
|
||||
primaryjoin=id == depends_association.c.first_id,
|
||||
secondaryjoin=id == depends_association.c.second_id,
|
||||
backref="parents"
|
||||
)
|
Loading…
Reference in New Issue
Block a user