def generate(self): """Generate and save the code for this snippet bundle.""" template = 'base/fetch_snippets.jinja' if self.client.startpage_version == '5': template = 'base/fetch_snippets_as.jinja' bundle_content = render_to_string( template, { '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, 'current_firefox_major_version': util.current_firefox_major_version(), }) if isinstance(bundle_content, unicode): bundle_content = bundle_content.encode('utf-8') if settings.BUNDLE_BROTLI_COMPRESS and self.client.startpage_version == '5': content_file = ContentFile(brotli.compress(bundle_content)) content_file.content_encoding = 'br' else: content_file = ContentFile(bundle_content) default_storage.save(self.filename, content_file) cache.set(self.cache_key, True, ONE_DAY)
def key(self): """A unique key for this bundle as a sha1 hexdigest.""" # Key should consist of snippets that are in the bundle. This part # accounts for all the properties sent by the Client, since the # self.snippets lists snippets are all filters and CMRs have been # applied. key_properties = [ '{id}-{date}-{templatedate}'.format(id=snippet.id, date=snippet.modified.isoformat(), templatedate=snippet.template.modified.isoformat()) for snippet in self.snippets] # Additional values used to calculate the key are the templates and the # variables used to render them besides snippets. key_properties.extend([ str(self.client.startpage_version), self.client.locale, util.current_firefox_major_version(), str(settings.BUNDLE_BROTLI_COMPRESS), ]) if self.client.startpage_version >= 5: key_properties.append(SNIPPET_FETCH_TEMPLATE_AS_HASH) else: key_properties.append(SNIPPET_FETCH_TEMPLATE_HASH) key_string = '_'.join(key_properties) return hashlib.sha1(key_string.encode('utf-8')).hexdigest()
def key(self): """A unique key for this bundle as a sha1 hexdigest.""" # Key should consist of snippets that are in the bundle. This part # accounts for all the properties sent by the Client, since the # self.snippets lists snippets are all filters and CMRs have been # applied. key_properties = [ '{id}-{date}-{templatedate}'.format( id=snippet.id, date=snippet.modified.isoformat(), templatedate=snippet.template.modified.isoformat()) for snippet in self.snippets ] # Additional values used to calculate the key are the templates and the # variables used to render them besides snippets. key_properties.extend([ self.client.startpage_version, self.client.locale, util.current_firefox_major_version(), str(settings.BUNDLE_BROTLI_COMPRESS), ]) if self.client.startpage_version == '5': key_properties.append(SNIPPET_FETCH_TEMPLATE_AS_HASH) else: key_properties.append(SNIPPET_FETCH_TEMPLATE_HASH) key_string = u'_'.join(key_properties) return hashlib.sha1(key_string.encode('utf-8')).hexdigest()
def generate(self): """Generate and save the code for this snippet bundle.""" template = 'base/fetch_snippets.jinja' if self.client.startpage_version == 5: template = 'base/fetch_snippets_as.jinja' bundle_content = render_to_string(template, { '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, 'current_firefox_major_version': util.current_firefox_major_version(), }) if isinstance(bundle_content, str): bundle_content = bundle_content.encode('utf-8') if (settings.BUNDLE_BROTLI_COMPRESS and self.client.startpage_version >= 5): content_file = ContentFile(brotli.compress(bundle_content)) content_file.content_encoding = 'br' else: content_file = ContentFile(bundle_content) default_storage.save(self.filename, content_file) cache.set(self.cache_key, True, ONE_DAY)
def preview_snippet(request): """ Build a snippet using info from the POST parameters, and preview that snippet on a mock about:home page. """ try: template_id = int(request.POST.get('template_id', None)) except (TypeError, ValueError): return HttpResponseBadRequest() template = get_object_or_none(SnippetTemplate, id=template_id) data = request.POST.get('data', None) # Validate that data is JSON. try: json.loads(data) except (TypeError, ValueError): data = None # If your parameters are wrong, I have no sympathy for you. if data is None or template is None: return HttpResponseBadRequest() # Build a snippet that isn't saved so we can render it. snippet = Snippet(template=template, data=data) if strtobool(request.POST.get('activity_stream', 'false')): template_name = 'base/preview_as.jinja' preview_client = Client('5', 'Firefox', '57.0', 'default', 'default', 'en-US', 'release', 'default', 'default', 'default') else: template_name = 'base/preview.jinja' preview_client = Client('4', 'Firefox', '24.0', 'default', 'default', 'en-US', 'release', 'default', 'default', 'default') skip_boilerplate = request.POST.get('skip_boilerplate', 'false') skip_boilerplate = strtobool(skip_boilerplate) if skip_boilerplate: template_name = 'base/preview_without_shell.jinja' return render( request, template_name, { 'snippets_json': json.dumps([snippet.to_dict()]), 'client': preview_client, 'preview': True, 'current_firefox_major_version': util.current_firefox_major_version(), })
def preview_snippet(request): """ Build a snippet using info from the POST parameters, and preview that snippet on a mock about:home page. """ try: template_id = int(request.POST.get('template_id', None)) except (TypeError, ValueError): return HttpResponseBadRequest() template = get_object_or_none(SnippetTemplate, id=template_id) data = request.POST.get('data', None) # Validate that data is JSON. try: json.loads(data) except (TypeError, ValueError): data = None # If your parameters are wrong, I have no sympathy for you. if data is None or template is None: return HttpResponseBadRequest() # Build a snippet that isn't saved so we can render it. snippet = Snippet(template=template, data=data) if strtobool(request.POST.get('activity_stream', 'false')): template_name = 'base/preview_as.jinja' preview_client = Client(5, 'Firefox', '57.0', 'default', 'default', 'en-US', 'release', 'default', 'default', 'default') else: template_name = 'base/preview.jinja' preview_client = Client(4, 'Firefox', '24.0', 'default', 'default', 'en-US', 'release', 'default', 'default', 'default') skip_boilerplate = request.POST.get('skip_boilerplate', 'false') skip_boilerplate = strtobool(skip_boilerplate) if skip_boilerplate: template_name = 'base/preview_without_shell.jinja' return render(request, template_name, { 'snippets_json': json.dumps([snippet.to_dict()]), 'client': preview_client, 'preview': True, 'current_firefox_major_version': util.current_firefox_major_version(), })
def show_snippet(request, snippet_id, uuid=False): preview_client = Client(4, 'Firefox', '24.0', 'default', 'default', 'en-US', 'release', 'default', 'default', 'default') if uuid: snippet = get_object_or_404(Snippet, uuid=snippet_id) else: snippet = get_object_or_404(Snippet, pk=snippet_id) if not snippet.published and not request.user.is_authenticated: raise Http404() template = 'base/preview.jinja' if snippet.on_startpage_5: template = 'base/preview_as.jinja' return render(request, template, { 'snippets_json': json.dumps([snippet.to_dict()]), 'client': preview_client, 'preview': True, 'current_firefox_major_version': util.current_firefox_major_version(), })
def show_snippet(request, snippet_id, uuid=False): preview_client = Client('4', 'Firefox', '24.0', 'default', 'default', 'en-US', 'release', 'default', 'default', 'default') if uuid: snippet = get_object_or_404(Snippet, uuid=snippet_id) else: snippet = get_object_or_404(Snippet, pk=snippet_id) if snippet.disabled and not request.user.is_authenticated(): raise Http404() template = 'base/preview.jinja' if snippet.on_startpage_5: template = 'base/preview_as.jinja' return render(request, template, { 'snippets_json': json.dumps([snippet.to_dict()]), 'client': preview_client, 'preview': True, 'current_firefox_major_version': util.current_firefox_major_version(), })