Esempio n. 1
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)
    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