def render_listRow(self, ctx: WovenContext, data=None):
        IEventBus("Web").register(self, ISubAccountChangedEvent)
        listCell = inevow.IQ(ctx).patternGenerator("listCell")
        self.expanded = False
        ctx.fillSlots('index', self._subAccount.sub)
        active = T.input(id='active', type='checkbox', checked=self._subAccount.active)
        name = T.input(id='name', value=self.name)
        sub = T.input(id='sub', value=self._subAccount.sub, disabled=True)

        if not self.employee.isAdministrator() or self.parent.selectable:
            active(disabled=True)
            name(disabled=True)
            sub(disabled=True)

        self.preprocess([active, name, sub])
        if self.length == 4:
            r = [listCell(data=dict(listItem='►'))(id='expand-button')[T.Tag("athena:handler")(event='onclick', handler='expand')],
                 listCell(data=dict(listItem='▼'))(style='display:none', id='unexpand-button')[T.Tag("athena:handler")(event='onclick', handler='expand')],
                 ]
        else:
            r = []
        r.extend([listCell(data=dict(listItem=sub))[Tag('athena:handler')(event='ondblclick', handler='expand')],
             listCell(data=dict(listItem=name)),
             listCell(data=dict(listItem=active))])
        return r
 def render_listRow(self, ctx: WovenContext, data):
     listCell = inevow.IQ(ctx).patternGenerator("listCell")
     o = []
     for idx, slot in enumerate(self.slots):
         o.append(listCell(data=dict(listItem=slot, index=idx)))
     ctx.fillSlots('searchclass', 'self.searchClass')
     return o
Example #3
0
 def patternContext(self, ctx, name):
     context = WovenContext(parent=ctx)
     self.rememberStuff(context)
     # XXX: preprocessors?
     doc = self.docFactory.load(context)
     context.tag = inevow.IQ(doc).onePattern(name)
     return context
    def test_oldFlattenableOverriddenRend(self):
        """
        Flattening L{Element} instances with an overridden C{rend} method
        flattened with the old flatten function results in the flattened
        version of whatever their C{rend} method returns.
        """
        renders = []

        class OldStyleElement(Element):
            docFactory = stan("Hello, world")

            def rend(self, ctx, data):
                return invisible(render=directive("foo"))[Element.rend(
                    self, ctx, data)]

            def foo(self, request, tag):
                renders.append((self, request))
                return p[tag]

            renderer(foo)

        result = []
        element = OldStyleElement()
        request = FakeRequest()
        context = WovenContext()
        context.remember(request)
        finished = oldFlatten(element, context, result.append, lambda ign: ign)

        def cbFinished(ignored):
            self.assertEqual("".join(result), "<p>Hello, world</p>")
            self.assertEqual(renders, [(element, request)])

        finished.addCallback(cbFinished)
        return finished
Example #5
0
    def process(self, context, boundTo, data, autoConfigure = True):
        """Knows how to process a dictionary of lists
        where the dictionary may contain a key with the same
        name as some of the arguments to the MethodBinding
        instance.
        """
        typedValue = self.original.typedValue
        results = {}
        failures = {}
        if '----' in data:
            ## ---- is the "direct object", the one argument you can specify using the command line without saying what the argument name is
            data[typedValue.arguments[0].name] = data['----']
            del data['----']
        for binding in typedValue.arguments:
            name = binding.name
            try:
                context = WovenContext(context, faketag)
                context.remember(binding, iformless.IBinding)
                results[name] = iformless.IInputProcessor(binding.typedValue).process(context, boundTo, data.get(name, ['']))
            except formless.InputError as e:
                results[name] = data.get(name, [''])[0]
                failures[name] = e.reason

        if failures:
            #print "There were validation errors. The errors were: ", failures
            raise formless.ValidateError(failures, "Error:", results)

        if autoConfigure:
            def _except(e):
                failures[''] = e.reason # self.original.name
                raise formless.ValidateError(failures, e.reason, results)
            return exceptblock(self.original.configure, _except, formless.InputError,
                               boundTo, results)
        return results
Example #6
0
    def doRendering(self, fragmentClass):
        """
        Verify that the given fragment class will render without raising an
        exception.
        """
        siteStore = Store()

        loginSystem = LoginSystem(store=siteStore)
        installOn(loginSystem, siteStore)
        p = Product(
            store=siteStore, types=["xmantissa.webadmin.LocalUserBrowser", "xmantissa.signup.SignupConfiguration"]
        )
        account = loginSystem.addAccount(u"testuser", u"localhost", None)
        p.installProductOn(account.avatars.open())
        f = fragmentClass(None, u"testuser", account)

        p = LivePage(docFactory=stan(html[head(render=directive("liveglue")), body(render=lambda ctx, data: f)]))
        f.setFragmentParent(p)

        ctx = WovenContext()
        req = FakeRequest()
        ctx.remember(req, IRequest)

        d = p.renderHTTP(ctx)

        def rendered(ign):
            p.action_close(None)

        d.addCallback(rendered)
        return d
Example #7
0
    def process(self, context, boundTo, data, autoConfigure = True):
        """Knows how to process a dictionary of lists
        where the dictionary may contain a key with the same
        name as some of the arguments to the MethodBinding
        instance.
        """
        typedValue = self.original.typedValue
        results = {}
        failures = {}
        if '----' in data:
            ## ---- is the "direct object", the one argument you can specify using the command line without saying what the argument name is
            data[typedValue.arguments[0].name] = data['----']
            del data['----']
        for binding in typedValue.arguments:
            name = binding.name
            try:
                context = WovenContext(context, faketag)
                context.remember(binding, iformless.IBinding)
                results[name] = iformless.IInputProcessor(binding.typedValue).process(context, boundTo, data.get(name, ['']))
            except formless.InputError as e:
                results[name] = data.get(name, [''])[0]
                failures[name] = e.reason

        if failures:
            #print "There were validation errors. The errors were: ", failures
            raise formless.ValidateError(failures, "Error:", results)

        if autoConfigure:
            def _except(e):
                failures[''] = e.reason # self.original.name
                raise formless.ValidateError(failures, e.reason, results)
            return exceptblock(self.original.configure, _except, formless.InputError,
                               boundTo, results)
        return results
    def render_listRow(self, ctx: WovenContext, data=None):
        IEventBus("Web").register(self, IWorkLocationChangedEvent)
        listCell = inevow.IQ(ctx).patternGenerator("listCell")
        self.expanded = False
        ctx.fillSlots('index', self._workLocation.workLocationID)
        active = T.input(id='active', type='checkbox', checked=self._workLocation.active)
        workLocationID = T.input(id='name', value=self._workLocation.workLocationID, disabled=True)
        descr = T.input(id='description', value=self._workLocation.description)

        if not self.employee.isAdministrator() or self.parent.selectable:
            active(disabled=True)
            workLocationID(disabled=True)
            descr(disabled=True)

        self.preprocess([active, workLocationID, descr])
        if self.length==4:
            r = [
                listCell(data=dict(listItem='►'))(id='expand-button')[
                    T.Tag("athena:handler")(event='onclick', handler='expand')],
                listCell(data=dict(listItem='▼'))(style='display:none', id='unexpand-button')[
                    T.Tag("athena:handler")(event='onclick', handler='expand')],
            ]
        else:
            r = []
        r.extend([listCell(data=dict(listItem=workLocationID))[T.Tag('athena:handler')(event='ondblclick', handler='expand')],
                  listCell(data=dict(listItem=descr)),
                  listCell(data=dict(listItem=active))])
        return r
