def get_object(self, project, aspect, pattern, element, variation): project = Project(project) aspect = Aspect(aspect, project) pattern = Pattern(pattern, aspect) element = Element(element, pattern) variation = Variation(variation, element) return variation
def get_object_by_dotted_name(name, project_id=None): """Return object identified by eg. a.b.c.d. If "a" is "self" then use project_id.""" # Avoid circular import from mote.models import Project, Aspect, Pattern, Element, Variation li = name.split(".") length = len(li) if li[0] == "self": if project_id is None: try: value = settings.MOTE["project"] except (AttributeError, KeyError): raise RuntimeError( """Pass a valid project_id or define MOTE["project"]""" + " setting for project lookup") project = Project(project_id) else: project = Project(li[0]) if length == 1: return project aspect = Aspect(li[1], project) if length == 2: return aspect pattern = Pattern(li[2], aspect) if length == 3: return pattern element = Element(li[3], pattern) if length == 4: return element variation = Variation(li[4], element) if length == 5: return variation return None
def get_object_by_dotted_name(name): """Return object identified by eg. a.b.c.d""" # Avoid circular import from mote.models import Project, Aspect, Pattern, Element, Variation li = name.split(".") length = len(li) project = Project(li[0]) if length == 1: return project aspect = Aspect(li[1], project) if length == 2: return aspect pattern = Pattern(li[2], aspect) if length == 3: return pattern element = Element(li[3], pattern) if length == 4: return element variation = Variation(li[4], element) if length == 5: return variation return None
def variation(self): return Variation(self.kwargs["variation"], self.element)
def render(self, context): # We must import late from mote.models import Project, Aspect, Pattern, Element, Variation element_or_identifier = self.element_or_identifier.resolve(context) # If element_or_identifier is a string convert it if isinstance(element_or_identifier, (unicode, str)): parts = element_or_identifier.split(".") length = len(parts) if length not in (4, 5): raise template.TemplateSyntaxError( "Invalid identifier %s" % element_or_identifier ) project = Project(parts[0]) aspect = Aspect(parts[1], project) pattern = Pattern(parts[2], aspect) obj = Element(parts[3], pattern) if length == 5: obj = Variation(parts[4], obj) else: obj = element_or_identifier # Resolve the kwargs resolved = {} for k, v in self.kwargs.items(): try: r = v.resolve(context) except VariableDoesNotExist: continue if isinstance(r, Promise): r = unicode(r) # Strings may be interpreted further if isinstance(r, (unicode, str)): # Attempt to resolve any variables by rendering t = template.Template(r) raw_struct = t.render(context) # Attempt to convert to JSON try: resolved[k] = json.loads(raw_struct) except ValueError: resolved[k] = r else: resolved[k] = r if isinstance(obj, Variation): url = reverse( "mote:variation-partial", kwargs=dict( project=obj.project.id, aspect=obj.aspect.id, pattern=obj.pattern.id, element=obj.element.id, variation=obj.id ) ) else: url = reverse( "mote:element-partial", kwargs=dict( project=obj.project.id, aspect=obj.aspect.id, pattern=obj.pattern.id, element=obj.id ) ) # Resolve needs any possible prefix removed url = re.sub(r"^%s" % get_script_prefix().rstrip('/'), '', url) view, args, kwargs = resolve(url) kwargs.update(resolved) # Compute a cache key li = [url, obj.modified] keys = resolved.keys() keys.sort() for key in keys: li.append('%s,%s' % (key, str(resolved[key]))) hashed = md5.new(':'.join([str(l) for l in li])).hexdigest() cache_key = 'render-element-%s' % hashed cached = cache.get(cache_key, None) if cached is not None: return cached # Call the view. Let any error propagate. request = context["request"] result = view(request, *args, **kwargs) if isinstance(result, TemplateResponse): # The result of a generic view result.render() html = result.rendered_content elif isinstance(result, HttpResponse): # Old-school view html = result.content # Make output beautiful for Chris if not settings.DEBUG: beauty = BeautifulSoup(html) html = beauty.prettify() cache.set(cache_key, html, 300) return html