def create(self, req, filename=None, verbose=True): html = [] chars = [] for entry in DBSession.query(Parameter).order_by(Parameter.name).limit(10000): if entry.name[0] not in chars: html.append('<h2>%s</h2>' % entry.name[0]) chars.append(entry.name[0]) html.append('<h3>%s</h3>' % entry.name) adapter = get_adapter( IRepresentation, entry, req, ext='snippet.html') html.append(adapter.render(entry, req)) html.append('<p class="separator"> <p>') with open(self.abspath(req).as_posix(), 'wb') as fp: pisa.CreatePDF( html_tmpl % ( css_tmpl.format(charis_font_spec_css()), req.dataset.name, """ <h1 style="font-size: 12mm;">%s</h1> <blockquote style="font-size: 7mm;">edited by %s</blockquote>""" % ( req.dataset.name, req.dataset.formatted_editors()), ''.join(html)), dest=fp)
def contribution_detail_html(context=None, request=None, **kw): if context.id == 's4': raise HTTPFound(request.route_url('genealogy')) c = context.description if '<body>' in c: c = c.split('<body>')[1].split('</body>')[0] adapter = get_adapter(IRepresentation, Feature(), request, ext='snippet.html') fids = [ m.group('fid') for m in re.finditer('__values_(?P<fid>[0-9A-Z]+)__', c) ] for feature in DBSession.query(Feature)\ .filter(Feature.id.in_(fids))\ .options(joinedload(Feature.domain)): counts = DBSession.query(Value.domainelement_pk, func.count(Value.pk))\ .filter(Value.domainelement_pk.in_([de.pk for de in feature.domain]))\ .group_by(Value.domainelement_pk) feature.counts = dict(counts) table = soup(adapter.render(feature, request)) values = '\n'.join('%s' % table.find(tag).extract() for tag in ['thead', 'tbody']) c = c.replace('__values_%s__' % feature.id, values) return {'text': c.replace('http://wals.info', request.application_url)}
def dataset_detail_html(context=None, request=None, **kw): return { 'stats': context.get_stats( [rsc for rsc in RESOURCES if rsc.name in 'language contributor valueset'.split()]), 'example_contribution': Contribution.get('1'), 'citation': get_adapter(IRepresentation, context, request, ext='md.txt')}
def coins(req, obj, label=''): if not isinstance(obj, bibtex.Record): adapter = get_adapter(interfaces.IMetadata, obj, req, ext='md.bib') if not adapter: return label obj = adapter.rec(obj, req) co = ContextObject.from_bibtex('clld', obj) return HTML.span(label, **co.span_attrs())
def coins(req, obj, label=''): if not isinstance(obj, bibtex.Record): adapter = get_adapter(interfaces.IMetadata, obj, req, ext='md.bib') if not adapter: return label obj = adapter.rec(obj, req) co = ContextObject.from_bibtex('clld', obj) return HTML.span(label, **co.span_attrs())
def view(interface, ctx, req): """renders a resource as pyramid response using the most appropriate adapter for the accept header sent. """ adapter = get_adapter( interface, ctx, req, ext=req.matchdict and req.matchdict.get('ext')) if not adapter: raise HTTPNotAcceptable() return adapter.render_to_response(ctx, req)
def test_get_adapter(self): from clld.web.adapters import get_adapter self.assertEqual( None, get_adapter(IIndex, Language, self.env['request'], name='text/html'))
def view(interface, ctx, req): """renders a resource as pyramid response using the most appropriate adapter for the accept header sent. """ adapter = get_adapter(interface, ctx, req, ext=req.matchdict and req.matchdict.get('ext')) if not adapter: raise HTTPNotAcceptable() return adapter.render_to_response(ctx, req)
def dataset_detail_html(context=None, request=None, **kw): return { 'stats': context.get_stats([ rsc for rsc in RESOURCES if rsc.name in 'language contributor valueset'.split() ]), 'example_contribution': Contribution.get('1'), 'citation': get_adapter(IRepresentation, context, request, ext='md.txt') }
def view(interface, ctx, req, getadapters=False): """renders a resource as pyramid response using the most appropriate adapter for the accept header sent. """ # # if req.matched_route.name.endswith('_alt') -> add rel="canonical" header # else: add rel="alternate" header # adapter, adapters = get_adapter( interface, ctx, req, ext=req.matchdict and req.matchdict.get('ext'), getall=True) if not adapter: raise pyramid.httpexceptions.HTTPNotAcceptable() res = adapter.render_to_response(ctx, req) if getadapters: res = (res, adapters) return res
def contribution_detail_html(context=None, request=None, **kw): if context.id == 's4': raise HTTPFound(request.route_url('genealogy')) p = path(wals3.__file__).dirname().joinpath( 'static', 'descriptions', str(context.id), 'body.xhtml') c = codecs.open(p, encoding='utf8').read() adapter = get_adapter(IRepresentation, Feature(), request, ext='snippet.html') for feature in DBSession.query(Feature)\ .filter(Feature.contribution_pk == context.pk)\ .options(joinedload_all(Feature.domain, DomainElement.values)): table = soup(adapter.render(feature, request)) values = '\n'.join(unicode(table.find(tag).extract()) for tag in ['thead', 'tbody']) c = c.replace('__values_%s__' % feature.id, values) return {'text': c.replace('http://wals.info', request.application_url)}
def view(interface, ctx, req, getadapters=False): """renders a resource as pyramid response using the most appropriate adapter for the accept header sent. """ # # if req.matched_route.name.endswith('_alt') -> add rel="canonical" header # else: add rel="alternate" header # adapter, adapters = get_adapter(interface, ctx, req, ext=req.matchdict and req.matchdict.get('ext'), getall=True) if not adapter: raise pyramid.httpexceptions.HTTPNotAcceptable() res = adapter.render_to_response(ctx, req) if getadapters: res = (res, adapters) return res
def view(interface, ctx, req, getadapters=False): """Render a resource as pyramid response. Using the most appropriate adapter for the accept header sent. :param getadapters: If True, the adapter used to render the response and the list of\ all available adapters for the context are returned as well. """ # # if req.matched_route.name.endswith('_alt') -> add rel="canonical" header # else: add rel="alternate" header # adapter, adapters = get_adapter( interface, ctx, req, ext=req.matchdict and req.matchdict.get('ext'), getall=True) if not adapter: raise pyramid.httpexceptions.HTTPNotAcceptable() res = adapter.render_to_response(ctx, req) if getadapters: return res, adapter, adapters return res # pragma: no cover
def view(interface, ctx, req, getadapters=False): """Render a resource as pyramid response. Using the most appropriate adapter for the accept header sent. :param getadapters: If True, the adapter used to render the response and the list of\ all available adapters for the context are returned as well. """ # # if req.matched_route.name.endswith('_alt') -> add rel="canonical" header # else: add rel="alternate" header # adapter, adapters = get_adapter( interface, ctx, req, ext=req.matchdict and req.matchdict.get('ext'), getall=True) if not adapter: raise pyramid.httpexceptions.HTTPNotAcceptable() res = adapter.render_to_response(ctx, req) if getadapters: return res, adapter, adapters return res # pragma: no cover
def contribution_detail_html(context=None, request=None, **kw): if context.id == 's4': raise HTTPFound(request.route_url('genealogy')) p = path(wals3.__file__).dirname().joinpath('static', 'descriptions', str(context.id), 'body.xhtml') c = codecs.open(p, encoding='utf8').read() adapter = get_adapter(IRepresentation, Feature(), request, ext='snippet.html') for feature in DBSession.query(Feature)\ .filter(Feature.contribution_pk == context.pk)\ .options(joinedload_all(Feature.domain, DomainElement.values)): table = soup(adapter.render(feature, request)) values = '\n'.join('%s' % table.find(tag).extract() for tag in ['thead', 'tbody']) c = c.replace('__values_%s__' % feature.id, values) return {'text': c.replace('http://wals.info', request.application_url)}
def create(self, req, filename=None, verbose=True, link_callback=None, lang=None): html = [] lang = lang or Language.get('afr') entries = list( DBSession.query(ValueSet).join(ValueSet.parameter).filter( ValueSet.language_pk == lang.pk).order_by( Taxon.kingdom, Taxon.phylum, Taxon.class_, Taxon.order, Taxon.family, Parameter.name).options(contains_eager(ValueSet.parameter), joinedload(ValueSet.values))) for kingdom, taxa1 in groupby(entries, key=lambda vs: vs.parameter.kingdom): html.append('<h2>Kingdom: %s</h2>' % (kingdom or 'other')) for phylum, taxa2 in groupby(taxa1, key=lambda vs: vs.parameter.phylum): html.append('<h3>Phylum: %s</h3>' % (phylum or 'other')) for class_, taxa3 in groupby( taxa2, key=lambda vs: vs.parameter.class_): html.append('<h4>Class: %s</h4>' % (class_ or 'other')) for order, taxa4 in groupby( taxa3, key=lambda vs: vs.parameter.order): html.append('<h5>Order: %s</h5>' % (order or 'other')) for family, taxa5 in groupby( taxa4, key=lambda vs: vs.parameter.family): html.append('<h6>Family: %s</h6>' % (family or 'other')) for entry in taxa5: adapter = get_adapter(IRepresentation, entry, req, ext='snippet.html') html.append(adapter.render(entry, req)) html.append('<p class="separator"> <p>') with open(str(download_path('%s.pdf' % lang.id)), 'wb') as fp: editors = '' if lang.contribution.contributor_assocs: editors = 'edited by ' + ' and '.join( c.last_first() for c in lang.contribution.primary_contributors) pisa.CreatePDF( html_tmpl % (css_tmpl.format( charis_font_spec_css()), req.resource_url(req.dataset), """ <h1 style="text-align: center; font-size: 12mm;">%(language)s names for Plants and Animals</h1> <h2 style="text-align: center; font-size: 8mm;">%(editors)s</h2> <p style="font-size: 5mm;"> This document was created from <a href="%(url)s">%(dataset)s</a> on %(date)s. </p> <p style="font-size: 5mm;"> %(dataset)s is published under a %(license)s and should be cited as </p> <blockquote style="font-size: 5mm;"><i>%(citation)s</i></blockquote> <p style="font-size: 5mm;"> A full list of contributors is available at <a href="%(url)scontributors">%(url)scontributors</a> </p> <p style="font-size: 5mm;"> The list of references cited in this document is available at <a href="%(url)ssources">%(url)ssources</a> </p> """ % dict(language=lang.name, editors=editors, dataset=req.dataset.name, url=req.resource_url(req.dataset), date=date.today(), citation=text_citation(req, req.dataset), license=req.dataset.jsondata['license_name']), ''.join(html)), dest=fp, link_callback=link_callback, )
def dump(self, req, fp, item, index): adapter = get_adapter(IRepresentation, item, req, ext=self.ext) self.dump_rendered(req, fp, item, index, adapter.render(item, req))
def test_get_adapter(env): from clld.web.adapters import get_adapter assert get_adapter(IIndex, Language, env['request'], name='text/html') is None
def create(self, req, filename=None, verbose=True, link_callback=None, lang=None): html = [] lang = lang or Language.get('afr') entries = list( DBSession.query(ValueSet).join(ValueSet.parameter) .filter(ValueSet.language_pk == lang.pk) .order_by( Taxon.kingdom, Taxon.phylum, Taxon.class_, Taxon.order, Taxon.family, Parameter.name) .options(contains_eager(ValueSet.parameter), joinedload(ValueSet.values))) for kingdom, taxa1 in groupby(entries, key=lambda vs: vs.parameter.kingdom): html.append('<h2>Kingdom: %s</h2>' % (kingdom or 'other')) for phylum, taxa2 in groupby(taxa1, key=lambda vs: vs.parameter.phylum): html.append('<h3>Phylum: %s</h3>' % (phylum or 'other')) for class_, taxa3 in groupby(taxa2, key=lambda vs: vs.parameter.class_): html.append('<h4>Class: %s</h4>' % (class_ or 'other')) for order, taxa4 in groupby(taxa3, key=lambda vs: vs.parameter.order): html.append('<h5>Order: %s</h5>' % (order or 'other')) for family, taxa5 in groupby(taxa4, key=lambda vs: vs.parameter.family): html.append('<h6>Family: %s</h6>' % (family or 'other')) for entry in taxa5: adapter = get_adapter( IRepresentation, entry, req, ext='snippet.html') html.append(adapter.render(entry, req)) html.append('<p class="separator"> <p>') with open(str(download_path('%s.pdf' % lang.id)), 'wb') as fp: editors = '' if lang.contribution.contributor_assocs: editors = 'edited by ' + ' and '.join( c.last_first() for c in lang.contribution.primary_contributors) pisa.CreatePDF( html_tmpl % ( css_tmpl.format(charis_font_spec_css()), req.resource_url(req.dataset), """ <h1 style="text-align: center; font-size: 12mm;">%(language)s names for Plants and Animals</h1> <h2 style="text-align: center; font-size: 8mm;">%(editors)s</h2> <p style="font-size: 5mm;"> This document was created from <a href="%(url)s">%(dataset)s</a> on %(date)s. </p> <p style="font-size: 5mm;"> %(dataset)s is published under a %(license)s and should be cited as </p> <blockquote style="font-size: 5mm;"><i>%(citation)s</i></blockquote> <p style="font-size: 5mm;"> A full list of contributors is available at <a href="%(url)scontributors">%(url)scontributors</a> </p> <p style="font-size: 5mm;"> The list of references cited in this document is available at <a href="%(url)ssources">%(url)ssources</a> </p> """ % dict( language=lang.name, editors=editors, dataset=req.dataset.name, url=req.resource_url(req.dataset), date=date.today(), citation=text_citation(req, req.dataset), license=req.dataset.jsondata['license_name']), ''.join(html)), dest=fp, link_callback=link_callback, )
def text_citation(req, ctx): citation = get_adapter(interfaces.IRepresentation, ctx, req, ext='md.txt') return citation.render(ctx, req)
def dump(self, req, fp, item, index): adapter = get_adapter(IRepresentation, item, req, ext=self.ext) self.dump_rendered(req, fp, item, index, adapter.render(item, req))
def test_get_adapter(self): from clld.web.adapters import get_adapter self.assertEqual( None, get_adapter(IIndex, Language, self.env['request'], name='text/html'))
def text_citation(req, ctx): citation = get_adapter(interfaces.IRepresentation, ctx, req, ext='md.txt') return citation.render(ctx, req)