def sync_bulk_create(cls, reg_details, reg_device_id, app, ussd=None):
        """Create devices in bulk."""
        try:
            flatten_imeis = []

            if ussd is None:
                imeis_lists = ast.literal_eval(reg_details.imeis)
            else:
                imeis_lists = []
                ims = reg_details.imeis
                imeis_lists.append(list(ims.strip('}{').split(",")))

            for device_imeis in imeis_lists:

                device = cls(device_imeis[0][:8], reg_details.id,
                             reg_device_id)
                device.save()

                for imei in device_imeis:
                    flatten_imeis.append(imei)
                    imei_device = ImeiDevice(imei, device.id)
                    db.session.add(imei_device)
                    imei_object = ApprovedImeis.query.filter_by(
                        imei=imei[:14]).first()
                    if not imei_object:
                        approved_imei = ApprovedImeis(imei[:14],
                                                      reg_details.id,
                                                      'pending', 'add')
                        db.session.add(approved_imei)
                    elif imei_object and imei_object.status == 'removed':
                        imei_object.status = 'pending'
                        imei_object.removed = False
                        imei_object.request_id = reg_details.id
                        db.session.add(imei_object)
            reg_details.update_processing_status('Processed')
            reg_details.update_report_status('Processing')
            db.session.commit()

            task_id = Utilities.generate_summary(flatten_imeis,
                                                 reg_details.tracking_id)

            if task_id:
                Utilities.pool_summary_request(task_id, reg_details, app)
            else:
                reg_details.update_report_status('Failed')
                db.session.commit()
                exit()

            if app.config['AUTOMATE_IMEI_CHECK'] or ussd:
                if Device.auto_approve(task_id, reg_details, flatten_imeis,
                                       app):
                    app.logger.info(
                        "Auto Approved/Rejected Registration Application Id:" +
                        str(reg_details.id))

        except Exception as e:  # pragma: no cover
            reg_details.update_processing_status('Failed')
            reg_details.update_report_status('Failed')
            app.logger.exception(e)
            db.session.commit()
Esempio n. 2
0
    def sync_bulk_create(cls, reg_details, reg_device_id, app):
        """Create devices in bulk."""
        try:
            flatten_imeis = []
            imeis_lists = ast.literal_eval(reg_details.imeis)
            for device_imeis in imeis_lists:
                device = cls(device_imeis[0][:8], reg_details.id, reg_device_id)
                device.save()
                for imei in device_imeis:
                    flatten_imeis.append(imei)
                    imei_device = ImeiDevice(imei, device.id)
                    db.session.add(imei_device)
                    imei_object = ApprovedImeis.query.filter_by(imei=imei[:14]).first()
                    if not imei_object:
                        approved_imei = ApprovedImeis(imei[:14], reg_details.id, 'pending', 'add')
                        db.session.add(approved_imei)
                    elif imei_object and imei_object.status == 'removed':
                        imei_object.status = 'pending'
                        imei_object.removed = False
                        imei_object.request_id = reg_details.id
                        db.session.add(imei_object)
            reg_details.update_processing_status('Processed')
            reg_details.update_report_status('Processing')
            db.session.commit()

            task_id = Utilities.generate_summary(flatten_imeis, reg_details.tracking_id)
            if task_id:
                Utilities.pool_summary_request(task_id, reg_details, app)
            else:
                reg_details.update_report_status('Failed')
                db.session.commit()
        except Exception:
            reg_details.update_processing_status('Failed')
            db.session.commit()
Esempio n. 3
0
def create_dummy_devices(data,
                         request_type,
                         request,
                         db=None,
                         file_path=None,
                         file_content=None):
    """Helper method to create a dummy request with devices and assign it to a dummy reviewer.
    based on request_type.
    """
    if request_type == 'Registration':
        data.update({'reg_details_id': request.id})
        device = RegDevice.create(data)
        device.technologies = DeviceTechnology.create(device.id,
                                                      data.get('technologies'))
        Device.create(RegDetails.get_by_id(request.id), device.id)
    else:  # De-registration
        # creating sample file
        if not os.path.exists(os.path.dirname(file_path)):
            os.makedirs(os.path.dirname(file_path))
        with open(file_path, 'w') as f:
            for content in file_content:
                f.write(content)
                f.write('\n')

        data = DeRegDevice.curate_args(data, request)
        imei_tac_map = Utilities.extract_imeis_tac_map(data, request)
        created = DeRegDevice.bulk_create(data, request)
        device_id_tac_map = Utilities.get_id_tac_map(created)
        for device in device_id_tac_map:
            device_imeis = imei_tac_map.get(device.get('tac'))
            dereg_imei_list = DeRegImei.get_deregimei_list(
                device.get('id'), device_imeis)
            db.session.execute(DeRegImei.__table__.insert(), dereg_imei_list)
    return request
