def validate_data(self, pin): res_log.debug('Check PIN length') if len(pin) > 4 or len(pin) < 4: raise ResellerException('PIN invalid length') res_log.debug('Check if Reseller exists') # check if reseller exists in the database and the PIN is valid try: cur = db_conn.cursor() cur.execute( 'SELECT msisdn,pin FROM resellers WHERE msisdn=%(msisdn)s', {'msisdn': str(self.reseller_msisdn)}) if cur.rowcount > 0: res_log.debug('Valid Reseller found') res_log.debug('Auth PIN') data = cur.fetchone() if data[1] != pin: raise ResellerException('Invalid PIN!') res_log.debug('Check if subscriber is valid') # check if subscriber exists try: sub = Subscriber() sub.get(self.subscriber_msisdn) except SubscriberException as e: raise ResellerException('Invalid subscriber') else: raise ResellerException('Invalid Reseller') except psycopg2.DatabaseError as e: raise ResellerException( 'Database error getting reseller msisdn: %s' % e)
def add(self, msisdn, pin, balance): # check if subscriber exists try: sub = Subscriber() sub.get(msisdn) except SubscriberException as e: raise ResellerException('Invalid subscriber: %s' % e) # provision the reseller try: cur = db_conn.cursor() cur.execute( 'INSERT INTO resellers(msisdn,pin,balance) VALUES(%(msisdn)s,%(pin)s,%(balance)s)', { 'msisdn': msisdn, 'pin': pin, 'balance': Decimal(str(balance)) }) db_conn.commit() except psycopg2.DatabaseError as e: raise ResellerException('PG_HLR error provisioning reseller: %s' % e)
def add(self, msisdn, credit): sub = Subscriber() sms = SMS() try: mysub = sub.get(msisdn) current_balance = sub.get_balance(msisdn) except SubscriberException as e: raise CreditException(e) new_balance = Decimal(str(credit)) + Decimal(str(current_balance)) # update subscriber balance try: cur = db_conn.cursor() cur.execute( 'UPDATE subscribers SET balance=%(new_balance)s WHERE msisdn=%(msisdn)s', { 'new_balance': Decimal(str(new_balance)), 'msisdn': msisdn }) db_conn.commit() sms.send(config['smsc'], msisdn, sms_credit_added % (credit, new_balance)) except psycopg2.DatabaseError as e: db_conn.rollback() raise CreditException( 'PG_HLR error updating subscriber balance: %s' % e) # insert transaction into the credit history try: cur = db_conn.cursor() cur.execute( 'INSERT INTO credit_history(msisdn,previous_balance,current_balance,amount) VALUES(%s,%s,%s,%s)', (msisdn, current_balance, new_balance, credit)) except psycopg2.DatabaseError as e: db_conn.rollback() raise CreditException( 'PG_HLR error inserting invoice in the history: %s' % e) finally: db_conn.commit()
def get(self, request, msisdn): api_log.info('%s - [GET] %s/%s', request.getHost().host, self.path, msisdn) try: sub = Subscriber() if msisdn == 'all_connected': data = json.dumps(sub.get_all_connected(), cls=PGEncoder) elif msisdn == 'all_sip': data = json.dumps(sub.get_sip_connected()) elif msisdn == 'unpaid_subscription': data = json.dumps(sub.get_unpaid_subscription(), cls=PGEncoder) elif msisdn == 'paid_subscription': data = json.dumps(sub.get_paid_subscription(), cls=PGEncoder) elif msisdn == 'unauthorized': data = json.dumps(sub.get_unauthorized(), cls=PGEncoder) elif msisdn == 'online': data = json.dumps(sub.get_online(), cls=PGEncoder) elif msisdn == 'offline': data = json.dumps(sub.get_offline(), cls=PGEncoder) elif msisdn == 'all_roaming': data = json.dumps(sub.get_roaming(), cls=PGEncoder) elif msisdn == 'all_foreign': if request.getClientIP().find("10.23") > -1: request.setHeader('Access-Control-Allow-Origin', '*') data = json.dumps(sub.get_all_foreign(), cls=PGEncoder) else: data = json.dumps(sub.get(msisdn), cls=PGEncoder) except SubscriberException as e: data = {'status': 'failed', 'error': str(e)} if msisdn != 'all_connected': api_log.info(data) else: api_log.debug(data) return data