def create(applicationDetails): """ This function creates a new application in the application structure based on the passed in application data :param application: application to create in application list :return: 201 on success, 406 on application exists """ # Remove id as it's created automatically if 'id' in applicationDetails: del applicationDetails['id'] # As discussed with Fabio, explicitly set resources to '[]' if not set if not type(applicationDetails.get('resources')) is list: applicationDetails['resources'] = [] applicationDetails['resources'] = json.dumps( applicationDetails.get('resources')) schema = ApplicationSchema() new_application = schema.load(applicationDetails, session=db.session) new_application.lastUpdated = ModelTools.get_utc_timestamp() db.session.add(new_application) db.session.commit() # Serialize and return the newly created application # in the response new_application.resources = json.loads(new_application.resources) schema = ExtendedApplicationSchema() data = schema.dump(new_application) app.logger.debug("application data:") app.logger.debug(pformat(data)) return data, 201
def update(id, application): """ This function updates an existing application in the application list :param id: id of the application to update in the application list :param application: application to update :return: updated application """ app.logger.debug("application: ") app.logger.debug(pformat(application)) # Does the application exist in applications? existing_application = Application.query.filter(Application.id == id).one_or_none() # Does application exist? if existing_application is not None: application['lastUpdated'] = ModelTools.get_utc_timestamp() Application.query.filter(Application.id == id).update(application) db.session.commit() # return the updated application in the response schema = ApplicationSchema() data = schema.dump(existing_application) return data, 200 # otherwise, nope, application doesn't exist, so that's an error else: abort(404, f"Application not found")
def read_one(id): """ This function responds to a request for /api/application/{id} with one matching application from applications :param application: id of the application to find :return: application matching the id """ application = (Application.query.filter(Application.id == id).one_or_none()) app.logger.debug("application data:") app.logger.debug(pformat(application)) if application is not None: # Serialize the data for the response application_schema = ApplicationSchema() data = application_schema.dump(application) app.logger.debug("application data:") app.logger.debug(pformat(data)) return data else: abort( 404, "Application with id {id} not found".format(id=id) )
def read_all(status=None, activatorId=None, environment=None, page=None, page_size=None, sort=None): """ This function responds to a request for /api/applications with the complete lists of applications :return: json string of list of applications """ # Create the list of applications from our data # pre-process sort instructions if (sort==None): application_query = Application.query.order_by(Application.id) else: try: sort_inst = [ si.split(":") for si in sort ] orderby_arr = [] for si in sort_inst: si1 = si[0] if len(si) > 1: si2 = si[1] else: si2 = "asc" orderby = "Application.{0}.{1}()".format(si1.strip(), si2.strip()) orderby_arr.append(eval(orderby)) #print("orderby: {}".format(orderby_arr)) application_query = Application.query.order_by(*orderby_arr) except Exception as e: print(e) application_query = Application.query.order_by(Application.id) application_query = application_query.filter( (status == None or Application.status == status), (activatorId == None or Application.activatorId == activatorId), (environment == None or Application.env == environment)) if (page==None or page_size==None): applications = application_query.all() else: applications = application_query.limit(page_size).offset(page * page_size).all() # Serialize the data for the response application_schema = ApplicationSchema(many=True) data = application_schema.dump(applications) app.logger.debug("application data:") app.logger.debug(pformat(data)) return data
def read_all(status=None, activatorId=None, environment=None): """ This function responds to a request for /api/applications with the complete lists of applications :return: json string of list of applications """ # Create the list of applications from our data applications = Application.query \ .filter(status == None or Application.status == status) \ .filter(activatorId == None or Application.activatorId == activatorId ) \ .filter(environment == None or Application.env == environment) \ .all() # Serialize the data for the response application_schema = ApplicationSchema(many=True) data = application_schema.dump(applications) app.logger.debug("application data:") app.logger.debug(pformat(data)) return data
def create(application): """ This function creates a new application in the application structure based on the passed in application data :param application: application to create in application list :return: 201 on success, 406 on application exists """ pprint(application) schema = ApplicationSchema() new_application = schema.load(application, session=db.session) new_application.lastUpdated = ModelTools.get_utc_timestamp() db.session.add(new_application) db.session.commit() # Serialize and return the newly created application # in the response data = schema.dump(new_application) app.logger.debug("application data:") app.logger.debug(pformat(data)) return data, 201