Skip to content
This repository has been archived by the owner on Apr 23, 2018. It is now read-only.

Commit

Permalink
Restore a global update_content but not really global
Browse files Browse the repository at this point in the history
  • Loading branch information
paradoxxxzero committed Jun 17, 2013
1 parent aee7435 commit 02310a9
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 38 deletions.
4 changes: 4 additions & 0 deletions pynuts/__init__.py
Expand Up @@ -44,6 +44,10 @@ def __init__(self, app, authed_url_for=False):
# at the /_pynuts/static/<path:filename> URL
self.app.add_url_rule('/_pynuts/static/<path:filename>',
'_pynuts-static', static)
self.app.add_url_rule(
'/_pynuts/update_content', '_pynuts-update_content',
lambda: document.update_content(self),
methods=('POST',))

class Document(document.Document):
"""Document base class of the application."""
Expand Down
77 changes: 41 additions & 36 deletions pynuts/document.py
Expand Up @@ -38,6 +38,7 @@ def __init__(cls, name, bases, dict_):
'/_pynuts/resource/%s/update_content' % cls.type_name,
'_pynuts_resource_%s_update_content' % cls.type_name,
cls.update_content, methods=('POST',))

if cls.model_path and not os.path.isabs(cls.model_path):
cls.model_path = os.path.join(
cls._app.root_path, cls.model_path)
Expand Down Expand Up @@ -325,42 +326,7 @@ def archive(cls, part='index.rst.jinja2', version=None,

@classmethod
def update_content(cls):
"""Update the ReST document.
It is used by the javascript/AJAX save function.
It gets the request as JSON and update all the parts of the document
return document's information as JSON.
'See the save function<>_' for more details.
"""

contents = request.json['data']
author_name = request.json['author']
author_email = request.json['author_email']
message = request.json['message']

documents = {}
for values in contents:
key = (values['document_type'], values['document_id'])
if key in documents:
document = documents[key]
else:
cls = cls._pynuts.documents[values['document_type']]
document = cls(values['document_id'], values['version'])
documents[key] = document
document.git.write(values['part'],
values['content'].encode('utf-8'))
for document in documents.values():
document.git.commit(
author_name or 'Pynuts',
author_email or 'pynut@pynuts.org',
message or 'Edit %s' % document.document_id)
return jsonify(documents=[{
'document_type': document.type_name,
'document_id': document.document_id,
'version': document.version}
for document in documents.values()])
return update_content(cls._pynuts)

@classmethod
def create(cls, author_name=None, author_email=None, message=None,
Expand Down Expand Up @@ -533,3 +499,42 @@ def write(self, content, author_name=None, author_email=None,
raise ConflictError
else:
return True


def update_content(pynuts):
"""Update the ReST document.
It is used by the javascript/AJAX save function.
It gets the request as JSON and update all the parts of the document
return document's information as JSON.
'See the save function<>_' for more details.
"""

contents = request.json['data']
author_name = request.json['author']
author_email = request.json['author_email']
message = request.json['message']

documents = {}
for values in contents:
key = (values['document_type'], values['document_id'])
if key in documents:
document = documents[key]
else:
cls = pynuts.documents[values['document_type']]
document = cls(values['document_id'], values['version'])
documents[key] = document
document.git.write(values['part'],
values['content'].encode('utf-8'))
for document in documents.values():
document.git.commit(
author_name or 'Pynuts',
author_email or 'pynut@pynuts.org',
message or 'Edit %s' % document.document_id)
return jsonify(documents=[{
'document_type': document.type_name,
'document_id': document.document_id,
'version': document.version}
for document in documents.values()])
4 changes: 2 additions & 2 deletions pynuts/static/javascript/save.js
Expand Up @@ -27,7 +27,7 @@ function save_content (options) {
var options = options || {};
//Params : document, message, author, author_email, ok_callback, error_callback, empty_callback
options.document = options.document ? options.document : $(document);

// Get all the contenteditable elements
var divs = $(options.document).contents().find('div[contenteditable]');
var span_containers = $(options.document).contents().find('div[data-content=true]');
Expand All @@ -36,7 +36,7 @@ function save_content (options) {
alert('There is no contenteditable on this document.');
return false;
}

var data = [];
if (divs.length != 0) {
$.each(divs, function () {
Expand Down

0 comments on commit 02310a9

Please sign in to comment.