def post(reg_id): """POST method handler, restarts processing of a request.""" 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: reg_details = RegDetails.get_by_id(reg_id) # day_passed = (datetime.now() - reg_details.updated_at) > timedelta(1) failed_status_id = Status.get_status_id('Failed') processing_failed = reg_details.processing_status in [failed_status_id] report_failed = reg_details.report_status in [failed_status_id] # report_timeout = reg_details.report_status == Status.get_status_id('Processing') and day_passed processing_required = processing_failed or report_failed if processing_required: # pragma: no cover reg_device = RegDevice.get_device_by_registration_id(reg_details.id) Device.create(reg_details, reg_device.id) response = {'message': 'Request performed successfully.'} else: response = app.json_encoder.encode({'message': _('This request cannot be processed')}) 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': _('failed to restart process') } return Response(app.json_encoder.encode(data), status=CODES.get('INTERNAL_SERVER_ERROR'), mimetype=MIME_TYPES.get('APPLICATION_JSON'))
def get(reg_id): """GET method handler, returns device details.""" if 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")) schema = DeviceDetailsSchema() try: reg_device = RegDevice.get_device_by_registration_id(reg_id) response = schema.dump(reg_device).data if reg_device else {} return Response(json.dumps(response), status=CODES.get("OK"), mimetype=MIME_TYPES.get("APPLICATION_JSON")) except Exception as e: # pragma: no cover app.logger.exception(e) error = { 'message': [_('Failed to retrieve response, please try later')] } return Response(app.json_encoder.encode(error), status=CODES.get('INTERNAL_SERVER_ERROR'), mimetype=MIME_TYPES.get('APPLICATION_JSON')) finally: db.session.close()
def put(): """PUT method handler, updates a device.""" 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: args = request.form.to_dict() schema = DeviceDetailsUpdateSchema() reg_details = RegDetails.get_by_id(reg_id) reg_device = RegDevice.get_device_by_registration_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")) # day_passed = (datetime.now() - reg_details.updated_at) > timedelta(1) processing_failed = reg_details.processing_status in [ Status.get_status_id('Failed'), Status.get_status_id('New Request') ] report_failed = reg_details.report_status == Status.get_status_id( 'Failed') # report_timeout = reg_details.report_status == Status.get_status_id('Processing') and day_passed processing_required = processing_failed or report_failed reg_device = RegDevice.update(reg_device, args) response = schema.dump(reg_device, many=False).data response['reg_details_id'] = reg_details.id if processing_required: Device.create(reg_details, reg_device.id) 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 device addition failed')} return Response(app.json_encoder.encode(data), status=CODES.get('INTERNAL_SERVER_ERROR'), mimetype=MIME_TYPES.get('APPLICATION_JSON')) finally: db.session.close()
def get(reg_id): """GET method handler, return registration sections.""" 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: reg_details = RegDetails.get_by_id(reg_id) reg_schema = RegistrationDetailsSchema() doc_schema = RegistrationDocumentsSchema() device_schema = DeviceDetailsSchema() reg_device = RegDevice.get_device_by_registration_id(reg_id) reg_documents = RegDocuments.get_by_reg_id(reg_id) registration_data = reg_schema.dump(reg_details).data device_data = device_schema.dump( reg_device).data if reg_device else {} document_data = doc_schema.dump(reg_documents, many=True).data response = { 'reg_details': registration_data, 'reg_device': device_data, 'reg_docs': document_data } return Response(json.dumps(response), status=CODES.get("OK"), mimetype=MIME_TYPES.get("APPLICATION_JSON")) except Exception as e: # pragma: no cover app.logger.exception(e) data = { 'message': [ _('Registration request failed, check upload path or database connection' ) ] } return Response(app.json_encoder.encode(data), status=CODES.get('INTERNAL_SERVER_ERROR'), mimetype=MIME_TYPES.get('APPLICATION_JSON')) finally: db.session.close()