Exemplo n.º 1
0
def add_team_member(team_id, member_id):
    team_member_schema = TeamMemberSchema(
        exclude=get_exclusion_list(request.args, ['team']))
    try:
        valid_attributes = team_member_schema.load(request.json,
                                                   partial=('team_id',
                                                            'member_id'))
    except ValidationError as err:
        return jsonify(err.messages), 422

    team = db.session.query(Team).filter_by(id=team_id).first()
    person = db.session.query(Person).filter_by(id=member_id).first()

    if not team:
        return jsonify(f"Team with id #{team_id} does not exist."), 404
    if not person:
        return jsonify(f"Person with id #{member_id} does not exist."), 404

    team_member = db.session.query(TeamMember).filter_by(
        team_id=team_id, member_id=member_id).first()

    if not team_member:
        new_entry = TeamMember(
            **{
                'team_id': team_id,
                'member_id': member_id,
                'active': valid_attributes['active']
            })
        db.session.add(new_entry)
        db.session.commit()
        return 'Team member successfully added.'
    else:
        return jsonify(
            f"Person with id #{member_id} is already on Team with id #{team_id}."
        ), 422
Exemplo n.º 2
0
def read_all_teams():
    team_schema = TeamSchema(
        exclude=get_exclusion_list(request.args, ['members', 'events']))
    query = db.session.query(Team)

    # -- return_inactives --
    # Filter assets based on active status
    return_group = request.args.get('return_group')
    if return_group == 'inactive':
        query = query.filter_by(active=False)
    elif return_group in ('all', 'both'):
        pass  # Don't filter
    else:
        query = query.filter_by(active=True)

    # -- description --
    # Filter events on a wildcard description string
    desc_filter = request.args.get('desc')
    if desc_filter:
        query = query.filter(Team.description.like(f"%{desc_filter}%"))

    # Sorting
    sort_filter = request.args.get('sort')
    if sort_filter:
        sort_column = None
        if sort_filter[:11] == 'description':
            sort_column = Team.description

        if sort_filter[-4:] == 'desc' and sort_column:
            sort_column = sort_column.desc()

        query = query.order_by(sort_column)

    result = query.all()
    return jsonify(team_schema.dump(result, many=True))
Exemplo n.º 3
0
def update_team(team_id):
    team_schema = TeamSchema(
        exclude=get_exclusion_list(request.args, ['members', 'events']))
    try:
        valid_attributes = team_schema.load(request.json, partial=True)
    except ValidationError as err:
        return jsonify(err.messages), 422

    return modify_entity(Team, team_schema, team_id, valid_attributes)
Exemplo n.º 4
0
def replace_asset(asset_id):
    asset_schema = AssetSchema(
        exclude=get_exclusion_list(request.args, ['location']))
    try:
        valid_asset = asset_schema.load(request.json)
    except ValidationError as err:
        return jsonify(err.messages), 422

    return modify_entity(Asset, asset_schema, asset_id, valid_asset)
Exemplo n.º 5
0
def update_asset(asset_id):
    asset_schema = AssetSchema(
        exclude=get_exclusion_list(request.args, ['location']))
    try:
        valid_attributes = asset_schema.load(request.json, partial=True)
    except ValidationError as err:
        return jsonify(err.messages), 422

    return modify_entity(Asset, asset_schema, asset_id, valid_attributes)
Exemplo n.º 6
0
def read_one_team(team_id):
    team_schema = TeamSchema(
        exclude=get_exclusion_list(request.args, ['members', 'events']))
    team = db.session.query(Team).filter_by(id=team_id).first()

    if not team:
        return jsonify(f"Team with id #{team_id} does not exist."), 404

    return jsonify(team_schema.dump(team))
Exemplo n.º 7
0
def replace_team(team_id):
    team_schema = TeamSchema(
        exclude=get_exclusion_list(request.args, ['members', 'events']))
    try:
        valid_team = team_schema.load(request.json)
    except ValidationError as err:
        return jsonify(err.messages), 422

    return modify_entity(Team, team_schema, team_id, valid_team)
Exemplo n.º 8
0
def read_one_event(event_id):
    event_schema = EventSchema(exclude=get_exclusion_list(
        request.args,
        ['assets', 'participants', 'persons', 'teams', 'images', 'groups']))
    event = db.session.query(Event).filter_by(id=event_id).first()

    if not event:
        return jsonify(f"Event with id #{event_id} does not exist."), 404

    return jsonify(event_schema.dump(event))
Exemplo n.º 9
0
def get_team_members(team_id):
    team_member_schema = TeamMemberSchema(
        exclude=get_exclusion_list(request.args, ['team']))
    team_members = db.session.query(TeamMember).filter_by(
        team_id=team_id).all()

    if not team_members:
        return jsonify(
            f"Team with id #{team_id} does not have any members."), 404

    return jsonify(team_member_schema.dump(team_members, many=True))
Exemplo n.º 10
0
def create_asset():
    asset_schema = AssetSchema(
        exclude=get_exclusion_list(request.args, ['location']))
    try:
        valid_asset = asset_schema.load(request.json)
    except ValidationError as err:
        return jsonify(err.messages), 422

    new_asset = Asset(**valid_asset)
    db.session.add(new_asset)
    db.session.commit()
    return jsonify(asset_schema.dump(new_asset)), 201
Exemplo n.º 11
0
def create_team():
    team_schema = TeamSchema(
        exclude=get_exclusion_list(request.args, ['members', 'events']))
    try:
        valid_team = team_schema.load(request.json)
    except ValidationError as err:
        return jsonify(err.messages), 422

    new_team = Team(**valid_team)
    db.session.add(new_team)
    db.session.commit()
    return jsonify(team_schema.dump(new_team)), 201
