Source

orgtool / orgtool / ext / events / commands.py

Full commit
from argh import alias, arg, command
from tool import app

from .schema import Event, Plan


def _get_plans(query=None):
    db = app.get_feature('document_storage').default_db
    plans = Plan.objects(db)
    if query:
        plans = plans.where(summary__matches_caseless=query)
    return plans

@command
@alias('ls')
def list_events(query=None, plan=None, format=u'{date_time} {summary}', date=None):
    "Displays a list of matching events."
    db = app.get_feature('document_storage').default_db
    events = Event.objects(db)
    if plan:
        plans = list(_get_plans(query=plan))
        events = events.where(plan__contains_any=plans)
    if query:
        events = events.where(summary__matches_caseless=query)
    for event in events:
        yield format.format(**event)

@alias('lsp')
@arg('-q', '--query')
def list_plans(args):
    for p in _get_plans(args.query):
        yield p

@alias('next')
@arg('-O', '--no-overdue')
def list_next_plans(args):
    pass

#@alias('schedule')
#@arg('-q', '--query')
#def schedule

def update_rrules(args):
    """Updates expected next event dates with regard to recurrence rules and
    current date.
    """
    for plan in _get_plans():
        new_date = plan.review_next_date()
        if new_date:
            yield u'* updating {summary}'.format(**plan)
    yield 'Done.'

available_commands = (list_events, list_plans, update_rrules)