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()
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()