Пример #1
0
 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
Пример #2
0
    def get_context_data(self, **kwargs):
        context = super(PatternView, self).get_context_data(**kwargs)
        project = Project(kwargs["project"])
        aspect = Aspect(kwargs["aspect"], project)
        pattern = Pattern(kwargs["pattern"], aspect)
        context["pattern"] = pattern

        # A pattern may have an intro view. First look in the pattern itself,
        # then fall back to normal template resolution.
        template_names = (
            "%s/%s/src/patterns/%s/mote/intro.html" %
            (project.id, aspect.id, pattern.id),
            "mote/pattern/intros/%s.html" % pattern.id,
        )
        intro = None
        for template_name in template_names:
            # todo: dont' render it here, only in the template. Just check that it loads.
            try:
                intro = render_to_string(template_name, {})
                break
            except TemplateDoesNotExist:
                pass
        context["intro"] = intro

        return context
Пример #3
0
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
Пример #4
0
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
Пример #5
0
 def get_object(self, project, aspect, pattern, element):
     project = Project(project)
     aspect = Aspect(aspect, project)
     pattern = Pattern(pattern, aspect)
     element = Element(element, pattern)
     return element
Пример #6
0
 def element(self):
     project = Project(self.kwargs["project"])
     aspect = Aspect(self.kwargs["aspect"], project)
     pattern = Pattern(self.kwargs["pattern"], aspect)
     element = Element(self.kwargs["element"], pattern)
     return element
Пример #7
0
 def get_context_data(self, **kwargs):
     context = super(AspectView, self).get_context_data(**kwargs)
     project = Project(kwargs["project"])
     context["aspect"] = Aspect(kwargs["aspect"], project)
     return context
Пример #8
0
    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
Пример #9
0
 def get_object(self, project, aspect):
     project = Project(project)
     aspect = Aspect(aspect, project)
     return aspect
Пример #10
0
 def get_object(self, project, aspect, pattern):
     project = Project(project)
     aspect = Aspect(aspect, project)
     pattern = Pattern(pattern, aspect)
     return pattern