コード例 #1
0
ファイル: handlers.py プロジェクト: banabana/fsb
    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
コード例 #2
0
    def testCacheGetOK(self):
        one = [1, 2, 3, 4]
        keyedcache.cache_set('ok', value=one, length=2)
        two = keyedcache.cache_get('ok')
        self.assertEqual(one, two)

        time.sleep(5)
        try:
            three = keyedcache.cache_get('ok')
            self.fail('should have raised NotCachedError, got %s' % three)
        except keyedcache.NotCachedError:
            pass
コード例 #3
0
ファイル: handlers.py プロジェクト: banabana/fsb
 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
コード例 #4
0
ファイル: tests.py プロジェクト: hnejadi/xerobis
    def testDisable(self):
        keyedcache.cache_set("disabled", value=False)
        v = keyedcache.cache_get("disabled")
        self.assertEqual(v, False)

        keyedcache.cache_enable(False)
        keyedcache.cache_set("disabled", value=True)
        try:
            keyedcache.cache_get("disabled")
            self.fail("should have raised NotCachedError")
        except keyedcache.NotCachedError, nce:
            key = keyedcache.cache_key("disabled")
            self.assertEqual(nce.key, key)
コード例 #5
0
    def testDisable(self):
        keyedcache.cache_set('disabled', value=False)
        v = keyedcache.cache_get('disabled')
        self.assertEqual(v, False)

        keyedcache.cache_enable(False)
        keyedcache.cache_set('disabled', value=True)
        try:
            keyedcache.cache_get('disabled')
            self.fail('should have raised NotCachedError')
        except keyedcache.NotCachedError, nce:
            key = keyedcache.cache_key('disabled')
            self.assertEqual(nce.key, key)
コード例 #6
0
ファイル: tests.py プロジェクト: grengojbo/fsa
 def testGatewayCache(self):
     gw = 'testgw'
     key_caches_gw = "gatewayw::{0}".format(gw)
     gateway = SofiaGateway.objects.get(name__exact=gw, enabled=True)
     keyedcache.cache_set(key_caches_gw, value=gateway)
     gwr = keyedcache.cache_get(key_caches_gw)
     #self.assertEquals(gwr.price, Decimal("0.21"))
     #gateway.price = Decimal("0.2")
     gateway.save()
     try:
         gwr = keyedcache.cache_get(key_caches_gw)
     except:
         gateway = SofiaGateway.objects.get(name__exact=gw, enabled=True)
         keyedcache.cache_set(key_caches_gw, value=gateway)
     gwr = keyedcache.cache_get(key_caches_gw)
コード例 #7
0
ファイル: handlers.py プロジェクト: banabana/fsb
    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
コード例 #8
0
ファイル: models.py プロジェクト: JvGinkel/openode
def find_setting(group, key):
    """Get a setting or longsetting by group and key, cache and return it."""

    setting = None
    use_db, overrides = (True, {})
    ck = cache_key('Setting', group, key)

    grp = overrides.get(group, None)

    if grp and key in grp:
        val = grp[key]
        setting = ImmutableSetting(key=key, group=group, value=val)
        log.debug('Returning overridden: %s', setting)
    elif use_db:
        try:
            setting = cache_get(ck)

        except NotCachedError, nce:
            if loading.app_cache_ready():
                try:
                    setting = Setting.objects.get(key__exact=key, group__exact=group)

                except Setting.DoesNotExist:
                    # maybe it is a "long setting"
                    try:
                        setting = LongSetting.objects.get(key__exact=key, group__exact=group)

                    except LongSetting.DoesNotExist:
                        pass

                cache_set(ck, value=setting)
コード例 #9
0
ファイル: models.py プロジェクト: zhilin-zheng/askbot-devel
def find_setting(group, key, site=None):
    """Get a setting or longsetting by group and key, cache and return it."""

    siteid = _safe_get_siteid(site)
    setting = None

    use_db, overrides = get_overrides(siteid)
    ck = cache_key('Setting', siteid, group, key)

    grp = overrides.get(group, None)

    if grp and key in grp:
        val = grp[key]
        setting = ImmutableSetting(key=key, group=group, value=val)
        log.debug('Returning overridden: %s', setting)
    elif use_db:
        try:
            setting = cache_get(ck)
        except NotCachedError, nce:
            if loading.app_cache_ready():
                try:
                    setting = Setting.objects.get(site__id__exact=siteid, key__exact=key, group__exact=group)

                except Setting.DoesNotExist:
                    # maybe it is a "long setting"
                    try:
                        setting = LongSetting.objects.get(site__id__exact=siteid, key__exact=key, group__exact=group)

                    except LongSetting.DoesNotExist:
                        pass

                cache_set(ck, value=setting)
コード例 #10
0
ファイル: models.py プロジェクト: ringemup/satchmo
    def getCCV(self):
        try:
            ccv = keyedcache.cache_get(self.encrypted_cc)
        except keyedcache.NotCachedError:
            ccv = ""

        return ccv
コード例 #11
0
ファイル: models.py プロジェクト: vvangelovski/askbot-devel
def find_setting(group, key, site=None):
    """Get a setting or longsetting by group and key, cache and return it."""
       
    siteid = _safe_get_siteid(site)
    setting = None
    
    use_db, overrides = get_overrides(siteid)
    ck = cache_key('Setting', siteid, group, key)
    
    if use_db:
        try:
            setting = cache_get(ck)

        except NotCachedError, nce:
            if loading.app_cache_ready():
                try:
                    setting = Setting.objects.get(site__id__exact=siteid, key__exact=key, group__exact=group)

                except Setting.DoesNotExist:
                    # maybe it is a "long setting"
                    try:
                        setting = LongSetting.objects.get(site__id__exact=siteid, key__exact=key, group__exact=group)
           
                    except LongSetting.DoesNotExist:
                        pass
            
                cache_set(ck, value=setting)
コード例 #12
0
ファイル: models.py プロジェクト: 34/T
    def getCCV(self):
        try:
            ccv = keyedcache.cache_get(self.encrypted_cc)
        except keyedcache.NotCachedError:
            ccv = ""

        return ccv
