Skip to content

Commit

Permalink
simplified requestCreateNotification based on 20220616_notification_s…
Browse files Browse the repository at this point in the history
  • Loading branch information
relativisticelectron committed Jun 20, 2022
1 parent 606a6ba commit 2b8ac1f
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 37 deletions.
36 changes: 18 additions & 18 deletions src/cryptoadvance/specter/server_endpoints/wallets_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,35 +145,35 @@ def create_notification():
The request.form must contain a dict. Only 'title' is mandatory
{
'title' : title,
'timeout' : timeout,
'notification_type' : notification_type,
'target_uis' : target_uis,
'body' : body,
'image' : image_url,
'icon' : icon,
'options':{
'timeout' : timeout,
'notification_type' : notification_type,
'target_uis' : target_uis,
'body' : body,
'image' : image_url,
'icon' : icon,
}
}
If a value is itself a list or dict (like target_uis) it has to be in a json format.
"""
arguments = dict(request.form)
# try reading everything with json
for key in arguments:
try:
arguments[key] = json.loads(arguments[key])
except:
pass

logger.debug(f"wallets_endpoint_api create_notification with arguments {arguments}")

if "title" not in arguments or not arguments["title"]:
title = request.form.get("title")
if not title:
return jsonify(
success=False,
error="The create_notification POST request must contain a 'title'",
)

options = json.loads(request.form.get("options", "{}"))
logger.debug(
f"wallets_endpoint_api create_notification with title {title} and options {options}"
)

return jsonify(
app.specter.notification_manager.create_and_show(
**arguments, user_id=app.specter.user_manager.get_user().id
title,
app.specter.user_manager.get_user().id,
**options,
)
)

Expand Down
21 changes: 2 additions & 19 deletions src/cryptoadvance/specter/templates/includes/notifications.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,11 @@


/* creating a notification from JS */
async function requestCreateNotification(title, options){
function add(key){
if (key in options){
formData.append(key, options[key]);
}
}
function addjson(key){
if (key in options){
formData.append(key, JSON.stringify( options[key]));
}
}

async function requestCreateNotification(title, options){
var url = "{{ url_for('wallets_endpoint_api.create_notification' ) }}";
var formData = new FormData();
formData.append("title", title)
add('timeout')
add('notification_type')
addjson('target_uis')
addjson('data')
add('body')
add('image')
add('icon')
formData.append('options', JSON.stringify( options));
return send_request(url, 'POST', "{{ csrf_token() }}", formData)
}

Expand Down

0 comments on commit 2b8ac1f

Please sign in to comment.