def get(self, **kwargs): """GET method handler for dashboard reports.""" for key, value in kwargs.items(): if value is None: res = {'error': ['{0} is required'.format(key)]} return Response(json.dumps(ErrorResponse().dump(res).data), status=422, mimetype='application/json') user_id = kwargs.get('user_id') user_type = kwargs.get('user_type') response_data = {"user_id": user_id, "user_type": user_type} if user_type == UserTypes.REVIEWER.value: registraton_info = RegDetails.get_dashboard_report( user_id, user_type) de_registraton_info = DeRegDetails.get_dashboard_report( user_id, user_type) response_data['registration'] = registraton_info response_data['de-registration'] = de_registraton_info elif user_type in [ UserTypes.IMPORTER.value, UserTypes.INDIVIDUAL.value ]: registraton_info = RegDetails.get_dashboard_report( user_id, user_type) response_data['registration'] = registraton_info elif user_type == UserTypes.EXPORTER.value: de_registraton_info = DeRegDetails.get_dashboard_report( user_id, user_type) response_data['de-registration'] = de_registraton_info response = Response(json.dumps(response_data), status=CODES.get("OK"), mimetype=MIME_TYPES.get("APPLICATION_JSON")) return response
def get(self, **kwargs): """GET method handler, return registration report.""" for key, value in kwargs.items(): if value is None: res = {'error': ['{0} is required'.format(key)]} return Response(json.dumps(ErrorResponse().dump(res).data), status=422, mimetype='application/json') user_type = kwargs.get('user_type') request_type = kwargs.get('request_type') request_id = kwargs.get('request_id') user_id = kwargs.get('user_id') if request_type == 'registration_request': if not request_id.isdigit() or not RegDetails.exists(request_id): return Response(app.json_encoder.encode(REG_NOT_FOUND_MSG), status=CODES.get("UNPROCESSABLE_ENTITY"), mimetype=MIME_TYPES.get("APPLICATION_JSON")) req = RegDetails.get_by_id(request_id) if not req.report: return Response(app.json_encoder.encode(REPORT_NOT_FOUND_MSG), status=CODES.get("UNPROCESSABLE_ENTITY"), mimetype=MIME_TYPES.get("APPLICATION_JSON")) report_allowed = True if user_type == 'reviewer' or \ (req.user_id == user_id and req.report_allowed) else False if report_allowed: if user_type == 'reviewer': report_name = req.report else: report_name = 'user_report-{}'.format(req.report) report = os.path.join(app.config['DRS_UPLOADS'], req.tracking_id, report_name) return send_file(report) else: return Response(app.json_encoder.encode(REPORT_NOT_ALLOWED_MSG), status=CODES.get("UNPROCESSABLE_ENTITY"), mimetype=MIME_TYPES.get("APPLICATION_JSON")) else: if not request_id.isdigit() or not DeRegDetails.exists(request_id): return Response(app.json_encoder.encode(DEREG_NOT_FOUND_MSG), status=CODES.get("UNPROCESSABLE_ENTITY"), mimetype=MIME_TYPES.get("APPLICATION_JSON")) req = DeRegDetails.get_by_id(request_id) if not req.report: return Response(app.json_encoder.encode(REPORT_NOT_FOUND_MSG), status=CODES.get("UNPROCESSABLE_ENTITY"), mimetype=MIME_TYPES.get("APPLICATION_JSON")) report_allowed = True if user_type == 'reviewer' or \ (req.user_id == user_id and req.report_allowed) else False if report_allowed: if user_type == 'reviewer': report_name = req.report else: report_name = 'user_report-{}'.format(req.report) report = os.path.join(app.config['DRS_UPLOADS'], req.tracking_id, report_name) return send_file(report) else: return Response(app.json_encoder.encode(REPORT_NOT_ALLOWED_MSG), status=CODES.get("UNPROCESSABLE_ENTITY"), mimetype=MIME_TYPES.get("APPLICATION_JSON"))
def post(self, **kwargs): """Post method handler, set report permissions.""" for key, value in kwargs.items(): if value is None: res = {'error': ['{0} is required'.format(key)]} return Response(json.dumps(ErrorResponse().dump(res).data), status=422, mimetype='application/json') user_type = kwargs.get('user_type') request_type = kwargs.get('request_type') request_id = kwargs.get('request_id') user_id = kwargs.get('user_id') if request_type == 'registration_request': if not request_id.isdigit() or not RegDetails.exists(request_id): return Response(app.json_encoder.encode(REG_NOT_FOUND_MSG), status=CODES.get("UNPROCESSABLE_ENTITY"), mimetype=MIME_TYPES.get("APPLICATION_JSON")) req = RegDetails.get_by_id(request_id) if not req.report: return Response(app.json_encoder.encode(REPORT_NOT_FOUND_MSG), status=CODES.get("UNPROCESSABLE_ENTITY"), mimetype=MIME_TYPES.get("APPLICATION_JSON")) if user_type == 'reviewer' and req.reviewer_id == user_id: response = RegDetails.toggle_permission(req) response = {'message': 'The permission to view report is changed', 'value': response} return Response(json.dumps(response), status=CODES.get("OK"), mimetype=MIME_TYPES.get("APPLICATION_JSON")) else: return Response(app.json_encoder.encode(REPORT_NOT_ALLOWED_MSG), status=CODES.get("UNPROCESSABLE_ENTITY"), mimetype=MIME_TYPES.get("APPLICATION_JSON")) else: if not request_id.isdigit() or not DeRegDetails.exists(request_id): return Response(app.json_encoder.encode(DEREG_NOT_FOUND_MSG), status=CODES.get("UNPROCESSABLE_ENTITY"), mimetype=MIME_TYPES.get("APPLICATION_JSON")) req = DeRegDetails.get_by_id(request_id) if not req.report: return Response(app.json_encoder.encode(REPORT_NOT_FOUND_MSG), status=CODES.get("UNPROCESSABLE_ENTITY"), mimetype=MIME_TYPES.get("APPLICATION_JSON")) if user_type == 'reviewer' and req.reviewer_id == user_id: response = DeRegDetails.toggle_permission(req) response = {'message': 'The permission to view report is changed', 'value': response} return Response(json.dumps(response), status=CODES.get("OK"), mimetype=MIME_TYPES.get("APPLICATION_JSON")) else: return Response(app.json_encoder.encode(REPORT_NOT_ALLOWED_MSG), status=CODES.get("UNPROCESSABLE_ENTITY"), mimetype=MIME_TYPES.get("APPLICATION_JSON"))