Example #9
0
    def doRendering(self, fragmentClass):
        """
        Verify that the given fragment class will render without raising an
        exception.
        """
        siteStore = Store()

        loginSystem = LoginSystem(store=siteStore)
        installOn(loginSystem, siteStore)
        p = Product(store=siteStore, types=["xmantissa.webadmin.LocalUserBrowser",
                                            "xmantissa.signup.SignupConfiguration"])
        account = loginSystem.addAccount(u'testuser', u'localhost', None)
        p.installProductOn(account.avatars.open())
        f = fragmentClass(None, u'testuser', account)

        p = LivePage(
            docFactory=stan(
                html[
                    head(render=directive('liveglue')),
                    body(render=lambda ctx, data: f)]))
        f.setFragmentParent(p)

        ctx = WovenContext()
        req = FakeRequest()
        ctx.remember(req, IRequest)

        d = p.renderHTTP(ctx)
        def rendered(ign):
            p.action_close(None)
        d.addCallback(rendered)
        return d
Example #10
0
 def patternContext(self, ctx, name):
     context = WovenContext(parent=ctx)
     self.rememberStuff(context)
     # XXX: preprocessors?
     doc = self.docFactory.load(context)
     context.tag = inevow.IQ(doc).onePattern(name)
     return context
Example #11
0
    def test_oldFlattenableOverriddenRend(self):
        """
        Flattening L{Element} instances with an overridden C{rend} method
        flattened with the old flatten function results in the flattened
        version of whatever their C{rend} method returns.
        """
        renders = []
        class OldStyleElement(Element):
            docFactory = stan("Hello, world")
            def rend(self, ctx, data):
                return invisible(render=directive("foo"))[Element.rend(self, ctx, data)]

            def foo(self, request, tag):
                renders.append((self, request))
                return p[tag]
            renderer(foo)

        result = []
        element = OldStyleElement()
        request = FakeRequest()
        context = WovenContext()
        context.remember(request)
        finished = oldFlatten(element, context, result.append, lambda ign: ign)
        def cbFinished(ignored):
            self.assertEqual("".join(result), "<p>Hello, world</p>")
            self.assertEqual(renders, [(element, request)])
        finished.addCallback(cbFinished)
        return finished
Example #12
0
    def test_bootstraps(self):
        """
        L{LivePage._bootstraps} should return a list of 2-tuples of
        (initialization method, arguments) of methods to call in JavaScript.

        Specifically, it should invoke Divmod.bootstrap with the page's own
        URL, and Nevow.Athena.bootstrap with the name of the client-side Page
        class to instantiate and the URL to instantiate it with.
        """
        SEG = "'" + '"'
        URI = "http://localhost/" + SEG
        req = FakeRequest(uri='/' + SEG, currentSegments=[SEG])
        ctx = WovenContext()
        ctx.remember(req, IRequest)
        self.page.clientID = 'asdf'
        self.assertEqual(
            self.page._bootstraps(ctx),
            [
                (
                    "Divmod.bootstrap",
                    # Nevow's URL quoting rules are weird, but this is the URL
                    # flattener's fault, not mine.  Adjust to taste if that changes
                    # (it won't) -glyph
                    [u"http://localhost/'%22"]),
                ("Nevow.Athena.bootstrap",
                 [u'Nevow.Athena.PageWidget', u'asdf'])
            ])
Example #13
0
 def _render(self, resource):
     """
     Test helper which tries to render the given resource.
     """
     ctx = WovenContext()
     req = FakeRequest(headers={'host': self.hostname})
     ctx.remember(req, IRequest)
     return req, resource.renderHTTP(ctx)
Example #14
0
 def _render(self, fragment):
     """
     Test helper which tries to render the given fragment.
     """
     ctx = WovenContext()
     req = FakeRequest()
     ctx.remember(req, IRequest)
     return Page(docFactory=stan(fragment)).renderString(ctx)
Example #15
0
 def _render(self, resource):
     """
     Test helper which tries to render the given resource.
     """
     ctx = WovenContext()
     req = FakeRequest(headers={'host': self.hostname})
     ctx.remember(req, IRequest)
     return req, resource.renderHTTP(ctx)
Example #16
0
 def _render(self, page):
     """
     Test helper which tries to render the given page.
     """
     ctx = WovenContext()
     req = FakeRequest()
     ctx.remember(req, IRequest)
     return page.renderHTTP(ctx).addCallback(lambda ign: req.v)
Example #17
0
 def _render(self, fragment, request=None):
     """
     Test helper which tries to render the given fragment.
     """
     ctx = WovenContext()
     if request is None:
         request = FakeRequest()
     ctx.remember(request, IRequest)
     return Page(docFactory=stan(fragment)).renderString(ctx)
Example #18
0
def _makeContext():
    """
    Create the request and context objects necessary for rendering a page.

    @return: A two-tuple of the created L{FakeRequest} and L{WovenContext},
    with the former remembered in the latter.
    """
    request = FakeRequest()
    context = WovenContext()
    context.remember(request, IRequest)
    return (request, context)
