def test_cached_with_unicode(self): u = ":".join(map(encoding.smart_str, [u"תיאור אוסף"])) obj = mock.Mock() obj.query_key.return_value = u"xxx" obj.flush_key.return_value = "key" f = lambda: 1 eq_(caching.cached_with(obj, f, "adf:%s" % u), 1)
def view(request, func): """ This isn't called directly by anything in urls.py. Since all the views in this module are quite similar, each function marked by @section just returns the queryset we should operate on. The rest of the structure is the same. """ qs = func(request).exclude(type=amo.ADDON_PERSONA).distinct() date_ = date.today() form = CategoryForm(request) if form.is_valid(): date_ = form.cleaned_data["date"] or date_ category = form.cleaned_data["category"] if category: qs = qs.filter(categories__slug=category) addons = amo.utils.paginate(request, qs, per_page=75) q = addons.object_list cache_key = "%s%s" % (q.query, date_) f = lambda: attach_stats(request, q, date_) addons.object_list = cached_with(q, f, cache_key) c = {"addons": addons, "section": func.__name__, "query": q, "form": form, "sections": get_sections()} return jingo.render(request, "nick/featured.html", c)
def process_request(self, list_type='recommended', addon_type='ALL', limit=10, platform='ALL', version=None): """ Find a list of new or featured add-ons. Filtering is done in Python for cache-friendliness and to avoid heavy queries. """ limit = min(MAX_LIMIT, int(limit)) APP, platform = self.request.APP, platform.lower() qs = Addon.objects.listed(APP) shuffle = True if list_type in ('by_adu', 'featured'): qs = qs.exclude(type=amo.ADDON_PERSONA) if list_type == 'newest': new = date.today() - timedelta(days=NEW_DAYS) addons = (qs.filter(created__gte=new) .order_by('-created'))[:limit + BUFFER] elif list_type == 'by_adu': addons = qs.order_by('-average_daily_users')[:limit + BUFFER] shuffle = False # By_adu is an ordered list. elif list_type == 'hotness': # Filter to type=1 so we hit visible_idx. Only extensions have a # hotness index right now so this is not incorrect. addons = (qs.filter(type=amo.ADDON_EXTENSION) .order_by('-hotness'))[:limit + BUFFER] shuffle = False else: ids = Addon.featured_random(APP, self.request.LANG) addons = manual_order(qs, ids[:limit + BUFFER], 'addons.id') shuffle = False args = (addon_type, limit, APP, platform, version, shuffle) f = lambda: self._process(addons, *args) return cached_with(addons, f, map(encoding.smart_str, args))
def test_cached_with_unicode(self): u = ':'.join(map(encoding.smart_str, [u'תיאור אוסף'])) obj = mock.Mock() obj.query_key.return_value = u'xxx' obj.flush_key.return_value = 'key' f = lambda: 1 eq_(caching.cached_with(obj, f, 'adf:%s' % u), 1)
def get_addons(self): addons = self.collection.addons.filter(status=amo.STATUS_PUBLIC) kw = dict(addon_type='ALL', limit=self.limit, app=self.request.APP, platform=self.platform, version=self.version, compat_mode=self.compat_mode) f = lambda: addon_filter(addons, **kw) return caching.cached_with(addons, f, repr(kw))
def version_detail(request, addon_id, version_num): addon = get_object_or_404(Addon.objects.valid(), pk=addon_id) qs = (addon.versions.filter(files__status__in=amo.VALID_STATUSES) .distinct().order_by('-created')) # Use cached_with since values_list won't be cached. f = lambda: _find_version_page(qs, addon_id, version_num) return caching.cached_with(qs, f, 'vd:%s:%s' % (addon_id, version_num))
def version_detail(request, addon, version_num): qs = addon.versions.filter(files__status__in=amo.VALID_STATUSES).distinct().order_by("-created") # Use cached_with since values_list won't be cached. def f(): return _find_version_page(qs, addon, version_num) return caching.cached_with(qs, f, "vd:%s:%s" % (addon.id, version_num))
def test_cached_with_unicode(self): u = encoding.smart_bytes('\\u05ea\\u05d9\\u05d0\\u05d5\\u05e8 ' '\\u05d0\\u05d5\\u05e1\\u05e3') obj = mock.Mock() obj.query_key.return_value = 'xxx' obj.flush_key.return_value = 'key' f = lambda: 1 eq_(base.cached_with(obj, f, 'adf:%s' % u), 1)
def test_cached_with_bad_object(self): """cached_with shouldn't fail if the object is missing a cache key.""" counter = mock.Mock() def f(): counter() return counter.call_count eq_(caching.cached_with([], f, 'key'), 1)
def finalize_response(self, request, response, *args, **kwargs): """ Returns a cached response if any, prior to rendering. """ response = super(ListView, self).finalize_response(request, response, args, kwargs) return cached_with(response.addons, lambda: response.render(), map(encoding.smart_str, args))
def version_detail(request, addon, version_num): beta = amo.VERSION_BETA.search(version_num) qs = _version_list_qs(addon, beta=beta) # Use cached_with since values_list won't be cached. def f(): return _find_version_page(qs, addon, version_num, beta=beta) return caching.cached_with(qs, f, 'vd:%s:%s' % (addon.id, version_num))
def creatured(cls): """Get a dict of {addon: [app,..]} for all creatured add-ons.""" qs = cls.objects.filter(feature=True) f = lambda: list(qs.values_list('addon', 'category__application')) vals = caching.cached_with(qs, f, 'creatured') rv = {} for addon, app in vals: rv.setdefault(addon, []).append(app) return rv
def test_cached_with_unicode(self): u = encoding.smart_bytes("\\u05ea\\u05d9\\u05d0\\u05d5\\u05e8 " "\\u05d0\\u05d5\\u05e1\\u05e3") obj = mock.Mock() obj.query_key.return_value = "xxx" obj.flush_key.return_value = "key" def f(): return 1 eq_(base.cached_with(obj, f, "adf:%s" % u), 1)
def blocked(cls, name): """ Check to see if a given name is in the (cached) blacklist. Return True if the name contains one of the blacklisted terms. """ name = name.lower() qs = cls.objects.all() f = lambda: [n.lower() for n in qs.values_list('name', flat=True)] blacklist = caching.cached_with(qs, f, 'blocked') return any(n in name for n in blacklist)
def blocked(cls, domain): qs = cls.objects.all() f = lambda: list(qs.values_list('domain', flat=True)) blacklist = caching.cached_with(qs, f, 'blocked') # because there isn't a good way to know if the domain is # "example.com" or "example.co.jp", we'll re-construct it... # so if it's "bad.example.co.jp", the following check the # values in ['bad.example.co.jp', 'example.co.jp', 'co.jp'] x = domain.lower().split('.') for d in ['.'.join(x[y:]) for y in range(len(x) - 1)]: if d in blacklist: return True
def test_cached_with(self): counter = mock.Mock() def expensive(): counter() return counter.call_count a = Addon.objects.get(id=1) f = lambda: base.cached_with(a, expensive, 'key') # Only gets called once. eq_(f(), 1) eq_(f(), 1) # Switching locales does not reuse the cache. old_locale = translation.get_language() translation.activate('fr') eq_(f(), 2) # Called again after flush. a.save() eq_(f(), 3) translation.activate(old_locale) eq_(f(), 4) counter.reset_mock() q = Addon.objects.filter(id=1) f = lambda: base.cached_with(q, expensive, 'key') # Only gets called once. eq_(f(), 1) eq_(f(), 1) # Called again after flush. list(q)[0].save() eq_(f(), 2) eq_(f(), 2)
def get_addons(self): addons = self.collection.addons.public() kw = { 'addon_type': 'ALL', 'limit': self.limit, 'app': self.request.APP, 'platform': self.platform, 'version': self.version, 'compat_mode': self.compat_mode } def _filter(): return addon_filter(addons, **kw) return caching.cached_with(addons, _filter, repr(kw))
def is_featured(self, app, lang): """is add-on globally featured for this app and language?""" qs = Addon.objects.featured(app) def _features(): vals = qs.extra(select={'locale': 'features.locale'}) d = collections.defaultdict(list) for id, locale in vals.values_list('id', 'locale'): d[id].append(locale) return dict(d) features = caching.cached_with(qs, _features, 'featured:%s' % app.id) if self.id in features: for locale in (None, '', lang): if locale in features[self.id]: return True return False
def process_request(self, list_type='recommended', addon_type='ALL', limit=10, platform='ALL', version=None, compat_mode='strict'): """ Find a list of new or featured add-ons. Filtering is done in Python for cache-friendliness and to avoid heavy queries. """ limit = min(MAX_LIMIT, int(limit)) APP, platform = self.request.APP, platform.lower() qs = Addon.objects.listed(APP) shuffle = True if list_type in ('by_adu', 'featured'): qs = qs.exclude(type=amo.ADDON_PERSONA) if list_type == 'newest': new = date.today() - timedelta(days=NEW_DAYS) addons = (qs.filter( created__gte=new).order_by('-created'))[:limit + BUFFER] elif list_type == 'by_adu': addons = qs.order_by('-average_daily_users')[:limit + BUFFER] shuffle = False # By_adu is an ordered list. elif list_type == 'hotness': # Filter to type=1 so we hit visible_idx. Only extensions have a # hotness index right now so this is not incorrect. addons = (qs.filter( type=amo.ADDON_EXTENSION).order_by('-hotness'))[:limit + BUFFER] shuffle = False else: ids = Addon.featured_random(APP, self.request.LANG) addons = manual_order(qs, ids[:limit + BUFFER], 'addons.id') shuffle = False args = (addon_type, limit, APP, platform, version, compat_mode, shuffle) def f(): return self._process(addons, *args) return cached_with(addons, f, map(force_bytes, args))
def process_request(self, list_type='recommended', addon_type='ALL', limit=10, platform='ALL', version=None): """ Find a list of new or featured add-ons. Filtering is done in Python for cache-friendliness and to avoid heavy queries. """ limit = min(MAX_LIMIT, int(limit)) APP, platform = self.request.APP, platform.lower() qs = Addon.objects.listed(APP) shuffle = True if list_type == 'newest': new = date.today() - timedelta(days=NEW_DAYS) addons = (qs.filter(created__gte=new) .order_by('-created'))[:limit + BUFFER] elif list_type == 'by_adu': addons = qs.order_by('-average_daily_users')[:limit + BUFFER] shuffle = False # By_adu is an ordered list. else: addons = Addon.objects.featured(APP).distinct() & qs args = (addon_type, limit, APP, platform, version, shuffle) f = lambda: self._process(addons, *args) return cached_with(addons, f, map(str, args))
def f(): return base.cached_with(q, expensive, "key")
def blocked(cls, username): """Check to see if a username is in the (cached) blacklist.""" qs = cls.objects.all() f = lambda: [u.lower() for u in qs.values_list('username', flat=True)] blacklist = caching.cached_with(qs, f, 'blocked') return username.lower() in blacklist
def version_detail(request, addon, version_num): qs = (addon.versions.filter(files__status__in=amo.VALID_STATUSES) .distinct().order_by('-created')) # Use cached_with since values_list won't be cached. f = lambda: _find_version_page(qs, addon, version_num) return caching.cached_with(qs, f, 'vd:%s:%s' % (addon.id, version_num))
def get_addons(self): addons = self.collection.addons.all() kw = dict(addon_type='ALL', limit=self.limit, app=self.request.APP, platform=self.platform, version=self.version, shuffle=True) f = lambda: addon_filter(addons, **kw) return caching.cached_with(addons, f, repr(kw))
def blocked(cls, name): """Check to see if a given name is in the (cached) blacklist.""" qs = cls.objects.all() f = lambda: [n.lower() for n in qs.values_list("name", flat=True)] blacklist = caching.cached_with(qs, f, "blocked") return name.lower() in blacklist
def f(): return base.cached_with(q, expensive, 'key') # Only gets called once. self.assertEqual(f(), 1)
def f(): return base.cached_with(q, expensive, 'key')