Beispiel #1
0
def matrixModeAction(mode):
    points = Point.get_points()
    users = User.get_users()

    names = users.keys()
    names.sort()

    if mode == 'received':
        matrix = []
        for user in names:
            entry = []

            for otherUser in names:
                value = 0
                if user in points and otherUser in points[user]['received']:
                    value = points[user]['received'][otherUser]

                entry.append(value)

            matrix.append(entry)
    else:
        matrix = []
        for user in names:
            entry = []

            for otherUser in names:
                value = 0
                if user in points and otherUser in points[user]['given']:
                    value = points[user]['given'][otherUser]

                entry.append(value)

            matrix.append(entry)

    return Response(json.dumps(matrix), mimetype='application/json')
Beispiel #2
0
    def do_get_events():
        events = {}

        users = User.get_users()
        for name in users:
            events[name] = Event.get_events(name)

        return jsonify(success=1, events=events)
Beispiel #3
0
def userAction(include_self=True):
    user_dict = User.get_users()
    user_names = user_dict.keys()
    user_names.sort()

    users = []
    for name in user_names:
        if name == current_user.name and not include_self:
            continue

        entry = user_dict[name]
        users.append(entry)

    return Response(json.dumps(users), mimetype='application/json')
Beispiel #4
0
    def get_points(week=False):
        points = {}

        users = User.get_users()
        for source in users:
            sourceUser = users[source]
            if not sourceUser:
                continue

            if sourceUser['disabled']:
                continue

            events = Event.get_events(source, False, week)
            for event in events:
                target = event['user']
                targetUser = User.get_user(target)
                if not targetUser:
                    continue

                if targetUser['disabled']:
                    continue

                if source not in points:
                    points[source] = {
                        'givenTotal': 0,
                        'receivedTotal': 0,
                        'given': {},
                        'received': {},
                    }

                amount = int(event['amount'])

                if event['type'] == 'give':
                    points[source]['givenTotal'] += amount

                    if target not in points[source]['given']:
                        points[source]['given'][target] = 0

                    points[source]['given'][target] += amount
                else:
                    points[source]['receivedTotal'] += amount

                    if target not in points[source]['received']:
                        points[source]['received'][target] = 0

                    points[source]['received'][target] += amount

        return points
Beispiel #5
0
    def get_timeline():
        events = {}

        users = User.get_users()
        for name in users:
            user_events = Event.get_events(name)
            for event in user_events:
                if event['type'] != 'give':
                    continue

                timestamp = float(datetime.strptime(event['date'], '%Y-%m-%dT%H:%M:%SZ').strftime('%s'))
                while timestamp in events:
                    timestamp += 0.001

                event['source'] = name
                events[timestamp] = event

        timeline = OrderedDict(sorted(events.items(), key=lambda t: -t[0]))

        return timeline
Beispiel #6
0
def userNameAction(name):
    name = name.encode('ascii')

    if name == 'list':
        return userAction(False)

    users = User.get_users()
    if name not in users:
        abort(404)

    user = users[name]
    user['events'] = Event.get_events(name)
    user['given'] = 0
    user['received'] = 0

    points = Point.get_points()
    if name in points:
        user['given'] = points[name]['givenTotal']
        user['received'] = points[name]['receivedTotal']

    return Response(json.dumps(user), mimetype='application/json')
Beispiel #7
0
def winners():
    totals = {}
    leaders = {}
    highest = {}

    current_week = datetime.now().strftime('%U %y 0')
    current_date = datetime.strptime(current_week, '%U %y %w').strftime('%Y-%m-%dT%H:%M:%S')

    users = User.get_users()
    for source in users:
        events = Event.get_events(source)
        for event in events:
            target = event['user']
            amount = int(event['amount'])

            week = datetime.strptime(event['date'], '%Y-%m-%dT%H:%M:%SZ').strftime('%U %y 0')
            date = datetime.strptime(week, '%U %y %w').strftime('%Y-%m-%dT%H:%M:%S')

            if not date in totals:
                totals[date] = {}

            if not source in totals[date]:
                totals[date][source] = {
                    'given': 0,
                    'received': 0,
                }

            if not target in totals[date]:
                totals[date][target] = {
                    'given': 0,
                    'received': 0,
                }

            if event['type'] == 'give':
                totals[date][source]['given'] += amount
                totals[date][target]['received'] += amount

    for date in totals:
        leaders[date] = {
            'date': date,
            'current': 0,
            'given': [],
            'received': [],
        }

        if current_date == date:
            leaders[date]['current'] = 1

        highest[date] = {
            'given': 0,
            'received': 0,
        }

        for person in totals[date]:
            if totals[date][person]['given'] > highest[date]['given']:
                leaders[date]['given'] = [{
                    'name': person,
                    'amount': totals[date][person]['given'],
                }]

                highest[date]['given'] = totals[date][person]['given']
            elif totals[date][person]['given'] == highest[date]['given']:
                leaders[date]['given'].append({
                    'name': person,
                    'amount': totals[date][person]['given'],
                })

            if totals[date][person]['received'] > highest[date]['received']:
                leaders[date]['received'] = [{
                    'name': person,
                    'amount': totals[date][person]['received'],
                }]

                highest[date]['received'] = totals[date][person]['received']
            elif totals[date][person]['received'] == highest[date]['received']:
                leaders[date]['received'].append({
                    'name': person,
                    'amount': totals[date][person]['received'],
                })

    return Response(json.dumps(leaders.values()), mimetype='application/json')
Beispiel #8
0
def userTargetsAction():
  users = User.get_users(include_self=False, exclude_disabled=True)
  return Response(json.dumps(users, cls=Encoder), mimetype='application/json')
Beispiel #9
0
def userAction():
  users = User.get_users()

  return Response(json.dumps(users, cls=Encoder), mimetype='application/json')
Beispiel #10
0
def userTargetsAction():
    users = User.get_users(include_self=False, exclude_disabled=True)
    return Response(json.dumps(users, cls=Encoder),
                    mimetype='application/json')
Beispiel #11
0
def userAction():
    users = User.get_users()

    return Response(json.dumps(users, cls=Encoder),
                    mimetype='application/json')