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)
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 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
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
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())
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
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
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
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
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)
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)