Esempio n. 4
0
    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()
Esempio n. 5
0
def test_remove_file(app, session):  # pylint: disable=unused-argument
    """Verify that the remove function works correctly."""
    tracking_id = uuid.uuid4()
    complete_path = os.path.join(app.config['DRS_UPLOADS'],
                                 '{0}'.format(tracking_id))
    Utilities.create_directory(tracking_id)
    assert os.path.isdir(complete_path)
    response = Utilities.remove_file('file.txt', tracking_id)
    assert response == 'file is missing'
 def trash_document(cls, type_id, reg_details):
     """Remove and purge a document from the system."""
     try:
         document = cls.query.filter_by(document_id=type_id,
                                        dereg_id=reg_details.id).one()
         cls.delete(document.id)
         Utilities.remove_file(document.filename, reg_details.tracking_id)
     except Exception:
         raise Exception
 def trash_document(cls, type_id, reg_details):
     """Purge documents of a request."""
     try:
         document = cls.query.filter_by(
             document_id=type_id, reg_details_id=reg_details.id).one()
         cls.delete(document.id)
         Utilities.remove_file(document.filename, reg_details.tracking_id)
     except Exception:
         raise Exception
    def post():
        """POST method handler, creates registration documents."""
        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:
            schema = RegistrationDocumentsSchema()
            time = datetime.now().strftime('%Y%m%d%H%M%S')
            args = request.form.to_dict()
            args = Utilities.update_args_with_file(request.files, args)
            reg_details = RegDetails.get_by_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"))

            tracking_id = reg_details.tracking_id
            created = RegDocuments.bulk_create(request.files, reg_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"))
            reg_details.update_status('Pending Review')
            message = schema.dump(created, many=True).data
            db.session.commit()
            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()
Esempio n. 9
0
 def async_bulk_create(cls, reg_details, reg_device_id,
                       app):  # pragma: no cover
     """Create devices async."""
     with app.app_context():
         from app import db
         try:
             filename = reg_details.file
             tracking_id = reg_details.tracking_id
             args = {
                 'imei_per_device': reg_details.imei_per_device,
                 'device_count': reg_details.device_count
             }
             response = Utilities.process_reg_file(filename, tracking_id,
                                                   args)
             imeis = []
             for device_imeis in response:
                 tac = Utilities.get_imei_tac(device_imeis[0])
                 device = cls(tac, reg_details.id, reg_device_id)
                 db.session.add(device)
                 db.session.flush()
                 imeis = imeis + device_imeis
                 for imei in device_imeis:
                     imei_device = ImeiDevice(imei, device.id)
                     db.session.add(imei_device)
                     imei_object = ApprovedImeis.query.filter_by(
                         imei=imei[:14]).first()
                     if not imei_object:
                         approved_imei = ApprovedImeis(
                             imei[:14], reg_details.id, 'pending', 'add')
                         db.session.add(approved_imei)
                     elif imei_object and imei_object.status == 'removed':
                         imei_object.status = 'pending'
                         imei_object.removed = False
                         imei_object.request_id = reg_details.id
                         db.session.add(imei_object)
             db.session.commit()
             reg_details.update_processing_status('Processed')
             db.session.commit()
             task_id = Utilities.generate_summary(imeis,
                                                  reg_details.tracking_id)
             app.logger.info(
                 'task with task_id: {0} initiated'.format(task_id))
             if not task_id:
                 reg_details.update_report_status('Failed')
                 app.logger.info(
                     'task with task_id: {0} failed'.format(task_id))
                 db.session.commit()
             else:
                 Utilities.pool_summary_request(task_id, reg_details, app)
         except Exception as e:
             app.logger.exception(e)
             db.session.rollback()
             reg_details.update_report_status('Failed')
             reg_details.update_processing_status('Failed')
             db.session.commit()
             raise e
Esempio n. 10
0
def test_pool_summary_request(app, session, dirbs_dvs):  # pylint: disable=unused-argument
    """Verify that the poll_summary function works correctly."""

    tracking_id = uuid.uuid4()
    request = create_registration(REG_REQ_DATA, tracking_id)
    Utilities.create_directory(tracking_id)
    assert request.tracking_id == tracking_id
    response = Utilities.pool_summary_request('mock-task-id', request, app)
    assert request.tracking_id == str(tracking_id)
    assert response is None
Esempio n. 11
0
def test_generate_summary(app, session, dirbs_dvs):  # pylint: disable=unused-argument
    """Verify that the generate function works correctly."""

    tracking_id = uuid.uuid4()
    request = create_registration(REG_REQ_DATA, tracking_id)
    Utilities.create_directory(tracking_id)
    assert request.tracking_id == tracking_id
    response = Utilities.generate_summary(IMEIS, tracking_id)
    # Async process
    assert not response
    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()
Esempio n. 14
0
def test_split_imeis(app):  # pylint: disable=unused-argument
    """Verify that the split imeis function works correctly."""

    imeis = "12345678976543, 34567897654321"
    response = Utilities.split_imeis(imeis)
    assert response
    assert len(response) == 2
Esempio n. 15
0
    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'))
Esempio n. 16
0
def test_convert_to_mbs(app):  # pylint: disable=unused-argument
    """Verify that the convert to mbs function works correctly."""

    filesize = 1024 * 1024

    response = Utilities.convert_to_mb(filesize)
    assert response
    assert response == 1
Esempio n. 17
0
def test_single_get_devices_description(app, session, dirbs_core):  # pylint: disable=unused-argument
    """Verify that the get_device_description function works correctly."""

    response = Utilities.get_devices_description([tacs[0]])
    assert response
    assert 'gsma' in response
    assert response['gsma']['allocation_date'] == 'string'
    assert response['tac'] == 'string'
Esempio n. 18
0
def test_extract_imeis(app, session):  # pylint: disable=unused-argument
    """Verify that the extract imeis function works correctly."""

    response = Utilities.extract_imeis(IMEI_TAC_MAP)
    assert len(response) == 4
    assert '12345678765432' in response
    assert '12345678986434' in response
    assert '23434343450987' in response
    assert '23434343565443' in response
Esempio n. 19
0
    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()
Esempio n. 20
0
    def post():
        """POST method handler, creates registration requests."""
        tracking_id = uuid.uuid4()
        try:
            args = RegDetails.curate_args(request)
            schema = RegistrationDetailsSchema()
            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"))
            if file:
                file_name = file.filename.split("/")[-1]
                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_reg_file(file_name, tracking_id,
                                                      args)
                if isinstance(response, list):
                    response = RegDetails.create(args, tracking_id)
                else:
                    return Response(
                        app.json_encoder.encode(response),
                        status=CODES.get("UNPROCESSABLE_ENTITY"),
                        mimetype=MIME_TYPES.get("APPLICATION_JSON"))
            else:
                Utilities.create_directory(tracking_id)
                response = RegDetails.create(args, tracking_id)
            db.session.commit()
            response = schema.dump(response, many=False).data
            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)
            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()
