def main(): import connection labels = Label.objects(name='_testing') if labels.count(): label = labels.first() else: label = labels.upsert_one(name='_testing') post1 = Page(title="Foo", content="<div>Foo bar</div>", url='https://foo.bar.com', label=label) print(repr(post1)) post1.save() print() post2 = Page(title="Bar", content="<div>Fizz</div>", url='https://fub.buzz.com', label=label) print(repr(post2)) post2.save() print() posts = Page.objects(label=label) print(posts.count()) print(posts) print() print("Deleting") post1.delete() post2.delete()
def handle_page_delete(page): # get form values cancel = request.form.has_key('cancel') if cancel: # redirect to view page return redirect_to_user_page(page.acct.uid, page.slug) # delete page uid = page.acct.uid private = page.private Page.delete(page) # redirect to home or private home, depending on visibility of deleted page target_slug = '__private' if private else '__home' return redirect_to_user_page(uid, target_slug)
def save(user_slug, wiki_slug, page_slug): """ Saves page changes in the database. -] patch - array of changed content blocks; 'undefined' if unchanged <- slugified page title """ user = User.get_by(name_slug=user_slug) if user != g.user: return False wiki = user.wiki_by_slug(wiki_slug) try: page = wiki.page_by_slug(page_slug) except NoResultFound: old_last_page = Page.get_by(wiki=wiki, next_page_id=-1) page = Page(wiki=wiki, title="", title_slug="", content="<h1>Untitled</h1>", next_page_id=-1) models.session.commit() old_last_page.next_page_id = page.id patch = request.form.getlist('patch') block = re.compile('<(?:h1|h2|h3|p)>.*?</(?:h1|h2|h3|p)>') content_blocks = block.findall(page.content) # If the page's title has been changed in the content... new_title = page.title if len(patch) and patch[0] != 'undefined': new_title = patch[0][4:-5] newline = re.compile('\n') new_title = re.sub(newline, "", new_title) if page.title != new_title: # ..if a page exists with this title, append " (alternative)" to it. try: same_title_page = Page.get_by(title=new_title) except NoResultFound: pass if same_title_page: stp = same_title_page header_end = 4 + len(stp.title) stp.title += " (alternative)" stp.title_slug = slugify(stp.title) stp.content = stp.content[:header_end] + " (alternative)" + \ stp.content[header_end:] # ..if the page with changed title was the main page, make a new one. if page.title == wiki.title: try: if new_title: second_page = page else: second_page = Page.get_by(id=page.next_page_id) next_page_id = second_page.id except NoResultFound: next_page_id = -1 new_main_page = Page(wiki=wiki, title=wiki.title, title_slug=slugify(wiki.title), content="<h1>%s</h1><p></p>" % wiki.title, next_page_id=next_page_id) models.session.commit() wiki.first_page_id = new_main_page.id # ..change the page's title in the database. page.title = new_title page.title_slug = slugify(new_title) # Replace content with patched content. for i, block in enumerate(patch): if block != 'undefined': if i < len(content_blocks): content_blocks[i] = block else: content_blocks.append(block) # Sanitize unsafe angle brackets. content = ''.join(content_blocks) unsafe_lt = re.compile(r"<(?!/?(h1|h2|h3|p|b|i|u)>)") content = re.sub(unsafe_lt, '<', content) # The content is reversed to get around regex lookbehind limitation. unsafe_gt = re.compile(r">(?!(1h|2h|3h|p|b|i|u)/?<)") content = re.sub(unsafe_gt, ';tg&', content[::-1]) page.content = content[::-1] # If the title is blank, delete the page. deleted = False blank_title = re.compile('<h1>(<br>)*</h1>') if blank_title.match(page.content): try: previous_page = Page.get_by(wiki=wiki, next_page_id=page.id) previous_page.next_page_id = page.next_page_id except NoResultFound: pass page.delete() deleted = True # Update the wiki's update date. wiki.update_date = datetime.now() # Commit to database! models.session.commit() if not deleted: return slugify(page.title) else: return "untitled!"
def delete(self): route = self.request.matchdict['route'] Page.delete(route) return HTTPFound(location=self.request.route_url('home'))
def test_delete(self): Page.delete(self.page) page = self.acct.get_page_by_title('Book List') self.assertIsNone(page) self.assertEqual(2, len(self.acct.pages))