def save_todo(context: 'micro_context', description: str, done: datetime=None, todo_id: int=None) -> dict: ''' creates or updates a todo broadcasts: SAVED_TODO returns: { id: int, person_id: int, description: str, created: datetime, done: datetime } ''' with context.session as session: user = context.get_user(session) assert user, 'login required' if todo_id: todo = session.query(model.ToDo).get(todo_id) else: todo = model.ToDo(person=user, created=datetime.datetime.now()) session.add(todo) todo.description = description todo.done = parse_date(done) session.commit() return context.broadcast("SAVED_TODO", todo_view(todo))
def active_subscriptions_for(session, user_id=None, group_id=None, on_date=None, loaded=True): if on_date is None: on_date = datetime.date.today() else: on_date = parse_date(on_date) result = session.query(model.Subscription).\ filter(and_(model.Subscription.from_date <= on_date, or_(model.Subscription.to_date > on_date, model.Subscription.to_date.is_(None)))) if user_id is not None: result = result.filter(model.Subscription.user_id == user_id) if group_id is not None: result = result.filter(model.Subscription.group_id == group_id) if loaded is True: result = result.options(subqueryload(model.Subscription.user)) result = result.options(subqueryload(model.Subscription.group)) result = result.options(subqueryload(model.Subscription.service)) return result