Example #1
0
 def create_indexes(self):
     """ method to create indexes on the database """
     with db.engine.connect() as conn:
         with conn.execution_options(isolation_level='AUTOCOMMIT'):
             try:
                 Pairing.create_index(conn)
                 Pairing_Codes.create_index(conn)
                 Imei.create_index(conn)
                 Devices.create_index(conn)
                 Owner.create_index(conn)
             except Exception as e:
                 sys.exit(1)
Example #2
0
def add_pair_db_insertion(session, db, t_sec_id, t_primary_id, t_sec_msisdn, t_imei_id):
    sec_add = Pairing(id=t_sec_id,  # adding secondary pair incase one or more secondary pairs already exists
                      primary_id=t_primary_id,
                      msisdn=t_sec_msisdn,
                      is_primary=False,
                      creation_date=strftime("%Y-%m-%d %H:%M:%S"),
                      add_pair_status=False,
                      imei_id=t_imei_id)
    session.add(sec_add)
    db.session.commit()
Example #3
0
def first_pair_db_insertion(session, db, t_pair_id, t_msisdn, t_mno, t_imei_id):

    primary_add = Pairing(id=t_pair_id,
                          primary_id=0,
                          msisdn=t_msisdn,
                          is_primary=True,
                          creation_date=strftime("%Y-%m-%d %H:%M:%S"),
                          operator_name=t_mno,
                          add_pair_status=True,
                          imei_id=t_imei_id)
    session.add(primary_add)
    db.session.commit()
def test_pairing_indexes(db, session):
    """Verify that the Pairing model works correctly."""

    with db.engine.connect() as conn:
        Pairing.create_index(conn)

    qry = session.execute(
        text(""" select * from pg_stat_user_indexes; """)).fetchall()
    for p in qry:
        if p.indexrelname == 'devices_msisdn_index':
            device_index = True
        elif p.indexrelname == 'devices_imsi_index':
            device_index = True
        elif p.indexrelname == 'devices_creation_date_index':
            device_index = True
        elif p.indexrelname == 'devices_end_date_index':
            device_index = True
        elif p.indexrelname == 'devices_operator_index':
            device_index = True
        print(p.indexrelname)
    # noinspection PyUnboundLocalVariable
    assert device_index
def add_pair(add_msisdn, sender_num):
    """ Function to create additional/secondary pairs for a particular device """
    try:
        rtn_msg = ""
        chk_1, chk_2, chk_3, cnfm_sms = False, False, False, False

        pattern_msisdn = re.compile(r'923\d{9,12}')
        match_primary = pattern_msisdn.fullmatch(sender_num)
        match_secondary = pattern_msisdn.fullmatch(add_msisdn)

        if match_primary and match_secondary:

            chk_primary = Pairing.query.filter(
                db.and_(Pairing.msisdn == '{}'.format(sender_num),
                        Pairing.is_primary == True, Pairing.end_date == None,
                        Pairing.msisdn != add_msisdn)).all()

            if chk_primary:

                chk_1 = True
                max_pair_id = db.session.query(func.max(
                    Pairing.id)).scalar()  # query to get maximum Pairing_id

                if max_pair_id is None:
                    max_pair_id = 1

                else:
                    max_pair_id = max_pair_id + 1

                for q in chk_primary:

                    chk_sec = Pairing.query.filter(Pairing.primary_id == '{}'.format(q.id))\
                                           .filter(Pairing.end_date == None).all()

                    if chk_sec:
                        for r in chk_sec:

                            if r.msisdn != add_msisdn:  # checking if MSISDN is already paired or not
                                chk_2 = True

                                if len(chk_sec) < conf[
                                        'pair_limit']:  # checking if pair-limit is exceeded or not
                                    chk_3 = True

                                else:
                                    chk_3 = False
                                    return conf['sms_pair_limit_breached']

                            else:
                                chk_2 = False
                                return "MSISDN ({})already paired with the device".format(
                                    add_msisdn)

                        if chk_1 and chk_2 and chk_3:

                            adding1 = Pairing(
                                id=max_pair_id,
                                primary_id=q.id,
                                msisdn=add_msisdn,
                                is_primary=False,
                                creation_date=strftime("%Y-%m-%d %H:%M:%S"),
                                add_pair_status=False,
                                imei_id=q.imei_id)
                            # adding secondary pair incase one or more secondary pairs already exists

                            db.session.add(adding1)
                            db.session.commit()

                            max_pair_id += 1

                            cnfm_sms = True

                            rtn_msg = "Secondary pair is added by ({}). Confirmation is awaited from ({})".\
                                format(sender_num, add_msisdn)

                    else:
                        adding2 = Pairing(
                            id=
                            max_pair_id,  # adding secondary pair for first time
                            primary_id=q.id,
                            msisdn=add_msisdn,
                            is_primary=False,
                            creation_date=strftime("%Y-%m-%d %H:%M:%S"),
                            add_pair_status=False,
                            imei_id=q.imei_id)

                        db.session.add(adding2)
                        db.session.commit()

                        max_pair_id += 1

                        cnfm_sms = True

                        rtn_msg = "Secondary pair is added by ({}). Confirmation is awaited from ({})".\
                            format(sender_num, add_msisdn)

            else:

                rtn_msg = "Request not made by Primary-Pair or number-to-be-added is Primary number"

                chk_1 = False

            if cnfm_sms:

                chg_msisdn = '0' + add_msisdn[2:]

                message = "CONFIRM [{}]\nPlease reply with Yes/No space {}".format(
                    sender_num, sender_num)

                payload = {
                    'username': '******',
                    'password': '******',
                    'smsc': 'at',
                    'from': '7787',
                    'to': chg_msisdn,
                    'text': message
                }

                r = requests.get(conf['kannel_sms'], params=payload)
                cnfm_sms = False

            return rtn_msg

        elif not match_primary:
            return "Primary MSISDN format is not correct"

        elif not match_secondary:
            return "Secondary MSISDN format is not correct"

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

    finally:
        db.session.close()
