def add_task(): """ Creates task and send operation status back to client """ user = current_user.self fields = 'proj_name', 'task_name' fields = proj_name, task_name = [request.args.get(i) for i in fields] if not all(fields): return dumps(dict(status='fail')) proj = Project.get_or_none(name=proj_name, owner=user) if not proj: return dumps(dict(status='fail')) global_max = (Task.select().order_by(Task.priority.desc())) priority = global_max.get().priority + 1 if global_max.exists() else 0 with db.atomic() as tract: try: task = Task.create(name=task_name, project=proj, priority=priority) return dumps(dict(status='success', task=get_task(task))) except PeeweeException: tract.rollback() return dumps(dict(status='fail'))
def change_task_prio(): """ Swaps priorities with current and lower/upper tasks """ user = current_user.self fields = 'proj_name', 'task_name', 'dir' fields = proj_name, task_name, dir_ = [request.args.get(i) for i in fields] if not all(fields) or not dir_ in ('1', '-1'): return dumps(dict(status='fail')) proj = Project.get_or_none(Project.owner == user and Project.name == proj_name) if not proj: return dumps(dict(status='fail')) task = Task.get_or_none(Task.project == proj and Task.name == task_name) if not task: return dumps(dict(status='fail')) i = task.priority swap = (Task.select().where(Task.project == proj and Task.priority > i if dir_ == '1' else Task.priority < i). order_by(Task.priority if dir_ == '1' else Task.priority.desc())) swap = swap.get() if swap.exists() else None if not swap: return dumps(dict(status='fail')) with db.atomic() as tract: try: tmp = task.priority swap.priority, task.priority = -1, swap.priority if not (swap.save() and task.save()): raise PeeweeException('failed to change tasks order') swap.priority = tmp if not swap.save(): raise PeeweeException('failed to change tasks order') query = (Task.select().where(Task.project == proj).order_by( Task.priority.desc())) return dumps( dict(status='success', tasks=[get_task(i) for i in query])) except PeeweeException: tract.rollback() return dumps(dict(status='fail'))
def change_status(): """ Handles change of task status """ user = current_user.self fields = 'proj_name', 'task_name' fields = proj_name, task_name = [request.args.get(i) for i in fields] if not all(fields): return 'fail' proj = Project.get_or_none(Project.owner == user and Project.name == proj_name) if not proj: return 'fail' task = Task.get_or_none(Task.project == proj and Task.name == task_name) if not task: return 'fail' with db.atomic() as tract: try: task.status = not task.status if not task.save(): raise PeeweeException('failed to change status') return 'success' except PeeweeException: tract.rollback() return 'fail'
def set_deadline(): """ Set new task deadline """ user = current_user.self fields = 'proj_name', 'task_name', 'dead' fields = proj_name, task_name, dead = [request.args.get(i) for i in fields] if not all(fields): return dumps(dict(status='fail')) proj = Project.get_or_none(Project.owner == user and Project.name == proj_name) if not proj: return dumps(dict(status='fail')) task = Task.get_or_none(Task.project == proj and Task.name == task_name) if not task: return dumps(dict(status='fail')) with db.atomic() as tract: try: task.deadline = datetime.strptime( dead, '%Y-%m-%dT%H:%M') if dead else None if not task.save(): raise PeeweeException('failed to change deadline') return dumps( dict(status='success', time=task.deadline.strftime("%d/%m/%y %H:%M"))) except PeeweeException: tract.rollback() return dumps(dict(status='fail'))
def remove_task(): """ Removes task and send operation status back to client """ user = current_user.self fields = 'proj_name', 'task_name' fields = proj_name, task_name = [request.args.get(i) for i in fields] if not all(fields): return 'fail' proj = Project.get_or_none(Project.owner == user and Project.name == proj_name) if not proj: return 'fail' task = Task.get_or_none(Task.project == proj and Task.name == task_name) if not task: return 'fail' with db.atomic() as tract: try: task.delete_instance() return 'success' except PeeweeException: tract.rollback() return 'fail'
def get_project(proj, with_tasks=True): """ Returns single project representation and all optionally embed depending task """ if with_tasks: query = (Task.select().where(Task.project == proj).order_by( Task.priority.desc())) else: query = [] return dict(pid=proj.id, name=proj.name, desc=proj.description, tasks=[get_task(t) for t in query])
def get_tasks(): """ Returns list of tasks associated with Project """ user = current_user.self proj_name = request.args.get('proj_name') proj = Project.get_or_none(Project.owner == user and Project.name == proj_name) if not proj: return dumps(dict(status='fail')) tasks = Task.select().where(Task.project == proj).order_by( Task.priority.desc()) return dumps(dict(status='success', tasks=list(map(get_task, tasks))))