コード例 #13
0
ファイル: price_calc.py プロジェクト: romali/ajax_show_stock
 def _get_price_from_mousercart(self,qty,purch_price):
     '''
         从mouser购物车获取cartprice
     '''
     try:
         c_list = cache_get('MOUSER_CART_PRICE',partno=self.partno.upper(),mfr=self.mfr.upper(),supplier=self.supplier,qty=qty)
         print 'Hit cache mouser cart price'
     except NotCachedError:        
         #根据partno及qty去看看mouser网给多少
         print 'Enter mouser cart...'
         mp = GetCartPrice()
         c_list = mp.get_list_cartprice(self.partno,qty)
         cache_set('MOUSER_CART_PRICE',value=c_list,length=60*60*5,partno=self.partno.upper(),mfr=self.mfr.upper(),supplier=self.supplier,qty=qty)
         
     if c_list[0] == 'right':
         if c_list[1:]:
             if len(c_list[1:]) == 1:
                 cartprice = c_list[1].get('cartprice',0.0)
             else:
                 for i in c_list[1:]:
                     if self.mfr.upper() == i['mfr'].upper():
                         cartprice = i.get('cartprice',0.0)
                         break
             #
             if cartprice:#不是''
                 cartprice = cartprice.replace('$','')
                 try:
                     cartprice = float(cartprice)
                 except Exception,e:
                     cartprice = 0
                     
                 if cartprice < purch_price and cartprice != 0:
                     #print 'get price from mouser cart : %s' %(cartprice)
                     purch_price = cartprice
コード例 #14
0
ファイル: models.py プロジェクト: pjrobertson/dj-livesettings
def find_setting(group, key, site=None):
    """Get a setting or longsetting by group and key, cache and return it."""
       
    siteid = _safe_get_siteid(site)
    setting = None
    
    use_db, overrides = get_overrides(siteid)
    ck = cache_key('Setting', siteid, group, key)
    
    if use_db:
        try:
            setting = cache_get(ck)

        except NotCachedError, nce:
            if loading.app_cache_ready():
                try:
                    setting = Setting.objects.get(site__id__exact=siteid, key__exact=key, group__exact=group)

                except Setting.DoesNotExist:
                    # maybe it is a "long setting"
                    try:
                        setting = LongSetting.objects.get(site__id__exact=siteid, key__exact=key, group__exact=group)
           
                    except LongSetting.DoesNotExist:
                        pass
            
                cache_set(ck, value=setting)
コード例 #15
0
ファイル: models.py プロジェクト: grengojbo/django-bursar
    def getCCV(self):
        """Get the CCV from cache"""
        try:
            ccv = keyedcache.cache_get(self.encrypted_cc)
        except keyedcache.NotCachedError:
            ccv = ""

        return ccv
コード例 #16
0
def highest_rated(count=0, site=None):
    """Get the most highly rated products"""
    if site is None:
        site = Site.objects.get_current()

    site_id = site.id

    try:
        pks = cache_get("BESTRATED", site=site_id, count=count)
        pks = [pk for pk in pks.split(',')]
        log.debug('retrieved highest rated products from cache')

    except NotCachedError as nce:
        # here were are going to do just one lookup for all product comments

        product_ratings = ProductRating.objects.rated_products().filter(
            comment__site__id=site_id).distinct().order_by(
                'comment__object_pk')

        # then make lists of ratings for each
        commentdict = {}
        for rating in product_ratings:
            commentdict.setdefault(rating.comment.object_pk,
                                   []).append(rating.rating)

        # now take the average of each, and make a nice list suitable for sorting
        ratelist = [(average(ratings), pk)
                    for pk, ratings in commentdict.items()]
        ratelist.sort()
        #log.debug(ratelist)

        # chop off the highest and reverse so highest is the first
        ratelist = ratelist[-count:]
        ratelist.reverse()

        pks = ["%s" % p[1] for p in ratelist]
        pkstring = ",".join(pks)
        log.debug('calculated highest rated products, set to cache: %s',
                  pkstring)
        cache_set(nce.key, value=pkstring)

    products = []
    if pks:
        ids = []
        for pk in pks:
            try:
                _id = int(pk)
                ids.append(_id)
            except ValueError:
                pass
        productdict = Product.objects.in_bulk(ids)
        for _id in ids:
            try:
                products.append(productdict[_id])
            except KeyError:
                pass
    return products
コード例 #17
0
ファイル: tests.py プロジェクト: grengojbo/fsa
    def test03EndpointCache(self):
        phone = '1003'
        key_caches_endpoint = "endpoint::{0}".format(phone)
        new_endpoint = Endpoint.objects.create_endpoint(self.user)

        endpoint = Endpoint.objects.get(uid__exact=phone, enable=True)
        keyedcache.cache_set(key_caches_endpoint, value=endpoint)
        ep = keyedcache.cache_get(key_caches_endpoint)
        self.assertEquals(ep.cidr_ip, '0.0.0.0')
        endpoint.cidr_ip = '127.0.0.1'
        endpoint.save()
        try:
            ep = keyedcache.cache_get(key_caches_endpoint)
        except:
            endpoint = Endpoint.objects.get(uid__exact=phone, enable=True)
            keyedcache.cache_set(key_caches_endpoint, value=endpoint)
        ep = keyedcache.cache_get(key_caches_endpoint)
        self.assertEquals(ep.cidr_ip, '127.0.0.1')
コード例 #18
0
    def testDisable(self):
        keyedcache.cache_set('disabled', value=False)
        v = keyedcache.cache_get('disabled')
        self.assertEqual(v, False)

        keyedcache.cache_enable(False)
        keyedcache.cache_set('disabled', value=True)
        try:
            keyedcache.cache_get('disabled')
            self.fail('should have raised NotCachedError')
        except keyedcache.NotCachedError as nce:
            key = keyedcache.cache_key('disabled')
            self.assertEqual(nce.key, key)

        keyedcache.cache_enable()
        v2 = keyedcache.cache_get('disabled')
        # should still be False, since the cache was disabled
        self.assertEqual(v2, False)
