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(dereg_id=None): """GET method handler, returns a deregistration request based on request id. """ schema = DeRegDetailsSchema() try: if dereg_id: if dereg_id.isdigit() and DeRegDetails.exists(dereg_id): response = DeRegDetails.get_by_id(dereg_id) response = schema.dump(response).data else: response = app.json_encoder.encode(DEREG_NOT_FOUND_MSG) else: response = DeRegDetails.get_all() response = schema.dump(response, many=True).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) 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 index_dereg_details(self, conn): """Method to index the De-Registration Details table.""" try: DeRegDetails.create_index(conn) return "De-Registration Detail indexed successfully" except SQLAlchemyError as e: raise e
def put(): """PUT method handler, updates 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(json.dumps(DEREG_NOT_FOUND_MSG), status=CODES.get("UNPROCESSABLE_ENTITY"), mimetype=MIME_TYPES.get("APPLICATION_JSON")) try: schema = DeRegDocumentsUpdateSchema() 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(json.dumps(validation_errors), status=CODES.get("UNPROCESSABLE_ENTITY"), mimetype=MIME_TYPES.get("APPLICATION_JSON")) tracking_id = dereg_details.tracking_id updated = DeRegDocuments.bulk_update(request.files, dereg_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 dereg_details.status == Status.get_status_id( 'Information Requested'): dereg_details.update_status('In Review') else: dereg_details.update_status('Pending Review') response = schema.dump(updated, many=True).data db.session.commit() return Response(json.dumps(response), status=CODES.get("OK"), mimetype=MIME_TYPES.get("APPLICATION_JSON")) except Exception as e: db.session.rollback() app.logger.exception(e) data = { 'message': 'request document updation failed, please try again later.' } return Response(json.dumps(data), status=CODES.get('INTERNAL_SERVER_ERROR'), mimetype=MIME_TYPES.get('APPLICATION_JSON')) finally: db.session.close()
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(): """POST method handler, Create/Submit a new De-Registration details. """ tracking_id = uuid.uuid4() try: schema = DeRegDetailsSchema() args = DeRegDetails.curate_args(request) file = request.files.get('file') 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")) response = Utilities.store_file(file, tracking_id) if response: return Response(json.dumps(response), status=CODES.get("UNPROCESSABLE_ENTITY"), mimetype=MIME_TYPES.get("APPLICATION_JSON")) response = Utilities.process_de_reg_file( file.filename.split("/")[-1], tracking_id, args) errored = 'device_count' in response or 'invalid_imeis' in response or \ 'duplicate_imeis' in response or 'invalid_format' in response if not errored: gsma_response = Utilities.get_device_details_by_tac(response) response = DeRegDetails.create(args, tracking_id) db.session.commit() response = schema.dump(response, many=False).data response = {'request': response, 'devices': gsma_response} return Response(json.dumps(response), status=CODES.get("OK"), mimetype=MIME_TYPES.get("APPLICATION_JSON")) else: return Response(app.json_encoder.encode(response), status=CODES.get("UNPROCESSABLE_ENTITY"), mimetype=MIME_TYPES.get("APPLICATION_JSON")) except Exception as e: # pragma: no cover db.session.rollback() Utilities.remove_directory(tracking_id) 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()
def post(): """POST method handler, creates new devices for request.""" 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_request = DeRegRequestSchema() device_schema = DeRegDeviceSchema() dereg = DeRegDetails.get_by_id(dereg_id) args = request.form.to_dict() args = DeRegDevice.curate_args(args, dereg) validation_errors = schema_request.validate(args) if validation_errors: return Response(app.json_encoder.encode(validation_errors), status=CODES.get("UNPROCESSABLE_ENTITY"), mimetype=MIME_TYPES.get("APPLICATION_JSON")) imei_tac_map = Utilities.extract_imeis_tac_map(args, dereg) imeis_list = Utilities.extract_imeis(imei_tac_map) not_registered_imeis = Utilities.get_not_registered_imeis( imeis_list) if not_registered_imeis: error = {'not_registered_imeis': not_registered_imeis} return Response(json.dumps(error), status=CODES.get("UNPROCESSABLE_ENTITY"), mimetype=MIME_TYPES.get("APPLICATION_JSON")) else: old_devices = list(map(lambda x: x.id, dereg.devices)) created = DeRegDevice.bulk_create(args, dereg) device_id_tac_map = Utilities.get_id_tac_map(created) devices = device_schema.dump(created, many=True) dereg.update_status('Awaiting Documents') db.session.commit() DeRegDevice.bulk_insert_imeis(device_id_tac_map, imei_tac_map, old_devices, imeis_list, dereg) response = {'devices': devices.data, 'dreg_id': dereg.id} 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 check_permissions(self, data): """Check if user have permission for this request.""" reg_details = DeRegDetails.get_by_id(data['dereg_id']) request_user = reg_details.user_id if 'user_id' in data and data['user_id'] != request_user: raise ValidationError('Permission denied for this request', field_names=['user_id'])
def get(dereg_id): """GET method handler, returns documents.""" no_error = dereg_id.isdigit() and DeRegDetails.exists(dereg_id) if not no_error: return Response(json.dumps(DEREG_NOT_FOUND_MSG), status=CODES.get("UNPROCESSABLE_ENTITY"), mimetype=MIME_TYPES.get("APPLICATION_JSON")) try: schema = DeRegDocumentsSchema() documents = DeRegDocuments.get_by_reg_id(dereg_id) documents = schema.dump(documents, many=True).data '''if doc_id: if not doc_id.isdigit(): documents = DOC_NOT_FOUND_MSG else: documents = list(filter(lambda doc: int(doc['id']) == int(doc_id), documents)) documents = documents[0] if documents else DOC_NOT_FOUND_MSG ''' return Response(json.dumps(documents), status=CODES.get("OK"), mimetype=MIME_TYPES.get("APPLICATION_JSON")) except Exception as e: app.logger.exception(e) data = {"message": "Error retrieving results. Please try later."} return Response(json.dumps(data), status=CODES.get("BAD_REQUEST"), mimetype=MIME_TYPES.get("APPLICATION_JSON")) finally: db.session.close()
def get(dereg_id): """GET method handler, returns documents.""" no_error = dereg_id.isdigit() and DeRegDetails.exists(dereg_id) if not no_error: return Response(app.json_encoder.encode(DEREG_NOT_FOUND_MSG), status=CODES.get("UNPROCESSABLE_ENTITY"), mimetype=MIME_TYPES.get("APPLICATION_JSON")) try: schema = DeRegDocumentsSchema() documents = DeRegDocuments.get_by_reg_id(dereg_id) documents = schema.dump(documents, many=True).data return Response(json.dumps(documents), status=CODES.get("OK"), mimetype=MIME_TYPES.get("APPLICATION_JSON")) except Exception as e: # pragma: no cover app.logger.exception(e) data = { "message": [_("Error retrieving results. Please try later.")] } return Response(app.json_encoder.encode(data), status=CODES.get("BAD_REQUEST"), mimetype=MIME_TYPES.get("APPLICATION_JSON")) finally: db.session.close()
def post(dereg_id): """POST method handler, restarts processing of a request.""" 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: dereg = DeRegDetails.get_by_id(dereg_id) # day_passed = (datetime.now() - dereg.updated_at) > timedelta(1) failed_status_id = Status.get_status_id('Failed') processing_failed = dereg.processing_status in [failed_status_id] report_failed = dereg.report_status in [failed_status_id] # report_timeout = dereg.report_status == Status.get_status_id('Processing') and day_passed processing_required = processing_failed or report_failed if processing_required: # pragma: no cover dereg_devices = DeRegDevice.get_devices_by_dereg_id(dereg.id) dereg_devices_data = DeRegDeviceSchema().dump(dereg_devices, many=True).data dereg_devices_ids = list( map(lambda x: x['id'], dereg_devices_data)) args = {'devices': dereg_devices_data} imei_tac_map = Utilities.extract_imeis_tac_map(args, dereg) imeis_list = Utilities.extract_imeis(imei_tac_map) created = DeRegDevice.bulk_create(args, dereg) device_id_tac_map = Utilities.get_id_tac_map(created) DeRegDevice.bulk_insert_imeis(device_id_tac_map, imei_tac_map, dereg_devices_ids, imeis_list, dereg) 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 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"))
def check_reg_id(self, data): """Validates request id.""" dereg_details_id = data['dereg_details'] dereg_details = DeRegDetails.get_by_id(dereg_details_id) if not dereg_details: raise ValidationError('The request id provided is invalid', field_names=['dereg_details_id']) elif 'user_id' in data and data['user_id'] != dereg_details.user_id: raise ValidationError('Permission denied for this request', field_names=['user_id'])
def test_review_sections_de_registration(flask_app, db): # pylint: disable=unused-argument """Verify that the api returns correct information of sections.""" # de-registration test reviewer_id = 'section-rev-1-0' reviewer_name = 'section rev0' dereg_data = { 'file': 'de-reg-test-file', 'device_count': 1, 'user_id': 'assign-rev-user-1', 'user_name': 'assign rev user 1', 'reason': 'because we have to run tests successfully' } request = create_assigned_dummy_request(dereg_data, 'De-Registration', reviewer_id, reviewer_name) assert request request_id = request.id reviewer_id = 'section-rev-2' reviewer_name = 'section rev 2' section = 'device_description' status = 7 comment = 'this is a test comment' DeRegDetails.add_comment(section, comment, reviewer_id, reviewer_name, status, request_id) rv = flask_app.get( '{0}?request_id={1}&request_type=de_registration_request'.format( SECTIONS_API, request_id)) assert rv.status_code == 200 data = json.loads(rv.data.decode('utf-8'))['sections'] for sect in data: if sect.get('comments'): assert sect.get('section_type') == section assert sect.get('section_status') == status sect_comment = sect.get('comments')[0] assert sect_comment.get('user_name') == reviewer_name assert sect_comment.get('user_id') == reviewer_id assert sect_comment.get('comment') == comment assert sect_comment.get('datetime') else: assert sect.get('section_type') in [ 'device_description', 'imei_classification', 'imei_registration', 'approval_documents' ] assert sect.get('comments') is None assert sect.get('section_status') is None
def get(dereg_id): """GET method handler, to return all section of a request.""" try: if 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")) dereg_details = DeRegDetails.get_by_id(dereg_id) dereg_schema = DeRegDetailsSchema() doc_schema = DeRegDocumentsSchema() device_schema = DeRegDeviceSchema() dereg_devices = DeRegDevice.get_devices_by_dereg_id(dereg_id) dereg_documents = DeRegDocuments.get_by_reg_id(dereg_id) deregistration_data = dereg_schema.dump(dereg_details).data device_data = device_schema.dump(dereg_devices, many=True).data document_data = doc_schema.dump(dereg_documents, many=True).data response = { 'dereg_details': deregistration_data, 'dereg_device': device_data, 'dereg_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': [ _('De-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()
def get_document_label(self, data): """Returns a document label.""" dereg_details = DeRegDetails.get_by_id(data.dereg_id) upload_dir_path = GLOBAL_CONF['upload_directory'] document = Documents.get_document_by_id(data.document_id) data.label = document.label data.required = Documents.required data.link = '{server_dir}/{local_dir}/{file_name}'.format( server_dir=upload_dir_path, local_dir=dereg_details.tracking_id, file_name=data.filename)
def check_reg_id(self, data): """Validate request id.""" dereg_details_id = data['dereg_details'] dereg_details = DeRegDetails.get_by_id(dereg_details_id) if not dereg_details: raise ValidationError('The request id provided is invalid', field_names=['dereg_id']) if 'user_id' in data and data['user_id'] != dereg_details.user_id: raise ValidationError('Permission denied for this request', field_names=['user_id']) else: status = Status.get_status_type(dereg_details.status) if status != 'Awaiting Documents': raise ValidationError(_('This step can only be performed for request with Awaiting Document status'), field_names=['status'])
def check_reg_id(self, data): """Validates ID of the request.""" dereg_details_id = data['dereg_id'] dereg_details = DeRegDetails.get_by_id(dereg_details_id) if not dereg_details: raise ValidationError('The request id provided is invalid', field_names=['dereg_id']) if 'user_id' in data and data['user_id'] != dereg_details.user_id: raise ValidationError('Permission denied for this request', field_names=['user_id']) else: status = Status.get_status_type(dereg_details.status) if status in self.update_restricted: raise ValidationError( 'Update is restricted for request in status {0}'.format( status), field_names=['status'])
def get(dereg_id): """GET method handler, returns device of a request.""" if 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 = DeRegDeviceSchema() dereg_devices = DeRegDevice.get_devices_by_dereg_id(dereg_id) response = schema.dump(dereg_devices, many=True).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) 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 async_create(cls, devices, imei_tac_map, old_devices, dereg_id, imeis_list, app): """Async create a new device.""" with app.app_context(): from app import db dereg = DeRegDetails.get_by_id(dereg_id) try: DeRegDevice.clear_devices(old_devices) for device in devices: device_imeis = imei_tac_map.get(device.get('tac')) dereg_imei_list = DeRegImei.get_deregimei_list( device.get('id'), device_imeis) res = db.engine.execute(DeRegImei.__table__.insert(), dereg_imei_list) res.close() dereg.update_processing_status('Processed') db.session.commit() task_id = Utilities.generate_summary(imeis_list, dereg.tracking_id) if task_id: Utilities.pool_summary_request(task_id, dereg, app) else: dereg.update_processing_status('Failed') db.session.commit() if app.config['AUTOMATE_IMEI_CHECK']: if DeRegDevice.auto_approve(task_id, dereg): print( "Auto Approved/Rejected DeRegistration Application Id:" + str(dereg.id)) except Exception as e: app.logger.exception(e) db.session.rollback() dereg.update_processing_status('Failed') dereg.update_report_status('Failed') db.session.commit()
def async_create(cls, devices, imei_tac_map, old_devices, dereg_id, imeis_list, app): """Async create a new device.""" with app.app_context(): from app import db dereg = DeRegDetails.get_by_id(dereg_id) try: DeRegDevice.clear_devices(old_devices) for device in devices: device_imeis = imei_tac_map.get(device.get('tac')) dereg_imei_list = DeRegImei.get_deregimei_list( device.get('id'), device_imeis) res = db.engine.execute(DeRegImei.__table__.insert(), dereg_imei_list) res.close() dereg.update_processing_status('Processed') db.session.commit() task_id = Utilities.generate_summary(imeis_list, dereg.tracking_id) Utilities.pool_summary_request(task_id, dereg, app) except Exception as e: app.logger.exception(e) dereg.update_processing_status('Failed') db.session.commit()
def auto_approve(task_id, reg_details): # TODO: Need to remove duplicated session which throws warning from app.api.v1.resources.reviewer import SubmitReview from app.api.v1.models.devicequota import DeviceQuota as DeviceQuotaModel from app.api.v1.models.status import Status from app.api.v1.models.eslog import EsLog import json sr = SubmitReview() try: result = Utilities.check_request_status(task_id) section_status = 6 sections_comment = "Auto" auto_approved_sections = [ 'device_quota', 'device_description', 'imei_classification', 'imei_registration' ] if result: if result['non_compliant'] != 0 or result['stolen'] != 0 or result['compliant_active'] != 0 \ or result['provisional_non_compliant'] != 0: sections_comment = sections_comment + ' Rejected, Device/s found in Non-Compliant State' status = 'Rejected' section_status = 7 message = 'Your request {id} has been rejected because Non-Compliant Device Found in it.'.format( id=reg_details.id) else: sections_comment = sections_comment + ' Approved' status = 'Approved' message = 'Your request {id} has been Approved'.format( id=reg_details.id) if status == 'Approved': # checkout device quota imeis = DeRegDetails.get_normalized_imeis(reg_details) user_quota = DeviceQuotaModel.get(reg_details.user_id) current_quota = user_quota.reg_quota user_quota.reg_quota = current_quota - len(imeis) DeviceQuotaModel.commit_quota_changes(user_quota) imeis = DeRegDetails.get_normalized_imeis(reg_details) Utilities.de_register_imeis(imeis) for section in auto_approved_sections: DeRegDetails.add_comment(section, sections_comment, reg_details.user_id, 'Auto Reviewed', section_status, reg_details.id) sr._SubmitReview__generate_notification( user_id=reg_details.user_id, request_id=reg_details.id, request_type='de-registration', request_status=section_status, message=message) reg_details.summary = json.dumps({'summary': result}) reg_details.report = result.get('compliant_report_name') reg_details.update_report_status('Processed') reg_details.update_status(status) reg_details.report_allowed = True reg_details.save() db.session.commit() # create log log = EsLog.auto_review(reg_details, "De-Registration Request", 'Post', status) EsLog.insert_log(log) return True else: reg_details.update_processing_status('Failed') reg_details.update_report_status('Failed') reg_details.update_status('Failed') db.session.commit() log = EsLog.auto_review( reg_details, "De-Registration Request", 'Post', Status.get_status_type(reg_details.status)) EsLog.insert_log(log) except Exception as e: # pragma: no cover app.logger.exception(e) db.session.rollback() reg_details.update_processing_status('Failed') reg_details.update_status('Failed') message = 'Your request {id} has failed please re-initiate device request'.format( id=reg_details.id) sr._SubmitReview__generate_notification( user_id=reg_details.user_id, request_id=reg_details.id, request_type='registration', request_status=7, message=message) db.session.commit() # create log log = EsLog.auto_review(reg_details, "De-Registration Request", 'Post', Status.get_status_type(reg_details.status)) EsLog.insert_log(log)
def create_de_registration(data, tracking_id): """ Helper method to create a registration request""" return DeRegDetails.create(data, tracking_id)
def put(self): """PUT method handler, updates existing registration request. """ try: # get the posted data args = RegDetails.curate_args(request) # create Marshmallow object schema = UssdDeleteSchema() # validate posted data validation_errors = schema.validate(args) if validation_errors: for key, value in validation_errors.items(): messages = { 'from': 'DRS-USSD', 'to': args['msisdn'], 'content': key + ':' + value[0] } self.messages_list.append(messages.copy()) jasmin_send_response = Jasmin.send_batch(self.messages_list, network=args['network']) print("Jasmin API response: " + str(jasmin_send_response.status_code)) return Response(app.json_encoder.encode(validation_errors), status=CODES.get("UNPROCESSABLE_ENTITY"), mimetype=MIME_TYPES.get("APPLICATION_JSON")) else: dereg_id = args['device_id'] if dereg_id and dereg_id.isdigit() and RegDetails.exists(dereg_id): # if record matches with the passed MSISDN device_info = UssdModel.get_by_id(dereg_id) if device_info.msisdn != args['msisdn']: messages = { 'from': 'DRS-USSD', 'to': args['msisdn'], 'content': "Your MSISDN does not match with the record." } self.messages_list.append(messages.copy()) jasmin_send_response = Jasmin.send_batch(self.messages_list, network=args['network']) print("Jasmin API response: " + str(jasmin_send_response.status_code)) data = {'message': [_("Your MSISDN does not match with the record.")]} return Response(app.json_encoder.encode(data), status=CODES.get("RECORD_MISMATCH"), mimetype=MIME_TYPES.get("APPLICATION_JSON")) else: dreg_details = RegDetails.get_by_id(dereg_id) else: messages = { 'from': 'DRS-USSD', 'to': args['msisdn'], 'content': 'Delete Request not found.' } self.messages_list.append(messages.copy()) jasmin_send_response = Jasmin.send_batch(self.messages_list, network=args['network']) print("Printing the jasmin send response") print(jasmin_send_response) if jasmin_send_response: print("Jasmin API response: " + str(jasmin_send_response.status_code)) else: print("Jasmin API response: " + str(jasmin_send_response)) return Response(app.json_encoder.encode({'message': [_('Delete Request not found.')]}), status=CODES.get("UNPROCESSABLE_ENTITY"), mimetype=MIME_TYPES.get("APPLICATION_JSON")) if dreg_details: normalized_imeis = Ussd_helper.get_normalized_imeis(dreg_details.imeis) Utilities.de_register_imeis(normalized_imeis) args.update({'status': dreg_details.status, 'processing_status': dreg_details.processing_status, 'report_status': dreg_details.report_status}) validation_errors = schema.validate(args) if validation_errors: for key, value in validation_errors.items(): messages = { 'from': 'DRS-USSD', 'to': args['msisdn'], 'content': key + ':' + value[0] } self.messages_list.append(messages.copy()) jasmin_send_response = Jasmin.send_batch(self.messages_list, network=args['network']) print("Jasmin API response: " + str(jasmin_send_response.status_code)) return Response(app.json_encoder.encode(validation_errors), status=CODES.get("UNPROCESSABLE_ENTITY"), mimetype=MIME_TYPES.get("APPLICATION_JSON")) if args.get('close_request', None) == 'True': response = DeRegDetails.close(dreg_details) if isinstance(response, dict): # let the user know about the deregistration messages = { 'from': 'DRS-USSD', 'to': args['msisdn'], 'content': str(response['message']) } self.messages_list.append(messages.copy()) jasmin_send_response = Jasmin.send_batch(self.messages_list, network=args['network']) print("Jasmin API response: " + str(jasmin_send_response.status_code)) print(str(response['message'])) return Response(app.json_encoder.encode(response), status=CODES.get("UNPROCESSABLE_ENTITY"), mimetype=MIME_TYPES.get("APPLICATION_JSON")) else: log = EsLog.new_request_serialize(dreg_details, 'USSD De-Registration', method='Put') EsLog.insert_log(log) response = schema.dump(response, many=False).data # let the user know about the deregistration messages = { 'from': 'DRS-USSD', 'to': args['msisdn'], 'content': str("Device with ID: " + str(dereg_id) + " has been DeRegistered") } self.messages_list.append(messages.copy()) jasmin_send_response = Jasmin.send_batch(self.messages_list, network=args['network']) print("Jasmin API response: " + str(jasmin_send_response.status_code)) response['response'] = messages['content'] return Response(json.dumps(response), status=CODES.get("OK"), mimetype=MIME_TYPES.get("APPLICATION_JSON")) response = DeRegDetails.update(args, dreg_details, file=False) response = schema.dump(response, many=False).data # let the user know about the deregistration messages = { 'from': 'DRS-USSD', 'to': args['msisdn'], 'content': str(response['message']) } jasmin_send_response = Jasmin.send_batch(self.messages_list, network=args['network']) self.messages_list.append(messages.copy()) print("Jasmin API response: " + str(jasmin_send_response.status_code)) 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': [_('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()
def put(): """PUT method handler, updates devices of the request.""" 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_request = DeRegRequestUpdateSchema() device_schema = DeRegDeviceSchema() dereg = DeRegDetails.get_by_id(dereg_id) args = request.form.to_dict() args = DeRegDevice.curate_args(args, dereg) validation_errors = schema_request.validate(args) if validation_errors: return Response(app.json_encoder.encode(validation_errors), status=CODES.get("UNPROCESSABLE_ENTITY"), mimetype=MIME_TYPES.get("APPLICATION_JSON")) imei_tac_map = Utilities.extract_imeis_tac_map(args, dereg) imeis_list = Utilities.extract_imeis(imei_tac_map) not_registered_imeis = Utilities.get_not_registered_imeis( imeis_list) if not_registered_imeis: error = {'not_registered_imeis': not_registered_imeis} return Response(json.dumps(error), status=CODES.get("UNPROCESSABLE_ENTITY"), mimetype=MIME_TYPES.get("APPLICATION_JSON")) else: # day_passed = (datetime.now() - dereg.updated_at) > timedelta(1) processing_failed = dereg.processing_status in [ Status.get_status_id('Failed'), Status.get_status_id('New Request') ] report_failed = dereg.report_status == Status.get_status_id( 'Failed') # report_timeout = dereg.report_status == Status.get_status_id('Processing') and day_passed processing_required = processing_failed or report_failed if processing_required: old_devices = list(map(lambda x: x.id, dereg.devices)) created = DeRegDevice.bulk_create(args, dereg) device_id_tac_map = Utilities.get_id_tac_map(created) devices = device_schema.dump(created, many=True) db.session.commit() DeRegDevice.bulk_insert_imeis(device_id_tac_map, imei_tac_map, old_devices, imeis_list, dereg) response = {'devices': devices.data, 'dreg_id': dereg.id} else: response = {'devices': [], 'dreg_id': dereg.id} 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 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 existing de registration request. """ dereg_id = request.form.to_dict().get('dereg_id', None) try: schema = DeRegDetailsUpdateSchema() if dereg_id and dereg_id.isdigit() and DeRegDetails.exists( dereg_id): dreg_details = DeRegDetails.get_by_id(dereg_id) else: return Response(app.json_encoder.encode(DEREG_NOT_FOUND_MSG), status=CODES.get("UNPROCESSABLE_ENTITY"), mimetype=MIME_TYPES.get("APPLICATION_JSON")) args = DeRegDetails.curate_args(request) file = request.files.get('file') tracking_id = dreg_details.tracking_id if dreg_details: args.update({ 'status': dreg_details.status, 'processing_status': dreg_details.processing_status, 'report_status': dreg_details.report_status }) 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")) if args.get('close_request', None) == 'True': response = DeRegDetails.close(dreg_details) if isinstance(response, dict): return Response( app.json_encoder.encode(response), status=CODES.get("UNPROCESSABLE_ENTITY"), mimetype=MIME_TYPES.get("APPLICATION_JSON")) else: log = EsLog.new_request_serialize(response, "Close De-Registration", method="Put") EsLog.insert_log(log) response = schema.dump(response, many=False).data return Response( json.dumps(response), status=CODES.get("OK"), mimetype=MIME_TYPES.get("APPLICATION_JSON")) if file: response = Utilities.store_file(file, tracking_id) imeis_list = Utilities.extract_imeis(response) if response: return Response( json.dumps(response), status=CODES.get("UNPROCESSABLE_ENTITY"), mimetype=MIME_TYPES.get("APPLICATION_JSON")) filename = file.filename elif dreg_details.status == Status.get_status_id('New Request'): filename = dreg_details.file args.update({'device_count': dreg_details.device_count}) else: filename = None if filename: response = Utilities.process_de_reg_file( filename, tracking_id, args) imeis_list = Utilities.extract_imeis(response) errored = 'device_count' in response or 'invalid_imeis' in response or \ 'duplicate_imeis' in response or 'invalid_format' in response if not errored: gsma_response = Utilities.get_device_details_by_tac( response) response = DeRegDetails.update(args, dreg_details, file=True) response = schema.dump(response, many=False).data log = EsLog.new_request_serialize(response, "DeRegistration", imeis=imeis_list, dereg=True, method="Put") EsLog.insert_log(log) response = {'request': response, 'devices': gsma_response} return Response( json.dumps(response), status=CODES.get("OK"), mimetype=MIME_TYPES.get("APPLICATION_JSON")) else: return Response( app.json_encoder.encode(response), status=CODES.get("UNPROCESSABLE_ENTITY"), mimetype=MIME_TYPES.get("APPLICATION_JSON")) else: response = DeRegDetails.update(args, dreg_details, file=False) response = schema.dump(response, many=False).data log = EsLog.new_request_serialize(response, "DeRegistration", dereg=True, method="Put") 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': [ _('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()