def results(request): params = rewrite_params(request).copy() del params['_escaped_fragment_'] if not len(params): return HttpResponseNotFound() if not params.has_key('page'): params['page'] = 1 if not params.has_key('sorting'): params['sorting'] = '_score' query = {} for param in params: query[param] = urllib2.unquote(unicode(params[param])) search = SearchHandler().create(InjectRequest(query)) loader = Loader() items = pystache.render( loader.load_template_source('pages/search/items.html')[0], search['results'] ) facets = '' if search.has_key('facets') and search['facets'].has_key('company.facet'): facets = pystache.render( loader.load_template_source('pages/search/facets.html')[0], {'facets' : search['facets']['company.facet']} ) term = '' if (query.has_key('what') and len(query['what'])) and (query.has_key('where') and len(query['where'])): term = '%s / %s' % (query['what'].lower(), query['where'].lower()) elif query.has_key('what'): term = query['what'].lower() elif query.has_key('where'): term = query['where'].lower() total = 0 if len(search['results']): total = search['results']['total'] pagination = '' if len(search['results']) and len(search['results']['pagination']) > 1: pagination = pystache.render( loader.load_template_source('pages/search/pagination.html')[0], {'pagination' : search['results']['pagination'] } ) content = render_to_string('pages/search.html', { 'term': term, 'total': total, 'facets': facets, 'items': items, 'pagination': pagination }, context_instance=RequestContext(request)) return direct_to_template(request, 'base.html', { 'search': content, 'job_count': count_documents() })
def write_override_diff(diff_filename='template_overrides.diff'): # Fetch latest version of each template override from database tos = TemplateOverride.objects.all().order_by('-version') newest_tos = {} for override in tos: if override.name not in newest_tos: newest_tos[override.name] = override print '%s (version %d)' % (override.name, override.version) # Fetch the template file in the working copy orig_templates = {} template_loader = Loader() for key in newest_tos: try: orig_templates[key] = template_loader.load_template_source(key) except: print 'Could not load original template %s' % key print 'Got %d overrides and %d original versions' % (len(newest_tos.keys()), len(orig_templates.keys())) # Compute diffs between original and overridden templates diff_filename = 'template_overrides.diff' file_out = open(diff_filename, 'w') for key in orig_templates: str1 = list([x.rstrip() for x in orig_templates[key][0].split('\n')]) str2 = list([x.rstrip() for x in newest_tos[key].content.split('\n')]) # print '\n'.join(str1) # print '\n'.join(str2) diff = difflib.unified_diff(str1, str2, key, key, lineterm='') for line in diff: file_out.write(line.encode('ascii', 'ignore') + '\n') file_out.close() print 'Wrote differences to %s' % diff_filename
def get_aliastemplate(): """Fetch template for displaying ifalias format as help to user""" templatepath = join(sysconfdir, "portadmin") templatename = "aliasformat.html" loader = Loader() rawdata, _ = loader.load_template_source(templatename, [templatepath]) tmpl = django.template.Template(rawdata) return tmpl
def upload_js(): """ return a jQuery Templates ref : http://api.jquery.com/category/plugins/templates/ """ path = os.path.join(settings.ROOT, 'templates/materials') loader = Loader() string, filename = loader.load_template_source('material_uploader_template.html', (path, )) return string
def raw_base_template(obj): """ Gets the base body template for an object. :param obj: A myblocks object with a valid base_template. :return: The base_template as a string. """ loader = Loader() return loader.load_template_source(obj.base_template)[0]
def raw_base_head(obj): """ Gets the base head template for an object if one exists. :param obj: A myblocks object. :return: If the object has a base_head attribute, a string containing the base_head template. Otherwise a blank string. """ if obj.base_head: loader = Loader() return loader.load_template_source(obj.base_head)[0] return ''
def load_template_source(self, template_name, template_dirs=None): if ":" not in template_name: raise TemplateDoesNotExist() app_name, template_name = template_name.split(":", 1) try: app = get_app(app_name) except ImproperlyConfigured: raise TemplateDoesNotExist() else: app_dir = path.dirname(app.__file__) app_templ_dir = path.join(app_dir, 'templates') if not path.isdir(app_templ_dir): raise TemplateDoesNotExist() return FileSystemLoader.load_template_source( self, template_name, template_dirs=[app_templ_dir])
def do_include_raw(parser, token): """ Performs a template include without parsing the context, just dumps the template in. Source: http://djangosnippets.org/snippets/1684/ """ bits = token.split_contents() if len(bits) != 2: raise template.TemplateSyntaxError( "%r tag takes one argument: the name of the template " "to be included" % bits[0] ) template_name = bits[1] if template_name[0] in ('"', "'") and template_name[-1] == template_name[0]: template_name = template_name[1:-1] template_loader = Loader() source, path = template_loader.load_template_source(template_name) return template.TextNode(source)
def get_dependencies(self, path): """ Finds dependencies hierarchically based on the included files. """ template = path.replace(os.sep, "/") logger.debug("Loading template [%s] and preprocessing" % template) loader = Loader() contents, file_name = loader.load_template_source(template) if self.preprocessor: resource = self.site.content.resource_from_relative_path(template) if resource: contents = self.preprocessor(resource, contents) or contents parsed_template = Template(contents) extend_nodes = parsed_template.nodelist.get_nodes_by_type(ExtendsNode) tpls = [node.parent_name for node in extend_nodes] deps = [] for dep in tpls: deps.append(dep) if dep: deps.extend(self.get_dependencies(dep)) return list(set(deps))
def load_template_source(self, template_name, template_dirs=None): if ":" not in template_name: raise TemplateDoesNotExist() app_name, template_name = template_name.split(":", 1) try: app = get_app(app_name) except ImproperlyConfigured: raise TemplateDoesNotExist() else: if path.basename(app.__file__).startswith('__init__'): # When "app.models" is a directory, app.__file__ will # be app/models/__init.py. app_dir = path.dirname(path.dirname(app.__file__)) else: app_dir = path.dirname(app.__file__) app_templ_dir = path.join(app_dir, 'templates') if not path.isdir(app_templ_dir): raise TemplateDoesNotExist() return FileSystemLoader.load_template_source( self, template_name, template_dirs=[app_templ_dir])
def js_template(parser, token): """ Include js template """ bits = token.split_contents() if len(bits) != 3: raise TemplateSyntaxError, "%r tag takes two arguments: the name of the template to be included, the id of template" % bits[0] template_name = bits[1] if template_name[0] in ('"', "'") and template_name[-1] == template_name[0]: template_name = template_name[1:-1] template_id = bits[2] if template_id[0] in ('"', "'") and template_id[-1] == template_id[0]: template_id = template_id[1:-1] loader = Loader() source, path = loader.load_template_source(template_name) begin = "<script id=\"%sTpl\" type=\"text/template\">\n" % template_id end = "\n</script>" return template.TextNode(begin + source + end)
def write_override_diff(diff_filename='template_overrides.diff'): # Fetch latest version of each template override from database tos = TemplateOverride.objects.all().order_by('-version') newest_tos = {} for override in tos: if override.name not in newest_tos: newest_tos[override.name] = override print '%s (version %d)' % (override.name, override.version) # Fetch the template file in the working copy orig_templates = {} template_loader = Loader() for key in newest_tos: try: orig_templates[key] = template_loader.load_template_source(key) except: print 'Could not load original template %s' % key print 'Got %d overrides and %d original versions' % (len( newest_tos.keys()), len(orig_templates.keys())) # Compute diffs between original and overridden templates diff_filename = 'template_overrides.diff' file_out = open(diff_filename, 'w') for key in orig_templates: str1 = list([x.rstrip() for x in orig_templates[key][0].split('\n')]) str2 = list([x.rstrip() for x in newest_tos[key].content.split('\n')]) # print '\n'.join(str1) # print '\n'.join(str2) diff = difflib.unified_diff(str1, str2, key, key, lineterm='') for line in diff: file_out.write(line.encode('ascii', 'ignore') + '\n') file_out.close() print 'Wrote differences to %s' % diff_filename