Example #1
0
def imei_batch_api(**kwargs):
    """
    IMEI API POST method handler for IMEI-Batch request.

    :param kwargs: input imei list
    :return: json
    """
    if bool(kwargs):
        imeis = kwargs.get('imeis')
        data = []
        with get_db_connection() as db_conn, db_conn.cursor() as cursor:
            for imei in imeis:
                imei_norm = validate_imei(imei)
                condition_results = get_conditions(cursor, imei_norm)

                response = {
                    'imei_norm':
                    imei_norm,
                    'block_date':
                    block_date(cursor, imei_norm),
                    'classification_state': {
                        'blocking_conditions': [
                            dict({
                                'condition_name': key,
                                'condition_met': value['result']
                            }) for key, value in condition_results.items()
                            if value['blocking']
                        ],
                        'informative_conditions': [
                            dict({
                                'condition_name': key,
                                'condition_met': value['result']
                            }) for key, value in condition_results.items()
                            if not value['blocking']
                        ]
                    },
                    'realtime_checks': {
                        'ever_observed_on_network':
                        ever_observed_on_network(cursor, imei_norm),
                        'invalid_imei':
                        False if re.match(r'^\d{14}$', imei_norm) else True,
                        'is_paired':
                        is_paired(cursor, imei_norm),
                        'is_exempted_device':
                        is_exempted_device(cursor, imei_norm)
                    },
                    'registration_status':
                    registration_list_status(cursor, imei_norm),
                    'stolen_status':
                    stolen_list_status(cursor, imei_norm)
                }

                data.append(IMEI().dump(response).data)
            return jsonify({'results': data})
    abort(400, 'Bad Input format (args cannot be empty)')
Example #2
0
def imei_api(imei: str, include_seen_with: bool = False, include_paired_with: bool = False) -> jsonify:
    """
    IMEI API handler.

    Arguments:
        imei: IMEI number in format [15, 16] digits
        include_seen_with: bool to include seen with information in response (default False)
        include_paired_with: bool to include paired with information in response (default False)
    Returns:
        JSON response
    """
    imei_norm = validate_imei(imei)

    tac = imei_norm[:8]
    with get_db_connection() as db_conn, db_conn.cursor() as cursor:
        cursor.execute('SELECT NOT EXISTS (SELECT * FROM gsma_data WHERE tac = %s) AS not_in_gsma', [tac])
        rt_gsma_not_found = cursor.fetchone()[0]

        condition_results = get_conditions(cursor, imei_norm)

        resp = {
            'imei_norm': imei_norm,
            'classification_state': {
                'blocking_conditions': {k: v['result'] for k, v in condition_results.items() if v['blocking']},
                'informative_conditions': {k: v['result'] for k, v in condition_results.items() if not v['blocking']}
            },
            'realtime_checks': {
                'invalid_imei': False if re.match(r'^\d{14}$', imei_norm) else True,
                'gsma_not_found': rt_gsma_not_found
            }
        }

        # add a real-time check for the registration list
        resp['realtime_checks']['in_registration_list'] = is_in_registration_list(db_conn, cursor, imei_norm)

        # add a real-time check for if IMEI was ever observed on the network
        resp['realtime_checks']['ever_observed_on_network'] = ever_observed_on_network(cursor, imei_norm)
        resp['is_paired'] = is_paired(cursor, imei_norm)
        if include_seen_with:
            resp['seen_with'] = get_subscribers(cursor, imei_norm)
        if include_paired_with:
            cursor.execute("""SELECT imsi
                                FROM pairing_list
                               WHERE imei_norm = %(imei_norm)s
                                 AND virt_imei_shard = calc_virt_imei_shard(%(imei_norm)s)""",
                           {'imei_norm': imei_norm})
            resp['paired_with'] = [x.imsi for x in cursor]

        return jsonify(IMEI().dump(resp).data)
Example #3
0
def imei_api(imei):
    """
    IMEI API handler.

    :param imei: IMEI
    :return: json
    """
    imei_norm = validate_imei(imei)

    with get_db_connection() as db_conn, db_conn.cursor() as cursor:
        condition_results = get_conditions(cursor, imei_norm)

        response = {
            'imei_norm': imei_norm,
            'block_date': block_date(cursor, imei_norm),
            'classification_state': {
                'blocking_conditions': [
                    dict({
                        'condition_name': key,
                        'condition_met': value['result']
                    }) for key, value in condition_results.items()
                    if value['blocking']
                ],
                'informative_conditions': [
                    dict({
                        'condition_name': key,
                        'condition_met': value['result']
                    }) for key, value in condition_results.items()
                    if not value['blocking']
                ]
            },
            'realtime_checks': {
                'ever_observed_on_network':
                ever_observed_on_network(cursor, imei_norm),
                'invalid_imei':
                False if re.match(r'^\d{14}$', imei_norm) else True,
                'is_paired':
                is_paired(cursor, imei_norm),
                'is_exempted_device':
                is_exempted_device(cursor, imei_norm)
            },
            'registration_status': registration_list_status(cursor, imei_norm),
            'stolen_status': stolen_list_status(cursor, imei_norm)
        }

        return jsonify(IMEI().dump(response).data)