示例#1
0
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)

    ck = cache_key('Setting', siteid, group, key)
    setting = None
    try:
        setting = cache_get(ck)

    except NotCachedError:
        if apps.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)

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

    return setting
示例#2
0
    def setCCV(self, ccv):
        """Put the CCV in the cache, don't save it for security/legal reasons."""
        if not self.encrypted_cc:
            raise ValueError(
                "CreditCardDetail expecting a credit card number to be stored before storing CCV"
            )

        caching.cache_set(self.encrypted_cc, skiplog=True, length=60 * 60, value=ccv)
示例#3
0
def bestsellers(count):
    """Look up the bestselling products and return in a list"""        
    cached = False
    try:
        pks = cache_get('BESTSELLERS', count=count)
        pks = [long(pk) for pk in pks.split(',')]
        productdict = Product.objects.in_bulk(pks)
        #log.debug(productdict)
        sellers = []
        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
            
        cached = True
        log.debug('retrieved bestselling products from cache')
    
    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
示例#4
0
    def testCacheGetOK(self):
        one = [1, 2, 3, 4]
        caching.cache_set('ok', value=one, length=2)
        two = caching.cache_get('ok')
        self.assertEqual(one, two)

        time.sleep(5)
        try:
            three = caching.cache_get('ok')
            self.fail('should have raised NotCachedError, got %s' % three)
        except caching.NotCachedError:
            pass
示例#5
0
def find_by_slug(cls, groupkey, slug):
    """A helper function to look up an object by slug"""
    ob = None
    try:
        ob = caching.cache_get(groupkey, slug)
    except caching.NotCachedError, e:
        try: 
            ob = cls.objects.get(slug__exact=slug)
            caching.cache_set(e.key, value=ob)

        except cls.DoesNotExist:
            log.debug("No such %s: %s", groupkey, slug)
示例#6
0
文件: models.py 项目: abrar78/satchmo
def find_by_slug(cls, groupkey, slug):
    """A helper function to look up an object by slug"""
    ob = None
    try:
        ob = caching.cache_get(groupkey, slug)
    except caching.NotCachedError, e:
        try:
            ob = cls.objects.get(slug__exact=slug)
            caching.cache_set(e.key, value=ob)

        except cls.DoesNotExist:
            log.debug("No such %s: %s", groupkey, slug)
示例#7
0
 def testCacheGetOK(self):
     one = [1,2,3,4]
     caching.cache_set('ok', value=one, length=2)
     two = caching.cache_get('ok')
     self.assertEqual(one, two)
     
     time.sleep(5)
     try:
         three = caching.cache_get('ok')
         self.fail('should have raised NotCachedError, got %s' % three)
     except caching.NotCachedError:
         pass
示例#8
0
    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 = caching.cache_get(
                "{}-{}".format(self.__class__.__name__, self.id),
                site=site,
                trans=attr,
                lang=language_code,
            )
        except caching.NotCachedError as 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
            log.info(nce.key)
            caching.cache_set(nce.key, value=trans)

        return trans
示例#9
0
    def testDisable(self):
        caching.cache_set('disabled', value=False)
        v = caching.cache_get('disabled')
        self.assertEqual(v, False)

        caching.cache_enable(False)
        caching.cache_set('disabled', value=True)
        try:
            caching.cache_get('disabled')
            self.fail('should have raised NotCachedError')
        except caching.NotCachedError, nce:
            key = caching.cache_key('disabled')
            self.assertEqual(nce.key, key)
示例#10
0
文件: tests.py 项目: jimmcgaw/levicci
 def testDisable(self):
     caching.cache_set('disabled', value=False)
     v = caching.cache_get('disabled')
     self.assertEqual(v, False)
     
     caching.cache_enable(False)
     caching.cache_set('disabled', value=True)
     try:
         caching.cache_get('disabled')
         self.fail('should have raised NotCachedError')
     except caching.NotCachedError, nce:
         key = caching.cache_key('disabled')
         self.assertEqual(nce.key, key)
示例#11
0
def find_by_key(cls, groupkey, key, raises=False):
    """A helper function to look up an object by key"""
    ob = None
    try:
        ob = caching.cache_get(groupkey, key)
    except caching.NotCachedError, e:
        try: 
            ob = cls.objects.get(key__exact=key)
            caching.cache_set(e.key, value=ob)

        except cls.DoesNotExist:
            log.debug("No such %s: %s", groupkey, key)
            if raises:
                raise
示例#12
0
def find_by_key(cls, groupkey, key, raises=False):
    """A helper function to look up an object by key"""
    ob = None
    try:
        ob = caching.cache_get(groupkey, key)
    except caching.NotCachedError, e:
        try: 
            ob = cls.objects.get(key__exact=key)
            caching.cache_set(e.key, value=ob)

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

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

        except cls.DoesNotExist:
            log.debug("No such %s: %s", groupkey, objectid)
            if raises:
                raise cls.DoesNotExist
示例#15
0
    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 = caching.cache_get([self.__class__.__name__, self.id],
                                      site=site,
                                      trans=attr,
                                      lang=language_code)
        except caching.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

            caching.cache_set(nce.key, value=trans)