Esempio n. 21
0
 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()
Esempio n. 22
0
def test_split_chunks(app):  # pylint: disable=unused-argument
    """Verify that the split chunks function works correctly."""

    response = Utilities.split_chunks(IMEIS, 2)
    assert response
Esempio n. 23
0
    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)
Esempio n. 24
0
def test_index_exist_idx(app, db):  # pylint: disable=unused-argument
    """Verify that the index exits function works correctly."""

    response = Utilities.exist_idx('test-index')
    assert response is False
Esempio n. 25
0
    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()
Esempio n. 26
0
    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(app.json_encoder.encode(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(app.json_encoder.encode(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')
                message = 'The request {id} has been updated.'.format(
                    id=dereg_details.id)
                notification = Notification(dereg_details.reviewer_id,
                                            dereg_details.id,
                                            'de_registration_request',
                                            dereg_details.status, message)
                notification.add()
            else:
                dereg_details.update_status('Pending Review')
            response = schema.dump(updated, many=True).data

            log = EsLog.new_doc_serialize(response,
                                          request_type="De-Registration",
                                          regdetails=dereg_details,
                                          reg_status="Pending Review",
                                          method='Put',
                                          request='De-Registration')
            db.session.commit()
            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':
                _('request document updation failed, please try again later.')
            }

            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()
Esempio n. 28
0
    def put():
        """PUT method handler, updates registration requests."""
        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"))

        args = RegDetails.curate_args(request)
        schema = RegistrationDetailsUpdateSchema()
        file = request.files.get('file')
        reg_details = RegDetails.get_by_id(reg_id)
        try:
            tracking_id = reg_details.tracking_id
            if reg_details:
                args.update({
                    'status': reg_details.status,
                    'reg_id': reg_details.id,
                    'processing_status': reg_details.processing_status,
                    'report_status': reg_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 = RegDetails.close(reg_details)
                if isinstance(response, dict):
                    return Response(
                        app.json_encoder.encode(response),
                        status=CODES.get("UNPROCESSABLE_ENTITY"),
                        mimetype=MIME_TYPES.get("APPLICATION_JSON"))
                else:
                    response = schema.dump(response, many=False).data
                    return Response(
                        json.dumps(response),
                        status=CODES.get("OK"),
                        mimetype=MIME_TYPES.get("APPLICATION_JSON"))
            if file:
                Utilities.remove_file(reg_details.file, tracking_id)
                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_reg_file(file.filename,
                                                      tracking_id, args)
                if isinstance(response, list):
                    response = RegDetails.update(args, reg_details, True)
                else:
                    return Response(
                        app.json_encoder.encode(response),
                        status=CODES.get("UNPROCESSABLE_ENTITY"),
                        mimetype=MIME_TYPES.get("APPLICATION_JSON"))
            else:
                response = RegDetails.update(args, reg_details, False)
            db.session.commit()
            response = schema.dump(response, many=False).data
            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 update 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()
Esempio n. 29
0
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, flatten_imeis, app):
        from app.api.v1.resources.reviewer import SubmitReview
        from app.api.v1.models.devicequota import DeviceQuota as DeviceQuotaModel
        from app.api.v1.models.eslog import EsLog
        from app.api.v1.models.status import Status
        import json
        sr = SubmitReview()
        try:
            result = Utilities.check_request_status(task_id)
            duplicate_imeis = RegDetails.get_duplicate_imeis(reg_details)
            res = RegDetails.get_imeis_count(reg_details.user_id)
            sections_comment = "Auto"
            section_status = 6
            auto_approved_sections = [
                'device_quota', 'device_description', 'imei_classification',
                'imei_registration'
            ]

            if result:
                flatten_imeis = Utilities.bulk_normalize(flatten_imeis)

                if result['non_compliant'] != 0 or result['stolen'] != 0 or result['compliant_active'] != 0 \
                        or result['provisional_non_compliant'] != 0 or result['provisional_compliant'] != 0:
                    sections_comment = sections_comment + ' Rejected, Device/Devices found in Non-Compliant States'
                    status = 'Rejected'
                    section_status = 7
                    message = 'Your request {id} has been rejected'.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 duplicate_imeis:
                    res.update({
                        'duplicated':
                        len(RegDetails.get_duplicate_imeis(reg_details))
                    })
                    Utilities.generate_imeis_file(duplicate_imeis,
                                                  reg_details.tracking_id,
                                                  'duplicated_imeis')
                    reg_details.duplicate_imeis_file = '{upload_dir}/{tracking_id}/{file}'.format(
                        upload_dir=app.config['DRS_UPLOADS'],
                        tracking_id=reg_details.tracking_id,
                        file='duplicated_imeis.txt')
                    sections_comment = "Auto"
                    status = 'Rejected'
                    sections_comment = sections_comment + ' Rejected, Duplicate IMEIS Found, Please check duplicate file'
                    section_status = 7
                    message = 'Your request {id} has been rejected because duplicate imeis found!'.format(
                        id=reg_details.id)

                if status == 'Approved':
                    # checkout device quota
                    imeis = RegDetails.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)
                    sr._SubmitReview__update_to_approved_imeis(flatten_imeis)
                else:
                    sr._SubmitReview__change_rejected_imeis_status(
                        flatten_imeis)

                for section in auto_approved_sections:
                    RegDetails.add_comment(section, sections_comment,
                                           reg_details.user_id,
                                           'Auto Reviewed', section_status,
                                           reg_details.id)

                reg_details.summary = json.dumps({'summary': result})
                reg_details.report = result.get('compliant_report_name')
                reg_details.update_report_status('Processed')
                reg_details.report_allowed = True
                reg_details.update_status(status)

                sr._SubmitReview__generate_notification(
                    user_id=reg_details.user_id,
                    request_id=reg_details.id,
                    request_type='registration',
                    request_status=section_status,
                    message=message)

                reg_details.save()
                db.session.commit()

                # create log
                log = EsLog.auto_review(reg_details, "Registration Request",
                                        'Post', status)
                EsLog.insert_log(log)

        except Exception as e:  # pragma: no cover
            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()
            app.logger.exception(e)
            # create log
            log = EsLog.auto_review(reg_details, "Registration Request",
                                    'Post',
                                    Status.get_status_type(reg_details.status))
            EsLog.insert_log(log)

        return True