Example #1
0
    def read(self, request, account=None, phone=None, si=None, tariff=None):
        """
        Returns a blogpost, if `title` is given,
        otherwise all the posts.

        Parameters:
         - `phone_number`: The title of the post to retrieve.
        """
        user = request.user
        if user.has_perm("billing.api_view"):
            if phone is not None and si is not None and tariff is not None:
                key_caches_site = "site::{0}".format(si)
                key_caches_tariff = "tariff::{0}::phone::{1}".format(
                    tariff, phone)
                key_caches_phone_site = "phone::{0}::site::{1}".format(
                    phone, si)
                try:
                    resp = keyedcache.cache_get(key_caches_phone_site)
                except:
                    lcr_query = "SELECT l.id AS id, l.digits AS digits, cg.name AS gw, l.rate AS rate, cg.prefix AS gw_prefix, cg.suffix AS suffix, l.price AS price, l.price_currency AS currency, l.name AS name FROM lcr l LEFT JOIN carrier_gateway cg ON l.carrier_id_id=cg.id LEFT JOIN django_site s ON l.site_id=s.id WHERE cg.enabled = '1' AND l.enabled = '1' AND l.digits IN ({0}) AND CURTIME() BETWEEN l.time_start AND l.time_end AND (DAYOFWEEK(NOW()) = l.weeks OR l.weeks = 0) AND s.name='{1}' ORDER BY  digits DESC, reliability DESC, quality DESC;".format(
                        pars_phone(phone), si)
                    log.debug(lcr_query)
                    resp = Lcr.objects.raw(lcr_query)[0]
                    keyedcache.cache_set(key_caches_phone_site, value=resp)
                    #resp = Lcr.objects.phone_lcr(phone, si)
                try:
                    respt = keyedcache.cache_get(key_caches_tariff)
                except:
                    #respt = Tariff.objects.phone_tariff(phone, tariff)
                    query = "select * from tariff where tariff_plan_id=%i AND digits IN (%s) ORDER BY digits DESC, rand();" % (
                        int(tariff), pars_phone(phone))
                    respt = Tariff.objects.raw(query)[0]
                    keyedcache.cache_set(key_caches_tariff, value=respt)
                return {
                    "lcr_rate": resp.rate,
                    "suffix": resp.suffix,
                    "lcr_digits": resp.digits,
                    "lcr_carrier": resp.gw,
                    "lcr_price": resp.price,
                    "lcr_currency": resp.currency,
                    "lcr_name": resp.name,
                    "nibble_rate": respt.rate
                }
            else:
                return rc.NOT_HERE

        else:
            return rc.FORBIDDEN
Example #2
0
 def phone_tariff(self, phone, tariff, site=None):
     """
     Search rate from tariff
     """
     bl = self.model()
     #return self.filter(digits=phone, enabled=True, tariff_plan__site__name__exact=site)
     query = "select * from tariff where tariff_plan_id=%i AND digits IN (%s) ORDER BY digits DESC, rand();" % (
         int(tariff), pars_phone(phone))
     return bl.raw(query)[0]
