Item add improvements

This commit is contained in:
Tim Van Baak 2023-06-19 13:55:40 -07:00
parent 095c6a7a41
commit 701bd0b19f
2 changed files with 60 additions and 26 deletions

View File

@ -332,16 +332,39 @@ def add_item():
) )
source = LocalSource(source_path.parent, source_path.name) source = LocalSource(source_path.parent, source_path.name)
# Clean up the fields fields = {"id": "{:x}".format(getrandbits(16 * 4))}
fields = {key: value for key, value in request.form.items() if value} if form_title := request.form.get("title"):
fields["id"] = "{:x}".format(getrandbits(16 * 4)) fields["title"] = form_title
# TODO: this doesn't support tags or ttX fields correctly if form_link := request.form.get("link"):
fields["link"] = form_link
if form_body := request.form.get("body"):
fields["body"] = form_body
if form_tags := request.form.get("tags"):
fields["tags"] = [
tag.strip()
for tag in form_tags.split()
if tag.strip()
]
if form_tts := request.form.get("tts"):
fields["tts"] = _get_ttx_for_date(datetime.fromisoformat(form_tts))
if form_ttl := request.form.get("ttl"):
fields["ttl"] = _get_ttx_for_date(datetime.fromisoformat(form_ttl))
if form_ttd := request.form.get("ttd"):
fields["ttd"] = _get_ttx_for_date(datetime.fromisoformat(form_ttd))
item = Item.create(source, **fields) item = Item.create(source, **fields)
source.save_item(item) source.save_item(item)
return redirect(url_for("source_feed", name="default")) return redirect(url_for("source_feed", name="default"))
def _get_ttx_for_date(dt: datetime) -> int:
"""Get the relative time difference between now and a date."""
ts = int(dt.timestamp())
now = int(time.time())
return ts - now
def wsgi(): def wsgi():
# init_default_logging() # init_default_logging()
return app return app

View File

@ -29,6 +29,10 @@ summary {
summary:focus { summary:focus {
outline: 1px dotted gray; outline: 1px dotted gray;
} }
.wide {
width: 100%;
resize: vertical;
}
</style> </style>
</head> </head>
<body> <body>
@ -65,28 +69,35 @@ summary:focus {
<details open> <details open>
<summary><span class="item-title">Add item</span></summary> <summary><span class="item-title">Add item</span></summary>
<form action="add" method="post"> <form action="add" method="post">
<label for="title">Title</label> <p>
<input type="text" name="title"> {# <label for="title">Title</label> #}
<br> <input type="text" name="title" class="wide" placeholder="Title">
<label for="body">Body</label> </p>
<br> <p>
<textarea name="body"></textarea> {# <label for="link">Link</label> #}
<br> <input type="url" name="link" class="wide" placeholder="Link">
<label for="link">Link</label> </p>
<input type="text" name="link"> <p>
<br> {# <label for="body">Body</label> #}
<label for="tags">Tags</label> <textarea name="body" class="wide" placeholder="Body"></textarea>
<input type="text" name="tags"> </p>
<br> <p>
<label for="tts">TTS</label> {# <label for="tags">Tags</label> #}
<input type="text" name="tts"> <input type="text" name="tags" class="wide" placeholder="Tags, comma-separated">
<br> </p>
<label for="ttl">TTL</label> <p>
<input type="text" name="ttl"> <label for="tts">TTS:</label>
<br> <input type="datetime-local" name="tts">
<label for="ttd">TTD</label> </p>
<input type="text" name="ttd"> <p>
<br> <label for="ttl">TTL:</label>
<input type="datetime-local" name="ttl">
</p>
<p>
<label for="ttd">TTD:</label>
<input type="datetime-local" name="ttd">
</p>
<p>
<input type="submit" value="Add"> <input type="submit" value="Add">
</form> </form>
</details> </details>