示例#1
0
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'
示例#2
0
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'
示例#3
0
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'))
示例#4
0
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'))
示例#5
0
def change_proj_name():
    """
    Handles change of Project name
    """

    user = current_user.self
    fields = 'curr_name', 'new_name'
    fields = curr_name, new_name = [request.args.get(i) for i in fields]

    if not all(fields):
        return 'fail'

    if Project.get_or_none(Project.owner == user and Project.name == new_name):
        return 'exists'

    proj = Project.get_or_none(Project.owner == user
                               and Project.name == curr_name)
    if not proj:
        return 'noexists'

    with db.atomic() as tract:
        try:
            proj.name = new_name
            ret = proj.save()
            if not ret:
                raise PeeweeException('failed to rename project')
        except PeeweeException:
            tract.rollback()
            return 'fail'

    return 'success'
示例#6
0
def change_desc():
    """
    Handles change of Project description
    """

    user = current_user.self
    fields = 'proj_name', 'desc'
    name, desc = [request.args.get(i) for i in fields]

    if not name:
        return 'fail'

    proj = Project.get_or_none(Project.owner == user and Project.name == name)
    if not proj:
        return 'fail'

    with db.atomic() as tract:
        try:
            proj.description = desc
            ret = proj.save()
            if not ret:
                raise PeeweeException('failed to change description')
        except PeeweeException:
            tract.rollback()
            return 'fail'

    return 'success'
示例#7
0
def create_project():
    """
    Creating Project instance and send view representation
    back to client
    """

    user = current_user.self
    name = request.args.get('name')

    if not name:
        return dumps(dict(status='fail'))

    if Project.get_or_none(name=name, owner=user):
        return dumps(dict(status='exists'))

    with db.atomic() as tract:
        try:
            proj = Project.create(name=name, owner=user)
            if not proj:
                raise PeeweeException('failed to create project')
        except PeeweeException:
            tract.rollback()
            return dumps(dict(status='fail'))

    return dumps(
        dict(status='success', project=get_project(proj, with_tasks=False)))
示例#8
0
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'))
示例#9
0
def remove_project():
    """
    Removes project and sends operation status back to client
    """

    user = current_user.self
    name = request.args.get('proj_name')

    proj = Project.get_or_none(Project.name == name and Project.owner == user)
    if not proj:
        return 'fail'

    with db.atomic() as tract:
        try:
            if not proj.delete_instance(recursive=True):
                raise PeeweeException('failed to delete project')
        except PeeweeException:
            tract.rollback()
            return 'fail'

    return 'success'
示例#10
0
def sign_up():
    """
    Handles user sign_up i.e. User instance creation
    and send operation status back to client
    """

    fields = 'username', 'password', 'email'
    fields = username, password, email = [request.args.get(i) for i in fields]

    if not all(fields):
        return 'not enough fields'

    if min(map(len, fields)) < 5:
        return 'short field'

    if not email_correct(email):
        return 'incorrect email'

    if User.get_or_none(User.username == username or User.email == email):
        return 'exists'

    p_h = crypt.generate_password_hash(password).decode('utf-8')

    with db.atomic() as tract:
        try:
            user = User.create(username=username,
                               password_hash=p_h,
                               email=email)
            if not user:
                raise PeeweeException('failed to create user')
        except PeeweeException:
            tract.rollback()
            return 'fail'

    login_user(user)

    return 'success'