Example #19
0
def _makeContext():
    """
    Create the request and context objects necessary for rendering a page.

    @return: A two-tuple of the created L{FakeRequest} and L{WovenContext},
    with the former remembered in the latter.
    """
    request = FakeRequest()
    context = WovenContext()
    context.remember(request, IRequest)
    return (request, context)
Example #20
0
 def test_renderReconnect(self):
     """
     L{LivePage.renderHTTP} should render a JSON-encoded version of its
     clientID rather than a rendered version of its template when provided
     with a special __athena_reconnect__ parameter.
     """
     req = FakeRequest(args={athena.ATHENA_RECONNECT: ["1"]})
     ctx = WovenContext()
     ctx.remember(req, IRequest)
     string = self.page.renderHTTP(ctx)
     jsonifiedID = '"%s"' % (self.page.clientID, )
     self.assertEqual(string, jsonifiedID)
Example #21
0
 def test_unsupportedBrowserPage(self):
     """
     Test that unsupported browsers get told they're unsupported.
     """
     ctx = WovenContext()
     page = athena.LivePage()
     req = FakeRequest()
     req.received_headers[
         'user-agent'] = "Mozilla/4.0 (compatible; MSIE 2.0; Windows NT 5.1)"
     ctx.remember(req, IRequest)
     d = renderPage(page, reqFactory=lambda: req)
     d.addCallback(self.assertEqual,
                   flat.flatten(page.unsupportedBrowserLoader))
     return d
Example #22
0
 def _renderHead(self, result):
     """
     Go through all the gyrations necessary to get the head contents
     produced by the page rendering process.
     """
     site = ISiteURLGenerator(self.siteStore)
     t = tags.invisible()
     ctx = WovenContext(tag=t)
     req = FakeRequest()
     ctx.remember(req, IRequest)
     expected = [th.head(req, site)
                 for th in self.pageFactory._preferredThemes()]
     head = result.render_head(ctx, None)
     return t, expected
Example #23
0
 def test_oldFlattenable(self):
     """
     Flattening L{Element} instances with the old flatten function results in
     the flattened version of whatever their C{render} method returns.
     """
     result = []
     element = Element()
     element.docFactory = stan(['"&<>'])
     request = FakeRequest()
     context = WovenContext()
     context.remember(request)
     finished = oldFlatten(element, context, result.append, lambda ign: ign)
     finished.addCallback(lambda ign: "".join(result))
     finished.addCallback(self.assertEqual, '"&amp;&lt;&gt;')
     return finished
Example #24
0
 def test_oldFlattenable(self):
     """
     Flattening L{Element} instances with the old flatten function results in
     the flattened version of whatever their C{render} method returns.
     """
     result = []
     element = Element()
     element.docFactory = stan(['"&<>'])
     request = FakeRequest()
     context = WovenContext()
     context.remember(request)
     finished = oldFlatten(element, context, result.append, lambda ign: ign)
     finished.addCallback(lambda ign: "".join(result))
     finished.addCallback(self.assertEqual, '"&amp;&lt;&gt;')
     return finished
Example #25
0
    def render_listRow(self, ctx: WovenContext, data):
        listCell = inevow.IQ(ctx).patternGenerator("listCell")
        ctx.fillSlots('searchclass', 'self.searchClass')
        save = self.preprocess([tags.input(id='saveList', type='button', value='Save')[tags.Tag('athena:handler')(event='onclick', handler='saveAll')]])
        if self.length == 1:
            return listCell(data=dict(listItem=save, index=0))
        o = []
        for i in range(self.start):
            o.append(listCell(data=dict(listItem='', index=i)))
        o.extend([listCell(data=dict(listItem="Save Changes", index=0)),
                  listCell(data=dict(listItem=save, index=self.start))])
        for idx in range(self.length - 2 - self.start):
            o.append(listCell(data=dict(listItem='', index=idx + 2 + self.start)))

        return o
Example #26
0
 def _renderHead(self, result):
     """
     Go through all the gyrations necessary to get the head contents
     produced by the page rendering process.
     """
     site = ISiteURLGenerator(self.siteStore)
     t = tags.invisible()
     ctx = WovenContext(tag=t)
     req = FakeRequest()
     ctx.remember(req, IRequest)
     expected = [
         th.head(req, site) for th in self.pageFactory._preferredThemes()
     ]
     head = result.render_head(ctx, None)
     return t, expected
Example #27
0
 def test_oldFlattenableInAttribute(self):
     """
     Flattening a L{Element} as the value of an attribute of a L{Tag} XML
     attribute escapes the element's flattened document.
     """
     result = []
     element = Element()
     element.docFactory = stan(['"&<>'])
     request = FakeRequest()
     context = WovenContext()
     context.remember(request)
     finished = oldFlatten(p(foo=element), context, result.append, lambda ign: ign)
     finished.addCallback(lambda ign: "".join(result))
     finished.addCallback(self.assertEqual, '<p foo="&quot;&amp;&lt;&gt;"></p>')
     return finished
Example #28
0
def precompile(stan, ctx=None):
    """Given the stan and the optional context, return a list of strings and
    Context instances, optimizing as much static content as possible into contiguous
    string runs.
    The Context instances will have Tag instances whose .children have also been
    precompiled.
    """
    from nevow.context import WovenContext
    newctx = WovenContext(precompile=True)
    if ctx is not None:
        macroFactory = inevow.IMacroFactory(ctx, default=None)
        if macroFactory is not None:
            newctx.remember(macroFactory, inevow.IMacroFactory)
    doc = []
    list(iterflatten(stan, newctx, doc.append))
    return doc
Example #29
0
    def _renderHTTP(self, ctx):
        request = inevow.IRequest(ctx)
        ## XXX request is really ctx now, change the name here
        if self.addSlash and inevow.ICurrentSegments(ctx)[-1] != '':
            request.redirect(request.URLPath().child(''))
            return ''

        log.msg(http_render=None, uri=request.uri)

        self.rememberStuff(ctx)

        def finishRequest():
            carryover = request.args.get('_nevow_carryover_', [None])[0]
            if carryover is not None and _CARRYOVER.has_key(carryover):
                del _CARRYOVER[carryover]
            if self.afterRender is not None:
                return util.maybeDeferred(self.afterRender,ctx)

        if self.buffered:
            io = StringIO()
            writer = io.write
            def finisher(result):
                request.write(io.getvalue())
                return util.maybeDeferred(finishRequest).addCallback(lambda r: result)
        else:
            writer = request.write
            def finisher(result):
                return util.maybeDeferred(finishRequest).addCallback(lambda r: result)

        preprocessors = _getPreprocessors(self)
        doc = self.docFactory.load(ctx, preprocessors)
        ctx =  WovenContext(ctx, tags.invisible[doc])

        return self.flattenFactory(doc, ctx, writer, finisher)
