def test_set_id(self): sm = SnippetManager() snippet = sm.getSnippet(self.doc.getId()) newId = "words" snippet.setId(newId) self.assertEqual(newId, snippet.getId())
def test_set_title(self): sm = SnippetManager() snippet = sm.getSnippet(self.doc.getId()) newTitle = "words" snippet.setTitle(newTitle) self.assertEqual(newTitle, snippet.getTitle())
def test_create_dup_snippet(self): sm = SnippetManager() try: out = sm.createSnippetDoc('testDoc') except LookupError: out = "exception" self.assertEqual( out, "exception" )
def test_get_inv_snippet(self): sm = SnippetManager() try: snippet = sm.getSnippet('Junk') except LookupError: snippet = "exception" self.assertEqual(snippet, "exception")
def create(self, data): sm = SnippetManager() #cleanup the title to create a more HTML friendly ID snippetId = re.sub(r'\W', '', data['title']) #TODO: #Include support for different folders from this form. snippet = sm.createSnippet(snippetId, None ,data) return snippet
def getContent(self): try: snippetId = self.request.form['form.widgets.id'] except KeyError: snippetId = self.request.get('snippet-id') if snippetId is None: return False sm = SnippetManager() snippet = sm.getSnippet(snippetId) return snippet
def __call__(self): if self.request.get('snippet-id'): sm = SnippetManager() snippetId = self.request.get('snippet-id') try: sm.deleteSnippet(snippetId) return True except KeyError: try: snippetId = re.sub(r'\W', '', self.request.get('snippet-id')) sm.deleteSnippet(snippetId) except KeyError: return False
class SnippetParser(): #The SnippetParser class handles the finding/replacing of snippets within a pages content. #The page content is passed to a Parser object, and it then tries to pattern match #the <span> tags used to represent snippets. If it finds a valid snippet tag, #it replaces it with the appropriate snippet text. def __init__(self): self.sm = SnippetManager() snippetRegex = '<span(?=.*?data-type="snippet_tag"\s*)(?=.*?data-snippet-id="([a-zA-Z0-9\s_-]+?)"\s*)[^>]+?>[^<>]*?<\/span>' def parsePage(self, pageText): result = self.parseSnippets(pageText) return result def parseSnippets(self, pageText): pattern = re.compile(self.snippetRegex) matches = pattern.finditer(pageText) snippets = self.sm.getSnippets(True) for match in matches: try: pageText = replace(pageText, match.group(0), snippets[match.group(1)].getText()) except KeyError: #The snippetID was invalid pageText = replace(pageText, match.group(0), '') return pageText
def applyChanges(form, data): sm = SnippetManager() snippet = sm.getSnippet(data['id']) changes = {} for item in data: attribute = getattr(snippet, item) if item == 'text': #For whatever reason, tinyMCE loves using the \xc2\xa0 code data[item] = str(data[item]).replace('\xc2\xa0', ' ') if attribute == data[item]: continue else: changes[item] = data[item] sm.updateDoc(data['id'], changes) return changes
def getSnippetList(self, snippetList): sm = SnippetManager() out = [] for snippet in snippetList: try: item = sm.getSnippet(snippet) except KeyError: #Getting here means the request snippetID doesn't exist #Setting the items "dead" tells the AJAX handler to remove the snippet tag, #if one exsists item = { 'id': snippet, 'dead': True } out.append( item ) return self.getSnippetAsJSON( out )
def handleAdd(self, action): data, errors = self.extractData() sm = SnippetManager() index = sm.indexSnippets() security = getSecurityManager() if not security.checkPermission(ModifyPortalContent, sm.folder): raise Unauthorized(u"You do not have the proper permissions to create/edit snippets.") if 'title' in data: if data['title'] in index: raise ActionExecutionError(Invalid(u"This title is already in use.")) if errors: self.status = self.formErrorsMessage return obj = self.createAndAdd(data) if obj is not None: # mark only as finished if we get the new object self._finishedAdd = True IStatusMessage(self.request).addStatusMessage(_(u"Snippet saved"), "info")
def getSnippets(self): sm = SnippetManager() snippets = sm.getSnippets() out = [] for snippet in snippets: security = getSecurityManager() doc = sm.folder[snippet.getId()] if security.checkPermission(ModifyPortalContent, doc): snippet.w = True else: snippet.w = False if security.checkPermission(DeleteObjects, doc): snippet.d = True else: snippet.d = False out.append(snippet) return out
def test_set_text(self): sm = SnippetManager() snippet = sm.getSnippet(self.doc.getId()) newText = " words words words" self.assertEqual(self.doc.getRawText(), snippet.getText())
def test_create_snippet(self): sm = SnippetManager() sm.createSnippetDoc('new') self.assertTrue('new' in self.folder)
def test_delete_snippet(self): sm = SnippetManager() sm.deleteSnippet('testDoc') self.assertFalse('testDoc' in self.folder)
def test_get_text(self): sm = SnippetManager() snippet = sm.getSnippet(self.doc.getId()) self.assertEqual(self.doc.getRawText(), snippet.getText())
def test_get_title(self): sm = SnippetManager() snippet = sm.getSnippet(self.doc.getId()) self.assertEqual(self.doc.Title(), snippet.getTitle())
def test_get_snippet(self): sm = SnippetManager() snippet = sm.getSnippet('testDoc') self.assertTrue(snippet.getId() == 'testDoc')
def test_get_snippets(self): sm = SnippetManager() snippets = sm.getSnippets() self.assertTrue(len(snippets) == 2)
def __init__(self): self.sm = SnippetManager()