示例#16
0
 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 = caching.cache_get("Config", site)
     except caching.NotCachedError, nce:
         try:
             shop_config = self.get(site__id__exact=site)
             caching.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()
示例#17
0
    def testDisable(self):
        caching.cache_set("disabled", value=False)
        v = caching.cache_get("disabled")
        self.assertEqual(v, False)

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

        caching.cache_enable()
        v2 = caching.cache_get("disabled")
        # should still be False, since the cache was disabled
        self.assertEqual(v2, False)
示例#18
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 = caching.cache_get('product_count', category, variations)
    except caching.NotCachedError:
        if not category:
            ct = Product.objects.active_by_site(variations=variations).count()
        else:
            ct = category.active_products(include_children=True, variations=variations).count()

        caching.cache_set('product_count', category, args, value=ct)
    return ct
示例#19
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 = caching.cache_get('product_count', category, variations)
    except caching.NotCachedError:
        if not category:
            ct = Product.objects.active_by_site(variations=variations).count()
        else:
            ct = category.active_products(include_children=True,
                                          variations=variations).count()

        caching.cache_set('product_count', category, args, value=ct)
    return ct
示例#20
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, 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)
示例#21
0
    def save(self, force_insert=False, force_update=False):
        caching.cache_delete("Config", self.site.id)
        # ensure the default country is in shipping countries
        mycountry = self.country
        
        if mycountry:
            if not self.sales_country:
                log.debug("%s: No sales_country set, adding country of store, '%s'", self, mycountry)
                self.sales_country = mycountry
        
# This code doesn't work when creating a new site. At the time of creation, all of the necessary relationships
# aren't setup. I modified the load_store code so that it would create this relationship manually when running 
# with sample data. This is a bit of a django limitation so I'm leaving this in here for now. - CBM
#            salescountry = self.sales_country
#            try:
#                need = self.shipping_countries.get(pk=salescountry.pk)
#            except Country.DoesNotExist:
#                log.debug("%s: Adding default country '%s' to shipping countries", self, salescountry.iso2_code)
#                self.shipping_countries.add(salescountry)
        else:
            log.warn("%s: has no country set", self)
            
        super(Config, self).save(force_insert=force_insert, force_update=force_update)
        caching.cache_set("Config", self.site.id, value=self)
示例#22
0
    def testDelete(self):
        caching.cache_set('del', value=True)

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

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

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

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

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

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

        # kill the rest
        killed = caching.cache_delete('del', children=True)
        self.assertFalse(caching.cache_get('del', default=False))
        for x in range(0, 10):
            self.assertFalse(caching.cache_get('del', 'x', x, default=False))
            for y in range(0, 5):
                self.assertFalse(
                    caching.cache_get('del', 'x', x, 'y', y, default=False))
示例#23
0
    def testDelete(self):
        caching.cache_set("del", value=True)

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

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

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

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

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

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

        # kill the rest
        killed = caching.cache_delete("del", children=True)
        self.assertFalse(caching.cache_get("del", default=False))
        for x in range(0, 10):
            self.assertFalse(caching.cache_get("del", "x", x, default=False))
            for y in range(0, 5):
                self.assertFalse(
                    caching.cache_get("del", "x", x, "y", y, default=False)
                )
示例#24
0
    def testDelete(self):
        caching.cache_set('del', value=True)
        
        for x in range(0,10):
            caching.cache_set('del', 'x', x, value=True)
            for y in range(0,5):
                caching.cache_set('del', 'x', x, 'y', y, value=True)

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

        # try to delete just one
        killed = caching.cache_delete('del','x',1)
        self.assertEqual(["del::x::1"], killed)
        self.assertFalse(caching.cache_get('del', 'x', 1, default=False))
        
        # but the others are still there
        self.assert_(caching.cache_get('del', 'x', 2, default=False))

        # now kill all of del::x::1
        killed = caching.cache_delete('del','x', 1, children=True)
        for y in range(0,5):
            self.assertFalse(caching.cache_get('del', 'x', 1, 'y', y, default=False))
        
        # but del::x::2 and children are there
        self.assert_(caching.cache_get('del','x',2,'y',1, default=False))
        
        # kill the rest
        killed = caching.cache_delete('del', children=True)
        self.assertFalse(caching.cache_get('del',default=False))
        for x in range(0,10):
            self.assertFalse(caching.cache_get('del', 'x', x, default=False))
            for y in range(0,5):
                self.assertFalse(caching.cache_get('del', 'x', x, 'y', y, default=False))
示例#25
0
 def setCCV(self, ccv):
     """Put the CCV in the cache, don't save it for security/legal reasons."""
     if not self.encrypted_cc:
         raise ValueError('CreditCardDetail expecting a credit card number to be stored before storing CCV')
         
     caching.cache_set(self.encrypted_cc, skiplog=True, length=60*60, value=ccv)
示例#26
0
文件: models.py 项目: abrar78/satchmo
 def cache_set(self, *args, **kwargs):
     val = kwargs.pop('value', self)
     key = self.cache_key(*args, **kwargs)
     caching.cache_set(key, value=val)
示例#27
0
 def cache_set(self, *args, **kwargs):
     val = kwargs.pop('value', self)
     key = self.cache_key(*args, **kwargs)
     caching.cache_set(key, value=val)