コード例 #1
0
def sngl_imsi(mno, msisdn, imsi):
    """ Function that provide method to upload single IMSI against provided MSISDN """
    try:

        rtn_msg = ""
        cases = ""
        chk_mno, chk_msisdn = False, False

        if msisdn['CC'] == conf['CC']:
            if msisdn['SN'].isdigit() and len(msisdn['SN']) < 13:
                f_msisdn = msisdn['CC'] + msisdn['SN']
                chk_msisdn = True

        pattern_imsi = re.compile(r'\d{15}')
        match_imsi = pattern_imsi.fullmatch(imsi)

        if chk_msisdn and match_imsi:

            for key, val in conf.items():
                if mno == val:
                    chk_mno = True

            if chk_mno:
                chk_imsi = Pairing.query.filter(
                    Pairing.imsi == '{}'.format(imsi)).first()

                if chk_imsi:
                    data = {"Error": "IMSI already exists"}
                    return data, 422
                else:
                    cases = sngl_imsi_update(mno, f_msisdn, imsi)
            else:
                data = {"Error": "Improper Operator-Name provided"}
                return data, 422
            data = {"msg": cases}
            if cases == 'IMSI added successfully':
                return data, 200
            else:
                return data, 422

        elif not chk_msisdn:
            rtn_msg = {"Error": "MSISDN format is not correct"}
            return rtn_msg, 422

        elif not match_imsi:
            rtn_msg = {"Error": "IMSI format is not correct"}
            return rtn_msg, 422

    except Exception as e:
        db.session.rollback()

    finally:
        db.session.close()
コード例 #2
0
def bulk_msisdns(mno):
    """ Function that leads to a method that provides MSISDNs for MNOs to provide IMSIs"""

    chk_mno = False

    for key, val in conf.items():  # checking valid MNOs
        if mno == val:
            chk_mno = True

    if chk_mno:
        c_path = mno_all_data(mno)

    else:
        rtn_msg = "wrong mno"
        return rtn_msg

    return c_path
コード例 #3
0
def fetch_msisdns(mno, st, lt):
    """ Function leads to method that fetches MSISDNs for MNOs """

    try:
        cases = []
        chk_mno = False

        pattern_start_limit = re.compile(r'\d+')

        match_start = pattern_start_limit.fullmatch(st)
        match_limit = pattern_start_limit.fullmatch(lt)

        if match_start and match_limit:

            start = int(st)
            limit = int(lt)

            for key, val in conf.items():
                if mno == val:
                    chk_mno = True

            if chk_mno:
                cases = mno_records(mno, start, limit)
            else:
                data = {
                        "Error": "improper Operator's name provided"
                       }
                return data, 422

            return cases

        else:
            data = {
                     "Error": "Start or limit is not correct"
                   }
            return data, 422

    except Exception as e:
        db.session.rollback()

    finally:
        db.session.close()
