Add /new/ endpoint

This commit is contained in:
Tim Van Baak 2021-02-12 17:06:59 -08:00
parent c263d8b469
commit e8fca58e71
2 changed files with 42 additions and 4 deletions

View File

@ -1,9 +1,9 @@
""" """
Logic for operations that depend on a whole collection of documents. Logic for managing documents.
""" """
import os 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: 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)) built_tabs.append(DocumentTab(category, built_tags))
return Document(built_tabs) 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)
)
])

View File

@ -14,8 +14,8 @@ from flask import (
url_for, url_for,
) )
from redstring.library import generate_index_document from redstring.library import generate_index_document, generate_default_document
from redstring.parser import load from redstring.parser import load, dump
CONFIG_ENVVAR = 'REDSTRING_CONFIG' CONFIG_ENVVAR = 'REDSTRING_CONFIG'
@ -44,6 +44,16 @@ def document(document_id):
return render_template('doc.jinja', document=doc, index=False) 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(): def main():
parser = argparse.ArgumentParser(description="Run the redstring server.") parser = argparse.ArgumentParser(description="Run the redstring server.")
parser.add_argument("--config", help="Config file path.") parser.add_argument("--config", help="Config file path.")