make "new bookmark" api work with arrays

This commit is contained in:
Jakob Ketterl 2021-03-30 18:30:08 +02:00
parent 2d142e45ed
commit 1b9e77982d
2 changed files with 14 additions and 10 deletions

View File

@ -297,12 +297,12 @@ $.fn.bookmarktable = function() {
);
$.ajax(document.location.href, {
data: JSON.stringify(data),
data: JSON.stringify([data]),
contentType: 'application/json',
method: 'POST'
}).done(function(data){
if ('bookmark_id' in data) {
row.attr('data-id', data['bookmark_id']);
if (data.length && data.length === 1 && 'bookmark_id' in data[0]) {
row.attr('data-id', data[0]['bookmark_id']);
var tds = row.find('td');
Object.values(inputs).forEach(function(input, index) {

View File

@ -103,19 +103,23 @@ class BookmarksController(AuthorizationMixin, WebpageController):
def new(self):
bookmarks = Bookmarks.getSharedInstance()
try:
data = json.loads(self.get_body())
def create(bookmark_data):
# sanitize
data = {
"name": data["name"],
"frequency": int(data["frequency"]),
"modulation": data["modulation"],
"name": bookmark_data["name"],
"frequency": int(bookmark_data["frequency"]),
"modulation": bookmark_data["modulation"],
}
bookmark = Bookmark(data)
bookmarks.addBookmark(bookmark)
return {"bookmark_id": id(bookmark)}
try:
data = json.loads(self.get_body())
result = [create(b) for b in data]
bookmarks.store()
self.send_response(json.dumps({"bookmark_id": id(bookmark)}), content_type="application/json", code=200)
self.send_response(json.dumps(result), content_type="application/json", code=200)
except json.JSONDecodeError:
self.send_response("{}", content_type="application/json", code=400)