Source

quicktasks / quicktasks / models.py

Full commit
import sqlahelper
import sqlalchemy as sa
import sqlalchemy.orm as orm
from sqlalchemy.ext.hybrid import hybrid_property, hybrid_method

Base = sqlahelper.get_base()
DBSession = sqlahelper.get_session()

class Project(Base):
    query = DBSession.query_property()
    __tablename__ = 'projects'
    id = sa.Column(sa.Integer, primary_key=True)
    name = sa.Column(sa.Unicode(255))
    user = sa.Column(sa.Unicode(255))

    def add_task(self, name):
        task = Task(name=name, project=self)
        self.tasks.append(task)
        return task

    @hybrid_method
    def contains(self, task):
        return task.project == self

    def query_tasks(self):
        return Task.query.filter(Task.project==self)

    def query_new_tasks(self):
        return self.query_tasks().filter(Task.new)

    def query_finished_tasks(self):
        return self.query_tasks().filter(Task.finished)

    def query_active_tasks(self):
        return self.query_tasks().filter(Task.active)

    def query_pending_tasks(self):
        return self.query_tasks().filter(Task.pending)

class Task(Base):
    query = DBSession.query_property()
    __tablename__ = 'tasks'
    id = sa.Column(sa.Integer, primary_key=True)
    name = sa.Column(sa.Unicode(255))
    project_id = sa.Column(sa.Integer, sa.ForeignKey('projects.id'))
    state = sa.Column(sa.Enum('new', 'active', 'pending', 'finished'), default="new")
    due = sa.Column(sa.DateTime)
    project = orm.relationship('Project', backref="tasks")


    @hybrid_property
    def pending(self):
        return self.state == 'pending'

    @hybrid_property
    def active(self):
        return self.state == 'active'

    @hybrid_property
    def finished(self):
        return self.state == 'finished'

    @hybrid_property
    def new(self):
        return self.state == 'new'

    def finish(self):
        self.state = 'finished'

    def activate(self):
        self.state = "active"

    def make_pending(self):
        self.state = "pending"