def nato_context(): context_script = b64encode( render_template_to_string('analyze/etcd-updater.sh', etcd_url=cfg.get('CFG_ANALYZE_ETCD_URL'), ttl=600, root=cfg.get('CFG_ANALYZE_NODES_KEY'))) context_script_path = '/usr/local/bin/etcd-updater.sh' crontab = b64encode( render_template_to_string('analyze/etcd_updater_cron', context_script_path=context_script_path)) context = { 'write_files': [ { 'encoding': 'b64', 'content': context_script, 'permissions': '755', 'path': context_script_path, }, { 'encoding': 'b64', 'content': crontab, 'permissions': '755', 'path': '/etc/cron.d/etcd_updater' }, ], # run it as soon as the VM is booted 'runcmd': [ [context_script_path], ], } if cfg.get('CFG_ANALYZE_PUBLIC_KEY'): context['ssh_authorized_keys'] = [cfg.get('CFG_ANALYZE_PUBLIC_KEY')] return context
def format_records(records, of='hb', ln=None, **ctx): """Return records using Jinja template.""" from flask import request from invenio.base.i18n import wash_language from .registry import export_formats of = of.lower() jrec = request.values.get('jrec', ctx.get('jrec', 1), type=int) rg = request.values.get('rg', ctx.get('rg', 10), type=int) ln = ln or wash_language(request.values.get('ln', cfg['CFG_SITE_LANG'])) ot = (request.values.get('ot', ctx.get('ot')) or '').split(',') if jrec > records: jrec = rg * (records // rg) + 1 context = dict( of=of, jrec=jrec, rg=rg, ln=ln, ot=ot, facets={}, time=time, records=records, export_formats=export_formats, format_record=format_record, **TEMPLATE_CONTEXT_FUNCTIONS_CACHE.template_context_functions ) context.update(ctx) return render_template_to_string( ['format/records/%s.tpl' % of, 'format/records/%s.tpl' % of[0], 'format/records/%s.tpl' % get_output_format_content_type(of). replace('/', '_')], **context)
def widget(self): uid = current_user.get_id() query_baskets = UserQueryBasket.query.filter( UserQueryBasket.id_user == uid).all() template = """ {{ _('You own the following') }} <div class="btn-group"> <a class="btn dropdown-toggle" data-toggle="dropdown" href="#"> <strong class="text-info">{{ query_baskets|length }}</strong> {{ _('alerts') }} <span class="caret"></span> </a> <ul class="dropdown-menu"> {%- for a in query_baskets -%} <li> <a href="#"> {{ a.alert_name }} </a> </li> {%- endfor -%} </ul> </div>""" return render_template_to_string(template, _from_string=True, query_baskets=query_baskets)
def email_alert(mapper, connection, target): """ Sends email alerts to message recipients. """ from invenio.ext.template import render_template_to_string from invenio.ext.email import send_email, scheduled_send_email m = target is_reminder = m.received_date is not None \ and m.received_date > datetime.now() alert = send_email if is_reminder: alert = lambda *args, **kwargs: scheduled_send_email( *args, other_bibtasklet_arguments= [m.received_date.strftime(datetext_format)], **kwargs) for u in m.recipients: if isinstance(u.settings, dict) and \ u.settings.get('webmessage_email_alert', True): try: alert(cfg['CFG_WEBCOMMENT_ALERT_ENGINE_EMAIL'], u.email, subject=m.subject, content=render_template_to_string( 'messages/email_alert.html', message=m, user=u)) except: # FIXME tests are not in request context pass
def test_email_text_template(self): """ Test email text template engine. """ from invenio.ext.template import render_template_to_string contexts = { 'ctx1': {'content': 'Content 1'}, 'ctx2': {'content': 'Content 2', 'header': 'Header 2'}, 'ctx3': {'content': 'Content 3', 'footer': 'Footer 3'}, 'ctx4': {'content': 'Content 4', 'header': 'Header 4', 'footer': 'Footer 4'} } msg_content = """Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: %s From: [email protected] To: [email protected]""" for name, ctx in iteritems(contexts): msg = render_template_to_string('mail_text.tpl', **ctx) send_email('*****@*****.**', ['*****@*****.**'], subject=name, **ctx) email = sys.stdout.getvalue() self.assertIn(msg_content % name, email) self.assertIn(msg, email) self.flush_mailbox()
def open_tag(self, subfield, **kwargs): """Render open tag.""" if self.html_tag: if subfield.name.endswith('__input__'): return '<%s>' % self.html_tag else: ctx = {} if(isinstance(subfield.data, six.string_types)): ctx['value'] = subfield.data elif subfield.data: ctx.update(subfield.data) return ( '<%s %s><button type="button" class="close remove-element"' ' data-dismiss="alert">×</button>' '<span class="tag-title">%s</span>' % ( self.html_tag, html_params( class_=self.class_ + ' ' + kwargs.get('class_', '') ), render_template_to_string( self.template, _from_string=True, **ctx ) ) ) return ''
def format_record(record, of, ln=None, verbose=0, search_pattern=None, xml_record=None, user_info=None, **kwargs): """Format a record in given output format. Return a formatted version of the record in the specified language, search pattern, and with the specified output format. The function will define which format template must be applied. The record to be formatted can be specified with its ID (with 'recID' parameter) or given as XML representation (with 'xml_record' parameter). If 'xml_record' is specified 'recID' is ignored (but should still be given for reference. A dummy recid 0 or -1 could be used). """ ln = ln or cfg['CFG_SITE_LANG'] template = decide_format_template(record, of) out = render_template_to_string( ['format/record/{0}'.format(template), template], recid=record['recid'], record=record, format_record=format_record, **(kwargs or {}) ) return out
def openaire_upload_notification(recid): """ Send a notification to all user collections. """ ctx = { 'record': get_record(recid), } ucolls = Community.from_recid(recid, provisional=True) for c in ucolls: try: if c.owner.email: ctx.update({ 'community': c, }) content = render_template_to_string( "communities/new_upload_email.html", **ctx) send_email( CFG_SITE_SUPPORT_EMAIL, c.owner.email.encode('utf8'), "[%s] New upload to %s" % ( CFG_SITE_NAME, c.title.encode('utf8') ), content=content.encode('utf8') ) logger.info("Sent email for new record %s to %s." % (recid, c.owner.email.encode('utf8'))) except AttributeError: pass
def format_element(bfo, template='bfe_files.html', show_subformat_icons='yes', focus_on_main_file='no', **kwargs): """ This is the default format for formatting fulltext links. When possible, it returns only the main file(s) (+ link to additional files if needed). If no distinction is made at submission time between main and additional files, returns all the files """ # Retrieve files (parsed_urls, old_versions, additionals) = get_files(bfo, \ distinguish_main_and_additional_files=focus_on_main_file.lower() == 'yes', include_subformat_icons=show_subformat_icons == 'yes') ctx = { 'recid': bfo.recID, 'bfo': bfo, 'others_urls': parsed_urls['others_urls'], 'main_urls': parsed_urls['main_urls'], } kwargs.update(ctx) return render_template_to_string(template, **kwargs)
def tmpl_pagefooter(self, req, **kwargs): """Creates a page footer Parameters: - 'ln' *string* - The language to display - 'lastupdated' *string* - when the page was last updated - 'pagefooteradd' *string* - additional page footer HTML code Output: - HTML code of the page headers """ ctx = dict(ln=CFG_SITE_LANG, lastupdated=None, pagefooteradd=None) ctx.update(kwargs) lastupdated = ctx.get('lastupdated') if lastupdated and lastupdated != '$Date$': if lastupdated.startswith("$Date: ") or lastupdated.startswith( "$Id: "): ctx['lastupdated'] = convert_datecvs_to_datestruct(lastupdated) return render_template_to_string("legacy_page.html", no_pagebody=True, no_pageheader=True, **ctx).encode('utf8')
def render_portalbox_bodies(self, templates): """Get a list of rendered portal boxes for this user collection.""" ctx = { 'community': self, } return map(lambda t: render_template_to_string(t, **ctx), templates)
def widget(self): user = User.query.get(current_user.get_id()) tag_count = user.tags_query.count() record_count = Bibrec.query.join(WtgTAGRecord).join(WtgTAG).filter(WtgTAG.user == user).count() return render_template_to_string("tags/user_settings.html", tag_count=tag_count, record_count=record_count)
def email_alert(mapper, connection, target): """ Sends email alerts to message recipients. """ from invenio.ext.template import render_template_to_string from invenio.ext.email import send_email, scheduled_send_email m = target is_reminder = m.received_date is not None \ and m.received_date > datetime.now() alert = send_email if is_reminder: alert = lambda *args, **kwargs: scheduled_send_email(*args, other_bibtasklet_arguments=[ m.received_date.strftime(datetext_format)], **kwargs) for u in m.recipients: if isinstance(u.settings, dict) and \ u.settings.get('webmessage_email_alert', True): try: alert( cfg['CFG_WEBCOMMENT_ALERT_ENGINE_EMAIL'], u.email, subject = m.subject, content = render_template_to_string( 'messages/email_alert.html', message=m, user=u)) except: # FIXME tests are not in request context pass
def widget(self): uid = current_user.get_id() query_baskets = UserQueryBasket.query.filter( UserQueryBasket.id_user == uid ).all() template = """ {{ _('You own the following') }} <div class="btn-group"> <a class="btn dropdown-toggle" data-toggle="dropdown" href="#"> <strong class="text-info">{{ query_baskets|length }}</strong> {{ _('alerts') }} <span class="caret"></span> </a> <ul class="dropdown-menu"> {%- for a in query_baskets -%} <li> <a href="#"> {{ a.alert_name }} </a> </li> {%- endfor -%} </ul> </div>""" return render_template_to_string(template, _from_string=True, query_baskets=query_baskets)
def template_context_function(recID): """ Displays next-hit/previous-hit/back-to-search links on the detailed record pages in order to be able to quickly flip between detailed record pages :param recID: detailed record ID :type recID: string :return: html output """ if recID is None: return "" # this variable is set to zero so nothing is displayed if not cfg['CFG_WEBSEARCH_PREV_NEXT_HIT_LIMIT']: return "" # search for a specific record having not done # any search before try: last_query = session['websearch-last-query'] recids = session["websearch-last-query-hits"] except: return "" if recids: return render_template_to_string( 'records/back_to_search_links.html', recID=int(recID), last_query=cfg['CFG_SITE_URL'] + last_query, recids=recids) else: # did not rich the limit CFG_WEBSEARCH_PREV_NEXT_HIT_LIMIT, # so nothing is displayed return ""
def tmpl_pageheader(self, req, **kwargs): """Creates a page header Parameters: - 'ln' *string* - The language to display - 'headertitle' *string* - the title of the HTML page, not yet escaped for HTML - 'description' *string* - description goes to the metadata in the header of the HTML page, not yet escaped for HTML - 'keywords' *string* - keywords goes to the metadata in the header of the HTML page, not yet escaped for HTML - 'userinfobox' *string* - the HTML code for the user information box - 'useractivities_menu' *string* - the HTML code for the user activities menu - 'adminactivities_menu' *string* - the HTML code for the admin activities menu - 'navtrailbox' *string* - the HTML code for the navigation trail box - 'pageheaderadd' *string* - additional page header HTML code - 'uid' *int* - user ID - 'secure_page_p' *int* (0 or 1) - are we to use HTTPS friendly page elements or not? - 'navmenuid' *string* - the id of the navigation item to highlight for this page - 'metaheaderadd' *string* - list of further tags to add to the <HEAD></HEAD> part of the page - 'rssurl' *string* - the url of the RSS feed for this page - 'body_css_classes' *list* - list of classes to add to the body tag Output: - HTML code of the page headers """ ctx = dict(ln=CFG_SITE_LANG, headertitle="", description="", keywords="", userinfobox="", useractivities_menu="", adminactivities_menu="", navtrailbox="", pageheaderadd="", uid=0, secure_page_p=0, navmenuid="admin", metaheaderadd="", rssurl=CFG_SITE_URL+"/rss", body_css_classes=None) ctx.update(kwargs) if ctx['body_css_classes'] is None: ctx['body_css_classes'] = [ctx.get('navmenuid', '')] else: ctx['body_css_classes'].append([ctx.get('navmenuid', '')]) return render_template_to_string( "legacy_page.html", no_pagebody=True, no_pagefooter=True, **ctx ).encode('utf8')
def test_simple_email_header(self): """ Test simple email header. """ from invenio.config import CFG_SITE_ADMIN_EMAIL from invenio.ext.template import render_template_to_string msg_content = """Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: Subject From: [email protected] To: %s""" % (CFG_SITE_ADMIN_EMAIL, ) msg = render_template_to_string('mail_text.tpl', content='Content') self.flush_mailbox() send_email('*****@*****.**', ['*****@*****.**'], subject='Subject', content='Content') email = self.stream.getvalue() self.assertIn(msg_content, email) self.assertIn(self.ADMIN_MESSAGE, email) self.assertNotIn('Bcc:', email) self.assertIn(msg, email) self.flush_mailbox() send_email('*****@*****.**', '*****@*****.**', subject='Subject', content='Content') email = self.stream.getvalue() self.assertIn(msg_content, email) self.assertIn(self.ADMIN_MESSAGE, email) self.assertNotIn('Bcc:', email) self.assertIn(msg, email) self.flush_mailbox()
def tmpl_pagefooter(self, req, **kwargs): """Creates a page footer Parameters: - 'ln' *string* - The language to display - 'lastupdated' *string* - when the page was last updated - 'pagefooteradd' *string* - additional page footer HTML code Output: - HTML code of the page headers """ ctx = dict(ln=CFG_SITE_LANG, lastupdated=None, pagefooteradd=None) ctx.update(kwargs) lastupdated = ctx.get('lastupdated') if lastupdated and lastupdated != '$Date$': if lastupdated.startswith("$Date: ") or lastupdated.startswith("$Id: "): ctx['lastupdated'] = convert_datecvs_to_datestruct(lastupdated) return render_template_to_string( "legacy_page.html", no_pagebody=True, no_pageheader=True, **ctx ).encode('utf8')
def record_context(recid, app_env, context): token = Token.query.filter(Token.user_id == current_user.get_id()).filter( Token.is_internal == True).first() if not token: token = Token.create_personal('analyze', current_user.get_id(), is_internal=True) user_map = { 'ssh': 'lw', 'jupyter-python': 'jupyter', 'jupyter-r': 'jupyter', } record_script = b64encode( render_template_to_string('analyze/lwget.sh', token=token, recid=recid, lw_user=user_map.get(app_env, 'ubuntu'))) record_script_path = '/usr/local/bin/lwget.sh' context['write_files'].append({ 'encoding': 'b64', 'content': record_script, 'permissions': '755', 'path': record_script_path, }) context['runcmd'].append([record_script_path])
def test_email_html_template(self): """ Test email html template engine. """ from invenio.ext.template import render_template_to_string contexts = { 'ctx1': {'html_content': '<b>Content 1</b>'}, 'ctx2': {'html_content': '<b>Content 2</b>', 'html_header': '<h1>Header 2</h1>'}, 'ctx3': {'html_content': '<b>Content 3</b>', 'html_footer': '<i>Footer 3</i>'}, 'ctx4': {'html_content': '<b>Content 4</b>', 'html_header': '<h1>Header 4</h1>', 'html_footer': '<i>Footer 4</i>'} } def strip_html_key(ctx): return dict(map(lambda (k, v): (k[5:], v), iteritems(ctx))) for name, ctx in iteritems(contexts): msg = render_template_to_string('mail_html.tpl', **strip_html_key(ctx)) send_email('*****@*****.**', ['*****@*****.**'], subject=name, content='Content Text', **ctx) email = sys.stdout.getvalue() self.assertIn('Content-Type: multipart/alternative;', email) self.assertIn('Content Text', email) self.assertIn(msg, email) self.flush_mailbox()
def widget(self): user = User.query.get(current_user.get_id()) email = user.email email_field = "8560_" deposit_count = len(perform_request_search(f=email_field, p=email, of="id")) return render_template_to_string('deposits_user_settings.html', email=email, email_field=email_field, deposit_count=deposit_count)
def __str__(self): uid = current_user.get_id() dbquery.update_user_inbox_for_reminders(uid) unread = db.session.query(db.func.count(UserMsgMESSAGE.id_msgMESSAGE)).\ filter(db.and_( UserMsgMESSAGE.id_user_to == uid, UserMsgMESSAGE.status == cfg['CFG_WEBMESSAGE_STATUS_CODE']['NEW'] )).scalar() return render_template_to_string("messages/menu_item.html", unread=unread)
def render_portalbox_bodies(self, templates): """Get a list of rendered portal boxes for this user collection.""" ctx = { 'community': self, } return map( lambda t: render_template_to_string(t, **ctx), templates )
def format_element(bfo, template='record_hb.html', **kwargs): #bfo.field('0247_a') ctx = { 'recid': bfo.recID, 'bfo': bfo, } kwargs.update(ctx) return render_template_to_string(template, **kwargs)
def __call__(self, field, **kwargs): """Render PLUpload widget.""" field_id = kwargs.pop('id', field.id) kwargs['class'] = u'plupload' return HTMLString( render_template_to_string(self.template, field=field, field_id=field_id, **kwargs))
def widget(self): user = User.query.get(current_user.get_id()) email = user.email email_field = "8560_" deposit_count = len( perform_request_search(f=email_field, p=email, of="id")) return render_template_to_string('deposits_user_settings.html', email=email, email_field=email_field, deposit_count=deposit_count)
def widget(self): user = User.query.get(current_user.get_id()) tag_count = user.tags_query.count() record_count = Bibrec.query.join(WtgTAGRecord)\ .join(WtgTAG)\ .filter(WtgTAG.user == user).count() return render_template_to_string('tags/user_settings.html', tag_count=tag_count, record_count=record_count)
def __call__(self, field, **kwargs): field_id = kwargs.pop('id', field.id) kwargs['class'] = u'plupload' return HTMLString( render_template_to_string( self.template, field=field, field_id=field_id, **kwargs ) )
def tmpl_navtrailbox_body(self, ln, title, previous_links, separator, prolog, epilog): """Bootstrap friendly-Create navigation trail box body Parameters: - 'ln' *string* - The language to display - 'title' *string* - page title; - 'previous_links' *string* - the trail content from site title until current page (both ends exclusive) - 'prolog' *string* - HTML code to prefix the navtrail item with - 'epilog' *string* - HTML code to suffix the navtrail item with - 'separator' *string* - HTML code that separates two navtrail items Output: - text containing the navtrail Note: returns empty string for Home page. (guessed by title). """ # load the right message language _ = gettext_set_language(ln) if title == CFG_SITE_NAME_INTL.get(ln, CFG_SITE_NAME): return "" # Breadcrumbs # breadcrumb objects should provide properties 'text' and 'url' # First element breadcrumbs = [ dict(text=_("Home"), url=CFG_SITE_URL), ] # Decode previous elements if previous_links: soup = BeautifulSoup(previous_links) for link in soup.find_all('a'): breadcrumbs.append( dict(text=unicode(' '.join(link.contents)), url=link.get('href'))) # Add head if title: breadcrumbs.append(dict(text=title, url='#')) return render_template_to_string( "breadcrumbs.html", breadcrumbs=breadcrumbs).encode('utf8')
def __call__(self, field, **kwargs): template = 'deposit/jsonwidget.html' field_id = kwargs.pop('id', field.id) return HTMLString( render_template_to_string( template, field=field, field_id=field_id, **kwargs ) )
def jupyter_context(app_env, ssh_key, context): jupyter_script = b64encode( # nothing to pass to the template? render_template_to_string('analyze/jupyter.sh', app_env=app_env)) jupyter_script_path = '/usr/local/bin/start-jupyter.sh' context['write_files'].append({ 'encoding': 'b64', 'content': jupyter_script, 'permissions': '755', 'path': jupyter_script_path, }) context['runcmd'].append([jupyter_script_path])
def tmpl_navtrailbox_body(self, ln, title, previous_links, separator, prolog, epilog): """Bootstrap friendly-Create navigation trail box body Parameters: - 'ln' *string* - The language to display - 'title' *string* - page title; - 'previous_links' *string* - the trail content from site title until current page (both ends exclusive) - 'prolog' *string* - HTML code to prefix the navtrail item with - 'epilog' *string* - HTML code to suffix the navtrail item with - 'separator' *string* - HTML code that separates two navtrail items Output: - text containing the navtrail Note: returns empty string for Home page. (guessed by title). """ # load the right message language _ = gettext_set_language(ln) if title == CFG_SITE_NAME_INTL.get(ln, CFG_SITE_NAME): return "" # Breadcrumbs # breadcrumb objects should provide properties 'text' and 'url' # First element breadcrumbs = [dict(text=_("Home"), url=CFG_SITE_URL), ] # Decode previous elements if previous_links: soup = BeautifulSoup(previous_links) for link in soup.find_all('a'): breadcrumbs.append(dict( text=unicode(' '.join(link.contents)), url=link.get('href'))) # Add head if title: breadcrumbs.append(dict(text=title, url='#')) return render_template_to_string("breadcrumbs.html", breadcrumbs=breadcrumbs).encode('utf8')
def references(self): """Return reference export for single record.""" out = "" number = 0 references = self.record["references"] for reference in references: number += 1 for reference_field in reference: if "recid" in reference_field: recid = reference["recid"] record = get_record(recid) if record: try: title = record["title"][0]["title"] authors = record["authors"][0]["full_name"] journal = record["publication_info"][0] ["journal_title"] +" " + record["publication_info"][0] ["journal_volume"] + " (" +record["publication_info"][0]["year"] + ") , " +record["publication_info"][0]["page_artid"] out += render_template_to_string( "references.html", number=str(number), recid=recid, title=title, authors=authors, journal=journal, ) except (UnboundLocalError, TypeError, KeyError): out += render_template_to_string( "references.html", number=str(number), recid=recid, title=title, authors=authors ) if "recid" not in reference: out += render_template_to_string("references.html", number=str(number), reference=reference) continue return out
def references(self): """Return reference export for single record.""" out = '' number = 0 references = self.record['references'] for reference in references: number += 1 for reference_field in reference: if 'recid' in reference_field: recid = reference['recid'] record = get_record(recid) if record: out += render_template_to_string( "references.html", number=str(number), record=record) if 'recid' not in reference: out += render_template_to_string( "references.html", number=str(number), reference=reference) continue return out
def widget(self): """Display search settings widget.""" uid = current_user.get_id() queries = db.session.query(db.func.count( UserQuery.id_query)).filter(UserQuery.id_user == uid).scalar() template = """ {{ _('You have made %(x_num_queries)d queries. A detailed list is available with a possibility to (a) view search results and (b) subscribe to an automatic email alerting service for these queries.', x_num_queries=queries) }} """ return render_template_to_string(template, _from_string=True, queries=queries)
def widget(self): """Display search settings widget.""" uid = current_user.get_id() queries = db.session.query(db.func.count(UserQuery.id_query)).filter( UserQuery.id_user == uid ).scalar() template = """ {{ _('You have made %(x_num_queries)d queries. A detailed list is available with a possibility to (a) view search results and (b) subscribe to an automatic email alerting service for these queries.', x_num_queries=queries) }} """ return render_template_to_string(template, _from_string=True, queries=queries)
def __call__(self, field, **kwargs): """Render given field using a tempalte. :param field: field that should be rendered. :param template: path to Jinja template. :type template: str """ template = kwargs.pop('template', field.template) field_id = kwargs.pop('id', field.id) return HTMLString( render_template_to_string(template, field=field, field_id=field_id, **kwargs))
def __call__(self, field, **kwargs): """Render given field using a tempalte. :param field: field that should be rendered. :param template: path to Jinja template. :type template: str """ template = kwargs.pop('template', field.template) field_id = kwargs.pop('id', field.id) return HTMLString( render_template_to_string( template, field=field, field_id=field_id, **kwargs ) )
def large_file_notification(sender, deposition=None, deposition_file=None, **kwargs): """ Send notification on large file uploads """ if deposition_file and deposition_file.size > 10485760: current_app.logger.info(deposition_file.__getstate__()) send_email(cfg['CFG_SITE_SUPPORT_EMAIL'], cfg['CFG_SITE_ADMIN_EMAIL'], subject="%s: %s file uploaded" % (cfg['CFG_SITE_NAME'], nice_size(deposition_file.size)), content=render_template_to_string( "deposit/email_large_file.html", deposition=deposition, deposition_file=deposition_file, ))
def print_records(recIDs, of='hb', ln=None, verbose=0, search_pattern='', on_the_fly=False, **ctx): """Return records using Jinja template.""" import time from math import ceil from flask import request from invenio.base.i18n import wash_language from invenio.ext.template import render_template_to_string from invenio.modules.search.models import Format from invenio.utils.pagination import Pagination from invenio.modules.formatter.engine import \ TEMPLATE_CONTEXT_FUNCTIONS_CACHE of = of.lower() jrec = request.values.get('jrec', ctx.get('jrec', 1), type=int) rg = request.values.get('rg', ctx.get('rg', 10), type=int) ln = ln or wash_language(request.values.get('ln', cfg['CFG_SITE_LANG'])) ot = (request.values.get('ot', ctx.get('ot')) or '').split(',') records = ctx.get('records', len(recIDs)) if jrec > records: jrec = rg * (records // rg) + 1 pages = int(ceil(jrec / float(rg))) if rg > 0 else 1 context = dict( of=of, jrec=jrec, rg=rg, ln=ln, ot=ot, facets={}, time=time, recids=recIDs, pagination=Pagination(pages, rg, records), verbose=verbose, export_formats=Format.get_export_formats(), format_record=format_record, **TEMPLATE_CONTEXT_FUNCTIONS_CACHE.template_context_functions ) context.update(ctx) return render_template_to_string( ['format/records/%s.tpl' % of, 'format/records/%s.tpl' % of[0], 'format/records/%s.tpl' % get_output_format_content_type(of). replace('/', '_')], **context)
def large_file_notification(sender, deposition=None, deposition_file=None, **kwargs): """ Send notification on large file uploads """ if deposition_file and deposition_file.size > 10485760: current_app.logger.info(deposition_file.__getstate__()) send_email( cfg['CFG_SITE_SUPPORT_EMAIL'], cfg['CFG_SITE_ADMIN_EMAIL'], subject="%s: %s file uploaded" % ( cfg['CFG_SITE_NAME'], nice_size(deposition_file.size) ), content=render_template_to_string( "deposit/email_large_file.html", deposition=deposition, deposition_file=deposition_file, ) )
def widget(self): uid = current_user.get_id() unread = db.session.query(db.func.count(UserMsgMESSAGE.id_msgMESSAGE)).\ filter(db.and_( UserMsgMESSAGE.id_user_to == uid, UserMsgMESSAGE.status == current_app.config[ 'CFG_WEBMESSAGE_STATUS_CODE']['NEW'] )).scalar() total = db.session.query(db.func.count(UserMsgMESSAGE.id_msgMESSAGE)).\ filter( UserMsgMESSAGE.id_user_to == uid ).scalar() template = """ {{ _("You have %(x_num_new)d new messages out of %(x_num_total)d messages.", x_num_new=unread, x_num_total=total) }} """ return render_template_to_string(template, _from_string=True, unread=unread, total=total)
def widget(self): uid = current_user.get_id() baskets = [] if (uid is not None and uid != 0): # list of tuples: (bskid, bsk_name, topic) bsk_from_db = get_all_personal_baskets_names(uid) baskets = [{ 'name': name, 'bskid': bskid } for (bskid, name, dummy_topic) in bsk_from_db] template = """ {{ _('You have') }} <div class="btn-group"> <a class="btn dropdown-toggle" data-toggle="dropdown" href="#"> <strong class="text-info">{{ baskets|length }}</strong> {{ _('personal baskets') }} <span class="caret"></span> </a> <ul class="dropdown-menu"> {%- for b in baskets -%} <li> <a href="/yourbaskets/display?bskid={{ b.bskid }}"> {{ b.name }} </a> </li> {%- endfor -%} </ul> </div>""" # If the list is too long ( >= 2 items! ), # it will not be properlt displayed # (it appears that the list cannot be displayed outside the # box, so the rest is cut off) return render_template_to_string(template, _from_string=True, baskets=baskets)
def test_email_text_template(self): """ Test email text template engine. """ from invenio.ext.template import render_template_to_string contexts = { 'ctx1': { 'content': 'Content 1' }, 'ctx2': { 'content': 'Content 2', 'header': 'Header 2' }, 'ctx3': { 'content': 'Content 3', 'footer': 'Footer 3' }, 'ctx4': { 'content': 'Content 4', 'header': 'Header 4', 'footer': 'Footer 4' } } msg_content = """Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: %s From: [email protected] To: [email protected]""" for name, ctx in iteritems(contexts): msg = render_template_to_string('mail_text.tpl', **ctx) send_email('*****@*****.**', ['*****@*****.**'], subject=name, **ctx) email = sys.stdout.getvalue() self.assertIn(msg_content % name, email) self.assertIn(msg, email) self.flush_mailbox()
def test_email_html_template(self): """ Test email html template engine. """ from invenio.ext.template import render_template_to_string contexts = { 'ctx1': { 'html_content': '<b>Content 1</b>' }, 'ctx2': { 'html_content': '<b>Content 2</b>', 'html_header': '<h1>Header 2</h1>' }, 'ctx3': { 'html_content': '<b>Content 3</b>', 'html_footer': '<i>Footer 3</i>' }, 'ctx4': { 'html_content': '<b>Content 4</b>', 'html_header': '<h1>Header 4</h1>', 'html_footer': '<i>Footer 4</i>' } } def strip_html_key(ctx): return dict(map(lambda (k, v): (k[5:], v), iteritems(ctx))) for name, ctx in iteritems(contexts): msg = render_template_to_string('mail_html.tpl', **strip_html_key(ctx)) send_email('*****@*****.**', ['*****@*****.**'], subject=name, content='Content Text', **ctx) email = sys.stdout.getvalue() self.assertIn('Content-Type: multipart/alternative;', email) self.assertIn('Content Text', email) self.assertIn(msg, email) self.flush_mailbox()
def widget(self): uid = current_user.get_id() baskets = [] if(uid is not None and uid != 0): # list of tuples: (bskid, bsk_name, topic) bsk_from_db = get_all_personal_baskets_names(uid) baskets = [{'name': name, 'bskid': bskid} for (bskid, name, dummy_topic) in bsk_from_db] template = """ {{ _('You have') }} <div class="btn-group"> <a class="btn dropdown-toggle" data-toggle="dropdown" href="#"> <strong class="text-info">{{ baskets|length }}</strong> {{ _('personal baskets') }} <span class="caret"></span> </a> <ul class="dropdown-menu"> {%- for b in baskets -%} <li> <a href="/yourbaskets/display?bskid={{ b.bskid }}"> {{ b.name }} </a> </li> {%- endfor -%} </ul> </div>""" # If the list is too long ( >= 2 items! ), # it will not be properlt displayed # (it appears that the list cannot be displayed outside the # box, so the rest is cut off) return render_template_to_string(template, _from_string=True, baskets=baskets)
def openaire_upload_notification(recid): """ Send a notification to all user collections. """ ctx = {"record": get_record(recid)} ucolls = Community.from_recid(recid, provisional=True) for c in ucolls: try: if c.owner.email: ctx.update({"community": c}) content = render_template_to_string("communities/new_upload_email.html", **ctx) title = c.title if isinstance(c.title, unicode) else unicode(c.title, "utf8") send_email( CFG_SITE_SUPPORT_EMAIL, c.owner.email.encode("utf8"), "[%s] New upload to %s" % (CFG_SITE_NAME, title.encode("utf8")), content=content.encode("utf8"), ) logger.info("Sent email for new record %s to %s." % (recid, c.owner.email.encode("utf8"))) except AttributeError: pass
def widget(self): uid = current_user.get_id() usergroups = UserUsergroup.query.filter( UserUsergroup.id_user == uid).all() template = """ {%- if usergroups -%} {{ _('You are involved in following groups:') }} <div> {%- for ug in usergroups -%} <span class="label label-default"> {{ ug.usergroup.name }} </span> {%- endfor -%} </div> {%- else -%} {{ _('You are not involved in any group.') }} {%- endif -%} """ rv = render_template_to_string(template, _from_string=True, usergroups=usergroups) return rv
def subscribe(self, user): """Subscribe a user to a group (done by users). Wrapper around ``add_member()`` which checks subscription policy. :param user: User to subscribe. :returns: Newly created Membership or None. """ if self.subscription_policy == SubscriptionPolicy.OPEN: return self.add_member(user) elif self.subscription_policy == SubscriptionPolicy.APPROVAL: email_body = render_template_to_string( 'groups/join_group_email.txt', group=self, ) subject = 'New Group Request at LifeWatch Open Science Framework' to = [User.query.get(a.admin_id).email for a in self.admins] scheduled_send_email(fromaddr=cfg['CFG_SITE_ADMIN_EMAIL'], toaddr=to, subject=subject, content=email_body) return self.add_member(user, state=MembershipState.PENDING_ADMIN) elif self.subscription_policy == SubscriptionPolicy.CLOSED: return None