def update_catalog(catalog_id, catalog_data): validations = validate_update_catalog(catalog_data) if validations: raise BadRequest(validations) catalog_data = catalog_data['catalog'] row = C.objects.get(uid=catalog_id) collected = getcollecteddata(row) try: C.objects(uid=catalog_id).update( name=catalog_data['name'] if 'name' in catalog_data else collected['name'], description=catalog_data['description'] if 'description' in catalog_data else collected['description'], ic_subset__append=catalog_data['ic_subset'] if 'ic_subset' in catalog_data else [], owner=catalog_data['owner'] if 'owner' in catalog_data else collected['owner'], status=catalog_data['status'] if 'status' in catalog_data else collected['status'], updated=datetime.datetime.now(), tags__append=catalog_data['tags'] if 'tags' in catalog_data else [], version=catalog_data['version'] if 'version' in catalog_data else collected['version'], visibility=catalog_data['visibility'] if 'visibility' in catalog_data else collected['visibility']) response = jsonify({"message": "Updated successfully", "code": 200}) return response except Exception as e: response = jsonify({"message": "hey there", "code": 500}) response.status_code = 500 return response
def validate_update_catalog(catalog_json): validation_errors = [] if 'catalog' not in catalog_json: raise BadRequest('CATALOG_ATTRS_MISSING') catalog_data = catalog_json['catalog'] if 'name' in catalog_data and str(catalog_data['name']) is '': validation_errors.append('CATALOG_NAME_MANDATORY') if 'ic_subset' in catalog_data and not catalog_data['ic_subset']: validation_errors.append('CATALOG_IC_SUBSET_MANDATORY') if 'visibility' in catalog_data and str(catalog_data['visibility']) is '': validation_errors.append('CATALOG_VISIBILITY_UNSPECIFIED') if 'owner' in catalog_data and str(catalog_data['owner']) is '': validation_errors.append('CATALOG_OWNER_UNSPECIFIED') return validation_errors
def put(self, catalog_id): """Edit a single catalog""" try: if not request.data: raise BadRequest("CATALOG_ATTRS_MISSING") response = update_catalog(catalog_id, request.get_json()) return response except BadRequest as be: response = jsonify(be.response) response.status_code = 400 return response except Exception as e: response = jsonify({"message": "hey there exception", "code": 500}) response.status_code = 500 return response
def validate_create_catalog(catalog_json): validation_errors = [] if 'catalog' not in catalog_json: raise BadRequest('CATALOG_ATTRS_MISSING') catalog_data = catalog_json['catalog'] if 'name' not in catalog_data or str(catalog_data['name']) is '': validation_errors.append('CATALOG_NAME_MANDATORY') if 'description' not in catalog_data or str( catalog_data['description']) is '': validation_errors.append('CATALOG_DESCRIPTION_MANDATORY') if 'ic_subset' not in catalog_data or not catalog_data['ic_subset']: validation_errors.append('CATALOG_IC_SUBSET_MANDATORY') if 'visibility' not in catalog_data or str( catalog_data['visibility']) is '': validation_errors.append('CATALOG_VISIBILITY_MANDATORY') return validation_errors
def post(self): """Create a new catalog""" try: if not request.data: raise BadRequest("CATALOG_ATTRS_MISSING") create_catalog(request.get_json()) response = jsonify({"message": "Successfully Inserted", "code": 200}) return response except BadRequest as be: response = jsonify(be.response) response.status_code = 400 return response except Exception as e: response = jsonify({"message": "hey there", "code":500}) response.status_code = 500 return response
def delete(self, catalog_id): """Delete a single catalog""" try: if not request.data: raise BadRequest("CATALOG_ATTRS_MISSING") delete_catalog(catalog_id) response = jsonify({ "message": "Successfully deleted", "code": 200 }) return response except BadRequest as be: response = jsonify(be.response) response.status_code = 400 return response except Exception as e: response = jsonify({ "message": "catalog_id not found", "code": 500 }) response.status_code = 500 return response
def create_catalog(catalog_data): validations = validate_create_catalog(catalog_data) if validations: raise BadRequest(validations) print(uuid.uuid4()) catalog_data_json = catalog_data['catalog'] print(catalog_data_json['ic_subset']) row = C.create( uid=uuid.uuid4(), created=datetime.datetime.now(), description=catalog_data_json['description'] if 'description' in catalog_data else '', ic_subset=catalog_data_json['ic_subset'], name=catalog_data_json['name'], owner=catalog_data_json['owner'], status=catalog_data_json['status'] if 'status' in catalog_data else '', tags=catalog_data_json['tags'] if 'tags' in catalog_data else [], updated=datetime.datetime.now(), version=catalog_data_json['version'] if 'version' in catalog_data else '', visibility=catalog_data_json['visibility']) return row