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