Example #1
0
def history_all():
    query = request.json

    Source = User.alias()
    Target = User.alias()

    history = Event.select(
        Event.id, Event.source, Event.target, Event.type, Event.amount,
        Event.created_at, Source.name.alias('source_name'),
        Target.name.alias('target_name')).join(
            Source, on=(Event.source == Source.id)).switch(Event).join(
                Target, on=(Event.target == Target.id)).switch(Event)

    # Process Query Parameters
    history = process_history_query(history, query)

    # Order By
    order_by = Event.created_at.desc()

    if 'sort' in query and query['sort'] is not None:
        field = query['sort']['field'] if 'field' in query[
            'sort'] else 'created_at'

        if field == 'source':
            field = 'source_name'
        elif field == 'target':
            field = 'target_name'

        order_by = SQL('%s %s' % (field, query['sort']['direction']))

    history = history.order_by(order_by)

    # Get Result Count
    count = history.count()

    # Paginate
    page = 1
    limit = 10

    if 'page' in query and query['page'] is not None:
        page = query['page']

    if 'limit' in query and query['limit'] is not None:
        limit = query['limit']

    history = history.paginate(page, limit)

    # Query and return as dictionary
    history = history.dicts()

    # Process results
    events = []
    for event in history:
        event['type'] = event['type'] if event['type'] != '' else 'other'
        events.append(event)

    result = {'history': events, 'count': count}

    return Response(json.dumps(result, cls=Encoder, use_decimal=True),
                    mimetype='application/json')
Example #2
0
def history_by_source():
  query = request.json

  Source = User.alias()
  Target = User.alias()

  history = Event.select(
    Event.id, Event.source, Source.name.alias('source_name'), fn.Sum(Event.amount).alias('amount')
  ).join(
    Source, on=(Event.source == Source.id)
  ).switch(Event)

  # Group by Giver
  history = history.group_by(Event.source)

  # Process Query Parameters
  history = process_history_query(history, query)

  # Order By
  history = history.order_by(SQL('amount DESC'))

  # Get Result Count
  count = history.count()

  # Paginate
  page = 1
  limit = 5

  if 'page' in query and query['page'] is not None:
    page = query['page']

  if 'limit' in query and query['limit'] is not None:
    limit = query['limit']

  history = history.paginate(page, limit)

  # Query and return as dictionary
  history = history.dicts()

  # Process results
  events = []
  for event in history:
    events.append(event)

  result = {
    'history': events,
    'count': count
  }

  return Response(json.dumps(result, cls=Encoder, use_decimal=True), mimetype='application/json')
Example #3
0
def history_by_source():
    query = request.json

    Source = User.alias()
    Target = User.alias()

    history = Event.select(Event.id, Event.source,
                           Source.name.alias('source_name'),
                           fn.Sum(Event.amount).alias('amount')).join(
                               Source,
                               on=(Event.source == Source.id)).switch(Event)

    # Group by Giver
    history = history.group_by(Event.source)

    # Process Query Parameters
    history = process_history_query(history, query)

    # Order By
    history = history.order_by(SQL('amount DESC'))

    # Get Result Count
    count = history.count()

    # Paginate
    page = 1
    limit = 5

    if 'page' in query and query['page'] is not None:
        page = query['page']

    if 'limit' in query and query['limit'] is not None:
        limit = query['limit']

    history = history.paginate(page, limit)

    # Query and return as dictionary
    history = history.dicts()

    # Process results
    events = []
    for event in history:
        events.append(event)

    result = {'history': events, 'count': count}

    return Response(json.dumps(result, cls=Encoder, use_decimal=True),
                    mimetype='application/json')
Example #4
0
def history_all():
  query = request.json

  Source = User.alias()
  Target = User.alias()

  history = Event.select(
    Event.id, Event.source, Event.target, Event.type, Event.amount, Event.created_at, Source.name.alias('source_name'), Target.name.alias('target_name')
  ).join(
    Source, on=(Event.source == Source.id)
  ).switch(Event).join(
    Target, on=(Event.target == Target.id)
  ).switch(Event)

  # Process Query Parameters
  history = process_history_query(history, query)

  # Order By
  order_by = Event.created_at.desc()

  if 'sort' in query and query['sort'] is not None:
    field = query['sort']['field'] if 'field' in query['sort'] else 'created_at'

    if field == 'source':
      field = 'source_name'
    elif field == 'target':
      field = 'target_name'

    order_by = SQL('%s %s' % (field, query['sort']['direction']))

  history = history.order_by(order_by)

  # Get Result Count
  count = history.count()

  # Paginate
  page = 1
  limit = 10

  if 'page' in query and query['page'] is not None:
    page = query['page']

  if 'limit' in query and query['limit'] is not None:
    limit = query['limit']

  history = history.paginate(page, limit)

  # Query and return as dictionary
  history = history.dicts()

  # Process results
  events = []
  for event in history:
    event['type'] = event['type'] if event['type'] != '' else 'other'
    events.append(event)

  result = {
    'history': events,
    'count': count
  }

  return Response(json.dumps(result, cls=Encoder, use_decimal=True), mimetype='application/json')