Exemplo n.º 12
0
def update_event(event_id):
    event_schema = EventSchema()
    try:
        valid_attributes = event_schema.load(request.json, partial=True)
    except ValidationError as err:
        return jsonify(err.messages), 422

    event_schema = EventSchema(exclude=get_exclusion_list(
        request.args,
        ['assets', 'participants', 'persons', 'teams', 'images', 'groups']))

    return modify_entity(Event, event_schema, event_id, valid_attributes)
Exemplo n.º 13
0
def create_event():
    event_schema = EventSchema(exclude=get_exclusion_list(
        request.args,
        ['assets', 'participants', 'persons', 'teams', 'images', 'groups']))
    try:
        valid_event = event_schema.load(request.json)
    except ValidationError as err:
        return jsonify(err.messages), 422

    new_event = Event(**valid_event)
    db.session.add(new_event)
    db.session.commit()
    return jsonify(event_schema.dump(new_event)), 201
Exemplo n.º 14
0
def read_one_asset(asset_id):
    asset_schema = AssetSchema(
        exclude=get_exclusion_list(request.args, ['location']))
    asset = db.session.query(Asset).filter_by(id=asset_id).add_columns(
        func.count(EventAsset.event_id).label('event_count')).join(
            EventAsset, isouter=True).group_by(Asset.id).first()

    if not asset:
        return jsonify(f"Asset with id #{asset_id} does not exist."), 404

    result = asset_schema.dump(asset[0])
    result['event_count'] = asset[1]

    return jsonify(asset_schema.dump(result))
Exemplo n.º 15
0
def read_all_assets():
    asset_schema = AssetSchema(
        exclude=get_exclusion_list(request.args, ['location']))
    query = db.session.query(Asset).add_columns(
        func.count(EventAsset.event_id).label('event_count'))

    # -- return_inactives --
    # Filter assets based on active status
    return_group = request.args.get('return_group')
    if return_group == 'inactive':
        query = query.filter_by(active=False)
    elif return_group in ('all', 'both'):
        pass  # Don't filter
    else:
        query = query.filter_by(active=True)

    # -- description --
    # Filter events on a wildcard description string
    desc_filter = request.args.get('desc')
    if desc_filter:
        query = query.filter(Asset.description.like(f"%{desc_filter}%"))

    # -- location --
    # Filter events on a wildcard location string?
    location_filter = request.args.get('location_id')
    if location_filter:
        query = query.filter_by(location_id=location_filter)

    # Sorting
    sort_filter = request.args.get('sort')
    if sort_filter:
        sort_column = None
        if sort_filter[:11] == 'description':
            sort_column = Asset.description

        if sort_filter[-4:] == 'desc' and sort_column:
            sort_column = sort_column.desc()

        query = query.order_by(sort_column)

    result = query.join(EventAsset, isouter=True).group_by(Asset.id).all()

    temp_result = list()
    for item in result:
        temp_result.append(asset_schema.dump(item[0]))
        temp_result[-1]['event_count'] = item[1]

    return jsonify(asset_schema.dump(temp_result, many=True))
Exemplo n.º 16
0
def modify_team_member(team_id, member_id):
    team_member_schema = TeamMemberSchema(
        exclude=get_exclusion_list(request.args, ['team']))
    try:
        valid_attributes = team_member_schema.load(request.json,
                                                   partial=('team_id',
                                                            'member_id'))
    except ValidationError as err:
        return jsonify(err.messages), 422

    team_member = db.session.query(TeamMember).filter_by(
        member_id=member_id).filter_by(team_id=team_id).first()

    if not team_member:
        return jsonify(
            f"Member with id #{member_id} is not associated with Team with id #{team_id}."
        ), 404

    setattr(team_member, 'active', valid_attributes['active'])
    db.session.commit()

    return jsonify(team_member_schema.dump(team_member))
Exemplo n.º 17
0
def read_all_events():
    event_schema = EventSchema(exclude=get_exclusion_list(
        request.args,
        ['assets', 'participants', 'persons', 'teams', 'images', 'groups']))
    query = db.session.query(Event)

    # -- return_inactives --
    # Filter events based on active status
    # True - see all events, False or missing - see only active events
    return_group = request.args.get('return_group')
    if return_group == 'inactive':
        query = query.filter_by(active=False)
    elif return_group in ('all', 'both'):
        pass  # Don't filter
    else:
        query = query.filter_by(active=True)

    # -- start, end --
    # Filter events to be greater than the start date and/or earlier than the end date (inclusive)
    start_filter = request.args.get('start')
    end_filter = request.args.get('end')
    if start_filter:
        query = query.filter(
            Event.start > (datetime.strptime(start_filter, '%Y-%m-%d') -
                           timedelta(days=1)))
    if end_filter:
        query = query.filter(
            Event.end < (datetime.strptime(end_filter, '%Y-%m-%d') +
                         timedelta(days=1)))

    # -- title --
    # Filter events on a wildcard title string
    title_filter = request.args.get('title')
    if title_filter:
        query = query.filter(Event.title.like(f"%{title_filter}%"))

    # -- location --
    # Filter events on a wildcard location string?
    location_filter = request.args.get('location_id')
    if location_filter:
        query = query.filter_by(location_id=location_filter)

    # Sorting
    sort_filter = request.args.get('sort')
    if sort_filter:
        sort_column = None
        if sort_filter[:5] == 'start':
            sort_column = Event.start
        elif sort_filter[:3] == 'end':
            sort_column = Event.end
        elif sort_filter[:5] == 'title':
            sort_column = Event.title

        if sort_filter[-4:] == 'desc' and sort_column:
            sort_column = sort_column.desc()

        query = query.order_by(sort_column)

    result = query.all()

    return jsonify(event_schema.dump(result, many=True))