def test_set_part(): """Confirms that the index is updated when a part is renamed.""" test_parts = { 'index': trim(""" Index - Part One - Part Two """), 'part-one': trim(""" Part One Text. """), 'part-two': trim(""" Part Two Text. """) } doc = Document(data) doc.set_parts('user-slug', 'doc-slug', test_parts) old_slug = 'part-one' new_text = trim(""" Part Three Text. """) new_part_slug = doc.set_part(old_slug, new_text) assert new_part_slug == "part-three" assert doc.parts == { 'index': trim(""" Index - Part Three - Part Two """), 'part-three': trim(""" Part Three Text. """), 'part-two': trim(""" Part Two Text. """) }
def post_edit_part(user_slug, doc_slug, part_slug): """ Wiki editor for existing doc part (or '_' if new). """ if user_slug == '_': # New user msg = "Blank user '_' not supported." bottle.abort(HTTP_BAD_REQUEST, msg) if doc_slug == '_' and part_slug == '_': msg = "Blank document and part '_/_' not supported." bottle.abort(HTTP_BAD_REQUEST, msg) if 'they_selected_save' in bottle.request.forms: require_authority_for_user(user_slug) if 'content' not in bottle.request.forms: bottle.abort(HTTP_BAD_REQUEST, "Form data was missing.") new_text = clean_text(bottle.request.forms.content) # Default slugs unless we change them new_part_slug = part_slug new_doc_slug, old_doc_slug = doc_slug, doc_slug document = Document(data) host = domain_name(bottle.request) document.set_host(host) if doc_slug == "_": # New article... new_doc_slug = document.set_index(new_text) new_doc_slug = data.userDocument_unique_slug(user_slug, new_doc_slug) document.set_slugs(user_slug, new_doc_slug) elif document.load(user_slug, doc_slug): if part_slug == 'index': new_doc_slug = document.set_index(new_text) if new_doc_slug != doc_slug: unique_slug = data.userDocument_unique_slug( user_slug, new_doc_slug) document.set_slugs(user_slug, unique_slug) else: new_part_slug = document.set_part(part_slug, new_text) else: msg = "Document '{:s}/{:s}' not found." bottle.abort(HTTP_NOT_FOUND, msg.format(user_slug, doc_slug)) okay_to_save = all([ 'they_selected_save' in bottle.request.forms, has_authority_for_user(user_slug) ]) if okay_to_save: new_doc_slug = document.save(pregenerate=True) old_doc = Document(data) if old_doc.load(user_slug, old_doc_slug): if old_doc.doc_slug != new_doc_slug: old_doc.delete() # Special redirects when editing fixtures uri = '/read/{:s}/{:s}'.format(user_slug, new_doc_slug) if doc_slug == 'fixtures': if part_slug == 'homepage': uri = '/' elif part_slug == 'author': uri = '/user/{:s}'.format(user_slug) bottle.redirect(uri) is_preview = 'they_selected_preview' in bottle.request.forms return show_editor(new_text, document.user_slug, document.doc_slug, new_part_slug, is_preview, can_be_saved=has_authority_for_user(user_slug))