Exemplo n.º 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()
Exemplo n.º 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()
Exemplo n.º 3
0
def create_event(**options):
    db = DB()

    user_id, status, error = db.auth(options['username'], options['password'])
    if status:
        return response_error(status, error)
    
    dt = datetime.utcfromtimestamp(options['timestamp']).strftime('%Y-%m-%d %H:%M:%S')
    event_id, status, error = db.create_event(user_id,
                                              options['sport_id'],
                                              dt,
                                              options['location'],
                                              options['description'],
                                              options['participants_number_max'],
                                              options['status_rating'])
    if status:
        return response_error(status, error)

    usernames, status, error = db.get_list_users(options['sport_id'], options['location'])
    if status:
        return response_error(status, error)

    if options['username'] in usernames:
        usernames.remove(options['username'])

    return response_ok({'event_id': event_id, 'usernames': usernames})
Exemplo n.º 4
0
def join_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)

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

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

    if len(participants) == event_info['participants_number_max']:
        return response_error(1, 'event is full')

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

    return response_ok()
Exemplo n.º 5
0
def get_follow(**options):
    db = DB()
    user_id, status, error = db.auth(options['username'], options['password'])
    if status:
        return response_error(status, error)

    data, status, error = db.get_follow_info(options['follow_id'])
    if status:
        return response_error(status, error)

    return response_ok({'follow_info': data})
Exemplo n.º 6
0
def get_list_follows(**options):
    db = DB()
    user_id, status, error = db.auth(options['username'], options['password'])
    if status:
        return response_error(status, error)

    follows, status, error = db.get_list_follows(user_id)
    if status:
        return response_error(status, error)

    return response_ok({'follow_ids': follows})
Exemplo n.º 7
0
def remove_follow(**options):
    db = DB()
    user_id, status, error = db.auth(options['username'], options['password'])
    if status:
        return response_error(status, error)

    data, status, error = db.get_follow_info(options['follow_id'])
    if status:
        return response_error(status, error)

    if data['user_id'] != user_id:
        return response_error(1, 'you cant remove others follows')

    status, error = db.remove_follow(options['follow_id'])
    if status:
        return response_error(status, error)

    return response_ok()
Exemplo n.º 8
0
def add_follow(**options):
    db = DB()
    user_id, status, error = db.auth(options['username'], options['password'])
    if status:
        return response_error(status, error)

    status, error = db.create_follow(user_id, options['sport_id'], options['location'])
    if status:
        return response_error(status, error)

    events, status, error = db.get_list_events(options['sport_id'], options['location'])
    if status:
        return response_error(status, error)

    suggested_events = []
    for event in events:
        participants, status, error = db.get_event_participants(event)
        if status:
            return response_error(status, error)

        if not options['username'] in participants:
            suggested_events.append(event)

    return response_ok({'event_ids': suggested_events})