def page(notebook, entryid): nbdb = g.dbconn[get_nb_dbname(notebook)] # fixme in denormalized land, we don't need to do the double-query col_entries = nbdb['entries'] col_revisions = nbdb['revisions'] doc = col_entries.find_one({"_id" : bson.objectid.ObjectId(entryid)}) if not doc: return "COULD NOT FIND THAT DOCUMENT" head = doc['head'] rev = nbdb.dereference(head) s = dm.page_rev_to_json(rev) return render_template("page.html", notebook = get_notebook(notebook), page_entry_json = json.dumps(dm.entry_to_json(doc)), page_rev_json = json.dumps(s), page_rev = s, session = session, version = app.config['VERSION_GIT_DESCRIBE'])
def page(entryid): # fixme in denormalized land, we don't need to do the double-query col_entries = g.db['entries'] col_revisions = g.db['revisions'] doc = col_entries.find_one({"_id" : bson.objectid.ObjectId(entryid)}) if not doc: return "COULD NOT FIND THAT DOCUMENT" head = doc['head'] rev = g.db.dereference(head) s = dm.page_rev_to_json(rev) return render_template("page.html", page_entry_json = json.dumps(dm.entry_to_json(doc)), page_rev_json = json.dumps(s), page_rev = s, session = session)
def api_entry_get_post(notebook, entryid): """ GET the latest entry and rev for an entry POST an updated rev. Note that we extract out the relevant fields from the submitted doc, and overwrite other relevant ones. Things that must be present include: class parent: Parent points to the old entry; this is also what we think the entry currently points to doc-specific entries we rewrite : author (current logged in author) date _id : with the new updated revid and update the entry If the entry is out of date, we reject, and instead return the latest entry/rev, with error 409 """ nbdb = g.dbconn[get_nb_dbname(notebook)] if request.method == 'POST': """ Perform an update, assuming that the doc is pretty well-formed """ if request.mimetype != "application/json": return "Invalid request type, must be application/json", HTTP_ERROR_CLIENT_BADREQUEST rd = request.json dclass = rd['class'] parent = rd['parent'] if dclass == 'text': rev = dm.text_entry_revision_create(rd['title'], rd['body']) elif dclass == 'figure': rev = dm.figure_entry_revision_create(rd['title'], rd['caption'], rd.get('maxsize', None), gallery = rd.get("gallery", False), images = rd['images']) elif dclass == 'markdown' : rev = dm.markdown_entry_revision_create(rd['title'], rd['body']) elif dclass == 'page': rev = dm.page_entry_revision_create(rd['title'], rd['entries']) else: raise Exception("Unknown entry class '%s'" % dclass) author = dbref("users", session["user_id"]) tags = rd.get("tags", []) pref = dbref("revisions", parent) rev.update(dm.revision_create(session["user_id"], app.config['DB_SYSTEM_DATABASE'], parent=pref, archived=rd.get('archived', False), tags = tags)) # save the revision new_rev_oid = nbdb.revisions.insert(rev, safe=True) rev["_id"] = new_rev_oid new_entry_doc = dm.entry_create(dbref('revisions', new_rev_oid), dclass, rev) res = nbdb.entries.update({'_id' : bson.objectid.ObjectId(entryid), 'head' : dbref('revisions', parent), 'class' : dclass}, new_entry_doc, safe=True) new_entry_doc["_id"] = bson.objectid.ObjectId(entryid) if res['updatedExisting'] == True: # success! # get the old revisions tags, compute the diff olddoc = nbdb.dereference(dbref('revisions', parent)) tagdeltas = tagutils.tagdelta(olddoc.get('tags', []), tags) [tagutils.inc_tag(nbdb, t) for t in tagdeltas[0]] [tagutils.dec_tag(nbdb, t) for t in tagdeltas[1]] new_rev_doc_json = dm.rev_to_json[dclass](rev) entry_doc_json = dm.entry_to_json(new_entry_doc) return jsonify({"latest_entry_doc" : entry_doc_json, "latest_revision_doc" : new_rev_doc_json}) else: # failed to update, meaning someone else updated the entry ahead of us nbdb.revisions.remove({'_id' : new_rev_oid}) entry_ref = dbref("entries", entryid) latest_entry_doc = nbdb.dereference(entry_ref) true_latest_rev_ref = latest_entry_doc['head'] latest_rev_doc = nbdb.dereference(true_latest_rev_ref) latest_rev_json = dm.page_rev_to_json(latest_rev_doc) entry_doc_json = dm.entry_to_json(latest_entry_doc) return jsonify_error({"reason" : "Incorrect latest", "latest_entry_doc": entry_doc_json, "latest_revision_doc" : latest_rev_json}, HTTP_ERROR_CLIENT_CONFLICT) elif request.method == "GET": # fixme update this to use denormalized stuff entry_ref = dbref("entries", entryid) entry_doc = nbdb.dereference(entry_ref) latest_page_ref = entry_doc['head'] latest_page_rev = nbdb.dereference(latest_page_ref) return jsonify({"entry" : dm.entry_to_json(entry_doc), "revision" : dm.rev_to_json[entry_doc['class']](latest_page_rev)})
def api_entry_get_post(entryid): """ GET the latest entry and rev for an entry POST an updated rev. Note that we extract out the relevant fields from the submitted doc, and overwrite other relevant ones. Things that must be present include: class parent: Parent points to the old entry; this is also what we think the entry currently points to doc-specific entries we rewrite : author (current logged in author) date _id : with the new updated revid and update the entry If the entry is out of date, we reject, and instead return the latest entry/rev """ if request.method == 'POST': """ Perform an update, assuming that the doc is pretty well-formed """ if request.mimetype != "application/json": return "Invalid request type, must be application/json", 400 rd = request.json dclass = rd['class'] parent = rd['parent'] if dclass == 'text': rev = dm.text_entry_revision_create(rd['title'], rd['body']) elif dclass == 'page': rev = dm.page_entry_revision_create(rd['title'], rd['entries']) else: raise Exception("Unknown entry") author = dbref("users", session["user_id"]) pref = dbref("revisions", parent) rev.update(dm.revision_create(author, parent=pref, archived=rd.get('archived', False))) # save the revision new_rev_oid = g.db.revisions.insert(rev, safe=True) rev["_id"] = new_rev_oid new_entry_doc = dm.entry_create(dbref('revisions', new_rev_oid), dclass, rev) res = g.db.entries.update({'_id' : bson.objectid.ObjectId(entryid), 'head' : dbref('revisions', parent), 'class' : dclass}, new_entry_doc, safe=True) new_entry_doc["_id"] = bson.objectid.ObjectId(entryid) if res['updatedExisting'] == True: # success! new_rev_doc_json = dm.rev_to_json[dclass](rev) entry_doc_json = dm.entry_to_json(new_entry_doc) return jsonify({"latest_entry_doc" : entry_doc_json, "latest_revision_doc" : new_rev_doc_json}) else: # failed to update, meaning someone else updated the entry ahead of us g.db.revisions.remove({'_id' : new_rev_oid}) entry_ref = dbref("entries", entryid) latest_entry_doc = g.db.dereference(entry_ref) true_latest_rev_ref = latest_entry_doc['head'] latest_rev_doc = g.db.dereference(true_latest_rev_ref) latest_rev_json = dm.page_rev_to_json(latest_rev_doc) resp = jsonify({"reason" : "Incorrect latest", "latest_revision_doc" : latest_rev_json}) resp.status = "400" return resp elif request.method == "GET": # fixme update this to use denormalized stuff entry_ref = dbref("entries", entryid) entry_doc = g.db.dereference(entry_ref) latest_page_ref = entry_doc['head'] latest_page_rev = g.db.dereference(latest_page_ref) return jsonify({"entry" : dm.entry_to_json(entry_doc), "revision" : dm.rev_to_json[entry_doc['class']](latest_page_rev)})