Example #30
0
def URLSerializer(original, context):
    """
    Serialize the given L{URL}.

    Unicode path, query and fragment components are handled according to the
    IRI standard (RFC 3987).
    """
    urlContext = WovenContext(parent=context, precompile=context.precompile, inURL=True)
    if original.scheme:
        # TODO: handle Unicode (see #2409)
        yield "%s://%s" % (original.scheme, original.netloc)
    for pathsegment in original._qpathlist:
        yield '/'
        yield serialize(pathsegment, urlContext)
    query = original._querylist
    if query:
        yield '?'
        first = True
        for key, value in query:
            if not first:
                # xhtml can't handle unescaped '&'
                if context.isAttrib is True:
                    yield '&amp;'
                else:
                    yield '&'
            else:
                first = False
            yield serialize(key, urlContext)
            if value is not None:
                yield '='
                yield serialize(value, urlContext)
    if original.fragment:
        yield "#"
        yield serialize(original.fragment, urlContext)
Example #31
0
def URLSerializer(original, context):
    urlContext = WovenContext(parent=context,
                              precompile=context.precompile,
                              inURL=True)
    if original.scheme:
        yield "%s://%s" % (original.scheme, original.netloc)
    for pathsegment in original._qpathlist:
        yield '/'
        yield serialize(pathsegment, urlContext)
    query = original._querylist
    if query:
        yield '?'
        first = True
        for key, value in query:
            if not first:
                yield '&'
            else:
                first = False
            yield serialize(key, urlContext)
            if value is not None:
                yield '='
                yield serialize(value, urlContext)
    if original.fragment:
        yield "#"
        yield serialize(original.fragment, urlContext)
Example #32
0
    def rend(self, context, data):
        # Create a new context so the current context is not polluted with
        # remembrances.
        context = WovenContext(parent=context)

        # Remember me as lots of things
        self.rememberStuff(context)

        preprocessors = _getPreprocessors(self)

        # This tidbit is to enable us to include Page objects inside
        # stan expressions and render_* methods and the like. But
        # because of the way objects can get intertwined, we shouldn't
        # leave the pattern changed.
        old = self.docFactory.pattern
        self.docFactory.pattern = 'content'
        self.docFactory.precompiledDoc = None
        try:
            try:
                doc = self.docFactory.load(context, preprocessors)
            finally:
                self.docFactory.pattern = old
                self.docFactory.precompiledDoc = None
        except TypeError, e:
            # Avert your eyes now! I don't want to catch anything but IQ
            # adaption exceptions here but all I get is TypeError. This whole
            # section of code is a complete hack anyway so one more won't
            # matter until it's all removed. ;-).
            if 'nevow.inevow.IQ' not in str(e):
                raise
            doc = self.docFactory.load(context, preprocessors)
Example #33
0
    def postForm(self, ctx, bindingName, args):
        """Accept a form post to the given bindingName. The bindingName
        can be dotted to indicate an attribute of this Configurable, eg
        addresses.0.changeEmail. The post arguments are given in args.
        Return a Resource which will be rendered in response.
        """
        from formless import iformless
        from nevow.tags import invisible
        request = ctx.locate(inevow.IRequest)
        pathSegs = bindingName.split('.')
        configurable = self

        cf = ctx.locate(iformless.IConfigurableFactory)
        ## Get the first binding
        firstSeg = pathSegs.pop(0)
        binding = configurable.getBinding(ctx, firstSeg)
        ctx.remember(binding, IBinding)
        ctx.remember(configurable, IConfigurable)
        ## I don't think this works right now, it needs to be fixed.
        ## Most cases it won't be triggered, because we're just traversing a
        ## single binding name
        for seg in pathSegs:
            assert 1 == 0, "Sorry, this doesn't work right now"
            binding = configurable.getBinding(ctx, seg)
            child = self.boundTo
            if not isinstance(binding, GroupBinding):
                accessor = inevow.IContainer(configurable.boundTo, None)
                if accessor is None:
                    child = getattr(configurable.boundTo, binding.name)
                else:
                    child = accessor.child(ctx, binding.name)
            ## If it's a groupbinding, we don't do anything at all for this path segment
            
            ## This won't work right now. We need to push the previous configurable
            ## as the configurableFactory somehow and ask that for hte next binding
            ## we also need to support deferreds coming back from locateConfigurable
            assert 'black' is 'white', "Deferred support is pending"
            configurable = cf.locateConfigurable(ctx, child)
            ctx = WovenContext(ctx, invisible(key=seg))
            ctx.remember(binding, IBinding)
            ctx.remember(configurable, IConfigurable)

        bindingProcessor = iformless.IInputProcessor(binding)
        rv = bindingProcessor.process(ctx, binding.boundTo, args)
        ctx.remember(rv, inevow.IHand)
        ctx.remember('%r success.' % bindingName, inevow.IStatusMessage)
        return rv
Example #34
0
def precompile(stan, ctx=None):
    """Given the stan and the optional context, return a list of strings and
    Context instances, optimizing as much static content as possible into contiguous
    string runs.

    The Context instances will have Tag instances whose .children have also been
    precompiled.
    """
    from nevow.context import WovenContext
    newctx = WovenContext(precompile=True)
    if ctx is not None:
        macroFactory = inevow.IMacroFactory(ctx, None)
        if macroFactory is not None:
            newctx.remember(macroFactory, inevow.IMacroFactory)
    doc = []
    list(iterflatten(stan, newctx, doc.append))
    return doc
