def technical_500_response(request, exc_type, exc_value, tb): """ Create a technical server error response. The last three arguments are the values returned from sys.exc_info() and friends. """ frames = [] while tb is not None: filename = tb.tb_frame.f_code.co_filename function = tb.tb_frame.f_code.co_name lineno = tb.tb_lineno - 1 pre_context_lineno, pre_context, context_line, post_context = _get_lines_from_file(filename, lineno, 7) frames.append( { "tb": tb, "filename": filename, "function": function, "lineno": lineno, "vars": tb.tb_frame.f_locals, "id": id(tb), "pre_context": pre_context, "context_line": context_line, "post_context": post_context, "pre_context_lineno": pre_context_lineno, } ) tb = tb.tb_next # Turn the settings module into a dict, filtering out anything that # matches HIDDEN_SETTINGS along the way. settings_dict = {} for k in dir(settings): if k.isupper(): if HIDDEN_SETTINGS.search(k): settings_dict[k] = "********************" else: settings_dict[k] = getattr(settings, k) t = Template(TECHNICAL_500_TEMPLATE) c = Context( { "exception_type": exc_type.__name__, "exception_value": exc_value, "frames": frames, "lastframe": frames[-1], "request": request, "request_protocol": os.environ.get("HTTPS") == "on" and "https" or "http", "settings": settings_dict, } ) return HttpResponseServerError(t.render(c))
def technical_404_response(request, exception): """ Create a technical 404 error response. The exception should be the Http404 exception. """ try: tried = exception.args[0]["tried"] except (IndexError, TypeError): tried = [] t = Template(TECHNICAL_404_TEMPLATE) c = Context( { "root_urlconf": settings.ROOT_URLCONF, "urlpatterns": tried, "reason": str(exception), "request": request, "request_protocol": os.environ.get("HTTPS") == "on" and "https" or "http", "settings": dict([(k, getattr(settings, k)) for k in dir(settings) if k.isupper()]), } ) return HttpResponseNotFound(t.render(c))
def render(self, context): from django.conf.settings import DEBUG if not include_is_allowed(self.filepath): if DEBUG: return "[Didn't have permission to include file]" else: return '' # Fail silently for invalid includes. try: fp = open(self.filepath, 'r') output = fp.read() fp.close() except IOError: output = '' if self.parsed: try: t = Template(output) return t.render(context) except (TemplateSyntaxError, e): if DEBUG: return "[Included template had syntax error: %s]" % e else: return '' # Fail silently for invalid included templates. return output
def get_feed(self, url=None): """ Returns a feedgenerator.DefaultFeed object, fully populated, for this feed. Raises FeedDoesNotExist for invalid parameters. """ if url: try: obj = self.get_object(url.split('/')) except (AttributeError, ObjectDoesNotExist): raise FeedDoesNotExist else: obj = None current_site = sites.get_current() link = self.__get_dynamic_attr('link', obj) link = add_domain(current_site.domain, link) feed = self.feed_type( title = self.__get_dynamic_attr('title', obj), link = link, description = self.__get_dynamic_attr('description', obj), language = LANGUAGE_CODE.decode(), feed_url = add_domain(current_site, self.feed_url), author_name = self.__get_dynamic_attr('author_name', obj), author_link = self.__get_dynamic_attr('author_link', obj), author_email = self.__get_dynamic_attr('author_email', obj), ) try: title_template = loader.get_template('feeds/%s_title' % self.slug) except TemplateDoesNotExist: title_template = Template('{{ obj }}') try: description_template = loader.get_template('feeds/%s_description' % self.slug) except TemplateDoesNotExist: description_template = Template('{{ obj }}') for item in self.__get_dynamic_attr('items', obj): link = add_domain(current_site.domain, self.__get_dynamic_attr('item_link', item)) enc = None enc_url = self.__get_dynamic_attr('item_enclosure_url', item) if enc_url: enc = feedgenerator.Enclosure( url = enc_url.decode('utf-8'), length = str(self.__get_dynamic_attr('item_enclosure_length', item)).decode('utf-8'), mime_type = self.__get_dynamic_attr('item_enclosure_mime_type', item).decode('utf-8'), ) author_name = self.__get_dynamic_attr('item_author_name', item) if author_name is not None: author_email = self.__get_dynamic_attr('item_author_email', item) author_link = self.__get_dynamic_attr('item_author_link', item) else: author_email = author_link = None feed.add_item( title = title_template.render(Context({'obj': item, 'site': current_site})).decode('utf-8'), link = link, description = description_template.render(Context({'obj': item, 'site': current_site})).decode('utf-8'), unique_id = link, enclosure = enc, pubdate = self.__get_dynamic_attr('item_pubdate', item), author_name = author_name, author_email = author_email, author_link = author_link, ) return feed
try: import docutils except ImportError: docutils = None # simple examples 'cause this isn't actually testing the markup, just # that the filters work as advertised ### test textile textile_content = """Paragraph 1 Paragraph 2 with "quotes" and @code@""" t = Template("{{ textile_content|textile }}") rendered = t.render(Context(locals())).strip() if textile: assert rendered == """<p>Paragraph 1</p> <p>Paragraph 2 with “quotes” and <code>code</code></p>""" else: assert rendered == textile_content ### test markdown markdown_content = """Paragraph 1 ## An h2""" t = Template("{{ markdown_content|markdown }}")