Example #3
0
 def read(self, request, phone=None, gw=None):
     user = request.user
     log.debug('BillingInHandler: {0} {1} {2}'.format(user, phone, gw))
     if user.has_perm("billing.api_view"):
         if phone is not None and gw is not None:
             key_caches_gw = "gatewayw::{0}".format(gw)
             key_caches_endpoint = "endpoint::{0}".format(phone)
             dialplan_caches_gw = "dialplan::gatewayw::{0}::endpoint::{1}".format(gw, phone)
             dialplan_caches_endpoint = "dialplan::endpoint::{0}".format(phone)
             try:
                 gateway = keyedcache.cache_get(key_caches_gw)
             except:
                 gateway = SofiaGateway.objects.get(name__exact=gw, enabled=True)
                 keyedcache.cache_set(key_caches_gw, value=gateway)
                 log.error("Is not gateway: {0}".format(gw))
             #log.debug(gateway.id)
             #lcr_query = "SELECT l.id AS id, l.digits AS digits, l.rate AS rate, l.price AS price, l.price_currency AS currency, l.name AS name FROM lcr l WHERE l.carrier_id_id= '{1}' AND l.enabled = '1' AND l.digits IN ({0}) AND CURTIME() BETWEEN l.time_start AND l.time_end AND (DAYOFWEEK(NOW()) = l.weeks OR l.weeks = 0) ORDER BY  digits DESC, reliability DESC, quality DESC;".format(pars_phone(phone), gateway.id)
             lcr_query = "SELECT l.id AS id, l.digits AS digits, l.rate AS rate, l.price AS price, l.price_currency AS currency, l.name AS name FROM lcr l WHERE l.carrier_id_id= '{1}' AND l.enabled = '1' AND l.digits IN ({0}) ORDER BY  digits DESC, reliability DESC, quality DESC;".format(pars_phone(phone), gateway.id)
             #log.debug(lcr_query)
             #resp = Lcr.objects.raw(lcr_query)[0]
             try:
                 lcr = keyedcache.cache_get(dialplan_caches_gw)
                 lcr_rate = lcr.lcr_rate
                 lcr_price = lcr.lcr_price
                 lcr_currency = lcr.lcr_currency
             except:
                 try:
                     resp = Lcr.objects.raw(lcr_query)[0]
                     lcr_price = resp.price
                     lcr_rate = resp.rate
                     lcr_currency = resp.currency
                     keyedcache.cache_set(dialplan_caches_gw, value={"lcr_rate": lcr_rate, "lcr_price": lcr_price, "lcr_currency": lcr_currency})
                 except:
                     log.error("Is not lcr gateway: {0}".format(gw))
                     lcr_rate = lcr_price = "0.18"
                     lcr_currency = "UAH"
             #endpoint = Endpoint.objects.get(uid__exact=phone, enable=True)
             try:
                 endpoint = keyedcache.cache_get(key_caches_endpoint)
             except:
                 try:
                     endpoint = Endpoint.objects.get(uid__exact=phone, enable=True)
                     keyedcache.cache_set(key_caches_endpoint, value=endpoint)
                 except:
                     endpoint = None
                     keyedcache.cache_set(key_caches_endpoint, value=endpoint)
             #return {"lcr_rate": resp.rate, "suffix": resp.suffix, "lcr_digits": resp.digits, "lcr_carrier": resp.gw, "lcr_price": resp.price, "lcr_currency": resp.currency, "lcr_name": resp.name, "nibble_rate": respt.rate }
             return {"lcr_rate": lcr_rate, "lcr_price": lcr_rate, "lcr_currency": lcr_currency, "endpoint": endpoint}
         else:
             return rc.NOT_HERE
     else:
         return rc.FORBIDDEN
Example #4
0
    def read(self, request, account=None, phone=None, si=None, tariff=None):
        """
        Returns a blogpost, if `title` is given,
        otherwise all the posts.

        Parameters:
         - `phone_number`: The title of the post to retrieve.
        """
        user = request.user
        if user.has_perm("billing.api_view"):
            if phone is not None and si is not None and tariff is not None:
                key_caches_site = "site::{0}".format(si)
                key_caches_tariff = "tariff::{0}::phone::{1}".format(tariff, phone)
                key_caches_phone_site = "phone::{0}::site::{1}".format(phone, si)
                try:
                    resp = keyedcache.cache_get(key_caches_phone_site)
                except:
                    lcr_query = "SELECT l.id AS id, l.digits AS digits, cg.name AS gw, l.rate AS rate, cg.prefix AS gw_prefix, cg.suffix AS suffix, l.price AS price, l.price_currency AS currency, l.name AS name FROM lcr l LEFT JOIN carrier_gateway cg ON l.carrier_id_id=cg.id LEFT JOIN django_site s ON l.site_id=s.id WHERE cg.enabled = '1' AND l.enabled = '1' AND l.digits IN ({0}) AND CURTIME() BETWEEN l.time_start AND l.time_end AND (DAYOFWEEK(NOW()) = l.weeks OR l.weeks = 0) AND s.name='{1}' ORDER BY  digits DESC, reliability DESC, quality DESC;".format(pars_phone(phone), si)
                    log.debug(lcr_query)
                    resp = Lcr.objects.raw(lcr_query)[0]
                    keyedcache.cache_set(key_caches_phone_site, value=resp)
                    #resp = Lcr.objects.phone_lcr(phone, si)
                try:
                    respt = keyedcache.cache_get(key_caches_tariff)
                except:
                    #respt = Tariff.objects.phone_tariff(phone, tariff)
                    query = "select * from tariff where tariff_plan_id=%i AND digits IN (%s) ORDER BY digits DESC, rand();" % (int(tariff), pars_phone(phone))
                    respt = Tariff.objects.raw(query)[0]
                    keyedcache.cache_set(key_caches_tariff, value=respt)
                return {"lcr_rate": resp.rate, "suffix": resp.suffix, "lcr_digits": resp.digits, "lcr_carrier": resp.gw, "lcr_price": resp.price, "lcr_currency": resp.currency, "lcr_name": resp.name, "nibble_rate": respt.rate }
            else:
                return rc.NOT_HERE

        else:
            return rc.FORBIDDEN
