def complete_task(task_id, user_id):
    task = get_task_by_id(task_id)

    if not task:
        raise LookupError('Task not found.')

    if task['employee_id'] != user_id:
        raise PermissionError("Not assigned to you.")

    if task['status'] == 'done':
        raise PermissionError("Already done.")

    q = qb.make('update')
    qb.add_values(q, [
        ('status', '"done"'),
        ('ok', 0)
    ])

    qb.add_where(q, [
        'id = {id}',
        'status = "open"',
        'ok = 1'
    ], {'id': task_id})

    c = update_tasks(q)

    if c == 0:
        raise PermissionError("Already done.")

    pay_user_for_task(user_id, task)

    mark_task_ok(task_id)
Exemple #2
0
def decrease_user_amount(user_id, amount):
    q = qb.make('update', __table__)
    qb.add_values(q, [('balance', 'balance - {amount}')], {'amount': amount})
    qb.add_where(q, '%s = {pk}' % __fields__[0], {'pk': user_id})

    raw_query(*qb.to_sql(q), commit=True)

    cache.delete(__cache_key__ % user_id)
def update_task(task):
    q = qb.make('update', __table__)
    qb.add_values(q, [
        (field, '{%s}' % field) for field in __fields__[1:]
    ])
    qb.add_where(q, '%(pk)s = {%(pk)s}' % {
        'pk': __fields__[0]
    })
    qb.add_params(q, task)

    cursor = raw_query(*qb.to_sql(q), commit=True)

    if not cursor.rowcount:
        raise RuntimeError('No tasks updated.')

    return task
Exemple #4
0
def create_transaction(task):
    q = qb.make('insert', __table__)

    qb.add_values(q, [(field, '{%s}' % field) for field in __fields__[1:]])

    if task[__fields__[0]]:
        qb.add_values(q, (__fields__[0], '{' + __fields__[0] + '}'))

    qb.add_params(q, task)

    cursor = raw_query(*qb.to_sql(q), commit=True)

    if not cursor.rowcount:
        raise RuntimeError('No transaction created.')

    task[__fields__[0]] = cursor.lastrowid

    return task
Exemple #5
0
def create_user(user):
    q = qb.make('insert', __table__)
    qb.set_ignore_mode(q, True)

    qb.add_values(q, [(field, '{' + field + '}') for field in __fields__[1:]])

    if user[__fields__[0]]:
        qb.add_values(q, (__fields__[0], '{' + __fields__[0] + '}'))

    qb.add_params(q, user)

    cursor = raw_query(*qb.to_sql(q), commit=True)

    if not cursor.rowcount:
        raise RuntimeError('No users created.')

    user[__fields__[0]] = cursor.lastrowid

    cache.store(__cache_key__ % user['id'], user, __cache_ttl__)

    return user
def assign_task(task_id, user_id):
    task = get_task_by_id(task_id)

    if not task:
        raise LookupError('Task not found.')

    if task['employee_id'] is not None:
        raise PermissionError("Task already assigned.")

    q = qb.make('update')
    qb.add_values(q, [
        ('employee_id', '{employee_id}')
    ], {'employee_id': user_id})

    qb.add_where(q, [
        'id = {id}',
        'employee_id IS NULL',
        'ok = 1'
    ], {'id': task_id, 'user_id': user_id})

    c = update_tasks(q)

    if c == 0:
        raise PermissionError("Task already assigned.")
def mark_task_ok(task_id):
    q = qb.make('update')
    qb.add_values(q, [('ok', '1')])
    qb.add_where(q, 'id = {id}', {'id': task_id})
    update_tasks(q)