def edit_page(request, slug): try: page = Page.objects.get(title_slug=slug) except Page.DoesNotExist: page = Page() page.title = slug page.title_slug = slug if update_post_with_request(page, request, update_user=True, force_slug=False): return HttpResponseRedirect(page.get_absolute_url()) else: return render_req_to_resp(request, 'post_edit.html', {'post': page})
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!"