Exemplo n.º 1
0
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)
Exemplo n.º 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)
Exemplo n.º 3
0
def build_authored(author_id):
    q = qb.make('select')
    qb.add_ordering(q, ('id', 'DESC'))

    qb.add_where(q, 'author_id = {author_id}', {'author_id': author_id})
    qb.add_where(q, 'status = "open"')

    return q
Exemplo n.º 4
0
def build_assigned(employee_id):
    q = qb.make('select')
    qb.add_ordering(q, ('id', 'DESC'))

    qb.add_where(q, 'employee_id = {employee_id}',
                 {'employee_id': employee_id})
    qb.add_where(q, 'status = "open"')

    return q
Exemplo n.º 5
0
def get_task_by_id(task_id):
    q = qb.make('select', __table__)

    qb.set_columns(q, __fields__)
    qb.add_where(q, '%(pk)s = {%(pk)s}' % {'pk': __fields__[0]}, {
        __fields__[0]: task_id
    })

    cursor = raw_query(*qb.to_sql(q))

    return make_task_from_row(cursor.fetchone())
Exemplo n.º 6
0
def get_user_by_credentials(email, password):
    q = qb.make('select', __table__)
    qb.set_columns(q, __fields__)
    qb.add_where(q, 'email = {email}', {'email': email})
    qb.set_limit(q, 1)

    cursor = raw_query(*qb.to_sql(q))

    user = make_user_from_row(cursor.fetchone())

    if user is not None and not check_password(password, user['password']):
        return None

    return user
Exemplo n.º 7
0
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
Exemplo n.º 8
0
def get_user_by_id(user_id):
    user_cached = cache.load(__cache_key__ % user_id)
    if user_cached:
        return user_cached

    q = qb.make('select', __table__)
    qb.set_columns(q, __fields__)
    qb.add_where(q, '%(pk)s = {%(pk)s}' % {'pk': __fields__[0]},
                 {__fields__[0]: user_id})

    cursor = raw_query(*qb.to_sql(q))

    user = make_user_from_row(cursor.fetchone())

    cache.store(__cache_key__ % user_id, user, __cache_ttl__)

    return user
Exemplo n.º 9
0
def build_unassigned():
    q = qb.make('select')
    qb.add_ordering(q, ('id', 'DESC'))

    qb.add_where(q, 'employee_id IS NULL')
    qb.add_where(q, 'status = "open"')
    qb.add_where(q, 'ok = 1')

    return q
Exemplo n.º 10
0
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.")
Exemplo n.º 11
0
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)
Exemplo n.º 12
0
def paginate_by_pk(q, last_pk, limit, pk_field='id'):
    if last_pk:
        qb.add_where(q, '%s < {last_pk}' % pk_field, {'last_pk': last_pk})

    qb.set_limit(q, limit)