def test_relative_url(self): """urlutils - test get_relative_url""" url_normal = "http://web.net" self.assertEqual("", get_relative_url(url_normal)) url_normal_trailing = "http://web.net/" self.assertEqual("", get_relative_url(url_normal_trailing)) url_more = "http://web.net/asd" self.assertEqual("/asd", get_relative_url(url_more)) url_more_trailing = "http://web.net/asd/" self.assertEqual("/asd", get_relative_url(url_more_trailing)) url_adv = "http://web.net/asd/qwe" self.assertEqual("/asd/qwe", get_relative_url(url_adv)) url_adv_trailing = "http://web.net/asd/qwe/" self.assertEqual("/asd/qwe", get_relative_url(url_adv_trailing))
def format_element(bfo, style, separator='; ', show_icons='no', focus_on_main_file='yes', show_subformat_icons='no'): """ This is the format for formatting fulltext links in the mini panel. @param separator: the separator between urls. @param style: CSS class of the link @param show_icons: if 'yes', print icons for fulltexts @param focus_on_main_file: if 'yes' and a doctype 'Main' is found, prominently display this doctype. In that case other doctypes are summarized with a link to the Files tab, named"Additional files". @param show_subformat_icons: shall we display subformats considered as icons? """ _ = gettext_set_language(bfo.lang) out = '' # 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') main_urls = parsed_urls['main_urls'] others_urls = parsed_urls['others_urls'] if parsed_urls.has_key('cern_urls'): cern_urls = parsed_urls['cern_urls'] # Prepare style if style != "": style = 'class="'+style+'"' # Build urls list. # Escape special chars for <a> tag value. additional_str = '' if additionals: additional_str = separator + '<small>(<a '+style+' href="'+CFG_BASE_URL+'/'+ CFG_SITE_RECORD +'/'+str(bfo.recID)+'/files/">%s</a>)</small>' % _("additional files") versions_str = '' #if old_versions: #versions_str = separator + '<small>(<a '+style+' href="'+CFG_SITE_URL+'/CFG_SITE_RECORD/'+str(bfo.recID)+'/files/">%s</a>)</small>' % _("older versions") if main_urls: # Put a big file icon if only one file if len(main_urls.keys()) == 1 and len(main_urls.items()[0][1]) == 1 and \ (not CFG_CERN_SITE or len(cern_urls) == 0) and len(others_urls) == 0 and \ show_icons.lower() == 'yes': file_icon = '<img style="border:none" src="%s/img/file-icon-text-34x48.gif" alt="%s" /><br />' % (CFG_BASE_URL, _("Download fulltext")) elif show_icons.lower() == 'yes': file_icon = '<img style="border:none" src="%s/img/file-icon-text-12x16.gif" alt="%s"/>' % (CFG_BASE_URL, _("Download fulltext")) else: file_icon = '' main_urls_keys = sort_alphanumerically(main_urls.keys()) for descr in main_urls_keys: urls = main_urls[descr] out += '<div><small class="detailedRecordActions">%s:</small> ' % descr urls_dict = {} for url, name, url_format in urls: if name not in urls_dict: urls_dict[name] = [(url, url_format)] else: urls_dict[name].append((url, url_format)) for name, urls_and_format in urls_dict.items(): if len(urls_dict) > 1: print_name = "<em>%s</em> - " % name url_list = [print_name] else: url_list = [] for url, url_format in urls_and_format: if CFG_CERN_SITE and url_format == 'ps.gz' and len(urls_and_format) > 1: ## We skip old PS.GZ files continue url_list.append('<a %(style)s href="%(url)s">%(file_icon)s%(url_format)s</a>' % { 'style': style, 'url': get_relative_url(escape(url, True)), 'file_icon': file_icon, 'url_format': escape(url_format.upper()) }) out += separator + " ".join(url_list) out += additional_str + versions_str + separator + "</div>" if CFG_CERN_SITE and cern_urls: # Put a big file icon if only one file if len(main_urls.keys()) == 0 and \ len(cern_urls) == 1 and len(others_urls) == 0 and \ show_icons.lower() == 'yes': file_icon = '<img style="border:none" src="%s/img/file-icon-text-34x48.gif" alt="%s" /><br />' % (CFG_BASE_URL, _("Download fulltext")) elif show_icons.lower() == 'yes': file_icon = '<img style="border:none" src="%s/img/file-icon-text-12x16.gif" alt="%s"/>' % (CFG_BASE_URL, _("Download fulltext")) else: file_icon = '' link_word = len(cern_urls) == 1 and _('%(x_sitename)s link') or _('%(x_sitename)s links') out += '<small class="detailedRecordActions">%s:</small><br />' % (link_word % {'x_sitename': 'CERN'}) url_list = [] for url, descr in cern_urls: url_list.append('<a '+style+' href="'+escape(url)+'">'+file_icon+escape(str(descr))+'</a>') out += '<small>' + separator.join(url_list) + '</small>' out += "<br/>" if others_urls: # Put a big file icon if only one file if len(main_urls.keys()) == 0 and \ (not CFG_CERN_SITE or len(cern_urls) == 0) and len(others_urls) == 1 and \ show_icons.lower() == 'yes': file_icon = '<img style="border:none" src="%s/img/file-icon-text-34x48.gif" alt="%s" /><br />' % (CFG_BASE_URL, _("Download fulltext")) elif show_icons.lower() == 'yes': file_icon = '<img style="border:none" src="%s/img/file-icon-text-12x16.gif" alt="%s"/>' % (CFG_BASE_URL, _("Download fulltext")) else: file_icon = '' external_link = len(others_urls) == 1 and _('external link') or _('external links') out += '<small class="detailedRecordActions">%s:</small>%s' % (external_link.capitalize(), separator) url_list = [] for url, descr in others_urls: # we don't need to show the plot links here, and all are pngs. if url.find('.png') > -1: continue url_list.append('<a '+style+' href="'+escape(url)+'">'+file_icon+escape(str(descr))+'</a>') out += '<small>' + separator.join(url_list) + '</small>' if out.endswith('<br />'): out = out[:-len('<br />')] return out
def format_element(bfo, style, separator='; ', show_icons='no', focus_on_main_file='yes', show_subformat_icons='no'): """ This is the format for formatting fulltext links in the mini panel. @param separator: the separator between urls. @param style: CSS class of the link @param show_icons: if 'yes', print icons for fulltexts @param focus_on_main_file: if 'yes' and a doctype 'Main' is found, prominently display this doctype. In that case other doctypes are summarized with a link to the Files tab, named"Additional files". @param show_subformat_icons: shall we display subformats considered as icons? """ _ = gettext_set_language(bfo.lang) out = '' # 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') main_urls = parsed_urls['main_urls'] others_urls = parsed_urls['others_urls'] if parsed_urls.has_key('cern_urls'): cern_urls = parsed_urls['cern_urls'] # Prepare style if style != "": style = 'class="' + style + '"' # Build urls list. # Escape special chars for <a> tag value. additional_str = '' if additionals: additional_str = separator + '<small>(<a ' + style + ' href="' + CFG_BASE_URL + '/' + CFG_SITE_RECORD + '/' + str( bfo.recID) + '/files/">%s</a>)</small>' % _("additional files") versions_str = '' #if old_versions: #versions_str = separator + '<small>(<a '+style+' href="'+CFG_SITE_URL+'/CFG_SITE_RECORD/'+str(bfo.recID)+'/files/">%s</a>)</small>' % _("older versions") if main_urls: # Put a big file icon if only one file if len(main_urls.keys()) == 1 and len(main_urls.items()[0][1]) == 1 and \ (not CFG_CERN_SITE or len(cern_urls) == 0) and len(others_urls) == 0 and \ show_icons.lower() == 'yes': file_icon = '<img style="border:none" src="%s/img/file-icon-text-34x48.gif" alt="%s" /><br />' % ( CFG_BASE_URL, _("Download fulltext")) elif show_icons.lower() == 'yes': file_icon = '<img style="border:none" src="%s/img/file-icon-text-12x16.gif" alt="%s"/>' % ( CFG_BASE_URL, _("Download fulltext")) else: file_icon = '' main_urls_keys = sort_alphanumerically(main_urls.keys()) for descr in main_urls_keys: urls = main_urls[descr] out += '<div><small class="detailedRecordActions">%s:</small> ' % descr urls_dict = {} for url, name, url_format in urls: if name not in urls_dict: urls_dict[name] = [(url, url_format)] else: urls_dict[name].append((url, url_format)) for name, urls_and_format in urls_dict.items(): if len(urls_dict) > 1: print_name = "<em>%s</em> - " % name url_list = [print_name] else: url_list = [] for url, url_format in urls_and_format: if CFG_CERN_SITE and url_format == 'ps.gz' and len( urls_and_format) > 1: ## We skip old PS.GZ files continue url_list.append( '<a %(style)s href="%(url)s">%(file_icon)s%(url_format)s</a>' % { 'style': style, 'url': get_relative_url(escape(url, True)), 'file_icon': file_icon, 'url_format': escape(url_format.upper()) }) out += separator + " ".join(url_list) out += additional_str + versions_str + separator + "</div>" if CFG_CERN_SITE and cern_urls: # Put a big file icon if only one file if len(main_urls.keys()) == 0 and \ len(cern_urls) == 1 and len(others_urls) == 0 and \ show_icons.lower() == 'yes': file_icon = '<img style="border:none" src="%s/img/file-icon-text-34x48.gif" alt="%s" /><br />' % ( CFG_BASE_URL, _("Download fulltext")) elif show_icons.lower() == 'yes': file_icon = '<img style="border:none" src="%s/img/file-icon-text-12x16.gif" alt="%s"/>' % ( CFG_BASE_URL, _("Download fulltext")) else: file_icon = '' link_word = len(cern_urls) == 1 and _('%(x_sitename)s link') or _( '%(x_sitename)s links') out += '<small class="detailedRecordActions">%s:</small><br />' % ( link_word % { 'x_sitename': 'CERN' }) url_list = [] for url, descr in cern_urls: url_list.append('<a ' + style + ' href="' + escape(url) + '">' + file_icon + escape(str(descr)) + '</a>') out += '<small>' + separator.join(url_list) + '</small>' out += "<br/>" if others_urls: # Put a big file icon if only one file if len(main_urls.keys()) == 0 and \ (not CFG_CERN_SITE or len(cern_urls) == 0) and len(others_urls) == 1 and \ show_icons.lower() == 'yes': file_icon = '<img style="border:none" src="%s/img/file-icon-text-34x48.gif" alt="%s" /><br />' % ( CFG_BASE_URL, _("Download fulltext")) elif show_icons.lower() == 'yes': file_icon = '<img style="border:none" src="%s/img/file-icon-text-12x16.gif" alt="%s"/>' % ( CFG_BASE_URL, _("Download fulltext")) else: file_icon = '' external_link = len(others_urls) == 1 and _('external link') or _( 'external links') out += '<small class="detailedRecordActions">%s:</small>%s' % ( external_link.capitalize(), separator) url_list = [] for url, descr in others_urls: # we don't need to show the plot links here, and all are pngs. if url.find('.png') > -1: continue url_list.append('<a ' + style + ' href="' + escape(url) + '">' + file_icon + escape(str(descr)) + '</a>') out += '<small>' + separator.join(url_list) + '</small>' if out.endswith('<br />'): out = out[:-len('<br />')] return out
def format_element(bfo, style, separator='; ', show_icons='no', focus_on_main_file='no', show_subformat_icons='no'): """ 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 @param separator: the separator between urls. @param style: CSS class of the link @param show_icons: if 'yes', print icons for fulltexts @param focus_on_main_file: if 'yes' and a doctype 'Main' is found, prominently display this doctype. In that case other doctypes are summarized with a link to the Files tab, named "Additional files" @param show_subformat_icons: shall we display subformats considered as icons? """ _ = gettext_set_language(bfo.lang) out = '' # 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') main_urls = parsed_urls['main_urls'] others_urls = parsed_urls['others_urls'] if parsed_urls.has_key('cern_urls'): cern_urls = parsed_urls['cern_urls'] # Prepare style and icon if style != "": style = 'class="'+style+'"' if show_icons.lower() == 'yes': file_icon = '<img style="border:none" src="%s/img/file-icon-text-12x16.gif" alt="%s"/>' % (CFG_BASE_URL, _("Download fulltext")) else: file_icon = '' # Build urls list. # Escape special chars for <a> tag value. additional_str = '' if additionals: additional_str = ' <small>(<a '+style+' href="'+CFG_BASE_URL+'/%s/' % CFG_SITE_RECORD + str(bfo.recID)+'/files/">%s</a>)</small>' % _("additional files") versions_str = '' #if old_versions: #versions_str = ' <small>(<a '+style+' href="'+CFG_BASE_URL+'/CFG_SITE_RECORD/'+str(bfo.recID)+'/files/">%s</a>)</small>' % _("older versions") if main_urls: main_urls_keys = sort_alphanumerically(main_urls.keys()) for descr in main_urls_keys: urls = main_urls[descr] if re.match(r'^\d+\s', descr) and urls[0][2] == 'png': # FIXME: we have probably hit a Plot (as link # description looks like '0001 This is Caption'), so # do not take it. This test is not ideal, we should # rather study doc type, and base ourselves on # Main/Additional/Plot etc. continue out += "<strong>%s:</strong> " % descr urls_dict = {} for url, name, url_format in urls: if name not in urls_dict: urls_dict[name] = [(get_relative_url(url), url_format)] else: urls_dict[name].append((get_relative_url(url), url_format)) for name, urls_and_format in urls_dict.items(): if len(urls_dict) > 1: print_name = "<em>%s</em> - " % name url_list = [print_name] else: url_list = [] for url, url_format in urls_and_format: if CFG_CERN_SITE and url_format == 'ps.gz' and len(urls_and_format) > 1: ## We skip old PS.GZ files continue url_list.append('<a %(style)s href="%(url)s">%(file_icon)s%(url_format)s</a>' % { 'style': style, 'url': escape(url, True), 'file_icon': file_icon, 'url_format': escape(url_format.upper()) }) out += " ".join(url_list) + additional_str + versions_str + separator if CFG_CERN_SITE and cern_urls: link_word = len(cern_urls) == 1 and _('%(x_sitename)s link') or _('%(x_sitename)s links') out += '<strong>%s</strong>: ' % (link_word % {'x_sitename': 'CERN'}) url_list = [] for url, descr in cern_urls: url_list.append('<a '+style+' href="'+escape(url)+'">'+ \ file_icon + escape(str(descr))+'</a>') out += separator.join(url_list) if others_urls: external_link = len(others_urls) == 1 and _('external link') or _('external links') out += '<strong>%s</strong>: ' % external_link.capitalize() url_list = [] for url, descr in others_urls: url_list.append('<a '+style+' href="'+escape(url)+'">'+ \ file_icon + escape(str(descr))+'</a>') out += separator.join(url_list) + '<br />' if out.endswith('<br />'): out = out[:-len('<br />')] # When exported to text (eg. in WebAlert emails) we do not want to # display the link to the fulltext: if out: out = '<!--START_NOT_FOR_TEXT-->' + out + '<!--END_NOT_FOR_TEXT-->' return out
def format_element(bfo, style, separator='; ', show_icons='no', focus_on_main_file='no', show_subformat_icons='no'): """ 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 @param separator: the separator between urls. @param style: CSS class of the link @param show_icons: if 'yes', print icons for fulltexts @param focus_on_main_file: if 'yes' and a doctype 'Main' is found, prominently display this doctype. In that case other doctypes are summarized with a link to the Files tab, named "Additional files" @param show_subformat_icons: shall we display subformats considered as icons? """ _ = gettext_set_language(bfo.lang) out = '' # 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', hide_doctypes=_CFG_BIBFORMAT_HIDDEN_DOCTYPES) main_urls = parsed_urls['main_urls'] others_urls = parsed_urls['others_urls'] if parsed_urls.has_key('cern_urls'): cern_urls = parsed_urls['cern_urls'] # Prepare style and icon if style != "": style = 'class="' + style + '"' if show_icons.lower() == 'yes': file_icon = '<img style="border:none" src="%s/img/file-icon-text-12x16.gif" alt="%s"/>' % ( CFG_BASE_URL, _("Download fulltext")) else: file_icon = '' # Build urls list. # Escape special chars for <a> tag value. additional_str = '' if additionals: additional_str = ' <small>(<a ' + style + ' href="' + CFG_BASE_URL + '/%s/' % CFG_SITE_RECORD + str( bfo.recID) + '/files/">%s</a>)</small>' % _("additional files") versions_str = '' #if old_versions: #versions_str = ' <small>(<a '+style+' href="'+CFG_BASE_URL+'/CFG_SITE_RECORD/'+str(bfo.recID)+'/files/">%s</a>)</small>' % _("older versions") if main_urls: main_urls_keys = sort_alphanumerically(main_urls.keys()) for descr in main_urls_keys: urls = main_urls[descr] out += "<strong>%s:</strong> " % descr urls_dict = {} for url, name, url_format in urls: if name not in urls_dict: urls_dict[name] = [(get_relative_url(url), url_format)] else: urls_dict[name].append((get_relative_url(url), url_format)) for name, urls_and_format in urls_dict.items(): if len(urls_dict) > 1: print_name = "<em>%s</em> - " % name url_list = [print_name] else: url_list = [] for url, url_format in urls_and_format: if CFG_CERN_SITE and url_format == 'ps.gz' and len( urls_and_format) > 1: ## We skip old PS.GZ files continue url_list.append( '<a %(style)s href="%(url)s">%(file_icon)s%(url_format)s</a>' % { 'style': style, 'url': escape(url, True), 'file_icon': file_icon, 'url_format': escape(url_format.upper()) }) out += " ".join( url_list) + additional_str + versions_str + separator if CFG_CERN_SITE and cern_urls: link_word = len(cern_urls) == 1 and _('%(x_sitename)s link') or _( '%(x_sitename)s links') out += '<strong>%s</strong>: ' % (link_word % {'x_sitename': 'CERN'}) url_list = [] for url, descr in cern_urls: url_list.append('<a '+style+' href="'+escape(url)+'">'+ \ file_icon + escape(str(descr))+'</a>') out += separator.join(url_list) if others_urls: external_link = len(others_urls) == 1 and _('external link') or _( 'external links') out += '<strong>%s</strong>: ' % external_link.capitalize() url_list = [] for url, descr in others_urls: url_list.append('<a '+style+' href="'+escape(url)+'">'+ \ file_icon + escape(str(descr))+'</a>') out += separator.join(url_list) + '<br />' if out.endswith('<br />'): out = out[:-len('<br />')] # When exported to text (eg. in WebAlert emails) we do not want to # display the link to the fulltext: if out: out = '<!--START_NOT_FOR_TEXT-->' + out + '<!--END_NOT_FOR_TEXT-->' return out
def format_element(bfo, separator=" ", style='', img_style='', text_style='font-size:small', print_links='yes', max_photos='', show_comment='yes', img_max_width='250px', display_all_version_links='yes'): """ Lists the photos of a record. Display the icon version, linked to its original version. This element works for photos appended to a record as BibDoc files, for which a preview icon has been generated. If there are several formats for one photo, use the first one found. @param separator: separator between each photo @param print_links: if 'yes', print links to the original photo @param style: style attributes of the whole image block. Eg: "padding:2px;border:1px" @param img_style: style attributes of the images. Eg: "width:50px;border:none" @param text_style: style attributes of the text. Eg: "font-size:small" @param max_photos: the maximum number of photos to display @param show_comment: if 'yes', display the comment of each photo @param display_all_version_links: if 'yes', print links to additional (sub)formats """ photos = [] bibarchive = BibRecDocs(bfo.recID) bibdocs = bibarchive.list_bibdocs() if max_photos.isdigit(): max_photos = int(max_photos) else: max_photos = len(bibdocs) for doc in bibdocs[:max_photos]: found_icons = [] found_url = '' for docfile in doc.list_latest_files(): if docfile.is_icon(): found_icons.append(( docfile.get_size(), get_relative_url(docfile.get_url()) )) else: found_url = get_relative_url(docfile.get_url()) found_icons.sort() if found_icons: additional_links = '' name = bibarchive.get_docname(doc.id) comment = doc.list_latest_files()[0].get_comment() preview_url = None if len(found_icons) > 1: preview_url = get_relative_url(found_icons[1][1]) additional_urls = [(docfile.get_size(), get_relative_url(docfile.get_url()), \ docfile.get_superformat(), docfile.get_subformat()) \ for docfile in doc.list_latest_files() if not docfile.is_icon()] additional_urls.sort() additional_links = [create_html_link(url, urlargd={}, \ linkattrd={'style': 'font-size:x-small'}, \ link_label="%s %s (%s)" % (format.strip('.').upper(), subformat, format_size(size))) \ for (size, url, format, subformat) in additional_urls] img = '<img src="%(icon_url)s" alt="%(name)s" style="max-width:%(img_max_width)s;_width:%(img_max_width)s;%(img_style)s" />' % \ {'icon_url': cgi.escape(get_relative_url(found_icons[0][1]), True), 'name': cgi.escape(name, True), 'img_style': img_style, 'img_max_width': img_max_width} if print_links.lower() == 'yes': img = '<a href="%s">%s</a>' % (cgi.escape(preview_url or found_url, True), img) if display_all_version_links.lower() == 'yes' and additional_links: img += '<br />' + ' '.join(additional_links) + '<br />' if show_comment.lower() == 'yes' and comment: img += '<div style="margin-auto;text-align:center;%(text_style)s">%(comment)s</div>' % \ {'comment': comment.replace('\n', '<br/>'), 'text_style': text_style} img = '<div style="vertical-align: middle;text-align:center;display:inline-block;display: -moz-inline-stack;zoom: 1;*display: inline;max-width:%(img_max_width)s;_width:%(img_max_width)s;text-align:center;%(style)s">%(img)s</div>' % \ {'img_max_width': img_max_width, 'style': style, 'img': img} photos.append(img) return '<div>' + separator.join(photos) + '</div>'