Example #5
0
 def phone_lcr(self, phone, site):
     #lc = self.model()
     #return self.filter(digits=phone, site__name__iexact=site)[0]
     query = "SELECT l.id AS id, l.digits AS digits, cg.name AS gw, l.rate AS rate, cg.prefix AS gw_prefix, cg.suffix AS suffix, l.price AS price, l.price_currency AS currency, l.name AS name FROM lcr l LEFT JOIN carrier_gateway cg ON l.carrier_id_id=cg.id LEFT JOIN django_site s ON l.site_id=s.id WHERE cg.enabled = '1' AND l.enabled = '1' AND l.digits IN ({0}) AND CURTIME() BETWEEN l.time_start AND l.time_end AND (DAYOFWEEK(NOW()) = l.weeks OR l.weeks = 0) AND s.name='{1}' ORDER BY  digits DESC, reliability DESC, quality DESC;".format(pars_phone(phone), site)
     log.debug(query)
     return self.raw(query)[0]
Example #6
0
 def read(self, request, phone=None, gw=None):
     user = request.user
     log.debug('BillingInHandler: {0} {1} {2}'.format(user, phone, gw))
     if user.has_perm("billing.api_view"):
         if phone is not None and gw is not None:
             key_caches_gw = "gatewayw::{0}".format(gw)
             key_caches_endpoint = "endpoint::{0}".format(phone)
             dialplan_caches_gw = "dialplan::gatewayw::{0}::endpoint::{1}".format(
                 gw, phone)
             dialplan_caches_endpoint = "dialplan::endpoint::{0}".format(
                 phone)
             try:
                 gateway = keyedcache.cache_get(key_caches_gw)
             except:
                 gateway = SofiaGateway.objects.get(name__exact=gw,
                                                    enabled=True)
                 keyedcache.cache_set(key_caches_gw, value=gateway)
                 log.error("Is not gateway: {0}".format(gw))
             #log.debug(gateway.id)
             #lcr_query = "SELECT l.id AS id, l.digits AS digits, l.rate AS rate, l.price AS price, l.price_currency AS currency, l.name AS name FROM lcr l WHERE l.carrier_id_id= '{1}' AND l.enabled = '1' AND l.digits IN ({0}) AND CURTIME() BETWEEN l.time_start AND l.time_end AND (DAYOFWEEK(NOW()) = l.weeks OR l.weeks = 0) ORDER BY  digits DESC, reliability DESC, quality DESC;".format(pars_phone(phone), gateway.id)
             lcr_query = "SELECT l.id AS id, l.digits AS digits, l.rate AS rate, l.price AS price, l.price_currency AS currency, l.name AS name FROM lcr l WHERE l.carrier_id_id= '{1}' AND l.enabled = '1' AND l.digits IN ({0}) ORDER BY  digits DESC, reliability DESC, quality DESC;".format(
                 pars_phone(phone), gateway.id)
             #log.debug(lcr_query)
             #resp = Lcr.objects.raw(lcr_query)[0]
             try:
                 lcr = keyedcache.cache_get(dialplan_caches_gw)
                 lcr_rate = lcr.lcr_rate
                 lcr_price = lcr.lcr_price
                 lcr_currency = lcr.lcr_currency
             except:
                 try:
                     resp = Lcr.objects.raw(lcr_query)[0]
                     lcr_price = resp.price
                     lcr_rate = resp.rate
                     lcr_currency = resp.currency
                     keyedcache.cache_set(dialplan_caches_gw,
                                          value={
                                              "lcr_rate": lcr_rate,
                                              "lcr_price": lcr_price,
                                              "lcr_currency": lcr_currency
                                          })
                 except:
                     log.error("Is not lcr gateway: {0}".format(gw))
                     lcr_rate = lcr_price = "0.18"
                     lcr_currency = "UAH"
             #endpoint = Endpoint.objects.get(uid__exact=phone, enable=True)
             try:
                 endpoint = keyedcache.cache_get(key_caches_endpoint)
             except:
                 try:
                     endpoint = Endpoint.objects.get(uid__exact=phone,
                                                     enable=True)
                     keyedcache.cache_set(key_caches_endpoint,
                                          value=endpoint)
                 except:
                     endpoint = None
                     keyedcache.cache_set(key_caches_endpoint,
                                          value=endpoint)
             #return {"lcr_rate": resp.rate, "suffix": resp.suffix, "lcr_digits": resp.digits, "lcr_carrier": resp.gw, "lcr_price": resp.price, "lcr_currency": resp.currency, "lcr_name": resp.name, "nibble_rate": respt.rate }
             return {
                 "lcr_rate": lcr_rate,
                 "lcr_price": lcr_rate,
                 "lcr_currency": lcr_currency,
                 "endpoint": endpoint
             }
         else:
             return rc.NOT_HERE
     else:
         return rc.FORBIDDEN
Example #7
0
 def phone_tariff(self, phone, tariff, site=None):
     """
     Search rate from tariff
     """
     bl = self.model()
     #return self.filter(digits=phone, enabled=True, tariff_plan__site__name__exact=site)
     query = "select * from tariff where tariff_plan_id=%i AND digits IN (%s) ORDER BY digits DESC, rand();" % (int(tariff), pars_phone(phone))
     return bl.raw(query)[0]