コード例 #19
0
ファイル: queries.py プロジェクト: xnester/satchmo
def bestsellers(count):
    """Look up the bestselling products and return in a list"""
    sellers = []
    cached = False
    try:
        pks = cache_get('BESTSELLERS', count=count)
        if pks:
            pks = [long(pk) for pk in pks.split(',')]
            productdict = Product.objects.in_bulk(pks)
            #log.debug(productdict)
            for pk in pks:
                try:
                    if (int(pk)) in productdict:
                        key = int(pk)
                    elif long(pk) in productdict:
                        key = long(pk)
                    else:
                        continue
                    sellers.append(productdict[key])
                except ValueError:
                    pass

            log.debug('retrieved bestselling products from cache')
        cached = True
    except NotCachedError:
        pass

    except ValueError:
        pass

    if not cached:
        products = Product.objects.active_by_site()
        work = []
        for p in products:
            ct = p.orderitem_set.count()
            if ct > 0:
                work.append((ct, p))

        work.sort()
        work = work[-count:]
        work.reverse()

        sellers = []
        pks = []

        for p in work:
            product = p[1]
            pks.append("%i" % product.pk)
            sellers.append(product)

        pks = ",".join(pks)
        log.debug('calculated bestselling %i products, set to cache: %s',
                  count, pks)
        cache_set('BESTSELLERS', count=count, value=pks)

    return sellers
コード例 #20
0
ファイル: tests.py プロジェクト: grengojbo/fsa
    def testSipProfileCache(self):
        profile = "internal"
        key_caches = "directory::gw::sites::{0}".format(profile)

        sofia = SipProfile.objects.get(enabled=True, name__exact=profile)
        sites = sofia.sites.all().values()
        keyedcache.cache_set(key_caches, value=sites)
        si = keyedcache.cache_get(key_caches)
        self.assertEquals(si.count(), 0)

        sofia.sites.add(self.site)
        sofia.save()
        try:
            si = keyedcache.cache_get(key_caches)
        except:
            sites = sofia.sites.all().values()
            keyedcache.cache_set(key_caches, value=sites)
        si = keyedcache.cache_get(key_caches)
        self.assertEquals(si.count(), 1)
コード例 #21
0
ファイル: models.py プロジェクト: ringemup/satchmo
 def _decryptCC(self):
     ccnum = _decrypt_code(self.encrypted_cc)
     if not config_value('PAYMENT', 'STORE_CREDIT_NUMBERS'):
         try:
             key = _encrypt_code(ccnum + '-card')
             encrypted_ccnum = keyedcache.cache_get(key)
             ccnum = _decrypt_code(encrypted_ccnum)
         except keyedcache.NotCachedError:
             ccnum = ""
     return ccnum
コード例 #22
0
ファイル: models.py プロジェクト: priyabrata88/EDURE
 def by_label(self, label):
     try:
         point = keyedcache.cache_get('pluginpoint', label)
     except keyedcache.NotCachedError, nce:
         try:
             point = self.select_related().get(label=label)
         except PluginPoint.DoesNotExist:
             point = None
             
         keyedcache.cache_set(nce.key, value=point)
コード例 #23
0
ファイル: models.py プロジェクト: priyabrata88/EDURE
 def by_app_name(self, app, name):
     try:
         plugin = keyedcache.cache_get('plugin', app, name)
     except keyedcache.NotCachedError, nce:
         try:
             plugin = self.get(label=u'.'.join([app, name]))
         except Plugin.DoesNotExist:
             plugin = None
         
         keyedcache.cache_set(nce.key, value=plugin)
コード例 #24
0
ファイル: models.py プロジェクト: 34/T
 def _decryptCC(self):
     ccnum = _decrypt_code(self.encrypted_cc)
     if not config_value('PAYMENT', 'STORE_CREDIT_NUMBERS'):
         try:
             key = _encrypt_code(ccnum + '-card')
             encrypted_ccnum = keyedcache.cache_get(key)
             ccnum = _decrypt_code(encrypted_ccnum)
         except keyedcache.NotCachedError:
             ccnum = ""
     return ccnum
コード例 #25
0
ファイル: models.py プロジェクト: grengojbo/django-bursar
 def decryptedCC(self):
     ccnum = _decrypt_code(self.encrypted_cc)
     if not get_bursar_setting('STORE_CREDIT_NUMBERS'):
         try:
             key = _encrypt_code(ccnum + '-card')
             encrypted_ccnum = keyedcache.cache_get(key)
             ccnum = _decrypt_code(encrypted_ccnum)
         except keyedcache.NotCachedError:
             ccnum = ""
     return ccnum
コード例 #26
0
ファイル: models.py プロジェクト: ThissDJ/designhub
 def by_app_name(self, app, name):
     try:
         plugin = keyedcache.cache_get('plugin', app, name)
     except keyedcache.NotCachedError, nce:
         try:
             plugin = self.get(label=u'.'.join([app, name]))
         except Plugin.DoesNotExist:
             plugin = None
         
         keyedcache.cache_set(nce.key, value=plugin)
コード例 #27
0
ファイル: models.py プロジェクト: ThissDJ/designhub
 def by_label(self, label):
     try:
         point = keyedcache.cache_get('pluginpoint', label)
     except keyedcache.NotCachedError, nce:
         try:
             point = self.select_related().get(label=label)
         except PluginPoint.DoesNotExist:
             point = None
             
         keyedcache.cache_set(nce.key, value=point)
コード例 #28
0
    def testDelete(self):
        keyedcache.cache_set('del', value=True)

        for x in range(0, 10):
            keyedcache.cache_set('del', 'x', x, value=True)
            for y in range(0, 5):
                keyedcache.cache_set('del', 'x', x, 'y', y, value=True)

        # check to make sure all the values are in the cache
        self.assertTrue(keyedcache.cache_get('del', default=False))
        for x in range(0, 10):
            self.assertTrue(keyedcache.cache_get('del', 'x', x, default=False))
            for y in range(0, 5):
                self.assertTrue(
                    keyedcache.cache_get('del', 'x', x, 'y', y, default=False))

        # try to delete just one
        killed = keyedcache.cache_delete('del', 'x', 1)
        self.assertEqual(["del::x::1"], killed)
        self.assertFalse(keyedcache.cache_get('del', 'x', 1, default=False))

        # but the others are still there
        self.assertTrue(keyedcache.cache_get('del', 'x', 2, default=False))

        # now kill all of del::x::1
        killed = keyedcache.cache_delete('del', 'x', 1, children=True)
        for y in range(0, 5):
            self.assertFalse(
                keyedcache.cache_get('del', 'x', 1, 'y', y, default=False))

        # but del::x::2 and children are there
        self.assertTrue(
            keyedcache.cache_get('del', 'x', 2, 'y', 1, default=False))

        # kill the rest
        killed = keyedcache.cache_delete('del', children=True)
        self.assertFalse(keyedcache.cache_get('del', default=False))
        for x in range(0, 10):
            self.assertFalse(keyedcache.cache_get('del', 'x', x,
                                                  default=False))
            for y in range(0, 5):
                self.assertFalse(
                    keyedcache.cache_get('del', 'x', x, 'y', y, default=False))
