def get_template(template_name, device=None, log=None): """ Returns a compiled Template object for the given template name, handling template inheritance recursively. """ source, origin = None, None exceptions_list = [] if device: device_path_list = get_device_template_paths(device, template_name) for device_path in device_path_list: try: source, origin = find_template_source(device_path) except TemplateDoesNotExist: exceptions_list.append(device_path) if source: template_log(device_path, log=log) break if not source: try: source, origin = find_template_source(template_name) template_log(template_name, log=log) except TemplateDoesNotExist: exceptions_list.append(template_name) raise TemplateDoesNotExist, exceptions_list template = get_template_from_string(source, origin, template_name) return template
def get_parent(self, context): if self.parent_name_expr: self.parent_name = self.parent_name_expr.resolve(context) parent = self.parent_name if not parent: error_msg = "Invalid template name in 'extends' tag: %r." % parent if self.parent_name_expr: error_msg += " Got this from the '%s' variable." % self.parent_name_expr.token raise TemplateSyntaxError, error_msg if hasattr(parent, 'render'): return parent # parent is a Template object exceptions_list = [] source, origin = None, None device = context.get('device', None) if device: device_path_list = get_device_template_paths(device, parent) for device_path in device_path_list: try: source, origin = find_template_source(device_path) except TemplateDoesNotExist: exceptions_list.append(device_path) if source: template_log(device_path, log="extends") break if not source: try: source, origin = find_template_source(parent) template_log(parent, log="extends") except TemplateDoesNotExist: exceptions_list.append(parent) raise TemplateSyntaxError, "Template (%s) cannot be extended, because it doesn't exist" % ", ".join(exceptions_list) return get_template_from_string(source, origin, parent)
def template_exists(templ): if templ is None: return False try: #loader.get_template(templ) loader.find_template_source(templ) except TemplateDoesNotExist: return False return True
def get_template_file(self, name): try: _template_source, template_origin = find_template_source(name) return template_origin.name except TemplateDoesNotExist: name = name[1:] _template_source, template_origin = find_template_source(name) return template_origin.name
def template_exists(templ): if templ is None: return False try: # loader.get_template(templ) loader.find_template_source(templ) except TemplateDoesNotExist: return False return True
def initialize(): # Setup paths depending on if a zipfile of the app is detected. # All added paths should be at the end so that in case of name # collisions the "real" app takes precendence. Its okay to break # this console app, but breaking the users app would not be good. sys.path.append(BASEDIR) if os.path.isfile(APPZIPPATH): # This is needed for static.py sys.path.append(os.path.dirname(BASEDIR)) sys.path.append(os.path.join(APPZIPPATH, APPDIRNAME)) sys.path.append(APPZIPPATH) # Must do this "wasted" import to have google do some special # configuration of the django module first. Then we can add # the custom zipfile template loader. Why isn't google's code # written to make this easier? from google.appengine.ext.webapp import template from django.conf import settings # Some django settings are cached and so can't reliably modified at # runtime. The TEMPLATE_LOADERS _is_ one of them up to 1.1.0 final. # So we must clear the cache settings.TEMPLATE_LOADERS += ('app.zip_loader.load_template_source',) import django.template from django.template import loader # Save the current cache and clear the cache to remake with new # loaders template_source_loaders = loader.template_source_loaders loader.template_source_loaders = None try: loader.find_template_source('__non-existant-template__.tmpl') except django.template.TemplateDoesNotExist: # Merge the new loaders with old cached ones, making sure the # new ones are tried after the old ones for ldr in loader.template_source_loaders: if template_source_loaders and ldr not in template_source_loaders: template_source_loaders += (ldr,) finally: # If not expected exception is raised or no exception, this # will set the cache back to its previous value. Otherwise, # it uses the new merged cache loader.template_source_loaders = template_source_loaders elif os.path.isdir(APPDIRPATH): sys.path.append(os.path.dirname(BASEDIR)) sys.path.append(os.path.join(BASEDIR, APPDIRNAME)) else: # No app directory or zipfile raise Exception("Could not find app")
def template_source(request): """ Return the source of a template, syntax-highlighted by Pygments if it's available. """ from django.template.loader import find_template_source from django.utils.safestring import mark_safe template_name = request.GET.get('template', None) if template_name is None: return HttpResponseBadRequest('"template" key is required') source, origin = find_template_source(template_name) try: from pygments import highlight from pygments.lexers import HtmlDjangoLexer from pygments.formatters import HtmlFormatter source = highlight(source, HtmlDjangoLexer(), HtmlFormatter()) source = mark_safe(source) except ImportError: pass return render_to_response('debug_toolbar/panels/template_source.html', { 'source': source, 'template_name': template_name })
def get_template_source(name): source = None for loader in get_loaders(): if loader.__module__.startswith('dbtemplates.'): # Don't give a damn about dbtemplates' own loader. continue module = import_module(loader.__module__) load_template_source = getattr( module, 'load_template_source', None) if load_template_source is None: load_template_source = loader.load_template_source try: source, origin = load_template_source(name) if source: return source except NotImplementedError: pass except TemplateDoesNotExist: pass if source is None and VERSION[:2] < (1, 2): # Django supported template source extraction still :/ try: from django.template.loader import find_template_source template, origin = find_template_source(name, None) if not hasattr(template, 'render'): return template except (ImportError, TemplateDoesNotExist): pass return None
def get_template_source(name): source = None for loader in get_loaders(): if loader.__module__.startswith('dbtemplates.'): # Don't give a damn about dbtemplates' own loader. continue module = import_module(loader.__module__) load_template_source = getattr(module, 'load_template_source', None) if load_template_source is None: load_template_source = loader.load_template_source try: source, origin = load_template_source(name) if source: return source except NotImplementedError: pass except TemplateDoesNotExist: pass if source is None and VERSION[:2] < (1, 2): # Django supported template source extraction still :/ try: from django.template.loader import find_template_source template, origin = find_template_source(name, None) if not hasattr(template, 'render'): return template except (ImportError, TemplateDoesNotExist): pass return None
def template_source(request): """ Return the source of a template, syntax-highlighted by Pygments if it's available. """ from django.template.loader import find_template_source from django.utils.safestring import mark_safe template_name = request.GET.get('template', None) if template_name is None: return HttpResponseBadRequest('"template" key is required') source, origin = find_template_source(template_name) try: from pygments import highlight from pygments.lexers import HtmlDjangoLexer from pygments.formatters import HtmlFormatter source = highlight(source, HtmlDjangoLexer(), HtmlFormatter()) source = mark_safe(source) except ImportError: pass return render_to_response('debug_toolbar/panels/template_source.html', { 'source': source, 'template_name': template_name })
def templates_static_serve(self, request, path): from django.template.loader import find_template_source try: template_source, _origin = find_template_source(path) except TemplateDoesNotExist: return HttpResponseNotFound(u'404: template not found: \"%s\"' % path) return HttpResponse(template_source)
def cached_get_template(template_name): global TEMPLATE_CACHE t = TEMPLATE_CACHE.get(template_name, None) if not t or SettingsCached.param.DEBUG: source, origin = loader.find_template_source(template_name) t = loader.get_template_from_string(source, origin, template_name) TEMPLATE_CACHE[template_name] = t return t
def find_template(template_list): for template in template_list: try: source, origin = loader.find_template_source(template) return (source, origin, template) except loader.TemplateDoesNotExist: pass raise loader.TemplateDoesNotExist, ", ".join(template_list)
def cached_get_template(template_name): global TEMPLATE_CACHE t = TEMPLATE_CACHE.get(template_name, None) if not t or settings.DEBUG: source, origin = loader.find_template_source(template_name) t = loader.get_template_from_string(source, origin, template_name) TEMPLATE_CACHE[template_name] = t return t
def template_source(request): """ Return the source of a template, syntax-highlighted by Pygments if it's available. """ from django.template import TemplateDoesNotExist from django.utils.safestring import mark_safe from django.conf import settings template_name = request.GET.get('template', None) if template_name is None: return HttpResponseBadRequest('"template" key is required') try: # Django 1.2 ... from django.template.loader import find_template_loader, make_origin loaders = [] for loader_name in settings.TEMPLATE_LOADERS: loader = find_template_loader(loader_name) if loader is not None: loaders.append(loader) for loader in loaders: try: source, display_name = loader.load_template_source( template_name) origin = make_origin(display_name, loader, template_name, settings.TEMPLATE_DIRS) break except TemplateDoesNotExist: source = "Template Does Not Exist: %s" % (template_name, ) except (ImportError, AttributeError): # Django 1.1 ... from django.template.loader import find_template_source source, origin = find_template_source(template_name) # Allow downloading of template if 'download' GET arg is present. if request.GET.get('download', None): httpresp = HttpResponse( content=source, content_type="text/plain", ) httpresp["Content-Disposition"] = 'attachment; filename={}'.format( os.path.split(template_name)[-1]) return httpresp try: from pygments import highlight from pygments.lexers import HtmlDjangoLexer from pygments.formatters import HtmlFormatter source = highlight(source, HtmlDjangoLexer(), HtmlFormatter()) source = mark_safe(source) source.pygmentized = True except ImportError: pass return render_to_response('debug_toolbar/panels/template_source.html', { 'source': source, 'template_name': template_name })
def load_haml_template(fname): """ Returns a haml template specified by filename from the locations specified in TEMPLATE_DIRS """ if settings.IGNORE_PATHS: return False else: tps, tpo = loader.find_template_source(fname) return template.Template(haml(tps))
def save(self, *args, **kwargs): self.last_changed = datetime.now() # If content is empty look for a template with the given name and # populate the template instance with its content. if not self.content: try: source, origin = find_template_source(self.name) if source: self.content = source except TemplateDoesNotExist: pass super(Template, self).save(*args, **kwargs)
def save(self, *args, **kwargs): self.last_changed = datetime.now() # If content is empty look for a template with the given name and # populate the template instance with its content. if not self.content: try: source, origin = find_template_source(self.name) if source: self.content = source except TemplateDoesNotExist: pass super(Template, self).save(*args, **kwargs)
def template_source(request): """ Return the source of a template, syntax-highlighted by Pygments if it's available. """ from django.template import TemplateDoesNotExist from django.utils.safestring import mark_safe from django.conf import settings template_name = request.GET.get('template', None) if template_name is None: return HttpResponseBadRequest('"template" key is required') try: # Django 1.2 ... from django.template.loader import find_template_loader, make_origin loaders = [] for loader_name in settings.TEMPLATE_LOADERS: loader = find_template_loader(loader_name) if loader is not None: loaders.append(loader) for loader in loaders: try: source, display_name = loader.load_template_source(template_name) origin = make_origin(display_name, loader, template_name, settings.TEMPLATE_DIRS) break except TemplateDoesNotExist: source = "Template Does Not Exist: %s" % (template_name,) except (ImportError, AttributeError): # Django 1.1 ... from django.template.loader import find_template_source source, origin = find_template_source(template_name) # Allow downloading of template if 'download' GET arg is present. if request.GET.get('download', None): httpresp = HttpResponse(content=source, content_type="text/plain", ) httpresp["Content-Disposition"] = 'attachment; filename={}'.format(os.path.split(template_name)[-1]) return httpresp try: from pygments import highlight from pygments.lexers import HtmlDjangoLexer from pygments.formatters import HtmlFormatter source = highlight(source, HtmlDjangoLexer(), HtmlFormatter()) source = mark_safe(source) source.pygmentized = True except ImportError: pass return render_to_response('debug_toolbar/panels/template_source.html', { 'source': source, 'template_name': template_name })
def get_parent(self, context): if self.parent_name_expr: self.parent_name = self.parent_name_expr.resolve(context) parent = theme_template_url() + '/' + self.parent_name if not parent: error_msg = "Invalid template name in 'extends' tag: %r." % parent if self.parent_name_expr: error_msg += " Got this from the '%s' variable." % self.parent_name_expr.token raise TemplateSyntaxError, error_msg try: source, origin = find_template_source(parent, self.template_dirs) except TemplateDoesNotExist: raise TemplateSyntaxError, "Template %r cannot be extended, because it doesn't exist" % parent else: return get_template_from_string(source, origin, parent)
def get_parent(self, context): if self.parent_name_expr: self.parent_name = self.parent_name_expr.resolve(context) parent = theme_template_url() +'/'+ self.parent_name if not parent: error_msg = "Invalid template name in 'extends' tag: %r." % parent if self.parent_name_expr: error_msg += " Got this from the '%s' variable." % self.parent_name_expr.token raise TemplateSyntaxError, error_msg try: source, origin = find_template_source(parent, self.template_dirs) except TemplateDoesNotExist: raise TemplateSyntaxError, "Template %r cannot be extended, because it doesn't exist" % parent else: return get_template_from_string(source, origin, parent)
def render_to_string_with_namespace( protected_ns, template_name, dictionary=None, context_instance=None): source, origin = find_template_source(template_name) private_ns = '_' + memoize_(pydigest_str, protected_ns + template_name) source = subns(protected_ns, private_ns, source) template = Template(source, origin, template_name) if context_instance: context_instance.update(dictionary) else: context_instance = Context(dictionary) return template.render(context_instance)
def template_source(request): """ Return the source of a template, syntax-highlighted by Pygments if it's available. """ from django.template import TemplateDoesNotExist from django.utils.safestring import mark_safe from django.conf import settings template_name = request.GET.get("template", None) if template_name is None: return HttpResponseBadRequest('"template" key is required') try: # Django 1.2 ... from django.template.loader import find_template_loader, make_origin loaders = [] for loader_name in settings.TEMPLATE_LOADERS: loader = find_template_loader(loader_name) if loader is not None: loaders.append(loader) for loader in loaders: try: source, display_name = loader.load_template_source(template_name) origin = make_origin(display_name, loader, template_name, settings.TEMPLATE_DIRS) break except TemplateDoesNotExist: source = "Template Does Not Exist: %s" % (template_name,) except (ImportError, AttributeError): # Django 1.1 ... from django.template.loader import find_template_source source, origin = find_template_source(template_name) try: from pygments import highlight from pygments.lexers import HtmlDjangoLexer from pygments.formatters import HtmlFormatter source = highlight(source, HtmlDjangoLexer(), HtmlFormatter()) source = mark_safe(source) source.pygmentized = True except ImportError: pass return render_to_response( "debug_toolbar/panels/template_source.html", {"source": source, "template_name": template_name} )
def template_source(request): """ Return the source of a template, syntax-highlighted by Pygments if it's available. """ from django.template import TemplateDoesNotExist from django.utils.safestring import mark_safe from django.conf import settings template_name = request.GET.get('template', None) if template_name is None: return HttpResponseBadRequest('"template" key is required') try: # Django 1.2 ... from django.template.loader import find_template_loader, make_origin loaders = [] for loader_name in settings.TEMPLATE_LOADERS: loader = find_template_loader(loader_name) if loader is not None: loaders.append(loader) for loader in loaders: try: source, display_name = loader.load_template_source( template_name) origin = make_origin(display_name, loader, template_name, settings.TEMPLATE_DIRS) except TemplateDoesNotExist: source = "Template Does Not Exist: %s" % (template_name, ) except ImportError: # Django 1.1 ... from django.template.loader import find_template_source source, origin = find_template_source(template_name) try: from pygments import highlight from pygments.lexers import HtmlDjangoLexer from pygments.formatters import HtmlFormatter source = highlight(source, HtmlDjangoLexer(), HtmlFormatter()) source = mark_safe(source) source.pygmentized = True except ImportError: pass return render_to_response('debug_toolbar/panels/template_source.html', { 'source': source, 'template_name': template_name })
def get_parent(self, context): if self.parent_name_expr: self.parent_name = self.parent_name_expr.resolve(context) parent = self.parent_name if not parent: error_msg = "Invalid template name in 'extends' tag: %r." % parent if self.parent_name_expr: error_msg += " Got this from the %r variable." % self.parent_name_expr #TODO nice repr. raise TemplateSyntaxError, error_msg if hasattr(parent, 'render'): return parent # parent is a Template object try: source, origin = find_template_source(parent, self.template_dirs) except TemplateDoesNotExist: raise TemplateSyntaxError, "Template %r cannot be extended, because it doesn't exist" % parent else: return get_template_from_string(source, origin, parent)
def template_edit(request): """Launch an editor and open the template file.""" from django.template.loader import find_template_source if not hasattr(settings, 'DEBUG_TOOLBAR_CONFIG') or \ 'EDITOR' not in settings.DEBUG_TOOLBAR_CONFIG: return HttpResponseBadRequest('No editor provided.') template_name = request.GET.get('template', None) if template_name is None: return HttpResponseBadRequest('"template" key is required') source, origin = find_template_source(template_name) cmd = ' '.join([settings.DEBUG_TOOLBAR_CONFIG['EDITOR'], origin.name]) subprocess.call(cmd, shell=True) return HttpResponse('')
def get_parent(self, context): if self.parent_name_expr: self.parent_name = self.parent_name_expr.resolve(context) parent = self.parent_name if not parent: error_msg = "Invalid template name in 'extends' tag: %r." % parent if self.parent_name_expr: error_msg += " Got this from the %r variable." % self.parent_name_expr #TODO nice repr. raise TemplateSyntaxError, error_msg if hasattr(parent, 'render'): return parent # parent is a Template object try: source, origin = find_template_source(parent, self.template_dirs) except TemplateDoesNotExist: raise TemplateSyntaxError, "Template %r cannot be extended, because it doesn't exist" % parent else: return get_template_from_string(source, origin, parent)
def _generate_invite (invite, out_filename): source, origin = loader.find_template_source('tarjeta_%s.svg' % invite.language) source = re.sub(r"%([^(])", r"%%\1", source) if not invite.invited_to_party: if invite.language == 'es': source = source.replace ("%(pueden)s avisarnos si %(vienen)s, y", "%(pueden)s conocer varios otros") source = source.replace (u"ayudarnos con varios aspectos de la", u"aspectos de nuestro casamiento") source = source.replace (u"organización.", u" ") else: source = source.replace ("You can let us know if you're coming,", "you can find out other details") source = source.replace ("and help us with several bits of", "about our wedding") source = source.replace ("the organization.", " ") in_filename = tempfile.mktemp() + '.svg' f = codecs.open(in_filename, 'w', encoding='utf-8') f.write(card) f.close() os.system ('inkscape --export-dpi=600 --export-pdf=%s %s' % (out_filename, in_filename)) os.unlink(in_filename)
def verify_email(self): import string import random from esp.users.models import PersistentQueryFilter from esp.dbmail.models import MessageRequest from django.template import loader symbols = string.ascii_uppercase + string.digits code = "".join([random.choice(symbols) for x in range(30)]) regProf = RegistrationProfile.getLastForProgram( get_current_request().user, self.program) if regProf.email_verified: return self.goToCore(tl) if request.method == 'POST' and request.POST.has_key('verify_me'): # create the variable modules variable_modules = { 'program': self.program, 'user': get_current_request().user } # get the filter object filterobj = PersistentQueryFilter.getFilterFromQ( Q(id=get_current_request().user.id), User, 'User %s' % get_current_request().user.username) newmsg_request = MessageRequest.createRequest( var_dict=variable_modules, subject='[ESP] Email Verification For esp.mit.edu', recipients=filterobj, sender='"MIT Educational Studies Program" <*****@*****.**>', creator=self, msgtext=loader.find_template_source('email/verify')[0]) newmsg_request.save() return render_to_response(self.baseDir() + 'emailsent.html', request, {}) return render_to_response(self.baseDir() + 'sendemail.html', request, {})
def send_mail_from_template(to, template_name, context=None, fail_silently=True): if not to: return # Build the context context = context or {} context['site'] = Site.objects.get_current() context = Context(context, autoescape=False) # Get the text email body source, origin = find_template_source('newsroom/emails/%s.txt' % template_name) template = get_template_from_string(source, origin, template_name) body = template.render(context) # Get the subject line match = RE_SUBJECT.search(source) if not match: raise ValueError('The email source did not contain a "SUBJECT:" line') subject_source = match.group(1) template = get_template_from_string(subject_source, origin, template_name) subject = template.render(context) # Fire off the email if isinstance(to, basestring): to = [to] msg = EmailMessage(subject, body, to=to) msg.send(fail_silently=fail_silently)
def template_path(template): """ Django 1.2 and higher moved to class-based template loaders and deprecated ``find_template_source`` - there's a ``find_template`` function that is similar but always returns ``None`` for the template path so it appears to be broken. This function works as far as retrieving the template path goes which is all we're concerned with. """ from django import VERSION if VERSION < (1, 2, 0): from django.template.loader import find_template_source return find_template_source(template)[1] from django.template.loader import find_template_loader, \ TemplateDoesNotExist for loader_name in settings.TEMPLATE_LOADERS: loader = find_template_loader(loader_name) if loader is not None: try: return loader.load_template_source(template, None)[1] except TemplateDoesNotExist: pass return None
def template_path(template): """ Django 1.2 and higher moved to class-based template loaders and deprecated ``find_template_source`` - there's a ``find_template`` function that is similar but always returns ``None`` for the template path so it appears to be broken. This function works as far as retrieving the template path goes which is all we're concerned with. """ from django import VERSION if VERSION < (1, 2, 0): from django.template.loader import find_template_source return find_template_source(template)[1] from django.template.loader import find_template_loader, \ TemplateDoesNotExist for loader_name in settings.TEMPLATE_LOADERS: loader = find_template_loader(loader_name) if loader is not None: try: return loader.load_template_source(template, None)[1] except TemplateDoesNotExist: pass return None
def template_source(request): """ Return the source of a template, syntax-highlighted by Pygments if it's available. """ template_name = request.GET.get('template', None) if template_name is None: return HttpResponseBadRequest('"template" key is required') try: # Django 1.2 ... from django.template.loader import find_template_loader, make_origin loaders = [] for loader_name in settings.TEMPLATE_LOADERS: loader = find_template_loader(loader_name) if loader is not None: loaders.append(loader) for loader in loaders: try: source, display_name = loader.load_template_source(template_name) origin = make_origin(display_name, loader, template_name, settings.TEMPLATE_DIRS) except TemplateDoesNotExist: source = "Template Does Not Exist: %s" % (template_name,) except ImportError: # Django 1.1 ... from django.template.loader import find_template_source source, origin = find_template_source(template_name) if PYGMENTS_AVAILABLE: source = highlight(source, HtmlDjangoLexer(), HtmlFormatter()) source = mark_safe(source) source.pygmentized = True return render_to_response('debug_toolbar/panels/template_source.html', { 'source': source, 'template_name': template_name })
def sync_app_plugins(delete_removed=False, verbosity=1): """ We need to do a complicated dance to sync the current registered and unregistered implicit plugins and points with the instances in the DB. Plugins and points which are removed (or can no longer be found) are marked REMOVED (sometimes only to be re-enabed shortly there after). There is the option to delete all entries which are marked removed, but this should be done carefully. Other apps may have relations to the plugins and points for user preferences and customizaiton settings. We do not want to blindly delete these in a chain delete, as they may need to be migrated to a renamed plugin or point which has now been created as part of the sync. The process: 1. for each registered Plugin Point 1.1. if no instance exists, create it enabled. 1.2. mark it registered 1.2. if it was 'removed' then mark it enabled 2. For each non-removed, registered instance for which there was no reg 2.1. mark removed 2.2. mark plugins removed 3. for each registered plugin 3.1. if there is no point, create it as unregistered 3.2. if there is no plugin, create it as reg and enabled, w/template 3.3. test the template load 3.4. if the plugin was marked REMOVED 3.4.1. mark it enabled 3.4.2. if its point is REMOVED and it is not marked reg, enable it 4. for each registered plugin for which there is no registration 4.1. mark it removed 5. for each non-REMOVED point, get all unregistered plugins via load: 5.1. if plugin instance does not exist, create unregistered and enable 5.2. if it does exist and is marked REMOVED, mark unreg-enabled w/ point 6. for each unreg-non-removed point 6.1. if all plugins are REMOVED, mark REMOVED. 7. if asked to delete the removed, do so. """ from app_plugins.library import libraries from app_plugins.models import Plugin, PluginPoint, REMOVED, ENABLED from app_plugins.models import construct_template_path instances = dict((p.label, p) for p in PluginPoint.objects.all()) ## section 1 - registered plugin points for app_label, lib in libraries.iteritems(): for label in lib.plugin_points: pp = instances.pop(label, None) if pp is None: if verbosity > 1: print "Creating registered PluginPoint:", label pp = PluginPoint(label=label) pp.registered = True if pp.status == REMOVED: if verbosity > 1: print "Updating registered PluginPoint:", label # re-enable a previously removed plugin point and its plugins pp.status = ENABLED for p in Plugin.objects.filter(point=pp, status=REMOVED): p.status = ENABLED p.save() pp.save() # search for unregistered plugins we do not yet know about? ## section 2 - removed plugin points for pp in instances.itervalues(): # don't remove pps with active plugins! if pp.status != REMOVED and not pp.plugin_set.exclude( status=REMOVED).count(): pp.status = REMOVED pp.save() for p in pp.plugin_set.all(): p.status = REMOVED p.save() instances = dict((p.label, p) for p in Plugin.objects.all()) ## section 3 - registered plugins for app_label, lib in libraries.iteritems(): for label in lib.plugins: p = instances.pop(label, None) # Don't forget the dot in label! Strip it out. # We could increment slice index by 1, but I'm # not sure if some labels/libs will have no app_name # and thus no joining dot?? point_label = label[len(lib.app_name):].strip('.') if p is None: p = Plugin() p.label = label if verbosity > 1: print "Creating registered Plugin:", label try: point = PluginPoint.objects.get(label=point_label) p.point = point if point.status == REMOVED: # point was removed at some point... point.status = ENABLED if point.registered: point.registered = False point.save() except PluginPoint.DoesNotExist: if verbosity > 1: print "Creating unregistered PluginPoint:", point_label point = PluginPoint(label=point_label) point.save() p.point = point p.registered = True if p.status == REMOVED: # re-enable a previously removed plugin if verbosity > 1: print "Updating registered Plugin:", p.label p.status = ENABLED options = lib.get_plugin_call(point_label).options default = construct_template_path(lib.app_name, point_label, options.get('ext', '.html')) # raise an error if it does not exist... template = options.get('template', default) loader.find_template_source(template) p.template = template p.save() ## section 4 - initial marking of unregistered known plugins for p in instances.itervalues(): if p.status != REMOVED: p.status = REMOVED p.save() ## section 5 - unregistered plugins instances = dict((p.label, p) for p in Plugin.objects.all()) for pp in PluginPoint.objects.exclude(status=REMOVED): ext = pp.get_options().get('ext', '.html') name = pp.label for app in settings.INSTALLED_APPS: label = u'.'.join([app, name]) template = construct_template_path(app, name, ext) bFound = True try: loader.find_template_source(template) except TemplateDoesNotExist: bFound = False p = instances.get(label, None) if p is None: if bFound: if verbosity > 1: print "Creating unregistered Plugin:", label p = Plugin(point=pp, label=label, template=template) else: if p.status == REMOVED and bFound: p.status = ENABLED p.template = template p.registered = False #if verbosity > 1: # print "Updating unregistered Plugin:", label elif not p.registered and not bFound and p.status != REMOVED: p.status = REMOVED else: p = None if p is not None: p.save() ## section 6 - removed unregistered plugin points for pp in PluginPoint.objects.filter(registered=False).exclude( status=REMOVED): if not pp.plugin_set.exclude(status=REMOVED).count(): if verbosity > 1: print "Removing unregistered PluginPoint:", pp.label pp.status = REMOVED pp.save() ## section 7 - delete removed if delete_removed: count = Plugin.objects.filter(status=REMOVED).count() if count: if verbosity > 1: print "Deleting %d Removed Plugins" % count Plugin.objects.filter(status=REMOVED).delete() count = PluginPoints.objects.filter(status=REMOVED).count() if count: if verbosity > 1: print "Deleting %d Removed PluginPoint" % count PluginPoints.objects.filter(status=REMOVED).delete()
def get_template(example_name): template_name = 'examples/examples/%s.html' % example_name template_code, origin = find_template_source(template_name) template_obj = get_template_from_string(template_code, origin, example_name) return template_obj, template_code
def test_xsl_template_can_be_found(self): """docstring for test_xsl_template_can_be_found""" template = find_template_source('xslt/model-to-xml.xsl') assert(template)
def sync_app_plugins(delete_removed=False, verbosity=1): """ We need to do a complicated dance to sync the current registered and unregistered implicit plugins and points with the instances in the DB. Plugins and points which are removed (or can no longer be found) are marked REMOVED (sometimes only to be re-enabed shortly there after). There is the option to delete all entries which are marked removed, but this should be done carefully. Other apps may have relations to the plugins and points for user preferences and customizaiton settings. We do not want to blindly delete these in a chain delete, as they may need to be migrated to a renamed plugin or point which has now been created as part of the sync. The process: 1. for each registered Plugin Point 1.1. if no instance exists, create it enabled. 1.2. mark it registered 1.2. if it was 'removed' then mark it enabled 2. For each non-removed, registered instance for which there was no reg 2.1. mark removed 2.2. mark plugins removed 3. for each registered plugin 3.1. if there is no point, create it as unregistered 3.2. if there is no plugin, create it as reg and enabled, w/template 3.3. test the template load 3.4. if the plugin was marked REMOVED 3.4.1. mark it enabled 3.4.2. if its point is REMOVED and it is not marked reg, enable it 4. for each registered plugin for which there is no registration 4.1. mark it removed 5. for each non-REMOVED point, get all unregistered plugins via load: 5.1. if plugin instance does not exist, create unregistered and enable 5.2. if it does exist and is marked REMOVED, mark unreg-enabled w/ point 6. for each unreg-non-removed point 6.1. if all plugins are REMOVED, mark REMOVED. 7. if asked to delete the removed, do so. """ from app_plugins.library import libraries from app_plugins.models import Plugin, PluginPoint, REMOVED, ENABLED from app_plugins.models import construct_template_path instances = dict((p.label, p) for p in PluginPoint.objects.all()) ## section 1 - registered plugin points for app_label, lib in libraries.iteritems(): for label in lib.plugin_points: pp = instances.pop(label, None) if pp is None: if verbosity > 1: print "Creating registered PluginPoint:", label pp = PluginPoint(label=label) pp.registered = True if pp.status == REMOVED: if verbosity > 1: print "Updating registered PluginPoint:", label # re-enable a previously removed plugin point and its plugins pp.status = ENABLED for p in Plugin.objects.filter(point=pp, status=REMOVED): p.status = ENABLED p.save() pp.save() # search for unregistered plugins we do not yet know about? ## section 2 - removed plugin points for pp in instances.itervalues(): # don't remove pps with active plugins! if pp.status != REMOVED and not pp.plugin_set.exclude(status=REMOVED).count(): pp.status = REMOVED pp.save() for p in pp.plugin_set.all(): p.status = REMOVED p.save() instances = dict((p.label, p) for p in Plugin.objects.all()) ## section 3 - registered plugins for app_label, lib in libraries.iteritems(): for label in lib.plugins: p = instances.pop(label, None) # Don't forget the dot in label! Strip it out. # We could increment slice index by 1, but I'm # not sure if some labels/libs will have no app_name # and thus no joining dot?? point_label = label[len(lib.app_name):].strip('.') if p is None: p = Plugin() p.label = label if verbosity > 1: print "Creating registered Plugin:", label try: point = PluginPoint.objects.get(label=point_label) p.point = point if point.status == REMOVED: # point was removed at some point... point.status = ENABLED if point.registered: point.registered = False point.save() except PluginPoint.DoesNotExist: if verbosity > 1: print "Creating unregistered PluginPoint:", point_label point = PluginPoint(label=point_label) point.save() p.point = point p.registered = True if p.status == REMOVED: # re-enable a previously removed plugin if verbosity > 1: print "Updating registered Plugin:", p.label p.status = ENABLED options = lib.get_plugin_call(point_label).options default = construct_template_path(lib.app_name, point_label, options.get('ext', '.html')) # raise an error if it does not exist... template = options.get('template', default) loader.find_template_source(template) p.template = template p.save() ## section 4 - initial marking of unregistered known plugins for p in instances.itervalues(): if p.status != REMOVED: p.status = REMOVED p.save() ## section 5 - unregistered plugins instances = dict((p.label, p) for p in Plugin.objects.all()) for pp in PluginPoint.objects.exclude(status=REMOVED): ext = pp.get_options().get('ext', '.html') name = pp.label for app in settings.INSTALLED_APPS: label = u'.'.join([app, name]) template = construct_template_path(app, name, ext) bFound = True try: loader.find_template_source(template) except TemplateDoesNotExist: bFound = False p = instances.get(label, None) if p is None: if bFound: if verbosity > 1: print "Creating unregistered Plugin:", label p = Plugin(point=pp, label=label, template=template) else: if p.status == REMOVED and bFound: p.status = ENABLED p.template = template p.registered = False #if verbosity > 1: # print "Updating unregistered Plugin:", label elif not p.registered and not bFound and p.status != REMOVED: p.status = REMOVED else: p = None if p is not None: p.save() ## section 6 - removed unregistered plugin points for pp in PluginPoint.objects.filter(registered=False).exclude(status=REMOVED): if not pp.plugin_set.exclude(status=REMOVED).count(): if verbosity > 1: print "Removing unregistered PluginPoint:", pp.label pp.status = REMOVED pp.save() ## section 7 - delete removed if delete_removed: count = Plugin.objects.filter(status=REMOVED).count() if count: if verbosity > 1: print "Deleting %d Removed Plugins" % count Plugin.objects.filter(status=REMOVED).delete() count = PluginPoints.objects.filter(status=REMOVED).count() if count: if verbosity > 1: print "Deleting %d Removed PluginPoint" % count PluginPoints.objects.filter(status=REMOVED).delete()
def _generate_django_exception(e, source=None): '''Generate a Django exception from a Jinja exception''' from django.views.debug import linebreak_iter import re if source: exception = DjangoTemplateSyntaxError(e.message) exception_dict = e.__dict__ del exception_dict['source'] # Fetch the entire template in a string template_string = source[0].reload() # Get the line number from the error message, if available match = re.match('.* at (\d+)$', e.message) start_index = 0 stop_index = 0 if match: # Convert the position found in the stacktrace to a position # the Django template debug system can use position = int(match.group(1)) + source[1][0] + 1 for index in linebreak_iter(template_string): if index >= position: stop_index = min(index, position + 3) start_index = min(index, position - 2) break start_index = index else: # So there wasn't a matching error message, in that case we # simply have to highlight the entire line instead of the specific # words ignore_lines = -1 for i, index in enumerate(linebreak_iter(template_string)): if source[1][0] > index: ignore_lines += 1 if i - ignore_lines == e.lineno: stop_index = index break start_index = index # Convert the positions to a source that is compatible with the # Django template debugger source = source[0], ( start_index, stop_index, ) else: # No source available so we let Django fetch it for us lineno = e.lineno - 1 template_string, source = django_loader.find_template_source(e.name) exception = DjangoTemplateSyntaxError(e.message) # Find the positions by the line number given in the exception start_index = 0 for i in range(lineno): start_index = template_string.index('\n', start_index + 1) source = source, ( start_index + 1, template_string.index('\n', start_index + 1) + 1, ) # Set our custom source as source for the exception so the Django # template debugger can use it exception.source = source return exception
def render_haml_to_response(tname, context): tps, tpo = loader.find_template_source(tname) tpl = template.Template(haml(tps)) return HttpResponse(tpl.render(template.Context(context)))
def test_automatic_sync(self): admin_base_template, origin = loader.find_template_source('admin/base.html') template = Template.objects.create(name='admin/base.html') self.assertEqual(admin_base_template, template.content)
def test_xsl_template_can_be_found(self): """docstring for test_xsl_template_can_be_found""" template = find_template_source('xslt/model-to-xml.xsl') assert(template)
def get_template(example_name): template_name = 'examples/examples/%s.html' % example_name template_code, origin = find_template_source(template_name) template_obj = get_template_from_string(template_code, origin, example_name) return template_obj, template_code