def test_anyuri_uri_value(self): _link = "http://arskom.com.tr/" _text = "Arskom" class C(ComplexModel): c = AnyUri class SomeService(ServiceBase): @srpc(_returns=Array(C)) def some_call(): return [C(c=AnyUri.Value(_link, text=_text))] app = Application( [SomeService], 'tns', in_protocol=HttpRpc(), out_protocol=HtmlColumnTable(field_name_attr='class')) server = WsgiApplication(app) out_string = call_wsgi_app_kwargs(server) elt = html.fromstring(out_string) print(html.tostring(elt, pretty_print=True)) assert elt.xpath('//td[@class="c"]')[0][0].tag == 'a' assert elt.xpath('//td[@class="c"]')[0][0].text == _text assert elt.xpath('//td[@class="c"]')[0][0].attrib['href'] == _link
def test_column_href_string_with_substitution(self): _link = "http://arskom.com.tr/?spyne_test=%s" class C(ComplexModel): c = Unicode(pa={HtmlColumnTable: dict(href=_link)}) class SomeService(ServiceBase): @srpc(_returns=C) def some_call(): return C(c="hello") app = Application( [SomeService], 'tns', in_protocol=HttpRpc(), out_protocol=HtmlColumnTable(field_name_attr='class')) server = WsgiApplication(app) out_string = call_wsgi_app_kwargs(server) elt = html.fromstring(out_string) print(html.tostring(elt, pretty_print=True)) assert elt.xpath('//td[@class="c"]')[0][0].tag == 'a' assert elt.xpath( '//td[@class="c"]')[0][0].attrib['href'] == _link % "hello"
def test_string_array(self): class SomeService(ServiceBase): @srpc(String(max_occurs='unbounded'), _returns=Array(String)) def some_call(s): return s app = Application([SomeService], 'tns', in_protocol=HttpRpc(), out_protocol=HtmlColumnTable( field_name_attr=None, field_type_name_attr=None)) server = WsgiApplication(app) out_string = call_wsgi_app(server, body_pairs=(('s', '1'), ('s', '2'))) elt = etree.fromstring(out_string) show(elt, "TestHtmlColumnTable.test_string_array") assert out_string.decode('utf8') == \ '<table class="string">' \ '<thead>' \ '<tr><th>some_callResponse</th></tr>' \ '</thead>' \ '<tbody>' \ '<tr><td>1</td></tr>' \ '<tr><td>2</td></tr>' \ '</tbody>' \ '</table>'
class ExtListScreen(ScreenBase): main = Array( Extensions.customize( child_attrs_all=dict(exc=False, ), child_attrs=dict(id=dict(prot=HrefWidget("/get_ext?id={}")), **EXTENSION_CUST), ), prot=HtmlColumnTable(before_table=_write_new_ext_link), )
class SipBuddyListScreen(ScreenBase): main = Array( SipBuddy.customize( child_attrs_all=dict(exc=True,), child_attrs=dict( id=dict(prot=HrefWidget("/get_sip_buddy?id={}")), **SIP_BUDDY_CUST ), ), prot=HtmlColumnTable(before_table=_write_new_sip_buddy_link), )
def test_complex_array(self): class SomeService(ServiceBase): @srpc(CCM, _returns=Array(CCM)) def some_call(ccm): return [ccm] * 5 app = Application( [SomeService], 'tns', in_protocol=HttpRpc(), out_protocol=HtmlColumnTable(field_name_attr='class')) server = WsgiApplication(app) out_string = call_wsgi_app_kwargs( server, ccm_i='456', ccm_s='def', ccm_c_i='123', ccm_c_s='abc', ) elt = etree.fromstring(out_string) show(elt, 'TestHtmlColumnTable.test_complex_array') elt = html.fromstring(out_string) row, = elt[0] # thead cell = row.findall('th[@class="i"]') assert len(cell) == 1 assert cell[0].text == 'i' cell = row.findall('th[@class="s"]') assert len(cell) == 1 assert cell[0].text == 's' for row in elt[1]: # tbody cell = row.xpath('td[@class="i"]') assert len(cell) == 1 assert cell[0].text == '456' cell = row.xpath('td[@class="c"]//td[@class="i"]') assert len(cell) == 1 assert cell[0].text == '123' cell = row.xpath('td[@class="c"]//td[@class="s"]') assert len(cell) == 1 assert cell[0].text == 'abc' cell = row.xpath('td[@class="s"]') assert len(cell) == 1 assert cell[0].text == 'def'
def test_string_array(self): class SomeService(ServiceBase): @srpc(String(max_occurs='unbounded'), _returns=Array(String)) def some_call(s): return s app = Application([SomeService], 'tns', in_protocol=HttpRpc(), out_protocol=HtmlColumnTable()) server = WsgiApplication(app) out_string = call_wsgi_app(server, body_pairs=(('s', '1'), ('s', '2'))) elt = etree.fromstring(out_string) show(elt, "TestHtmlColumnTable.test_string_array") assert out_string == \ '<table xmlns="http://www.w3.org/1999/xhtml" class="string">' \ '<thead><tr><th class="some_callResponse">some_callResponse</th></tr></thead>' \ '<tbody><tr><td>1</td></tr><tr><td>2</td></tr></tbody>' \ '</table>'
def test_anyuri_string(self): _link = "http://arskom.com.tr/" class C(ComplexModel): c = AnyUri class SomeService(ServiceBase): @srpc(_returns=Array(C)) def some_call(): return [C(c=_link)] app = Application( [SomeService], 'tns', in_protocol=HttpRpc(), out_protocol=HtmlColumnTable(field_name_attr='class')) server = WsgiApplication(app) out_string = call_wsgi_app_kwargs(server) elt = html.fromstring(out_string) show(elt, "TestHtmlColumnTable.test_anyuri_string") assert elt.xpath('//td[@class="c"]')[0][0].tag == 'a' assert elt.xpath('//td[@class="c"]')[0][0].attrib['href'] == _link
class CardListScreen(ScreenBase): main = Array( Card.customize(child_attrs=dict( id=dict(prot=HrefWidget("/get_card?id={}")), ), ), prot=HtmlColumnTable(before_table=_write_new_card_link), )
def test_row_subprot(self): from lxml.html.builder import E from spyne.protocol.html import HtmlBase from spyne.util.six.moves.urllib.parse import urlencode from spyne.protocol.html import HtmlMicroFormat class SearchProtocol(HtmlBase): def to_parent(self, ctx, cls, inst, parent, name, **kwargs): s = self.to_unicode(cls._type_info['query'], inst.query) q = urlencode({"q": s}) parent.write( E.a("Search %s" % inst.query, href="{}?{}".format(inst.uri, q))) def column_table_gen_header(self, ctx, cls, parent, name): parent.write( E.thead(E.th("Search", **{'class': 'search-link'}))) def column_table_before_row(self, ctx, cls, inst, parent, name, **_): ctxstack = getattr(ctx.protocol[self], 'array_subprot_ctxstack', []) tr_ctx = parent.element('tr') tr_ctx.__enter__() ctxstack.append(tr_ctx) td_ctx = parent.element('td', **{'class': "search-link"}) td_ctx.__enter__() ctxstack.append(td_ctx) ctx.protocol[self].array_subprot_ctxstack = ctxstack def column_table_after_row(self, ctx, cls, inst, parent, name, **kwargs): ctxstack = ctx.protocol[self].array_subprot_ctxstack for elt_ctx in reversed(ctxstack): elt_ctx.__exit__(None, None, None) del ctxstack[:] class Search(ComplexModel): query = Unicode uri = Unicode SearchTable = Array( Search.customize(prot=SearchProtocol()), prot=HtmlColumnTable(field_type_name_attr=None), ) class SomeService(ServiceBase): @srpc(_returns=SearchTable) def some_call(): return [ Search(query='Arskom', uri='https://www.google.com/search'), Search(query='Spyne', uri='https://www.bing.com/search'), ] app = Application([SomeService], 'tns', in_protocol=HttpRpc(), out_protocol=HtmlMicroFormat()) server = WsgiApplication(app) out_string = call_wsgi_app_kwargs(server) elt = html.fromstring(out_string) print(html.tostring(elt, pretty_print=True)) assert elt.xpath('//td[@class="search-link"]/a/text()') == \ ['Search Arskom', 'Search Spyne'] assert elt.xpath('//td[@class="search-link"]/a/@href') == [ 'https://www.google.com/search?q=Arskom', 'https://www.bing.com/search?q=Spyne', ] assert elt.xpath('//th[@class="search-link"]/text()') == ["Search"]