コード例 #29
0
ファイル: queries.py プロジェクト: kidaa30/pops
def bestsellers(count):
    """Look up the bestselling products and return in a list"""
    sellers = []
    cached = False
    try:
        pks = cache_get("BESTSELLERS", count=count)
        if pks:
            pks = [long(pk) for pk in pks.split(",")]
            productdict = Product.objects.in_bulk(pks)
            # log.debug(productdict)
            for pk in pks:
                try:
                    if (int(pk)) in productdict:
                        key = int(pk)
                    elif long(pk) in productdict:
                        key = long(pk)
                    else:
                        continue
                    sellers.append(productdict[key])
                except ValueError:
                    pass

            log.debug("retrieved bestselling products from cache")
        cached = True
    except NotCachedError:
        pass

    except ValueError:
        pass

    if not cached:
        products = Product.objects.active_by_site()
        work = []
        for p in products:
            ct = p.orderitem_set.count()
            if ct > 0:
                work.append((ct, p))

        work.sort()
        work = work[-count:]
        work.reverse()

        sellers = []
        pks = []

        for p in work:
            product = p[1]
            pks.append("%i" % product.pk)
            sellers.append(product)

        pks = ",".join(pks)
        log.debug("calculated bestselling %i products, set to cache: %s", count, pks)
        cache_set("BESTSELLERS", count=count, value=pks)

    return sellers
コード例 #30
0
ファイル: mixins.py プロジェクト: ppp0/openbroadcast
    def _find_translation(self, language_code=None, attr='translations'):
        """
        Look up a translation for an attr.
        
        Ex: self._find_translation(language_code='en-us', attr='translations')
        """
        if not language_code:
            language_code = get_language()

        try:
            site = Site.objects.get_current()
            trans = keyedcache.cache_get([self.__class__.__name__, self.id],
                                         site=site,
                                         trans=attr,
                                         lang=language_code)
        except keyedcache.NotCachedError, nce:

            translations = getattr(self, attr)

            c = translations.filter(languagecode__exact=language_code)
            ct = c.count()

            if not c or ct == 0:
                pos = language_code.find('-')
                if pos > -1:
                    short_code = language_code[:pos]
                    #log.debug("%s: Trying to find root language content for: "
                    #          "[%s]", self, short_code)
                    c = translations.filter(languagecode__exact=short_code)
                    ct = c.count()
                    if ct > 0:
                        #log.debug("%s: Found root language content for: [%s]",
                        #          self, short_code)
                        pass

            if not c or ct == 0:
                #log.debug("Trying to find default language content for: %s",
                #          self)
                c = translations.filter(
                    languagecode__istartswith=settings.LANGUAGE_CODE)
                ct = c.count()

            if not c or ct == 0:
                #log.debug("Trying to find *any* language content for: %s",
                #          self)
                c = translations.all()
                ct = c.count()

            if ct > 0:
                trans = c[0]
            else:
                trans = None

            keyedcache.cache_set(nce.key, value=trans)
コード例 #31
0
ファイル: mixins.py プロジェクト: alainwolf/openbroadcast.org
    def _find_translation(self, language_code=None, attr='translations'):
        """
        Look up a translation for an attr.
        
        Ex: self._find_translation(language_code='en-us', attr='translations')
        """
        if not language_code:
            language_code = get_language()
            
        try:
            site = Site.objects.get_current()
            trans = keyedcache.cache_get([self.__class__.__name__, self.id],
                                         site=site, trans=attr,
                                         lang=language_code)
        except keyedcache.NotCachedError, nce:
            
            translations = getattr(self, attr)

            c = translations.filter(languagecode__exact = language_code)
            ct = c.count()

            if not c or ct == 0:
                pos = language_code.find('-')
                if pos>-1:
                    short_code = language_code[:pos]
                    #log.debug("%s: Trying to find root language content for: "
                    #          "[%s]", self, short_code)
                    c = translations.filter(languagecode__exact = short_code)
                    ct = c.count()
                    if ct>0:
                        #log.debug("%s: Found root language content for: [%s]",
                        #          self, short_code)
                        pass

            if not c or ct == 0:
                #log.debug("Trying to find default language content for: %s",
                #          self)
                c = translations.filter(
                    languagecode__istartswith = settings.LANGUAGE_CODE)
                ct = c.count()

            if not c or ct == 0:
                #log.debug("Trying to find *any* language content for: %s",
                #          self)
                c = translations.all()
                ct = c.count()

            if ct > 0:
                trans = c[0]
            else:
                trans = None

            keyedcache.cache_set(nce.key, value=trans)
コード例 #32
0
def cached_check_marketo_complete(course_id, email, course_map):
    # email = '*****@*****.**'
    cachekey = cache_key('marketo_complete_cache',
                         course=course_id, email=email)
    try:
        value = cache_get(cachekey)
    except NotCachedError:
        value = None
    if value is None:
        # import pdb; pdb.set_trace()
        return check_marketo_complete(course_id, email, course_map)
    else:
        return value
コード例 #33
0
ファイル: emitters.py プロジェクト: bkroeze/playaevents
 def _dict(data):
     """
     Dictionaries.
     """
     key = cache_key('json', data)
     try:
         ret = cache_get(key)
         log.debug('got json serialized data from cache for %s', key)
     except NotCachedError:
         ret = dict([ (k, _any(v)) for k, v in data.iteritems() ])
         log.debug('setting json serialized data to cache for %s', key)
         cache_set(key, value=ret)
     return ret
