Fix importing of bookmarks from TOML.

This commit is contained in:
Schimon Jehudah, Adv. 2024-08-23 03:56:10 +03:00
parent 3024f57466
commit 6934b77bd8
2 changed files with 132 additions and 111 deletions

View file

@ -1444,17 +1444,17 @@ class HttpInstance:
override: str = Form(None)):
jabber_id = Utilities.is_jid_matches_to_session(accounts, sessions, request)
if jabber_id:
xmpp_instance = accounts[jabber_id]
if file:
# TODO If node does not exist, redirect to result page with
# a message that bookmarks are empty.
# NOTE No.
# TODO match/case public, private, read
node_id = node[node]['name']
node_title = node[node]['title']
node_subtitle = node[node]['subtitle']
node_access_model = node[node]['access_model']
node_id = nodes[node]['name']
node_title = nodes[node]['title']
node_subtitle = nodes[node]['subtitle']
node_access_model = nodes[node]['access_model']
if not await XmppPubsub.is_node_exist(xmpp_instance, node_id):
iq = XmppPubsub.create_node_atom(
xmpp_instance, jabber_id, node_id, node_title,
@ -1463,33 +1463,45 @@ class HttpInstance:
#return {"filename": file.filename}
content = file.file.read().decode()
# TODO Add match/case for filetype.
entries = tomllib.loads(content)
# entries_node = entries[node]
#breakpoint()
#for entry in entries: print(entry)
name = jabber_id.split('@')[0]
# timestamp = datetime.now().isoformat()
db_file = 'main.sqlite'
for entry in entries:
url_hash = item_id = Utilities.hash_url_to_md5(entry['link'])
instances = SQLite.get_entry_instances_by_url_hash(db_file, url_hash)
entry = {'title' : entry['title'],
'link' : entry['link'],
'summary' : entry['summary'],
'published' : entry['published'],
'updated' : entry['published'],
#'updated' : entry['updated'],
'tags' : entry['tags'],
'url_hash' : url_hash,
'jid' : jabber_id,
'name' : name,
'instances' : instances}
#message = 'Discover new links and see who shares them'
xmpp_instance = accounts[jabber_id]
payload = Syndication.create_rfc4287_entry(entry)
iq = await XmppPubsub.publish_node_item(
xmpp_instance, jabber_id, node_id, item_id, payload)
#await iq.send(timeout=15)
message = 'Blasta system message » Imported {} items.'.format(len(entries))
counter = 0
for entry_type in entries:
for entry in entries[entry_type]:
url_hash = item_id = Utilities.hash_url_to_md5(entry['link'])
instances = SQLite.get_entry_instances_by_url_hash(db_file, url_hash)
entry_new = {
'title' : entry['title'],
'link' : entry['link'],
'summary' : entry['summary'],
'published' : entry['published'],
'updated' : entry['published'],
#'updated' : entry['updated'],
'tags' : entry['tags'],
'url_hash' : url_hash,
'jid' : jabber_id,
'name' : name,
'instances' : instances}
#message = 'Discover new links and see who shares them'
xmpp_instance = accounts[jabber_id]
payload = Syndication.create_rfc4287_entry(entry_new)
iq = await XmppPubsub.publish_node_item(
xmpp_instance, jabber_id, node_id, item_id, payload)
#await iq.send(timeout=15)
counter += 1
message = 'Blasta system message » Imported {} items.'.format(counter)
description = 'Import successful'
path = 'profile'
return result_post(request, jabber_id, description, message, path)

View file

@ -117,10 +117,10 @@
</dt>
<dd>
<a download="{{jabber_id}}_private.json"
href="/profile/export/private/json">
href="/profile/export/private/json">
JSON</a>,
<a download="{{jabber_id}}_private.toml"
href="/profile/export/private/toml">
href="/profile/export/private/toml">
TOML</a>.
</dd>
<dt>
@ -128,10 +128,10 @@
</dt>
<dd>
<a download="{{jabber_id}}_public.json"
href="/profile/export/public/json">
href="/profile/export/public/json">
JSON</a>,
<a download="{{jabber_id}}_public.toml"
href="/profile/export/public/toml">
href="/profile/export/public/toml">
TOML</a>.
</dd>
<dt>
@ -139,10 +139,10 @@
</dt>
<dd>
<a download="{{jabber_id}}_read.json"
href="/profile/export/read/json">
href="/profile/export/read/json">
JSON</a>,
<a download="{{jabber_id}}_read.toml"
href="/profile/export/read/toml">
href="/profile/export/read/toml">
TOML</a>.
</dd>
</dl>
@ -153,9 +153,9 @@
your bookmarks to.
</p>
<form action="/profile/import"
enctype="multipart/form-data"
id="import"
method="post">
enctype="multipart/form-data"
id="import"
method="post">
<table>
<tr>
<td>
@ -165,10 +165,10 @@
</td>
<td>
<input id="file"
name="file"
placeholder="Select a file to import."
required=""
type="file" />
name="file"
placeholder="Select a file to import."
required=""
type="file" />
</td>
</tr>
<tr>
@ -179,20 +179,20 @@
</td>
<td>
<input id="node-private"
name="node"
required=""
type="radio"
value="private"/>
name="node"
required=""
type="radio"
value="private"/>
<label for="node-private">Private</label>
<input id="node-public"
name="node"
type="radio"
value="public"/>
name="node"
type="radio"
value="public"/>
<label for="node-public">Public</label>
<input id="node-read"
name="node"
type="radio"
value="read"/>
name="node"
type="radio"
value="read"/>
<label for="node-read">Read</label>
</td>
</tr>
@ -204,19 +204,20 @@
</td>
<td>
<input id="merge"
disabled=""
name="merge"
type="checkbox"/>
disabled=""
name="merge"
type="checkbox"/>
<label for="merge"
title="Merge tags.">
title="Merge tags.">
Merge
</label>
<input id="override"
disabled=""
name="override"
type="checkbox"/>
checked=""
disabled=""
name="override"
type="checkbox"/>
<label for="override"
title="Replace properties by the
title="Replace properties by the
properties of the imported entries.">
Override
</label>
@ -229,10 +230,12 @@ merge tags.">
</tr>
</table>
<input class="submit"
type="submit"
value="Import Bookmarks"/>
type="submit"
value="Import Bookmarks"/>
</form>
<h4 id="permissions">Permissions</h4>
<h4 id="permissions">
Permissions
</h4>
<p>
Choose the desired
<a href="https://xmpp.org/extensions/xep-0060.html#accessmodels">
@ -250,25 +253,24 @@ merge tags.">
</td>
<td>
<input id="private-authorize"
{% if access_models['private'] == 'authorize' %}checked=""{% endif %}
disabled=""
name="private"
type="radio"/>
{% if access_models['private'] == 'authorize' %}checked=""{% endif %}
disabled=""
name="private"
type="radio"/>
<label for="private-authorize"
title="The node owner must approve
all subscription requests, and only subscribers may retrieve items from the
node.">
title="The node owner must approve all
subscription requests, and only subscribers may retrieve items from the node.">
Authorize
</label>
</td>
<td>
<input id="private-whitelist"
{% if access_models['private'] == 'whitelist' %}checked=""{% endif %}
disabled=""
name="private"
type="radio"/>
{% if access_models['private'] == 'whitelist' %}checked=""{% endif %}
disabled=""
name="private"
type="radio"/>
<label for="private-whitelist"
title="An entity may subscribe or
title="An entity may subscribe or
retrieve items only if on a whitelist managed by the node owner.">
Whitelist
</label>
@ -282,14 +284,14 @@ retrieve items only if on a whitelist managed by the node owner.">
</td>
<td>
<input id="public-open"
{% if access_models['public'] == 'open' %}checked=""{% endif %}
disabled=""
name="public"
type="radio"/>
{% if access_models['public'] == 'open' %}checked=""{% endif %}
disabled=""
name="public"
type="radio"/>
<label for="public-open"
title="Any entity may subscribe to
the node (i.e., without the necessity for subscription approval) and any entity
may retrieve items from the node (i.e., without being subscribed).">
title="Any entity may subscribe to the
node (i.e., without the necessity for subscription approval) and any entity may
retrieve items from the node (i.e., without being subscribed).">
Open
</label>
</td>
@ -300,8 +302,8 @@ may retrieve items from the node (i.e., without being subscribed).">
name="public"
type="radio"/>
<label for="public-presence"
title="Any entity with a subscription
of type 'from' or 'both' may subscribe to the node and retrieve items from the
title="Any entity with a subscription of
type 'from' or 'both' may subscribe to the node and retrieve items from the
node; this access model applies mainly to instant messaging systems (see RFC
3921).">
Presence
@ -309,14 +311,14 @@ node; this access model applies mainly to instant messaging systems (see RFC
</td>
<td>
<input id="public-roster"
{% if access_models['public'] == 'roster' %}checked=""{% endif %}
disabled=""
name="public"
type="radio"/>
{% if access_models['public'] == 'roster' %}checked=""{% endif %}
disabled=""
name="public"
type="radio"/>
<label for="public-roster"
title="Any entity in the specified
roster group(s) may subscribe to the node and retrieve items from the node; this
access model applies mainly to instant messaging systems (see RFC 3921).">
title="Any entity in the specified roster
group(s) may subscribe to the node and retrieve items from the node; this access
model applies mainly to instant messaging systems (see RFC 3921).">
Roster
</label>
</td>
@ -329,25 +331,24 @@ access model applies mainly to instant messaging systems (see RFC 3921).">
</td>
<td>
<input id="read-authorize"
{% if access_models['read'] == 'authorize' %}checked=""{% endif %}
disabled=""
name="read"
type="radio"/>
{% if access_models['read'] == 'authorize' %}checked=""{% endif %}
disabled=""
name="read"
type="radio"/>
<label for="read-authorize"
title="The node owner must approve
all subscription requests, and only subscribers may retrieve items from the
node.">
title="The node owner must approve all
subscription requests, and only subscribers may retrieve items from the node.">
Authorize
</label>
</td>
<td>
<input id="read-whitelist"
{% if access_models['read'] == 'whitelist' %}checked=""{% endif %}
disabled=""
name="read"
type="radio"/>
{% if access_models['read'] == 'whitelist' %}checked=""{% endif %}
disabled=""
name="read"
type="radio"/>
<label for="read-whitelist"
title="An entity may subscribe or
title="An entity may subscribe or
retrieve items only if on a whitelist managed by the node owner.">
Whitelist
</label>
@ -355,39 +356,47 @@ retrieve items only if on a whitelist managed by the node owner.">
</tr>
</table>
<input class="submit"
disabled=""
type="submit"
value="Save Permissions"/>
disabled=""
type="submit"
value="Save Permissions"/>
<input class="submit"
disabled=""
type="reset"
value="Reset Permissions"/>
disabled=""
type="reset"
value="Reset Permissions"/>
</form>
<h4 id="routine">Routine</h4>
<h4 id="routine">
Routine
</h4>
<p>
Choose a routine (i.e. default) directory (i.e. node).
</p>
<form action="/profile"
method="post">
method="post">
<input id="routine-private"
{% if routine == 'private' %}checked=""{% endif %}
name="routine"
required=""
type="radio"
value="private"/>
<label for="routine-private">Private</label>
<label for="routine-private">
Private
</label>
<input id="routine-public"
{% if not routine or routine == 'public' %}checked=""{% endif %}
name="routine"
type="radio"
value="public"/>
<label for="routine-public">Public</label>
<label for="routine-public">
Public
</label>
<input id="routine-read"
{% if routine == 'read' %}checked=""{% endif %}
name="routine"
type="radio"
value="read"/>
<label for="routine-read">Read</label>
<label for="routine-read">
Read
</label>
<br/>
<input class="submit"
type="submit"