def read(self, request, vendor, name, version): from_version = request.GET.get('from') if from_version is not None: try: from_version = Version(from_version) except: return build_error_response( request, 422, _("Missing parameter: template_uri or file")) resource = get_object_or_404(CatalogueResource, vendor=vendor, short_name=name, version=version) resource_info = resource.get_processed_info(process_urls=False) if resource_info['changelog'] == '': raise Http404 doc_relative_path = url2pathname(resource_info['changelog']) doc_base_url = force_trailing_slash( urljoin(resource.get_template_url(request=request, for_base=True), pathname2url(os.path.dirname(doc_relative_path)))) doc_path = os.path.join( catalogue_utils.wgt_deployer.get_base_dir(vendor, name, version), doc_relative_path) (doc_filename_root, doc_filename_ext) = os.path.splitext(doc_path) localized_doc_path = doc_filename_root + '.' + get_language( ) + doc_filename_ext try: doc_code = download_local_file(localized_doc_path).decode('utf-8') except: try: doc_code = download_local_file(doc_path).decode('utf-8') except: msg = _('Error opening the changelog file') doc_code = '<div class="margin-top: 10px"><p>%s</p></div>' % msg doc_pre_html = markdown.markdown(doc_code, output_format='xhtml5', extensions=[ 'markdown.extensions.codehilite', 'markdown.extensions.fenced_code' ]) if from_version: doc_pre_html = filter_changelog(doc_pre_html, from_version) if doc_pre_html.strip() == '': raise Http404 doc = clean_html(doc_pre_html, base_url=doc_base_url) return HttpResponse( doc, content_type='application/xhtml+xml; charset=UTF-8')
def read(self, request, vendor, name, version): from_version = request.GET.get('from') if from_version is not None: try: from_version = Version(from_version) except: return build_error_response(request, 422, _("Missing parameter: template_uri or file")) resource = get_object_or_404(CatalogueResource, vendor=vendor, short_name=name, version=version) resource_info = resource.get_processed_info(process_urls=False) if resource_info['changelog'] == '': raise Http404 doc_relative_path = url2pathname(resource_info['changelog']) doc_base_url = force_trailing_slash(urljoin(resource.get_template_url(request=request, for_base=True), pathname2url(os.path.dirname(doc_relative_path)))) doc_path = os.path.join(catalogue_utils.wgt_deployer.get_base_dir(vendor, name, version), doc_relative_path) (doc_filename_root, doc_filename_ext) = os.path.splitext(doc_path) localized_doc_path = doc_filename_root + '.' + get_language() + doc_filename_ext try: doc_code = download_local_file(localized_doc_path).decode('utf-8') except: try: doc_code = download_local_file(doc_path).decode('utf-8') except: msg = _('Error opening the changelog file') doc_code = '<div class="margin-top: 10px"><p>%s</p></div>' % msg doc_pre_html = markdown.markdown(doc_code, output_format='xhtml5', extensions=['codehilite', 'fenced_code']) if from_version: doc_pre_html = filter_changelog(doc_pre_html, from_version) if doc_pre_html.strip() == '': raise Http404 doc = clean_html(doc_pre_html, base_url=doc_base_url) return HttpResponse(doc, content_type='application/xhtml+xml; charset=UTF-8')
def test_filter_changelog_not_exact_version(self): # Filter html, exact version not found, but there is a lower version. initial_code = '<h1>1.0.2</h1><p>1.0.2 change list</p><h1>1.0.1</h1><p>1.0.1 change list</p><h1>1.0.0</h1><p>Initial release</p>' expected_code = '<h1>1.0.2</h1><p>1.0.2 change list</p><h1>1.0.1</h1><p>1.0.1 change list</p>' self.assertEqual(filter_changelog(initial_code, Version('1.0.0.1')), expected_code)
def test_filter_changelog_version_not_found(self): initial_code = '<h1>1.0.2</h1><p>1.0.2 change list</p><h1>1.0.1</h1><p>1.0.1 change list</p><h1>1.0.0</h1><p>Initial release</p>' expected_code = initial_code self.assertEqual(filter_changelog(initial_code, Version('0.9.0')), expected_code)
def test_filter_changelog_headers_with_extra_content(self): initial_code = '<h1>1.0.2 (2015-05-01)</h1><p>1.0.2 change list</p><h1>1.0.1 (2015-04-01)</h1><p>1.0.1 change list</p><h1>1.0.0 (2015-03-01)</h1><p>Initial release</p>' expected_code = '<h1>1.0.2 (2015-05-01)</h1><p>1.0.2 change list</p>' self.assertEqual(filter_changelog(initial_code, Version('1.0.1')), expected_code)
def test_filter_changelog_mixed(self): # Filter html, there are mixed sections between version sections initial_code = '<h2>v1.0.2</h2><p>v1.0.2 change list</p><h2>Extra header</h2><p>my extra info</p><h2>v1.0.1</h2><p>v1.0.1 change list</p><h2>v1.0.0</h2><p>Initial release</p>' expected_code = '<h2>v1.0.2</h2><p>v1.0.2 change list</p><h2>Extra header</h2><p>my extra info</p>' self.assertEqual(filter_changelog(initial_code, Version('1.0.1')), expected_code)
def test_filter_changelog_nested(self): # Filter html, exact version found, there is extra info before that must be discarded initial_code = '<h1>My Widgets changes</h1> my intro<h2>v1.0.2</h2><p>v1.0.2 change list</p><h2>v1.0.1</h2><p>v1.0.1 change list</p><h2>v1.0.0</h2><p>Initial release</p>' expected_code = '<h2>v1.0.2</h2><p>v1.0.2 change list</p>' self.assertEqual(filter_changelog(initial_code, Version('1.0.1')), expected_code)
def test_filter_changelog_no_changes_v(self): # Filter html, exact version found, but there are no change info before initial_code = '<h2>v1.0.2</h2> tail <p>v1.0.2 change list</p><h2>v1.0.1</h2><p>v1.0.1 change list</p><h2>v1.0.0</h2><p>Initial release</p> tail' expected_code = '' self.assertEqual(filter_changelog(initial_code, Version('1.0.2')), expected_code)
def test_filter_changelog(self): initial_code = '<h1>1.0.2</h1><p>1.0.2 change list</p><h1>1.0.1</h1><p>1.0.1 change list</p><h1>1.0.0</h1><p>Initial release</p>' expected_code = '<h1>1.0.2</h1><p>1.0.2 change list</p>' self.assertEqual(filter_changelog(initial_code, Version('1.0.1')), expected_code)
def test_filter_changelog_not_exact_version_v(self): # Filter html, exact version not found, but there is a lower version. initial_code = '<h1>v1.0.2</h1><p>v1.0.2 change list</p><h1>v1.0.1</h1><p>v1.0.1 change list</p><h1>v1.0.0</h1><p>Initial release</p>' expected_code = '<h1>v1.0.2</h1><p>v1.0.2 change list</p><h1>v1.0.1</h1><p>v1.0.1 change list</p>' self.assertEqual(filter_changelog(initial_code, Version('1.0.0.1')), expected_code)
def test_filter_changelog_headers_with_extra_content_v(self): initial_code = '<h1>v1.0.2 (2015-05-01)</h1><p>v1.0.2 change list</p><h1>v1.0.1 (2015-04-01)</h1><p>v1.0.1 change list</p><h1>v1.0.0 (2015-03-01)</h1><p>Initial release</p>' expected_code = '<h1>v1.0.2 (2015-05-01)</h1><p>v1.0.2 change list</p>' self.assertEqual(filter_changelog(initial_code, Version('1.0.1')), expected_code)