コード例 #34
0
ファイル: tests.py プロジェクト: kunaldeo/django-keyedcache3
    def testDelete(self):
        keyedcache.cache_set('del', value=True)

        for x in range(0, 10):
            keyedcache.cache_set('del', 'x', x, value=True)
            for y in range(0, 5):
                keyedcache.cache_set('del', 'x', x, 'y', y, value=True)

        # check to make sure all the values are in the cache
        self.assertTrue(keyedcache.cache_get('del', default=False))
        for x in range(0, 10):
            self.assertTrue(keyedcache.cache_get('del', 'x', x, default=False))
            for y in range(0, 5):
                self.assertTrue(keyedcache.cache_get('del', 'x', x, 'y', y, default=False))

        # try to delete just one
        killed = keyedcache.cache_delete('del', 'x', 1)
        self.assertEqual(["del::x::1"], killed)
        self.assertFalse(keyedcache.cache_get('del', 'x', 1, default=False))

        # but the others are still there
        self.assertTrue(keyedcache.cache_get('del', 'x', 2, default=False))

        # now kill all of del::x::1
        killed = keyedcache.cache_delete('del', 'x', 1, children=True)
        for y in range(0, 5):
            self.assertFalse(keyedcache.cache_get('del', 'x', 1, 'y', y, default=False))

        # but del::x::2 and children are there
        self.assertTrue(keyedcache.cache_get('del', 'x', 2, 'y', 1, default=False))

        # kill the rest
        killed = keyedcache.cache_delete('del', children=True)
        self.assertFalse(keyedcache.cache_get('del', default=False))
        for x in range(0, 10):
            self.assertFalse(keyedcache.cache_get('del', 'x', x, default=False))
            for y in range(0, 5):
                self.assertFalse(keyedcache.cache_get('del', 'x', x, 'y', y, default=False))
コード例 #35
0
ファイル: tests.py プロジェクト: hnejadi/xerobis
    def testDelete(self):
        keyedcache.cache_set("del", value=True)

        for x in range(0, 10):
            keyedcache.cache_set("del", "x", x, value=True)
            for y in range(0, 5):
                keyedcache.cache_set("del", "x", x, "y", y, value=True)

        # check to make sure all the values are in the cache
        self.assert_(keyedcache.cache_get("del", default=False))
        for x in range(0, 10):
            self.assert_(keyedcache.cache_get("del", "x", x, default=False))
            for y in range(0, 5):
                self.assert_(keyedcache.cache_get("del", "x", x, "y", y, default=False))

        # try to delete just one
        killed = keyedcache.cache_delete("del", "x", 1)
        self.assertEqual([keyedcache.CACHE_PREFIX + "::del::x::1"], killed)
        self.assertFalse(keyedcache.cache_get("del", "x", 1, default=False))

        # but the others are still there
        self.assert_(keyedcache.cache_get("del", "x", 2, default=False))

        # now kill all of del::x::1
        killed = keyedcache.cache_delete("del", "x", 1, children=True)
        for y in range(0, 5):
            self.assertFalse(keyedcache.cache_get("del", "x", 1, "y", y, default=False))

        # but del::x::2 and children are there
        self.assert_(keyedcache.cache_get("del", "x", 2, "y", 1, default=False))

        # kill the rest
        killed = keyedcache.cache_delete("del", children=True)
        self.assertFalse(keyedcache.cache_get("del", default=False))
        for x in range(0, 10):
            self.assertFalse(keyedcache.cache_get("del", "x", x, default=False))
            for y in range(0, 5):
                self.assertFalse(keyedcache.cache_get("del", "x", x, "y", y, default=False))
コード例 #36
0
def find_by_id(cls, groupkey, objectid, raises=False):
    """A helper function to look up an object by id"""
    ob = None
    try:
        ob = keyedcache.cache_get(groupkey, objectid)
    except keyedcache.NotCachedError, e:
        try: 
            ob = cls.objects.get(pk=objectid)
            keyedcache.cache_set(e.key, value=ob)

        except cls.DoesNotExist:
            log.debug("No such %s: %s", groupkey, objectid)
            if raises:
                raise cls.DoesNotExist
コード例 #37
0
def find_by_slug(cls, groupkey, slug, raises=False):
    """A helper function to look up an object by slug"""
    ob = None
    try:
        ob = keyedcache.cache_get(groupkey, slug)
    except keyedcache.NotCachedError, e:
        try: 
            ob = cls.objects.get(slug__exact=slug)
            keyedcache.cache_set(e.key, value=ob)

        except cls.DoesNotExist:
            log.debug("No such %s: %s", groupkey, slug)
            if raises:
                raise
コード例 #38
0
def find_by_slug(cls, groupkey, slug, raises=False):
    """A helper function to look up an object by slug"""
    ob = None
    try:
        ob = keyedcache.cache_get(groupkey, slug)
    except keyedcache.NotCachedError, e:
        try: 
            ob = cls.objects.get(slug__exact=slug)
            keyedcache.cache_set(e.key, value=ob)

        except cls.DoesNotExist:
            log.debug("No such %s: %s", groupkey, slug)
            if raises:
                raise
コード例 #39
0
def find_by_id(cls, groupkey, objectid, raises=False):
    """A helper function to look up an object by id"""
    ob = None
    try:
        ob = keyedcache.cache_get(groupkey, objectid)
    except keyedcache.NotCachedError, e:
        try: 
            ob = cls.objects.get(pk=objectid)
            keyedcache.cache_set(e.key, value=ob)

        except cls.DoesNotExist:
            log.debug("No such %s: %s", groupkey, objectid)
            if raises:
                raise cls.DoesNotExist
コード例 #40
0
ファイル: emitters.py プロジェクト: bkroeze/playaevents
        def _list(data):
            """
            Lists.
            """

            key = cache_key('jsonx', data)
            try:
                ret = cache_get(key)
                log.debug('got json serialized list from cache for %s', key)
            except NotCachedError:
                ret = [ _any(v) for v in data ]
                log.debug('setting json serialized list to cache for %s', key)
                cache_set(key, value=ret)
            return ret
コード例 #41
0
def find_by_key(cls, groupkey, key, raises=False):
    """A helper function to look up an object by key"""
    ob = None
    try:
        ob = keyedcache.cache_get(groupkey, key)
    except keyedcache.NotCachedError as e:
        try:
            ob = cls.objects.get(key__exact=key)
            keyedcache.cache_set(e.key, value=ob)

        except cls.DoesNotExist:
            log.debug("No such %s: %s", groupkey, key)
            if raises:
                raise

    return ob
