예제 #1
0
def join_to_event(current_user, event_id):
    team_id = request.args.get('teamId', None, type=int)

    try:
        int(event_id)
    except ValueError:
        return response('failed', 'Please provide a valid Event Id', 400)

    user = User.get_by_id(current_user.id)
    event = Event.get_by_id(event_id)
    if not event:
        abort(404)

    if event == EventStatus.canceled:
        return response('failed', 'Event was canceled', 400)

    if event == EventStatus.ended:
        return response('failed', 'Event was ended', 400)

    if team_id:
        team = Team.get_by_id(team_id)

        if not team:
            return response('failed', 'Team cannot be found', 404)

        if not team in event.teams:
            return response(
                'failed', 'The Event with Id ' + event_id +
                ' doesn\'t have Team with Id ' + team_id, 404)

    elif event.type is EventType.training:
        team = event.training.team
    else:
        return response('failed',
                        'Event with given Id requires teamId to join', 404)

    #check requirements to join
    if not (event.participants_age_from <= user.age <=
            event.participants_age_to):
        return response(
            'failed', 'User\'s age does\'t meet the requirements of the event',
            400)

    if user in team.participants:
        return response('failed', 'User already joined to the team', 400)

    if team.is_full:
        return response('failed', 'Team is full', 400)

    # remove user from other team in event
    for t in event.teams:
        if user in t.participants:
            t.participants.remove(user)

    # add user to team
    team.participants.append(user)
    team.update()

    return response_for_created_event(event.json(current_user), 201)
예제 #2
0
def get_team(current_user, team_id):
    try:
        int(team_id)
    except ValueError:
        return response('failed', 'Please provide a valid Team Id', 400)
    else:
        team = Team.get_by_id(team_id)
        if team:
            return response_for_team(team.json())
        return response('failed', "Team not found", 404)