Example #1
0
def close_event(**options):
    db = DB()
    user_id, status, error = db.auth(options['username'], options['password'])
    if status:
        return response_error(status, error)

    admin_id, status, error = db.get_event_admin_id(options['event_id'])
    if status:
        return response_error(status, error)

    if admin_id != user_id:
        return response_error(1, 'you are not event admin')

    if options['event_status'] == 'Closed':
        participants, status, error = db.get_event_participants(options['event_id'])
        if status:
            return response_error(status, error)

        if not all(elem in participants for elem in options['results'].keys()):
            return response_error(1, "list of results doesnt match with participants")

        # for username, result in options['results'].items():
        for username in participants:
            result = random.choice(['W', 'D', 'L']) 
            points = 2 if result == 'W' else 1 if result == 'D' else 0
            status, errot = db.set_result(options['event_id'], username, result, points)
            if status:
                return response_error(status, error)

    status, error = db.update_event_status(options['event_id'], options['event_status'])
    if status:
        return response_error(status, error)

    return response_ok()
Example #2
0
def leave_event(**options):
    db = DB()
    user_id, status, error = db.auth(options['username'], options['password'])
    if status:
        return response_error(status, error)

    participants, status, error = db.get_event_participants(options['event_id'])
    if status:
        return response_error(status, error)

    admin_id, status, error = db.get_event_admin_id(options['event_id'])
    if status:
        return response_error(status, error)

    if user_id == admin_id:
        return response_error(1, 'admin cant leave event, you should close it')

    if options['username'] not in participants:
        return response_error(1, 'you are not in participants list')

    status, error = db.leave_event(user_id, options['event_id'])
    if status:
        return response_error(status, error)

    return response_ok()