Example #35
0
 def test_pageJsClassDependencies(self):
     """
     L{LivePage.render_liveglue} should include modules that the
     L{LivePage}'s jsClass depends on.
     """
     self.page.jsClass = u'PythonTestSupport.Dependor.PageTest'
     freq = FakeRequest()
     self.page._becomeLive(url.URL.fromRequest(freq))
     ctx = WovenContext(tag=tags.div())
     ctx.remember(freq, IRequest)
     self.assertEqual(self.page.render_liveglue(ctx, None), ctx.tag)
     expectDependor = flat.flatten(
         self.page.getImportStan(u'PythonTestSupport.Dependor'))
     expectDependee = flat.flatten(
         self.page.getImportStan(u'PythonTestSupport.Dependee'))
     result = flat.flatten(ctx.tag, ctx)
     self.assertIn(expectDependor, result)
     self.assertIn(expectDependee, result)
Example #36
0
    def rend(self, context, data):
        # Create a new context so the current context is not polluted with
        # remembrances.
        context = WovenContext(parent=context)

        # Remember me as lots of things
        self.rememberStuff(context)

        preprocessors = _getPreprocessors(self)

        # This tidbit is to enable us to include Page objects inside
        # stan expressions and render_* methods and the like. But
        # because of the way objects can get intertwined, we shouldn't
        # leave the pattern changed.
        old = self.docFactory.pattern
        self.docFactory.pattern = 'content'
        self.docFactory.precompiledDoc = None
        try:
            try:
                doc = self.docFactory.load(context, preprocessors)
            finally:
                self.docFactory.pattern = old
                self.docFactory.precompiledDoc = None
        except TypeError as e:
            # Avert your eyes now! I don't want to catch anything but IQ
            # adaption exceptions here but all I get is TypeError. This whole
            # section of code is a complete hack anyway so one more won't
            # matter until it's all removed. ;-).
            if 'nevow.inevow.IQ' not in str(e):
                raise
            doc = self.docFactory.load(context, preprocessors)
        except NodeNotFound:
            doc = self.docFactory.load(context, preprocessors)
        else:
            if old == 'content':
                warnings.warn(
                    """[v0.5] Using a Page with a 'content' pattern is
                               deprecated.""",
                    DeprecationWarning,
                    stacklevel=2)

        context.tag = tags.invisible[doc]
        return context
Example #37
0
    def postForm(self, ctx, bindingName, args):
        """Accept a form post to the given bindingName. The bindingName
        can be dotted to indicate an attribute of this Configurable, eg
        addresses.0.changeEmail. The post arguments are given in args.
        Return a Resource which will be rendered in response.
        """
        from formless import iformless
        from nevow.tags import invisible
        request = ctx.locate(inevow.IRequest)
        pathSegs = bindingName.split('.')
        configurable = self

        cf = ctx.locate(iformless.IConfigurableFactory)
        ## Get the first binding
        firstSeg = pathSegs.pop(0)
        binding = configurable.getBinding(ctx, firstSeg)
        ctx.remember(binding, IBinding)
        ctx.remember(configurable, IConfigurable)
        ## I don't think this works right now, it needs to be fixed.
        ## Most cases it won't be triggered, because we're just traversing a
        ## single binding name
        for seg in pathSegs:
            assert 1 == 0, "Sorry, this doesn't work right now"
            binding = configurable.getBinding(ctx, seg)
            child = self.boundTo
            if not isinstance(binding, GroupBinding):
                accessor = inevow.IContainer(configurable.boundTo, None)
                if accessor is None:
                    child = getattr(configurable.boundTo, binding.name)
                else:
                    child = accessor.child(ctx, binding.name)
            ## If it's a groupbinding, we don't do anything at all for this path segment
            
            ## This won't work right now. We need to push the previous configurable
            ## as the configurableFactory somehow and ask that for hte next binding
            ## we also need to support deferreds coming back from locateConfigurable
            assert 'black' is 'white', "Deferred support is pending"
            configurable = cf.locateConfigurable(ctx, child)
            ctx = WovenContext(ctx, invisible(key=seg))
            ctx.remember(binding, IBinding)
            ctx.remember(configurable, IConfigurable)

        bindingProcessor = iformless.IInputProcessor(binding)
        rv = bindingProcessor.process(ctx, binding.boundTo, args)
        ctx.remember(rv, inevow.IHand)
        ctx.remember('%r success.' % bindingName, inevow.IStatusMessage)
        return rv
 def render_listRow(self, ctx: WovenContext, data=None):
     listCell = inevow.IQ(ctx).patternGenerator("listCell")
     self.expanded = False
     ctx.fillSlots('index', self._employee.employee_id)
     if self.length == 3:
         r = [
             listCell(data=dict(listItem='►'))(id='expand-button')[
                 T.Tag("athena:handler")(event='onclick', handler='expand')],
             listCell(data=dict(listItem='▼'))(style='display:none', id='unexpand-button')[
                 T.Tag("athena:handler")(event='onclick', handler='expand')],
         ]
     else:
         r = []
     for k in self.keys:
         if k == 'expanded':
             if not self.parent.selectable:
                 r.append(T.td(style='display:none', id='expanded')[self.tableDocFactory.load(ctx, self.preprocessors)])
             continue
         r.append(listCell(data=dict(listItem=getattr(self._employee, k)))[Tag('athena:handler')(event='ondblclick', handler='expand')])
     return r
Example #39
0
    def test_oldFlattenableError(self):
        """
        If the old flattener encounters an asynchronous Failure while
        flattening an L{IRenderable}, the returned L{Deferred} fires with the
        failure.
        """
        result = Deferred()
        element = Element()
        element.docFactory = stan(result)

        request = FakeRequest()
        context = WovenContext()
        context.remember(request)

        accumulator = []
        finished = oldFlatten(element, context, accumulator.append, lambda ign: ign)
        result.addErrback(lambda err: err.trap(RuntimeError))
        finished = self.assertFailure(finished, RuntimeError)
        result.errback(RuntimeError("test error"))
        return finished
Example #40
0
File: page.py Project: calston/tums
    def rend(self, ctx, data):
        # Unfortunately, we still need a context to make the rest of the
        # rendering process work.  A goal should be to elimate this completely.
        context = WovenContext()

        if self.docFactory is None:
            raise MissingDocumentFactory(self)

        preprocessors = _getPreprocessors(self)

        doc = self.docFactory.load(context, preprocessors)

        context.remember(self, IData)
        context.remember(self, IRenderer)
        context.remember(self, IRendererFactory)
        context.tag = invisible[doc]
        return context
