From 30a7e223c3cecc466e102f05a12a8c116f3cd87a Mon Sep 17 00:00:00 2001 From: Danny Robson Date: Tue, 9 Jun 2020 13:54:23 +1000 Subject: [PATCH] Split CLI and DB components apart --- src/{pyelephant.py => cli.py} | 53 ++++++++--------------------------- src/db.py | 34 ++++++++++++++++++++++ 2 files changed, 46 insertions(+), 41 deletions(-) rename src/{pyelephant.py => cli.py} (69%) create mode 100644 src/db.py diff --git a/src/pyelephant.py b/src/cli.py similarity index 69% rename from src/pyelephant.py rename to src/cli.py index 41780ee..77681e5 100755 --- a/src/pyelephant.py +++ b/src/cli.py @@ -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) diff --git a/src/db.py b/src/db.py new file mode 100644 index 0000000..a440cf4 --- /dev/null +++ b/src/db.py @@ -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" + )