コード例 #42
0
ファイル: models.py プロジェクト: jtslade/satchmo-svn
 def get_current(self, site=None):
     """Convenience method to get the current shop config"""
     if not site:
         site = Site.objects.get_current()
     
     site = site.id
         
     try:
         shop_config = keyedcache.cache_get("Config", site)
     except keyedcache.NotCachedError, nce:
         try:
             shop_config = self.get(site__id__exact=site)
             keyedcache.cache_set(nce.key, value=shop_config)
         except Config.DoesNotExist:
             log.warning("No Shop Config found, using test shop config for site=%s.", site)
             shop_config = NullConfig()
コード例 #43
0
ファイル: models.py プロジェクト: kunaldeo/django-keyedcache3
def find_by_key(cls, groupkey, key, raises=False):
    """A helper function to look up an object by key"""
    ob = None
    try:
        ob = keyedcache.cache_get(groupkey, key)
    except keyedcache.NotCachedError as e:
        try:
            ob = cls.objects.get(key__exact=key)
            keyedcache.cache_set(e.key, value=ob)

        except cls.DoesNotExist:
            log.debug("No such %s: %s", groupkey, key)
            if raises:
                raise

    return ob
コード例 #44
0
ファイル: models.py プロジェクト: jes5199/playaevents
    def get_and_cache(self, **kwargs):
        key = cache_key('PlayaEvent', 'all', **kwargs)
        try:
            results = cache_get(key)
            log.debug('got all events from cache')
        except NotCachedError:
            log.debug('getting events from db')
            if kwargs:
                results = self.filter(**kwargs)
            else:
                results = self.all()

            results = list(results)
            cache_set(key, value=results, length=60*60*24) # set for one day

        return results
コード例 #45
0
ファイル: views.py プロジェクト: grengojbo/fsa
def gw(request):
    p = request.POST
    key_value = name = 'result'
    xml_context = '<result status="not found" />'
    key_caches = "directory:gw:{0}".format(p.get('profile'))
    try:
        sofia = keyedcache.cache_get(key_caches)
        return request.Context({'hostname':request.POST.get('hostname'), 'sofia':sofia}).render_response('directory/gw.xml')
    except keyedcache.NotCachedError, nce:
        try:
            sofia = SipProfile.objects.get(enabled=True, name__exact=p.get('profile'))
            keyedcache.cache_set(key_caches, value=sofia)
            return request.Context({'hostname':request.POST.get('hostname'), 'sofia':sofia}).render_response('directory/gw.xml')
        except:
            #SipProfile.DoesNotExist:
            return request.Context({'name':name, 'key_value':key_value, 'xml_context':xml_context}).render_response('server/fs.xml')
        return request.Context({'name':name, 'key_value':key_value, 'xml_context':xml_context}).render_response('server/fs.xml')
コード例 #46
0
ファイル: queries.py プロジェクト: russellmayhew/satchmo
def highest_rated(count=0, site=None):
    """Get the most highly rated products"""
    if site is None:
        site = Site.objects.get_current()

    site_id = site.id

    try:
        pks = cache_get("BESTRATED", site=site_id, count=count)
        pks = [pk for pk in pks.split(',')]
        log.debug('retrieved highest rated products from cache')

    except NotCachedError, nce:
        # here were are going to do just one lookup for all product comments

        comments = Comment.objects.filter(
            content_type__app_label__exact='product',
            content_type__model__exact='product',
            site__id__exact=site_id,
            productrating__rating__gt=0,
            is_public__exact=True).order_by('object_pk')

        # then make lists of ratings for each
        commentdict = {}
        for comment in comments:
            if hasattr(comment, 'productrating'):
                rating = comment.productrating.rating
                if rating > 0:
                    commentdict.setdefault(comment.object_pk,
                                           []).append(rating)

        # now take the average of each, and make a nice list suitable for sorting
        ratelist = [(average(ratings), int(pk))
                    for pk, ratings in commentdict.items()]
        ratelist.sort()
        #log.debug(ratelist)

        # chop off the highest and reverse so highest is the first
        ratelist = ratelist[-count:]
        ratelist.reverse()

        pks = ["%i" % p[1] for p in ratelist]
        pkstring = ",".join(pks)
        log.debug('calculated highest rated products, set to cache: %s',
                  pkstring)
        cache_set(nce.key, value=pkstring)
コード例 #47
0
def product_count(category, args=''):
    """Get a count of products for the base object.

    If `category` is None, then count everything.
    If it is a `Category` object then count everything in the category and subcategories.
    """
    args, kwargs = get_filter_args(args, boolargs=('variations'))
    variations = kwargs.get('variations', False)
    try:
        ct = keyedcache.cache_get('product_count', category, variations)
    except keyedcache.NotCachedError:
        if not category:
            ct = Product.objects.active_by_site(variations=variations).count()
        else:
            ct = category.active_products(include_children=True,
                                          variations=variations).count()

        keyedcache.cache_set('product_count', category, args, value=ct)
    return ct
コード例 #48
0
ファイル: models.py プロジェクト: allieus/askbot3
def find_setting(group, key, site=None):
    'Get a setting or longsetting by group and key, cache and return it.'

    site_id = _safe_get_site_id(site)
    setting = None

    use_db, overrides = get_overrides(site_id)
    ck = cache_key_get('Setting', site_id, group, key)

    grp = overrides.get(group, None)

    if grp and (key in grp):
        val = grp[key]
        setting = ImmutableSetting(key=key, group=group, value=val)
        logger.debug('Returning overridden: %s', setting)

    elif use_db:
        try:
            setting = cache_get(ck)
        except NotCachedError:
            model_classes = (Setting, LongSetting)
            for model_cls in model_classes:
                try:
                    setting = model_cls.objects.get(site__id=site_id, key=key, group=group)
                    break
                except model_cls.DoesNotExist:
                    pass

            cache_set(ck, value=setting)
    else:
        grp = overrides.get(group, None)
        if grp and key in grp:
            val = grp[key]
            setting = ImmutableSetting(key=key, group=group, value=val)
            logger.debug('Returning overridden: %s', setting)

    if setting is None:
        raise SettingNotSet(key, cachekey=ck)

    return setting
