class SnippetBundle(object): """ Group of snippets to be sent to a particular client configuration. """ def __init__(self, client): self.client = client self.storage = OverwriteStorage() self._snippets = None @property def key(self): """A unique key for this bundle as a sha1 hexdigest.""" # Key should consist of snippets that are in the bundle plus any # properties of the client that may change the snippet code # being sent. key_properties = [snippet.id for snippet in self.snippets] key_properties.extend([ self.client.startpage_version, self.client.locale, ]) key_string = u'_'.join(unicode(prop) for prop in key_properties) return hashlib.sha1(key_string).hexdigest() @property def cache_key(self): return u'bundle_' + self.key @property def expired(self): """ If True, the code for this bundle should be re-generated before use. """ return not cache.get(self.cache_key) @property def filename(self): return u'bundles/{0}.html'.format(self.key) @property def url(self): return self.storage.url(self.filename) @property def snippets(self): # Lazy-load snippets on first access. if self._snippets is None: self._snippets = (Snippet.cached_objects .filter(disabled=False) .match_client(self.client) .order_by('priority') .select_related('template') .filter_by_available()) return self._snippets def generate(self): """Generate and save the code for this snippet bundle.""" bundle_content = render_to_string('base/fetch_snippets.html', { 'snippets': self.snippets, 'client': self.client, 'locale': self.client.locale, }) self.storage.save(self.filename, ContentFile(bundle_content)) cache.set(self.cache_key, True, settings.SNIPPET_BUNDLE_TIMEOUT)
class SnippetBundle(object): """ Group of snippets to be sent to a particular client configuration. """ def __init__(self, client): self.client = client self.storage = OverwriteStorage() self._snippets = None @property def key(self): """A unique key for this bundle as a sha1 hexdigest.""" # Key should consist of snippets that are in the bundle plus any # properties of the client that may change the snippet code # being sent. key_properties = ['{id}-{date}'.format(id=snippet.id, date=snippet.modified.isoformat()) for snippet in self.snippets] key_properties.extend([ self.client.startpage_version, self.client.locale, ]) key_string = u'_'.join(unicode(prop) for prop in key_properties) return hashlib.sha1(key_string).hexdigest() @property def cache_key(self): return u'bundle_' + self.key @property def expired(self): """ If True, the code for this bundle should be re-generated before use. """ return not cache.get(self.cache_key) @property def filename(self): return u'bundles/bundle_{0}.html'.format(self.key) @property def url(self): return self.storage.url(self.filename) @property def snippets(self): # Lazy-load snippets on first access. if self._snippets is None: self._snippets = (Snippet.cached_objects .filter(disabled=False) .match_client(self.client) .order_by('priority') .select_related('template') .prefetch_related('countries', 'exclude_from_search_providers') .filter_by_available()) return self._snippets def generate(self): """Generate and save the code for this snippet bundle.""" bundle_content = render_to_string('base/fetch_snippets.html', { 'snippets': self.snippets, 'client': self.client, 'locale': self.client.locale, }) if isinstance(bundle_content, unicode): bundle_content = bundle_content.encode('utf-8') self.storage.save(self.filename, ContentFile(bundle_content)) cache.set(self.cache_key, True, settings.SNIPPET_BUNDLE_TIMEOUT)
class SnippetBundle(object): """ Group of snippets to be sent to a particular client configuration. """ def __init__(self, client): self.client = client self.storage = OverwriteStorage() self._snippets = None @property def key(self): """A unique key for this bundle as a sha1 hexdigest.""" # Key should consist of snippets that are in the bundle plus any # properties of the client that may change the snippet code # being sent. key_properties = [ '{id}-{date}'.format(id=snippet.id, date=snippet.modified.isoformat()) for snippet in self.snippets ] key_properties.extend([ self.client.startpage_version, self.client.locale, self.client.channel, SNIPPET_JS_TEMPLATE_HASH, SNIPPET_CSS_TEMPLATE_HASH, SNIPPET_FETCH_TEMPLATE_HASH, ]) key_string = u'_'.join(unicode(prop) for prop in key_properties) return hashlib.sha1(key_string).hexdigest() @property def cache_key(self): return u'bundle_' + self.key @property def expired(self): """ If True, the code for this bundle should be re-generated before use. """ return not cache.get(self.cache_key) @property def filename(self): return u'bundles/bundle_{0}.html'.format(self.key) @property def url(self): bundle_url = self.storage.url(self.filename) site_url = getattr(settings, 'CDN_URL', settings.SITE_URL) full_url = urljoin(site_url, bundle_url) return full_url @property def snippets(self): # Lazy-load snippets on first access. if self._snippets is None: self._snippets = (Snippet.cached_objects.filter( disabled=False).match_client(self.client).order_by( 'priority').select_related('template').prefetch_related( 'countries', 'exclude_from_search_providers').filter_by_available()) return self._snippets def generate(self): """Generate and save the code for this snippet bundle.""" bundle_content = render_to_string( 'base/fetch_snippets.html', { 'snippet_ids': [snippet.id for snippet in self.snippets], 'snippets_json': json.dumps( [s.to_dict() for s in self.snippets]), 'client': self.client, 'locale': self.client.locale, 'settings': settings, }) if isinstance(bundle_content, unicode): bundle_content = bundle_content.encode('utf-8') self.storage.save(self.filename, ContentFile(bundle_content)) cache.set(self.cache_key, True, settings.SNIPPET_BUNDLE_TIMEOUT)