From e8fca58e7194dec8fd73e73e18c9440cecd4c0be Mon Sep 17 00:00:00 2001 From: Tim Van Baak Date: Fri, 12 Feb 2021 17:06:59 -0800 Subject: [PATCH] Add /new/ endpoint --- redstring/library.py | 32 ++++++++++++++++++++++++++++++-- redstring/server.py | 14 ++++++++++++-- 2 files changed, 42 insertions(+), 4 deletions(-) diff --git a/redstring/library.py b/redstring/library.py index 097916c..d74d325 100644 --- a/redstring/library.py +++ b/redstring/library.py @@ -1,9 +1,9 @@ """ -Logic for operations that depend on a whole collection of documents. +Logic for managing documents. """ import os -from redstring.parser import load, DocumentTag, DocumentTab, Document +from redstring.parser import load, DocumentTag, DocumentTab, Document, TabOptions, TagOptions def generate_index_document(directory: str) -> Document: @@ -69,3 +69,31 @@ def generate_index_document(directory: str) -> Document: built_tabs.append(DocumentTab(category, built_tags)) return Document(built_tabs) + + +def generate_default_document(doc_id) -> Document: + """ + Generate a blank document. + """ + return Document(tabs=[ + DocumentTab( + name='tags', + tags=[ + DocumentTag('id', doc_id), + DocumentTag('title', ''), + DocumentTag('author', ''), + DocumentTag('date', ''), + DocumentTag('source', ''), + DocumentTag('summary', ''), + DocumentTag('category', 'index', TagOptions(private=True)), + DocumentTag('topic', 'uncategorized', TagOptions(private=True)), + ] + ), + DocumentTab( + name='notes', + tags=[ + DocumentTag('notes', ''), + ], + options=TabOptions(private=True, hide_names=True) + ) + ]) diff --git a/redstring/server.py b/redstring/server.py index 0911d70..864aa9a 100644 --- a/redstring/server.py +++ b/redstring/server.py @@ -14,8 +14,8 @@ from flask import ( url_for, ) -from redstring.library import generate_index_document -from redstring.parser import load +from redstring.library import generate_index_document, generate_default_document +from redstring.parser import load, dump CONFIG_ENVVAR = 'REDSTRING_CONFIG' @@ -44,6 +44,16 @@ def document(document_id): return render_template('doc.jinja', document=doc, index=False) +@app.route('/new/') +def new(): + document_id = 'new' + new_doc = generate_default_document(document_id) + doc_path = safe_join(current_app.config['root'], f'{document_id}.json') + with open(doc_path, 'w') as f: + dump(new_doc, f) + return redirect(url_for('document', document_id=document_id)) + + def main(): parser = argparse.ArgumentParser(description="Run the redstring server.") parser.add_argument("--config", help="Config file path.")