コード例 #49
0
def bestsellers(count):
    """Look up the bestselling products and return in a list"""
    sellers = []
    cached = False
    try:
        pks = cache_get('BESTSELLERS', count=count)
        if pks:
            pks = [int(pk) for pk in pks.split(',')]
            productdict = Product.objects.in_bulk(pks)
            #log.debug(productdict)
            for pk in pks:
                try:
                    if (int(pk)) in productdict:
                        key = int(pk)
                    else:
                        continue
                    sellers.append(productdict[key])
                except ValueError:
                    pass

            log.debug('retrieved bestselling products from cache')
        cached = True
    except NotCachedError:
        pass

    except ValueError:
        pass

    if not cached:
        products = Product.objects.active_by_site().annotate(item_count=Count('orderitem')) \
                                                   .filter(item_count__gt=0).order_by('-item_count')
        sellers = products[:count]
        pks = ",".join(
            str(pk) for pk in products.values_list('pk', flat=True)[:count])
        log.debug('calculated bestselling %i products, set to cache: %s',
                  count, pks)
        cache_set('BESTSELLERS', count=count, value=pks)

    return sellers
コード例 #50
0
 def cache_get(self, *args, **kwargs):
     key = self.cache_key(*args, **kwargs)
     return keyedcache.cache_get(key)
コード例 #51
0
 def testCacheGetDefault(self):
     chk = keyedcache.cache_get('default', default='-')
     self.assertEqual(chk, '-')
コード例 #52
0
    def calculate(self, cart, contact):
        """
        Based on the chosen UPS method, we will do our call to UPS and see how much it will
        cost.
        We will also need to store the results for further parsing and return via the
        methods above
        """
        from satchmo_store.shop.models import Config

        settings = config_get_group('shipping.modules.ups')
        self.delivery_days = _("3 - 4")  #Default setting for ground delivery
        shop_details = Config.objects.get_current()
        # Get the code and description for the packaging
        container = settings.SHIPPING_CONTAINER.value
        container_description = settings.SHIPPING_CONTAINER.choices[int(
            container)][1]
        configuration = {
            'xml_key': settings.XML_KEY.value,
            'account': settings.ACCOUNT.value,
            'userid': settings.USER_ID.value,
            'password': settings.USER_PASSWORD.value,
            'container': container,
            'container_description': container_description,
            'pickup': settings.PICKUP_TYPE.value,
            'ship_type': self.service_type_code,
            'shop_details': shop_details,
        }

        shippingdata = {
            'single_box': False,
            'config': configuration,
            'contact': contact,
            'cart': cart,
            'shipping_address': shop_details,
            'shipping_phone': shop_details.phone,
            'shipping_country_code': shop_details.country.iso2_code
        }

        if settings.SINGLE_BOX.value:
            log.debug("Using single-box method for ups calculations.")

            box_weight = Decimal("0.00")
            for product in cart.get_shipment_list():
                if product.smart_attr('weight') is None:
                    log.warn(
                        "No weight on product (skipping for ship calculations): %s",
                        product)
                else:
                    box_weight += product.smart_attr('weight')
                if product.smart_attr('weight_units') and product.smart_attr(
                        'weight_units') != "":
                    box_weight_units = product.smart_attr('weight_units')
                else:
                    log.warn("No weight units for product")

            if box_weight < Decimal("0.1"):
                log.debug("Total box weight too small, defaulting to 0.1")
                box_weight = Decimal("0.1")

            shippingdata['single_box'] = True
            shippingdata['box_weight'] = '%.1f' % box_weight
            shippingdata['box_weight_units'] = box_weight_units.upper()

        total_weight = 0
        for product in cart.get_shipment_list():
            try:
                total_weight += product.smart_attr('weight')
            except TypeError:
                pass

        signals.shipping_data_query.send(Shipper,
                                         shipper=self,
                                         cart=cart,
                                         shippingdata=shippingdata)
        c = Context(shippingdata)
        t = loader.get_template('shipping/ups/request.xml')
        request = t.render(c)
        self.is_valid = False
        if settings.LIVE.value:
            connection = settings.CONNECTION.value
        else:
            connection = settings.CONNECTION_TEST.value

        cachekey = cache_key(
            'UPS_SHIP',
            #service_type = self.service_type_code,
            weight=str(total_weight),
            country=shop_details.country.iso2_code,
            zipcode=contact.shipping_address.postal_code)

        try:
            tree = cache_get(cachekey)
        except NotCachedError:
            tree = None

        if tree is not None:
            self.verbose_log('Got UPS info from cache [%s]', cachekey)
        else:
            self.verbose_log("Requesting from UPS [%s]\n%s", cachekey, request)
            cache_set(cachekey, value=request, length=600)
            tree = self._process_request(connection, request)
            self.verbose_log("Got from UPS [%s]:\n%s", cachekey, self.raw)
            cache_set(cachekey, value=tree)

        try:
            status_code = tree.getiterator('ResponseStatusCode')
            status_val = status_code[0].text
            self.verbose_log("UPS Status Code for cart #%s = %s", int(cart.id),
                             status_val)
        except AttributeError:
            status_val = "-1"

        if status_val == '1':
            self.is_valid = False
            self._calculated = False
            all_rates = tree.getiterator('RatedShipment')
            for response in all_rates:
                if self.service_type_code == response.find(
                        './/Service/Code').text:
                    self.charges = response.find(
                        './/TotalCharges/MonetaryValue').text
                    if response.find('.//GuaranteedDaysToDelivery').text:
                        self.delivery_days = response.find(
                            './/GuaranteedDaysToDelivery').text
                    self.is_valid = True
                    self._calculated = True

            if not self.is_valid:
                self.verbose_log("UPS Cannot find rate for code: %s [%s]",
                                 self.service_type_code,
                                 self.service_type_text)

        else:
            self.is_valid = False
            self._calculated = False

            try:
                errors = tree.find('.//Error')
                log.info("UPS %s Error: Code %s - %s" %
                         (errors[0].text, errors[1].text, errors[2].text))
            except AttributeError:
                log.info("UPS error - cannot parse response:\n %s", self.raw)

        if self.is_valid and settings.TIME_IN_TRANSIT.value:
            self.verbose_log('Now getting time in transit for cart')
            self.time_in_transit(contact, cart)
コード例 #53
0
 def testCacheGetFail(self):
     try:
         keyedcache.cache_get('x')
         self.fail('should have raised NotCachedError')
     except keyedcache.NotCachedError:
         pass
