def create(team): """ This function creates a new team in the team list based on the passed in team data :param team: team to create in team structure :return: 201 on success, 406 on team exists """ key = team.get("key", None) value = team.get("value", None) # Does the team exist already? existing_team = (Team.query.filter(Team.key == key).one_or_none()) if existing_team is None: schema = TeamSchema() new_team = schema.load(team, session=db.session) db.session.add(new_team) db.session.commit() # Serialize and return the newly created deployment # in the response data = schema.dump(new_team) return data, 201 # Otherwise, it already exists, that's an error else: abort(406, f"Deployment already exists")
def update(key, team): """ This function updates an existing team in the team list :param key: key of the team to update in the team list :param team: team to update :return: updated team """ app.logger.debug(pformat(team)) if team["key"] != key: abort(400, f"Key mismatch in path and body") # Does the team exist in team list? existing_team = Team.query.filter(Team.key == key).one_or_none() # Does team exist? if existing_team is not None: schema = TeamSchema() update_team = schema.load(team, session=db.session) update_team.key = team['key'] db.session.merge(update_team) db.session.commit() # return the updted team in the response data = schema.dump(update_team) return data, 200 # otherwise, nope, deployment doesn't exist, so that's an error else: abort(404, f"Team not found")
def update(oid, teamDetails): """ Updates an existing team in the team list :param id: id of the team to update in the team list :param team: team to update :return: updated team. """ app.logger.debug(pformat(teamDetails)) if teamDetails.get("id") and teamDetails.get("id") != int(oid): abort(400, "Id mismatch in path and body") # Does the team exist in team list? existing_team = db.session.query(Team).filter(Team.id == oid).one_or_none() # Does team exist? if existing_team is not None: schema = TeamSchema() update_team = schema.load(teamDetails, session=db.session) update_team.name = teamDetails.get("name", existing_team.name) update_team.description = teamDetails.get("description", existing_team.description) update_team.cloudIdentityGroup = teamDetails.get( "cloudIdentityGroup", existing_team.cloudIdentityGroup) update_team.businessUnitId = teamDetails.get( "businessUnitId", existing_team.businessUnitId) update_team.accessRequestedById = teamDetails.get( "accessRequestedById", existing_team.accessRequestedById) update_team.lastUpdated = ModelTools.get_utc_timestamp() update_team.isActive = teamDetails.get("isActive", existing_team.isActive) db.session.merge(update_team) try: db.session.commit() except exc.IntegrityError as err: db.session.rollback() app.logger.debug(str(err)) if "Duplicate entry" in str(err): return abort(400, "Team already exists") else: return abort(400, "Unknown Integrity Error adding team") except Exception as err: db.session.rollback() app.logger.debug(str(err)) return abort(400, "Unknown error adding team") # return the updted team in the response data = schema.dump(update_team) return data, 200 # otherwise, nope, deployment doesn't exist, so that's an error abort(404, "Team not found")
def read_all_by_user_id(userId): teams = (db.session.query(Team).filter( TeamMember.teamId == Team.id, TeamMember.userId == userId, Team.isActive, TeamMember.isActive, ).all()) schema = TeamSchema(many=True) # Convert to JSON (Serialization) data = schema.dump(teams) app.logger.debug(f"{data} type: {type(data)}") return data, 200
def read_all(): """ This function responds to a request for /api/team with the complete lists of teams :return: json string of list of teams """ # Create the list of teams from our data team = Team.query.order_by(Team.key).all() app.logger.debug(pformat(team)) # Serialize the data for the response team_schema = TeamSchema(many=True) data = team_schema.dump(team) return data
def read_one(key): """ This function responds to a request for /api/team/{key} with one matching team from teams :param application: key of team to find :return: team matching key """ team = (Team.query.filter(Team.key == key).one_or_none()) if team is not None: # Serialize the data for the response team_schema = TeamSchema() data = team_schema.dump(team) return data else: abort(404, "Team with key {key} not found".format(key=key))
def read_keyValues(): """ Responds to a request for /api/keyValues/team with the complete lists of teams :return: json string of list of teams """ # Create the list of teams from our data team = db.session.query(Team).order_by(Team.id).all() app.logger.debug(pformat(team)) # Serialize the data for the response team_schema = TeamSchema(many=True) data = team_schema.dump(team) app.logger.debug(data) # Convert the data to keyvalue pairs of id and name column keyValues = [] for d in data: keyValuePair = {} keyValuePair["key"] = d.get("id") keyValuePair["value"] = d.get("name") keyValues.append(keyValuePair) print(keyValues) return keyValues
def create(teamDetails): """ Creates a new team in the team list based on the passed in team data :param team: team to create in team structure :return: 201 on success, 406 on team exists. """ # Remove id as it's created automatically if "id" in teamDetails: del teamDetails["id"] # Does the team exist already? schema = TeamSchema(many=False) new_team = schema.load(teamDetails, session=db.session) new_team.lastUpdated = ModelTools.get_utc_timestamp() app.logger.debug(f"new_team: {new_team} type: {type(new_team)}") db.session.add(new_team) try: db.session.commit() except exc.IntegrityError as err: db.session.rollback() app.logger.debug(str(err)) if "Duplicate entry" in str(err): return abort(400, "Team already exists") else: return abort(400, "Unknown Integrity Error adding team") except Exception as err: db.session.rollback() app.logger.debug(str(err)) return abort(400, "Unknown error adding team") # Serialize and return the newly created deployment # in the response data = schema.dump(new_team) return data, 201
from flask import request from flask_restful import Resource from models import db, Team, SubmissionSchema, Submission, TeamSchema submission_schema = SubmissionSchema() team_schema = TeamSchema() class SubmissionResource(Resource): def post(self): # grab data from json input submission_json_data = request.get_json(force=True) if not submission_json_data: return { 'status': 'error', 'message': 'No input data provided' }, 400 # grab team name from args team_name = request.args.get('team_name') if not team_name: return { 'status': 'error', 'message': 'No argument team_name provided' }, 400 # separate input json into two, one for team, one for submission team_json_data = {"team_name": team_name} input_submission = submission_schema.load(submission_json_data) input_team = team_schema.load(team_json_data)