def test_de_register_single_imeis(app, session): # pylint: disable=unused-argument """Verify that the de_register_imeis function works correctly.""" response = Utilities.de_register_imeis(IMEIS[0]) assert response is False
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 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()