コード例 #54
0
    def ups_time_in_transit(self,
                            contact,
                            pickup_date=None,
                            price=None,
                            test=False):
        """Calculate est delivery days for a zipcode, from Store Zipcode"""

        from satchmo_store.shop.models import Config

        delivery_days = None

        if pickup_date is None:
            pickup_date = timezone.now() + timezone.timedelta(days=1)

        # UPS doesn't pick up on weekends
        if pickup_date.day == 5:
            pickup_date += timezone.timedelta(days=2)

        if pickup_date.day == 6:
            pickup_date += timezone.timedelta(days=1)

        if price is None:
            price = Decimal('10.0')

        shipaddr = contact.shipping_address
        shop_details = Config.objects.get_current()
        settings = config_get_group('shipping.modules.ups')

        configuration = {
            'xml_key': settings.XML_KEY.value,
            'account': settings.ACCOUNT.value,
            'userid': settings.USER_ID.value,
            'password': settings.USER_PASSWORD.value,
            'container': settings.SHIPPING_CONTAINER.value,
            'pickup': settings.PICKUP_TYPE.value,
            'shop_details': shop_details,
        }

        shippingdata = {
            'config': configuration,
            'zipcode': shipaddr.postal_code,
            'contact': contact,
            'shipping_address': shop_details,
            'shipping_phone': shop_details.phone,
            'shipping_country_code': shop_details.country.iso2_code,
            'pickup_date': pickup_date.strftime('%Y%m%d'),
            'price': "%.2f" % price
        }

        c = Context(shippingdata)
        t = loader.get_template('shipping/ups/transit_request.xml')
        request = t.render(c)

        if settings.LIVE.value and not test:
            connection = 'https://wwwcie.ups.com/ups.app/xml/TimeInTransit'
        else:
            connection = 'https://onlinetools.ups.com/ups.app/xml/TimeInTransit'

        cachekey = cache_key("UPS-TIT", shipaddr.postal_code,
                             pickup_date.strftime('%Y%m%d'), "%.2f" % price)

        try:
            ups = cache_get(cachekey)
        except NotCachedError:
            ups = None

        if ups is None:
            log.debug('Requesting from UPS: %s\n%s', connection, request)
            conn = urllib.request.Request(url=connection,
                                          data=request.encode("utf-8"))
            f = urllib.request.urlopen(conn)
            all_results = f.read()

            self.verbose_log("Received from UPS:\n%s", all_results)
            ups = fromstring(all_results)
            needs_cache = True
        else:
            needs_cache = False

        ok = False
        try:
            ok = ups.find('Response/ResponseStatusCode').text == '1'
        except AttributeError:
            log.warning('Bad response from UPS TimeInTransit')
            pass

        if not ok:
            try:
                response = ups.find('Response/ResponseStatusDescription').text
                log.warning('Bad response from UPS TimeInTransit: %s',
                            response)
            except AttributeError:
                log.warning('Unknown UPS TimeInTransit response')

        if ok:
            services = ups.findall('TransitResponse/ServiceSummary')
            for service in services:
                transit_code = service.find('Service/Code').text
                if self.service_type_code == TRANSIT_CODE_MAP.get(
                        transit_code, ''):
                    try:
                        delivery_days = service.find(
                            'EstimatedArrival/BusinessTransitDays').text
                        self.verbose_log('Found delivery days %s for %s',
                                         delivery_days, self.service_type_code)
                    except AttributeError:
                        log.warning(
                            'Could not find BusinessTransitDays in UPS response'
                        )

                    try:
                        delivery_days = int(delivery_days)
                    except ValueError:
                        pass

                    break

            if delivery_days is not None and needs_cache:
                cache_set(cachekey, value=ups, length=600)

        return delivery_days
コード例 #55
0
ファイル: handlers.py プロジェクト: banabana/fsb
 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
コード例 #56
0
ファイル: sites.py プロジェクト: xiaol/eachcloud_community
def by_host(host=None, id_only=False, called_recursive=None):
    """Get the current site by looking at the request stored in the thread.

    Returns the best match found in the `django.contrib.sites` app.  If not
    found, then returns the default set as given in `settings.SITE_ID`

    Params:
     - `host`: optional, host to look up
     - `id_only`: if true, then do not retrieve the full site, just the id.
    """
    global _WARNED
    if id_only:
        site = -1
    else:
        site = None

    debug_domain = None
    debug_target = None
    if settings.DEBUG:
        raw = get_threadlocal_setting('DEBUG_DOMAIN')
        if raw:
            parts = raw.split('=')
            if len(parts) == 2:
                debug_domain = parts[0]
                debug_target = parts[1]
            else:
                debug_domain = raw
                debug_target = 'com'

    if not host:
        request = threadlocals.get_current_request()
        if request:
            host = request.get_host()
        else:
            log.debug('No request')
            site = by_settings(id_only=id_only)

    if host:
        if app_cache_ready():
            try:
                site = keyedcache.cache_get('SITE', host=host, id_only=id_only)
                if id_only:
                    site = site.id

            except keyedcache.NotCachedError, nce:
                try:
                    log.debug('looking up site by host: %s', host)
                    site = Site.objects.get(domain=host)
                except Site.DoesNotExist:
                    if host.find(":") > -1:
                        try:
                            # strip the port
                            host = host.split(":")[0]
                            site = Site.objects.get(domain=host)
                        except Site.DoesNotExist:
                            pass
                    if debug_domain and host.endswith(debug_domain):
                        host = host[:-len(debug_domain)] + debug_target
                        log.debug('Using debug domain: %s', host)
                        try:
                            site = Site.objects.get(domain=host)
                        except Site.DoesNotExist:
                            pass

                if not site and get_threadlocal_setting(
                        'AUTO_WWW') and not called_recursive:
                    if host.startswith('www'):
                        log.debug('trying site lookup without www')
                        site = by_host(host=host[4:],
                                       id_only=id_only,
                                       called_recursive=True)
                    else:
                        log.debug('trying site lookup with www')
                        site = by_host(host='www.%s' % host,
                                       id_only=id_only,
                                       called_recursive=True)

                if site:
                    keyedcache.cache_set(nce.key, value=site)

                    if id_only:
                        site = site.id

                else:
                    if not host in _WARNED:
                        log.warn(
                            "Site for '%s' is not configured on this server - add to sites in admin",
                            host)
                        _WARNED[host] = True

                    site = by_settings(id_only=id_only)

        else:
            log.debug('app cache not ready')
            site = by_settings(id_only=id_only)