Example #41
0
    def test_rendering(self):
        """
        Test that L{webadmin.UserInteractionFragment} renders without raising
        any exceptions.
        """
        f = UserInteractionFragment(self.browser)

        p = LivePage(docFactory=stan(html[head(render=directive("liveglue")), body(render=lambda ctx, data: f)]))
        f.setFragmentParent(p)

        ctx = WovenContext()
        req = FakeRequest()
        ctx.remember(req, IRequest)

        d = p.renderHTTP(ctx)

        def rendered(ign):
            p.action_close(None)

        d.addCallback(rendered)
        return d
def _ctxForRequest(request, slotData, renderFactory, inAttribute):
    """
    Create a L{WovenContext} which can be used to by the
    backwards-compatibility support of L{IRenderer} and L{getFlattener} to
    continue rendering a response for the given request.
    """
    ctx = WovenContext()
    ctx.isAttrib = inAttribute
    ctx.remember(None, IData) # Even though IData(ctx) can never return None,
                              # remembering None here is somehow very important
                              # for preventing a TypeError from happening when
                              # ctx.locate(IData) is invoked, since it is
                              # sometimes invoked from a codepath other than
                              # __conform__. -exarkun
    ctx.remember(request, IRequest)
    for slotGroup in slotData:
        if slotGroup is not None:
            for k, v in slotGroup.items():
                ctx.fillSlots(k, v)
    if renderFactory is not None:
        ctx.remember(_OldRendererFactory(renderFactory), IRendererFactory)
    return ctx
Example #43
0
 def process(self, context, boundTo, data, autoConfigure = True):
     """Knows how to process a dictionary of lists
     where the dictionary may contain a key with the same
     name as some of the arguments to the MethodBinding
     instance.
     """
     typedValue = self.original.typedValue
     results = {}
     failures = {}
     if data.has_key('----'):
         ## ---- is the "direct object", the one argument you can specify using the command line without saying what the argument name is
         data[typedValue.arguments[0].name] = data['----']
         del data['----']
     for binding in typedValue.arguments:
         name = binding.name
         try:
             context = WovenContext(context, faketag)
             context.remember(binding, iformless.IBinding)
             results[name] = iformless.IInputProcessor(binding.typedValue).process(context, boundTo, data.get(name, ['']))
         except formless.InputError, e:
             results[name] = data.get(name, [''])[0]
             failures[name] = e.reason
Example #44
0
 def process(self, context, boundTo, data, autoConfigure=True):
     """Knows how to process a dictionary of lists
     where the dictionary may contain a key with the same
     name as some of the arguments to the MethodBinding
     instance.
     """
     typedValue = self.original.typedValue
     results = {}
     failures = {}
     if data.has_key("----"):
         data[typedValue.arguments[0].name] = data["----"]
         del data["----"]
     for binding in typedValue.arguments:
         name = binding.name
         try:
             context = WovenContext(context, faketag)
             context.remember(binding, iformless.IBinding)
             results[name] = iformless.IInputProcessor(binding.typedValue).process(
                 context, boundTo, data.get(name, [""])
             )
         except formless.InputError, e:
             results[name] = data.get(name, [""])[0]
             failures[name] = e.reason
Example #45
0
    def test_rendering(self):
        """
        Test that L{webadmin.UserInteractionFragment} renders without raising
        any exceptions.
        """
        f = UserInteractionFragment(self.browser)

        p = LivePage(
            docFactory=stan(
                html[
                    head(render=directive('liveglue')),
                    body(render=lambda ctx, data: f)]))
        f.setFragmentParent(p)

        ctx = WovenContext()
        req = FakeRequest()
        ctx.remember(req, IRequest)

        d = p.renderHTTP(ctx)
        def rendered(ign):
            p.action_close(None)
        d.addCallback(rendered)
        return d
Example #46
0
    def _render(self, element):
        """
        Put the given L{IRenderer} provider into an L{athena.LivePage} and
        render it.  Return a Deferred which fires with the request object used
        which is an instance of L{nevow.testutil.FakeRequest}.
        """
        p = LivePage(docFactory=stan(html[head(
            render=directive('liveglue')), body[invisible(
                render=lambda ctx, data: element)]]))
        element.setFragmentParent(p)

        ctx = WovenContext()
        req = FakeRequest()
        ctx.remember(req, IRequest)

        d = p.renderHTTP(ctx)

        def rendered(ign):
            p.action_close(None)
            return req

        d.addCallback(rendered)
        return d
Example #47
0
    def _render(self, element):
        """
        Put the given L{IRenderer} provider into an L{athena.LivePage} and
        render it.  Return a Deferred which fires with the request object used
        which is an instance of L{nevow.testutil.FakeRequest}.
        """
        p = LivePage(
            docFactory=stan(
                html[
                    head(render=directive('liveglue')),
                    body[
                        invisible(render=lambda ctx, data: element)]]))
        element.setFragmentParent(p)

        ctx = WovenContext()
        req = FakeRequest()
        ctx.remember(req, IRequest)

        d = p.renderHTTP(ctx)
        def rendered(ign):
            p.action_close(None)
            return req
        d.addCallback(rendered)
        return d
Example #48
0
def FunctionSerializer(original, context, nocontextfun=FunctionSerializer_nocontext):
    if context.precompile:
        return WovenContext(tag=invisible(render=original))
    else:
        data = convertToData(context.locate(IData), context)
        try:
            nocontext = nocontextfun(original)
            if nocontext is True:
                result = original(data)
            else:
                if nocontext is PASS_SELF:
                    renderer = context.locate(IRenderer)
                    result = original(renderer, context, data)
                else:
                    result = original(context, data)
        except StopIteration:
            raise RuntimeError, "User function %r raised StopIteration." % original
        return serialize(result, context)
