def test_html_headcheck(self): from Products.CMFDefault.utils import html_headcheck self.assertEqual( html_headcheck(FAUX_HTML_LEADING_TEXT), 0 ) self.assertEqual( html_headcheck(SIMPLE_HTML), 1 ) self.assertEqual( html_headcheck(SIMPLE_STRUCTUREDTEXT), 0 ) self.assertEqual( html_headcheck(SIMPLE_XHTML), 1 ) self.assertEqual( html_headcheck(STX_WITH_HTML), 0 )
def test_html_headcheck(self): from Products.CMFDefault.utils import html_headcheck self.assertEqual(html_headcheck(FAUX_HTML_LEADING_TEXT), 0) self.assertEqual(html_headcheck(SIMPLE_HTML), 1) self.assertEqual(html_headcheck(SIMPLE_STRUCTUREDTEXT), 0) self.assertEqual(html_headcheck(SIMPLE_XHTML), 1) self.assertEqual(html_headcheck(STX_WITH_HTML), 0)
def PUT(self, REQUEST, RESPONSE): """ Handle HTTP (and presumably FTP?) PUT requests """ if not NoWL: self.dav__init(REQUEST, RESPONSE) self.dav__simpleifhandler(REQUEST, RESPONSE, refresh=1) body = REQUEST.get('BODY', '') guessedformat = REQUEST.get_header('Content-Type', 'text/plain') ishtml = (guessedformat == 'text/html') or html_headcheck(body) if ishtml: self.setFormat('text/html') else: self.setFormat('text/plain') try: headers, body, format = self.handleText(text=body) self.setMetadata(headers) self.setStartDate(headers['StartDate']) self.setEndDate(headers['EndDate']) self.edit( location=headers['Location'] , contact_name=headers['ContactName'] , contact_email=headers['ContactEmail'] , contact_phone=headers['ContactPhone'] , event_url=headers['EventURL'] ) except ResourceLockedError, msg: get_transaction().abort() RESPONSE.setStatus(423) return RESPONSE
def setFormat(self, format): """ Set text format and Dublin Core resource format. """ value = str(format) old_value = self.text_format text_formats = ('structured-text', 'plain', 'restructured-text') if value == 'text/html' or value == 'html': self.text_format = 'html' elif value == 'text/plain': if self.text_format not in text_formats: self.text_format = 'structured-text' elif value == 'plain': self.text_format = 'plain' elif value == 'restructured-text': self.text_format = 'restructured-text' else: self.text_format = 'structured-text' # Did the format change? We might need to re-cook the content. if value != old_value: if html_headcheck(self.text) and value != 'plain': self.text = bodyfinder(self.text) self._edit(self.text)
def edit(self, text_format, text, file='', safety_belt=''): """ Update the document. To add webDav support, we need to check if the content is locked, and if so return ResourceLockedError if not, call _edit. Note that this method expects to be called from a web form, and so disables header processing """ self.failIfLocked() if not self._safety_belt_update(safety_belt=safety_belt): msg = _(u'Intervening changes from elsewhere detected. ' u'Please refetch the document and reapply your changes. ' u'(You may be able to recover your version using the ' u"browser 'back' button, but will have to apply them to " u'a freshly fetched copy.)') raise EditingConflict(msg) if file and (type(file) is not type('')): contents = file.read() if contents: text = contents if html_headcheck(text) and text_format.lower() != 'plain': text = bodyfinder(text) self.setFormat(text_format) self._edit(text) self.reindexObject()
def PUT(self, REQUEST, RESPONSE): """ Handle HTTP (and presumably FTP?) PUT requests """ self.dav__init(REQUEST, RESPONSE) self.dav__simpleifhandler(REQUEST, RESPONSE, refresh=1) body = REQUEST.get('BODY', '') guessedformat = REQUEST.get_header('Content-Type', 'text/plain') ishtml = (guessedformat == 'text/html') or html_headcheck(body) if ishtml: self.setFormat('text/html') else: self.setFormat('text/plain') try: headers, body, format = self.handleText(text=body) self.setMetadata(headers) self.setStartDate(headers['StartDate']) self.setEndDate(headers['EndDate']) self.edit( location=headers['Location'] , contact_name=headers['ContactName'] , contact_email=headers['ContactEmail'] , contact_phone=headers['ContactPhone'] , event_url=headers['EventURL'] ) except ResourceLockedError, msg: get_transaction().abort() RESPONSE.setStatus(423) return RESPONSE
def edit(self, text_format, text, file='', safety_belt=''): """ Update the document. To add webDav support, we need to check if the content is locked, and if so return ResourceLockedError if not, call _edit. Note that this method expects to be called from a web form, and so disables header processing """ self.failIfLocked() if not self._safety_belt_update(safety_belt=safety_belt): msg = _(u'Intervening changes from elsewhere detected. ' u'Please refetch the document and reapply your changes. ' u'(You may be able to recover your version using the ' u"browser 'back' button, but will have to apply them to " u'a freshly fetched copy.)') raise EditingConflict(msg) if file and (type(file) is not type('')): contents=file.read() if contents: text = contents if html_headcheck(text) and text_format.lower() != 'plain': text = bodyfinder(text) self.setFormat(text_format) self._edit(text) self.reindexObject()
def test_html_headcheck(self): self.assertEqual( html_headcheck(FAUX_HTML_LEADING_TEXT), 0 ) self.assertEqual( html_headcheck(SIMPLE_HTML), 1 ) self.assertEqual( html_headcheck(SIMPLE_STRUCTUREDTEXT), 0 ) self.assertEqual( html_headcheck(SIMPLE_XHTML), 1 ) self.assertEqual( html_headcheck(STX_WITH_HTML), 0 )
def guessFormat(self, text): """ Simple stab at guessing the inner format of the text """ if html_headcheck(text): return 'html' else: return 'structured-text'
def guessFormat(self, text): """ Simple stab at guessing the inner format of the text """ if html_headcheck(text): return 'text/html' else: return 'text/structured'
def test_html_headcheck(self): self.assertEqual(html_headcheck(FAUX_HTML_LEADING_TEXT), 0) self.assertEqual(html_headcheck(SIMPLE_HTML), 1) self.assertEqual(html_headcheck(SIMPLE_STRUCTUREDTEXT), 0) self.assertEqual(html_headcheck(SIMPLE_XHTML), 1) self.assertEqual(html_headcheck(STX_WITH_HTML), 0)