コード例 #4
0
    def bulk_imsis():

        try:
            rtn_msg = ""
            chk_mno = False
            mno = request.form.get("mno")

            for key, val in conf.items():   # checking for correct operator's name
                if mno == val:
                    chk_mno = True

            if not chk_mno:
                data = {
                    "Error": "improper Operator-name provided"
                }
                return data, 422

            else:

                file = request.files.get('file')

                if file and file_allowed(file.filename):

                    filename = secure_filename(file.filename)
                    file_path = os.path.join(UPLOAD_FOLDER, filename)
                    file.save(os.path.join(UPLOAD_FOLDER, filename))
                    f = magic.Magic(mime=True)
                    file_type = f.from_file(file_path)
                    if file_type != 'text/plain':
                        data = {
                            "Error": "File type is not valid"
                        }
                        if os.path.isfile(file_path):
                            os.remove(file_path)
                        return data, 422

                    # ---------------------------------------------------------------------------------------------------

                    pat1 = re.compile(r'923\d{9}')
                    pat2 = re.compile(r'\d{15}')
                    try:
                        newfile = open(file_path, 'r')
                        df = pd.read_csv(newfile, usecols=range(2), dtype={"MSISDN": str, "IMSI": str})
                        newfile.close()
                    except Exception as e:
                        if e:
                            newfile.close()
                            data = {
                                "Error": "File content is not Correct"
                            }
                            if os.path.isfile(file_path):
                                os.remove(file_path)
                            return data, 422

                    total_rows, total_columns = df.shape

                    if df.columns[0] != 'MSISDN' or df.columns[1] != 'IMSI':
                        data = {
                            "Error": "File headers are missing or incorrect"
                        }
                        if os.path.isfile(file_path):
                            os.remove(file_path)
                        return data, 422

                    df_dup = df[df.duplicated(['IMSI'], keep='first')]  # To detect duplicated IMSIs in uploaded File

                    if not df_dup.empty:
                        data = {
                            "Error": "File contains duplicated IMSIs"
                        }
                        if os.path.isfile(file_path):
                            os.remove(file_path)
                        return data, 422

                    else:
                        df2 = df[df.isnull().any(axis=1)]
                        df1 = df.dropna()
                        df3 = df1[(df1.IMSI.astype(str).str.len() > 15)]
                        df1 = df1[~(df1.IMSI.astype(str).str.len() > 15)]
                        df4 = df1[(df1.MSISDN.astype(str).str.len() > 12)]
                        df1 = df1[~(df1.MSISDN.astype(str).str.len() > 12)]
                        df5 = df1[~(df1.IMSI.astype(str).str.match(pat2))]
                        df1 = df1[(df1.IMSI.astype(str).str.match(pat2))]
                        df6 = df1[~(df1.MSISDN.astype(str).str.match(pat1))]
                        df1 = df1[(df1.MSISDN.astype(str).str.match(pat1))]

                        final_rows, final_columns = df1.shape
                        del_rec = (total_rows - final_rows)
                        df1.to_csv(file_path, index=False)

                        lst_df = [df2, df3, df4, df5, df6]
                        dfs = pd.concat(lst_df, ignore_index=False)

                        # ---------------------------------------------------------------------------------------------------

                        con = connect()
                        cur = con.cursor()
                        filename1 = os.path.join(UPLOAD_FOLDER, filename)

                        cur.execute(""" CREATE TABLE if not exists test_mno (t_msisdn text, t_imsi text); """)

                        f = open(filename1)
                        cur.copy_from(f, 'test_mno', sep=",")

                        cur.execute(""" select msisdn, imsi, t_msisdn, t_imsi, change_type, export_status, old_imsi 
                                        from pairing 
                                        inner join test_mno 
                                        on (pairing.msisdn = test_mno.t_msisdn) and (pairing.end_date is null)
                                        and (pairing.add_pair_status = true) 
                                        and (pairing.operator_name = '{}'); """.format(mno))

                        cur.execute(""" update pairing set imsi = test_mno.t_imsi, change_type = 'ADD', export_status = false, 
                                        updated_at = date_trunc('second', NOW()) 
                                        from test_mno 
                                        where pairing.msisdn = test_mno.t_msisdn 
                                        and pairing.end_date is null and pairing.add_pair_status = true 
                                        and pairing.operator_name = '{}'; """.format(mno))

                        cur.execute(""" drop table if exists test_mno; """)

                        con.commit()

                        con.close()
                        f.close()

                        if del_rec:
                            error_file = "Error-Records_" + mno + '_' + strftime("%Y-%m-%d_%H-%M-%S") + '.csv'
                            download_path = os.path.join(DOWNLOAD_FOLDER, error_file)
                            file.save(download_path)
                            dfs.to_csv(download_path, index=False)
                        else:
                            download_path = "No error file available"

                        rtn_msg = {
                            "msg": "File successfully loaded",
                            "Total_Records": total_rows,
                            "Successful_Records": final_rows,
                            "Deleted_Record": del_rec,
                            "link": download_path
                        }
                        return rtn_msg, 200

                else:

                    rtn_msg = {
                        "Error": "No file or improper file found"
                    }

                    return rtn_msg, 422

        except Exception as e:
            db.session.rollback()

        finally:
            db.session.close()