def post(): """POST method handler, creates documents.""" dereg_id = request.form.to_dict().get('dereg_id', None) if not dereg_id or not dereg_id.isdigit() or not DeRegDetails.exists( dereg_id): return Response(app.json_encoder.encode(DEREG_NOT_FOUND_MSG), status=CODES.get("UNPROCESSABLE_ENTITY"), mimetype=MIME_TYPES.get("APPLICATION_JSON")) try: schema = DeRegDocumentsSchema() time = datetime.now().strftime('%Y%m%d%H%M%S') args = request.form.to_dict() args = Utilities.update_args_with_file(request.files, args) dereg_details = DeRegDetails.get_by_id(dereg_id) if dereg_details: args.update({ 'dereg_details': dereg_details.id, 'status': dereg_details.status }) else: args.update({'dereg_details': ''}) validation_errors = schema.validate(args) if validation_errors: return Response(app.json_encoder.encode(validation_errors), status=CODES.get("UNPROCESSABLE_ENTITY"), mimetype=MIME_TYPES.get("APPLICATION_JSON")) tracking_id = dereg_details.tracking_id created = DeRegDocuments.bulk_create(request.files, dereg_details, time) response = Utilities.store_files(request.files, tracking_id, time) if response: return Response(app.json_encoder.encode(response), status=CODES.get("UNPROCESSABLE_ENTITY"), mimetype=MIME_TYPES.get("APPLICATION_JSON")) dereg_details.update_status('Pending Review') message = schema.dump(created, many=True).data log = EsLog.new_doc_serialize(message, request_type="De-Registration", regdetails=dereg_details, reg_status="Pending Review", method='Post', request='De-Registration') db.session.commit() EsLog.insert_log(log) return Response(json.dumps(message), status=CODES.get("OK"), mimetype=MIME_TYPES.get("APPLICATION_JSON")) except Exception as e: # pragma: no cover db.session.rollback() app.logger.exception(e) data = { 'message': _('request document addition failed, check for valid formats.') } return Response(app.json_encoder.encode(data), status=CODES.get('INTERNAL_SERVER_ERROR'), mimetype=MIME_TYPES.get('APPLICATION_JSON')) finally: db.session.close()
def put(): """PUT method handler, updates documents.""" reg_id = request.form.to_dict().get('reg_id', None) if not reg_id or not reg_id.isdigit() or not RegDetails.exists(reg_id): return Response(app.json_encoder.encode(REG_NOT_FOUND_MSG), status=CODES.get("UNPROCESSABLE_ENTITY"), mimetype=MIME_TYPES.get("APPLICATION_JSON")) try: schema = RegistrationDocumentsUpdateSchema() time = datetime.now().strftime('%Y%m%d%H%M%S') args = request.form.to_dict() args = Utilities.update_args_with_file(request.files, args) reg_details = RegDetails.get_by_id(reg_id) if reg_details: args.update({ 'reg_details_id': reg_details.id, 'status': reg_details.status }) else: args.update({'reg_details_id': ''}) validation_errors = schema.validate(args) if validation_errors: return Response(app.json_encoder.encode(validation_errors), status=CODES.get("UNPROCESSABLE_ENTITY"), mimetype=MIME_TYPES.get("APPLICATION_JSON")) tracking_id = reg_details.tracking_id updated = RegDocuments.bulk_update(request.files, reg_details, time) response = Utilities.store_files(request.files, tracking_id, time) if response: return Response(json.dumps(response), status=CODES.get("UNPROCESSABLE_ENTITY"), mimetype=MIME_TYPES.get("APPLICATION_JSON")) if reg_details.status == Status.get_status_id( 'Information Requested'): reg_details.update_status('In Review') message = 'The request {id} has been updated.'.format( id=reg_details.id) notification = Notification(reg_details.reviewer_id, reg_details.id, 'registration_request', reg_details.status, message) notification.add() else: reg_details.update_status('Pending Review') response = schema.dump(updated, many=True).data message = schema.dump(updated, many=True).data log = EsLog.new_doc_serialize( message, request_type="Update Registration Documents", regdetails=reg_details, reg_status="Pending Review", method='Put', request='Registration') db.session.commit() EsLog.insert_log(log) return Response(json.dumps(response), status=CODES.get("OK"), mimetype=MIME_TYPES.get("APPLICATION_JSON")) except Exception as e: # pragma: no cover db.session.rollback() app.logger.exception(e) data = { 'message': _('request document updation failed, please try again later.') } return Response(app.json_encoder.encode(data), status=CODES.get('INTERNAL_SERVER_ERROR'), mimetype=MIME_TYPES.get('APPLICATION_JSON')) finally: db.session.close()