def first_pair(sms_paircode, sender_num, mno):
    """ Function to create the first/primary pair for the mobile devices """

    try:
        rtn_msg = ""
        max_pair_count = 0

        pattern_sender_no = re.compile(r'923\d{9,12}')
        match_sender_no = pattern_sender_no.fullmatch(sender_num)

        pattern_paircode = re.compile(r'[a-zA-Z0-9]{8}')
        match_paircode = pattern_paircode.fullmatch(sms_paircode)

        pattern_mno = re.compile(r'[a-zA-Z0-9]{1,20}')
        match_mno = pattern_mno.fullmatch(mno)

        if match_sender_no and match_paircode and match_mno:
            chk_pc = Pairing_Codes.query.filter(Pairing_Codes.pair_code == '{}'.format(sms_paircode),
                                                Pairing_Codes.is_active == True).first()

            if chk_pc:

                chk_primary = Pairing.query.filter(Pairing.msisdn == '{}'.format(sender_num))\
                                           .filter(Pairing.is_primary == True)\
                                           .filter(Pairing.end_date == None).first()

                if not chk_primary:

                    max_pair_count = db.session.query(func.max(Pairing.id)).scalar()  # query to get maximum Pairing_id

                    if max_pair_count is None:
                        max_pair_count = 1

                    else:
                        max_pair_count += 1

                    chk_imei = Imei.query.filter(Imei.device_id == '{}'.format(chk_pc.device_id)).all()

                    for q in chk_imei:

                        first_add = Pairing(id=max_pair_count,   # Inserting Primary-Pair in pairing table
                                            primary_id=0,
                                            msisdn=sender_num,
                                            is_primary=True,
                                            creation_date=strftime("%Y-%m-%d %H:%M:%S"),
                                            operator_name=mno,
                                            add_pair_status=True,
                                            imei_id=q.id)

                        db.session.add(first_add)

                        max_pair_count += 1

                        db.session.commit()

                    chk_pc.is_active = False           # de-activating pair-code in PairCodes Table

                    db.session.commit()

                    return "PairCode ({}) is valid and your pair will be " \
                           "added in next 24 to 48 hours".format(sms_paircode), 200

                else:
                    return "MSISDN already exists as Primary-Pair", 422

            else:
                return "Pair Code ({}) is not Valid".format(sms_paircode), 422

        elif not match_sender_no:
            return "Sender MSISDN format is not correct", 422

        elif not match_paircode:
            return "Pair-Code format is not correct", 422

        elif not match_mno:
            return "MNO's name is not in correct format", 422

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

    finally:
        db.session.close()