Example #49
0
 def postForm(self, ctx, bindingName, args):
     """Accept a form post to the given bindingName. The bindingName
     can be dotted to indicate an attribute of this Configurable, eg
     addresses.0.changeEmail. The post arguments are given in args.
     Return a Resource which will be rendered in response.
     """
     from formless import iformless
     from nevow.tags import invisible
     request = ctx.locate(inevow.IRequest)
     pathSegs = bindingName.split('.')
     configurable = self
     cf = ctx.locate(iformless.IConfigurableFactory)
     firstSeg = pathSegs.pop(0)
     binding = configurable.getBinding(ctx, firstSeg)
     ctx.remember(binding, IBinding)
     ctx.remember(configurable, IConfigurable)
     for seg in pathSegs:
         assert 1 == 0, "Sorry, this doesn't work right now"
         binding = configurable.getBinding(ctx, seg)
         child = self.boundTo
         if not isinstance(binding, GroupBinding):
             accessor = inevow.IContainer(configurable.boundTo, None)
             if accessor is None:
                 child = getattr(configurable.boundTo, binding.name)
             else:
                 child = accessor.child(ctx, binding.name)
         assert 'black' is 'white', "Deferred support is pending"
         configurable = cf.locateConfigurable(ctx, child)
         ctx = WovenContext(ctx, invisible(key=seg))
         ctx.remember(binding, IBinding)
         ctx.remember(configurable, IConfigurable)
     bindingProcessor = iformless.IInputProcessor(binding)
     rv = bindingProcessor.process(ctx, binding.boundTo, args)
     ctx.remember(rv, inevow.IHand)
     ctx.remember('%r success.' % bindingName, inevow.IStatusMessage)
     return rv
Example #50
0
    def renderString(self, ctx=None):
        """Render this page outside of the context of a web request, returning
        a Deferred which will result in a string.

        If twisted is not installed, this method will return a string result immediately,
        and this method is equivalent to renderSynchronously.
        """
        io = StringIO()
        writer = io.write

        def finisher(result):
            return io.getvalue()

        ctx = PageContext(parent=ctx, tag=self)
        self.rememberStuff(ctx)
        doc = self.docFactory.load(ctx)
        ctx = WovenContext(ctx, tags.invisible[doc])

        return self.flattenFactory(doc, ctx, writer, finisher)
Example #51
0
    def renderSynchronously(self, ctx=None):
        """Render this page synchronously, returning a string result immediately.
        Raise an exception if a Deferred is required to complete the rendering
        process.
        """
        io = StringIO()

        ctx = PageContext(parent=ctx, tag=self)
        self.rememberStuff(ctx)
        doc = self.docFactory.load(ctx)
        ctx = WovenContext(ctx, tags.invisible[doc])

        def raiseAlways(item):
            raise NotImplementedError("renderSynchronously can not support"
                                      " rendering: %s" % (item, ))

        list(flat.iterflatten(doc, ctx, io.write, raiseAlways))

        return io.getvalue()
Example #52
0
def URLSerializer(original, context):
    """
    Serialize the given L{URL}.

    Unicode path, query and fragment components are handled according to the
    IRI standard (RFC 3987).
    """
    def _maybeEncode(s):
        if isinstance(s, str):
            s = s.encode('utf-8')
        return s
    urlContext = WovenContext(parent=context, precompile=context.precompile, inURL=True)
    if original.scheme:
        # TODO: handle Unicode (see #2409)
        yield b"%s://%s" % (toBytes(original.scheme), toBytes(original.netloc))
    for pathsegment in original._qpathlist:
        yield b'/'
        yield serialize(_maybeEncode(pathsegment), urlContext)
    query = original._querylist
    if query:
        yield b'?'
        first = True
        for key, value in query:
            if not first:
                # xhtml can't handle unescaped '&'
                if context.isAttrib is True:
                    yield b'&amp;'
                else:
                    yield b'&'
            else:
                first = False
            yield serialize(_maybeEncode(key), urlContext)
            if value is not None:
                yield b'='
                yield serialize(_maybeEncode(value), urlContext)
    if original.fragment:
        yield b"#"
        yield serialize(_maybeEncode(original.fragment), urlContext)
    def render_listRow(self, ctx: WovenContext, data=None):
        IEventBus("Web").register(self, ITimeEntryChangedEvent)
        listCell = inevow.IQ(ctx).patternGenerator("listCell")
        original = self._timeEntry.original
        self.expanded = False
        st = self._timeEntry.period.startTime()
        et = self._timeEntry.period.endTime(False)
        ctx.fillSlots('index', self._timeEntry.storeID)
        approved = T.input(id='approved', type='checkbox', checked=self._timeEntry.approved)[
            T.Tag('athena-handler')(event='onchange', handler='approvedClicked')
        ]
        te_type = T.input(id='entryType', type='text', disabled=True, value=self._timeEntry.type.name)
        if self._timeEntry.workLocation:
            WLs = self._timeEntry.getEmployee().getWorkLocations()
            missing = False
            if self._timeEntry.workLocation not in WLs:
                WLs.append(self._timeEntry.workLocation)
                missing = True
            workLocationID = T.select(id='workLocation', value=self._timeEntry.workLocation.workLocationID)[
                [T.option(value=i.workLocationID, selected=self._timeEntry.workLocation == i)[i.description] for i in
                 WLs]
            ]
            if missing:
                workLocationID(style='background-color:red')
        else:
            workLocationID = T.input(id='workLocation', disabled=True, value='None')
        if self._timeEntry.subAccount:
            SAs = list(self._timeEntry.getEmployee().getSubAccounts())
            missing = False
            if self._timeEntry.subAccount not in SAs:
                SAs.append(self._timeEntry.subAccount)
                missing = True
            subAccount = T.select(id='subAccount', value=self._timeEntry.subAccount.sub)[
                [T.option(value=i.sub, selected=self._timeEntry.subAccount==i)[i.name] for i in SAs]
            ]
            if missing:
                subAccount(style='background-color:red')
        else:
            subAccount = T.input(id='subAccount', disabled=True, value="None")
        duration = T.input(id='duration', value=formatTimeDelta(self._timeEntry.period.duration()))
        if self._timeEntry.type == IEntryType("Work"):
            ET = T.input(id='endTime', value=et.strftime('%Y-%m-%d %H:%M:%S %Z') if et else 'None')
            et = listCell(data=dict(listItem=ET))
            reject = T.input(id='denied', type='checkbox', checked=self._timeEntry.denied)[
                T.Tag('athena-handler')(event='onchange', handler='deniedClicked')
            ]

            rj = listCell(data=dict(listItem=reject))
            duration(disabled=True)
        else:
            ET = T.input(style='display:none', id='entryType', value=self._timeEntry.type.getTypeName())
            et = listCell(data=dict(listItem=ET))
            reject = T.input(id='denied', type='checkbox', checked=self._timeEntry.denied)
            rj = listCell(data=dict(listItem=reject))

        startTime = T.input(id='startTime', value=st.strftime('%Y-%m-%d %H:%M:%S %Z') if st else 'None')
        if self._timeEntry.employee.timeEntry is self._timeEntry:
            ET(disabled=True)
        if not self.employee.isAdministrator() or self.parent.selectable or self.employee is self._timeEntry.employee:

            workLocationID(disabled=True)
            subAccount(disabled=True)
            startTime(disabled=True)
            ET(disabled=True)
            duration(disabled=True)
            if self._timeEntry.denied or \
                    self._timeEntry.approved or \
                    not self.employee.isSupervisor() or \
                    self._timeEntry.employee not in ISupervisor(self.employee).getEmployees() or \
                    self._timeEntry.subAccount not in ISupervisor(self.employee).getSubAccounts() or \
                    self.parent.selectable or \
                    self.employee is self._timeEntry.employee:
                reject(disabled=True)
                approved(disabled=True)
        startday = self._timeEntry.startTime().date()
        endday = startday.replace(hours=23, minutes=59, seconds=59)

        store = self._timeEntry.store
        q = AND(
            TimeEntry.period==TimePeriod.storeID,
            TimeEntry.employee==self._timeEntry.employee,
            TimeEntry.denied==False,
            TimePeriod._startTime <= endday,
            OR(TimePeriod._endTime >= startday,
               TimePeriod._endTime==None
            )
        )
        if store:
            entries = ICalendarData([i[0] for i in store.query((TimeEntry, TimePeriod), q)]).between(startday, endday)
        else:
            entries = ICalendarData([])

        lastEntryOfDay = entries.entries and entries.entries[-1].startTime() == self._timeEntry.startTime()
        if lastEntryOfDay:
            total = T.input(id='total', value=formatTimeDelta(entries.sumBetween(startday, endday)))
            total(disabled=True)
            total = listCell(data=dict(listItem=total))
        else:
            total = listCell(data=dict(listItem=""))(style='opacity: 0')
        self.preprocess([approved, workLocationID, subAccount, startTime, ET, duration, reject, total])
        r = [listCell(data=dict(listItem=te_type)),
             listCell(data=dict(listItem=workLocationID if not self._showEmployee else T.input(disabled=True, value=self._timeEntry.employee.name))),
             listCell(data=dict(listItem=subAccount)),
             listCell(data=dict(listItem=startTime)),
             et,
             listCell(data=dict(listItem=duration)),
             total,
             listCell(data=dict(listItem=approved)),
             rj]
        if original and original.period:
            workLocationID(title=original.workLocation.description)
            subAccount(title=original.subAccount.name)
            startTime(title=original.startTime().strftime('%Y-%m-%d %H:%M:%S %Z'))
            ET(title=original.endTime(False).strftime('%Y-%m-%d %H:%M:%S %Z') if original.endTime(False) else 'None')
        return r
