Merge pull request #83 from doordash/delete-page

Implement delete page
This commit is contained in:
Matthew Scragg 2015-09-30 14:27:28 -05:00
commit 5022276863
8 changed files with 74 additions and 11 deletions

View file

@ -2,7 +2,6 @@ from realms.modules.wiki.models import Wiki
from realms import search from realms import search
@Wiki.after('write_page') @Wiki.after('write_page')
def wiki_write_page(name, content, message=None, username=None, email=None, **kwargs): def wiki_write_page(name, content, message=None, username=None, email=None, **kwargs):
@ -19,5 +18,18 @@ def wiki_write_page(name, content, message=None, username=None, email=None, **kw
@Wiki.after('rename_page') @Wiki.after('rename_page')
def wiki_rename_page(*args, **kwargs): def wiki_rename_page(old_name, *args, **kwargs):
pass
if not hasattr(search, 'index_wiki'):
return
return search.delete_wiki(old_name)
@Wiki.after('delete_page')
def wiki_delete_page(name, *args, **kwargs):
if not hasattr(search, 'index_wiki'):
return
return search.delete_wiki(name)

View file

@ -92,9 +92,19 @@ class WhooshSearch(BaseSearch):
writer.update_document(path=id_.decode("utf-8"), body=body["content"].decode("utf-8")) writer.update_document(path=id_.decode("utf-8"), body=body["content"].decode("utf-8"))
writer.commit() writer.commit()
def delete(self, id_):
with self.search_index.searcher() as s:
doc_num = s.document_number(path=id_.decode("utf-8"))
writer = self.search_index.writer()
writer.delete_document(doc_num)
writer.commit()
def index_wiki(self, name, body): def index_wiki(self, name, body):
self.index('wiki', 'page', id_=name, body=body) self.index('wiki', 'page', id_=name, body=body)
def delete_wiki(self, name):
self.delete(id_=name)
def delete_index(self, index): def delete_index(self, index):
from whoosh import index as whoosh_index from whoosh import index as whoosh_index
self.search_index.close() self.search_index.close()
@ -133,9 +143,15 @@ class ElasticSearch(BaseSearch):
def index(self, index, doc_type, id_=None, body=None): def index(self, index, doc_type, id_=None, body=None):
return self.elastic.index(index=index, doc_type=doc_type, id=id_, body=body) return self.elastic.index(index=index, doc_type=doc_type, id=id_, body=body)
def delete(self, index, doc_type, id_):
return self.elastic.delete(index=index, doc_type=doc_type, id=id_)
def index_wiki(self, name, body): def index_wiki(self, name, body):
self.index('wiki', 'page', id_=name, body=body) self.index('wiki', 'page', id_=name, body=body)
def delete_wiki(self, name):
self.delete('wiki', 'page', id_=name)
def delete_index(self, index): def delete_index(self, index):
return self.elastic.indices.delete(index=index, ignore=[400, 404]) return self.elastic.indices.delete(index=index, ignore=[400, 404])

View file

@ -156,11 +156,14 @@ class Wiki(HookMixin):
message = "Deleted %s" % name message = "Deleted %s" % name
filename = cname_to_filename(name) filename = cname_to_filename(name)
# gittle.rm won't actually remove the file, have to do it ourselves
os.remove(os.path.join(self.path, filename))
self.gittle.rm(filename) self.gittle.rm(filename)
commit = self.gittle.commit(name=username, commit = self.gittle.commit(name=username,
email=email, email=email,
message=message, message=message,
files=[str(filename)]) files=[filename])
cache.delete_many(name) cache.delete_many(name)
return commit return commit

View file

@ -52,6 +52,11 @@ class WikiTest(WikiBaseTest):
self.assert_403(self.client.delete(url_for('wiki.page_write', name='test'))) self.assert_403(self.client.delete(url_for('wiki.page_write', name='test')))
self.app.config['WIKI_LOCKED_PAGES'] = [] self.app.config['WIKI_LOCKED_PAGES'] = []
# Create page, check it exists
self.create_page('test', message='test message', content='testing')
self.assert_200(self.client.get(url_for('wiki.page', name='test')))
# Delete page
self.assert_200(self.client.delete(url_for('wiki.page_write', name='test'))) self.assert_200(self.client.delete(url_for('wiki.page_write', name='test')))
rv = self.client.get(url_for('wiki.page', name='test')) rv = self.client.get(url_for('wiki.page', name='test'))

View file

@ -151,12 +151,12 @@ def page_write(name):
return dict(sha=sha) return dict(sha=sha)
else: elif request.method == 'DELETE':
# DELETE # DELETE
if cname in current_app.config.get('WIKI_LOCKED_PAGES'): if cname in current_app.config.get('WIKI_LOCKED_PAGES'):
return dict(error=True, message="Page is locked"), 403 return dict(error=True, message="Page is locked"), 403
sha = g.current_wiki.delete_page(name, sha = g.current_wiki.delete_page(cname,
username=current_user.username, username=current_user.username,
email=current_user.email) email=current_user.email)

View file

@ -58,10 +58,31 @@ $(function(){
}); });
$("#delete-page-btn").click(function() { $("#delete-page-btn").click(function() {
bootbox.alert("Not Done Yet! Sorry"); bootbox.confirm('Are you sure you want to delete this page?', function(result) {
if (result) {
deletePage();
}
});
}); });
}); });
var deletePage = function() {
var pageName = $page_name.val();
var path = Config['RELATIVE_PATH'] + '/' + pageName;
$.ajax({
type: 'DELETE',
url: path,
}).done(function(data) {
var msg = 'Deleted page: ' + pageName;
bootbox.alert(msg, function() {
location.href = '/';
});
}).fail(function(data, status, error) {
bootbox.alert('Error deleting page!');
});
};
var aced = new Aced({ var aced = new Aced({
editor: $('#entry-markdown-content').find('.editor').attr('id'), editor: $('#entry-markdown-content').find('.editor').attr('id'),
renderer: function(md) { return MDR.convert(md) }, renderer: function(md) { return MDR.convert(md) },
@ -73,7 +94,11 @@ var aced = new Aced({
content: content content: content
}; };
var path = Config['RELATIVE_PATH'] + '/' + data['name']; // If renaming an existing page, use the old page name for the URL to PUT to
var subPath = (PAGE_NAME) ? PAGE_NAME : data['name']
var path = Config['RELATIVE_PATH'] + '/' + subPath;
var newPath = Config['RELATIVE_PATH'] + '/' + data['name'];
var type = (Commit.info['sha']) ? "PUT" : "POST"; var type = (Commit.info['sha']) ? "PUT" : "POST";
$.ajax({ $.ajax({
@ -87,7 +112,7 @@ var aced = new Aced({
$page_name.addClass('parsley-error'); $page_name.addClass('parsley-error');
bootbox.alert("<h3>" + res['message'] + "</h3>"); bootbox.alert("<h3>" + res['message'] + "</h3>");
} else { } else {
location.href = path; location.href = newPath;
} }
}); });
} }

View file

@ -3,6 +3,8 @@
<script> <script>
var Commit = {}; var Commit = {};
Commit.info = {{ info|tojson }}; Commit.info = {{ info|tojson }};
var PAGE_NAME = '{{ name }}';
</script> </script>
<script src="{{ url_for('static', filename='js/editor.js') }}"></script> <script src="{{ url_for('static', filename='js/editor.js') }}"></script>

View file

@ -36,7 +36,7 @@ setup(name='realms-wiki',
'click==3.3', 'click==3.3',
'gevent==1.0.2', 'gevent==1.0.2',
'ghdiff==0.4', 'ghdiff==0.4',
'gittle==0.4.0', 'gittle==0.5.0',
'gunicorn==19.3', 'gunicorn==19.3',
'itsdangerous==0.24', 'itsdangerous==0.24',
'markdown2==2.3.0', 'markdown2==2.3.0',