def test_clean_xhtml_mixed2(self): expected_clean = "<p>First line first line cont'd.</p><p>second line</p><p>third line</p>" dirty1 = """<p>First line\nfirst line cont'd.\n\nsecond line</p>\nthird line\n""" dirty2 = """<p>First line\nfirst line cont'd.\n\nsecond line</p>\nthird line\n\n""" # Ensure that the cleaned XHTML is the same regardless of trailing newlines. clean1 = clean_xhtml(dirty1) clean2 = clean_xhtml(dirty2) assert clean1 == clean2, results_text % (clean1, clean2)
def test_clean_xhtml_mixed1(self): expected_clean = "<p>First line first line cont'd.</p><p>second line</p><p>third line</p>" dirty = """<p>First line\nfirst line cont'd.\n\nsecond line</p>\nthird line\n\n""" # Ensure that the cleaned XHTML is what we expected clean = clean_xhtml(dirty) assert clean == expected_clean, expected_text % (expected_clean, clean) # Ensure that re-cleaning the XHTML provides the same result. clean = clean_xhtml(clean) assert clean == expected_clean, expected_text % (expected_clean, clean)
def test_xhtmltextarea_logic(self): """Mimics the input -> clean -> display -> input... cycle of the XHTMLTextArea widget.""" expected_clean = "<p>First line first line cont'd</p><p>second line</p><p>third line</p><p>fourth line</p>" dirty = "First line\nfirst line cont'd\n\nsecond line\n\nthird line\n\n\n\nfourth line" # Ensure that the cleaned XHTML is what we expected clean = clean_xhtml(dirty) assert clean == expected_clean, expected_text % (expected_clean, clean) # Ensure that re-cleaning the XHTML provides the same result. displayed = line_break_xhtml(clean) clean = clean_xhtml(displayed) assert clean == expected_clean, expected_text % (expected_clean, clean)
def test_text_do_not_change_after_a_clean_xhtml_and_line_break_xhtml_cycle(self): """Mimics the input -> clean -> display -> input... cycle of the XHTMLTextArea widget. """ expected_html = '<p>first line</p><p>second line</p>' htmlified_text = clean_xhtml('first\nline\n\nsecond line') assert_equals(expected_html, htmlified_text) # Ensure that re-cleaning the XHTML provides the same result. display_text = line_break_xhtml(htmlified_text) assert_equals('<p>first line</p>\n<p>second line</p>', display_text) assert_equals(expected_html, clean_xhtml(display_text))
def test_text_do_not_change_after_a_clean_xhtml_and_line_break_xhtml_cycle( self): """Mimics the input -> clean -> display -> input... cycle of the XHTMLTextArea widget. """ expected_html = '<p>first line</p><p>second line</p>' htmlified_text = clean_xhtml('first\nline\n\nsecond line') assert_equals(expected_html, htmlified_text) # Ensure that re-cleaning the XHTML provides the same result. display_text = line_break_xhtml(htmlified_text) assert_equals('<p>first line</p>\n<p>second line</p>', display_text) assert_equals(expected_html, clean_xhtml(display_text))
def _to_python(self, value, state=None): """Convert the given plain text or HTML into valid XHTML. Invalid elements are stripped or converted. Essentially a wapper for :func:`~mediacore.helpers.clean_xhtml`. """ return clean_xhtml(value)
def comment(self, slug, **values): """Post a comment from :class:`~mediacore.forms.media.PostCommentForm`. :param slug: The media :attr:`~mediacore.model.media.Media.slug` :returns: Redirect to :meth:`view` page for media. """ if tmpl_context.form_errors: if request.is_xhr: return dict( success = False, errors = tmpl_context.form_errors ) else: redirect(action='view') media = fetch_row(Media, slug=slug) c = Comment() c.status = 'unreviewed' c.author = AuthorWithIP(values['name'], None, request.environ['REMOTE_ADDR']) c.subject = 'Re: %s' % media.title c.body = helpers.clean_xhtml(values['body']) media.comments.append(c) DBSession.add(media) email.send_comment_notification(media, c) if request.is_xhr: return dict(success = True) else: redirect(action='view')
def _to_python(self, value, state=None): """Strip XML tags and convert XHTML entities in to unicode.""" cleaner_settings = dict( convert_entities = BeautifulStoneSoup.ALL_ENTITIES, filters = ['strip_tags'], valid_tags = [], ) return clean_xhtml( value, p_wrap=False, _cleaner_settings = cleaner_settings, )
def document(self, *args, **kwargs): """Render the error document for the general public. Essentially, when an error occurs, a second request is initiated for the URL ``/error/document``. The URL is set on initialization of the :class:`pylons.middleware.StatusCodeRedirect` object, and can be overridden in :func:`tg.configuration.add_error_middleware`. Also, before this method is called, some potentially useful environ vars are set in :meth:`pylons.middleware.StatusCodeRedirect.__call__` (access them via :attr:`tg.request.environ`). :rtype: Dict :returns: prefix The environ SCRIPT_NAME. vars A dict containing the first 2 KB of the original request. code Integer error code thrown by the original request, but it can also be overriden by setting ``tg.request.params['code']``. message A message to display to the user. Pulled from ``tg.request.params['message']``. """ request = self._py_object.request environ = request.environ original_request = environ.get('pylons.original_request', None) original_response = environ.get('pylons.original_response', None) default_message = '<p>%s</p>' % _("We're sorry but we weren't able " "to process this request.") message = request.params.get('message', default_message) message = clean_xhtml(message) return dict( prefix=environ.get('SCRIPT_NAME', ''), code=int( request.params.get( 'code', getattr(original_response, 'status_int', 500))), message=message, vars=dict(POST_request=unicode(original_request)[:2048]), )
def document(self, *args, **kwargs): """Render the error document for the general public. Essentially, when an error occurs, a second request is initiated for the URL ``/error/document``. The URL is set on initialization of the :class:`pylons.middleware.StatusCodeRedirect` object, and can be overridden in :func:`tg.configuration.add_error_middleware`. Also, before this method is called, some potentially useful environ vars are set in :meth:`pylons.middleware.StatusCodeRedirect.__call__` (access them via :attr:`tg.request.environ`). :rtype: Dict :returns: prefix The environ SCRIPT_NAME. vars A dict containing the first 2 KB of the original request. code Integer error code thrown by the original request, but it can also be overriden by setting ``tg.request.params['code']``. message A message to display to the user. Pulled from ``tg.request.params['message']``. """ request = self._py_object.request environ = request.environ original_request = environ.get('pylons.original_request', None) original_response = environ.get('pylons.original_response', None) default_message = '<p>%s</p>' % _("We're sorry but we weren't able " "to process this request.") message = request.params.get('message', default_message) message = clean_xhtml(message) return dict( prefix = environ.get('SCRIPT_NAME', ''), code = int(request.params.get('code', getattr(original_response, 'status_int', 500))), message = message, vars = dict(POST_request=unicode(original_request)[:2048]), )
def save_edit(self, id, body, **kwargs): """Save an edit from :class:`~mediacore.forms.comments.EditCommentForm`. :param id: Comment ID :type id: ``int`` :rtype: JSON dict :returns: success bool body The edited comment body after validation/filtering """ comment = fetch_row(Comment, id) comment.body = helpers.clean_xhtml(body) DBSession.add(comment) return dict( success = True, body = comment.body, )
def _save_media_obj(self, name, email, title, description, tags, file): # cope with anonymous posters if name is None: name = 'Anonymous' # create our media object as a status-less placeholder initially media_obj = Media() media_obj.author = Author(name, email) media_obj.title = title media_obj.slug = get_available_slug(Media, title) media_obj.description = helpers.clean_xhtml(description) media_obj.status = 'draft,unencoded,unreviewed' media_obj.notes = helpers.fetch_setting('wording_additional_notes') media_obj.set_tags(tags) # Create a media object, add it to the media_obj, and store the file permanently. media_file = _add_new_media_file(media_obj, file.filename, file.file) # Add the final changes. media_obj.update_type() media_obj.update_status() DBSession.add(media_obj) return media_obj
def test_trailing_newlines_are_removed_in_output(self): assert_equals(clean_xhtml('first\n'), clean_xhtml('first\n\n'))
def test_can_replace_linebreaks_with_p_tags(self): htmlified_text = clean_xhtml('first\nline\n\nsecond line') assert_equals('<p>first line</p><p>second line</p>', htmlified_text) assert_equals(htmlified_text, clean_xhtml(htmlified_text))