Example #54
0
def TagSerializer(original, context, contextIsMine=False):
    """
    Original is the tag.
    Context is either:
      - the context of someone up the chain (if contextIsMine is False)
      - this tag's context (if contextIsMine is True)
    """
    #    print "TagSerializer:",original, "ContextIsMine",contextIsMine, "Context:",context
    visible = bool(original.tagName)

    if visible and context.isAttrib:
        raise RuntimeError("Tried to render tag '%s' in an tag attribute context." % (original.tagName))

    if context.precompile and original.macro:
        toBeRenderedBy = original.macro
        ## Special case for directive; perhaps this could be handled some other way with an interface?
        if isinstance(toBeRenderedBy, directive):
            toBeRenderedBy = IMacroFactory(context).macro(context, toBeRenderedBy.name)
        original.macro = Unset
        newContext = WovenContext(context, original)
        yield serialize(toBeRenderedBy(newContext), newContext)
        return

    ## TODO: Do we really need to bypass precompiling for *all* specials?
    ## Perhaps just render?
    if context.precompile and (
        [x for x in list(original._specials.values()) if x is not None and x is not Unset] or original.slotData
    ):
        ## The tags inside this one get a "fresh" parent chain, because
        ## when the context yielded here is serialized, the parent
        ## chain gets reconnected to the actual parents at that
        ## point, since the render function here could change
        ## the actual parentage hierarchy.
        nestedcontext = WovenContext(precompile=context.precompile, isAttrib=context.isAttrib)

        # If necessary, remember the MacroFactory onto the new context chain.
        macroFactory = IMacroFactory(context, None)
        if macroFactory is not None:
            nestedcontext.remember(macroFactory, IMacroFactory)

        original = original.clone(deep=False)
        if not contextIsMine:
            context = WovenContext(context, original)
        context.tag.children = precompile(context.tag.children, nestedcontext)

        yield context
        return

    ## Don't render patterns
    if original.pattern is not Unset and original.pattern is not None:
        return

    if not contextIsMine:
        if original.render:
            ### We must clone our tag before passing to a render function
            original = original.clone(deep=False)
        context = WovenContext(context, original)

    if original.data is not Unset:
        newdata = convertToData(original.data, context)
        if isinstance(newdata, util.Deferred):
            yield newdata.addCallback(lambda newdata: _datacallback(newdata, context))
        else:
            _datacallback(newdata, context)

    if original.render:
        ## If we have a render function we want to render what it returns,
        ## not our tag
        toBeRenderedBy = original.render
        # erase special attribs so if the renderer returns the tag,
        # the specials won't be on the context twice.
        original._clearSpecials()
        yield serialize(toBeRenderedBy, context)
        return

    if not visible:
        for child in original.children:
            yield serialize(child, context)
        return

    yield "<%s" % original.tagName
    if original.attributes:
        attribContext = WovenContext(parent=context, precompile=context.precompile, isAttrib=True)
        for (k, v) in sorted(original.attributes.items()):
            if v is None:
                continue
            yield ' %s="' % k
            yield serialize(v, attribContext)
            yield '"'
    if not original.children:
        if original.tagName in allowSingleton:
            yield " />"
        else:
            yield "></%s>" % original.tagName
    else:
        yield ">"
        for child in original.children:
            yield serialize(child, context)
        yield "</%s>" % original.tagName
Example #55
0
 def data_tableHeader(self, ctx: WovenContext, data):
     ctx.fillSlots('tableTitle', self.name)
     tw = len(self.cols)
     ctx.fillSlots('titleWidth', tw)