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, { $.ajax(document.location.href, {
data: JSON.stringify(data), data: JSON.stringify([data]),
contentType: 'application/json', contentType: 'application/json',
method: 'POST' method: 'POST'
}).done(function(data){ }).done(function(data){
if ('bookmark_id' in data) { if (data.length && data.length === 1 && 'bookmark_id' in data[0]) {
row.attr('data-id', data['bookmark_id']); row.attr('data-id', data[0]['bookmark_id']);
var tds = row.find('td'); var tds = row.find('td');
Object.values(inputs).forEach(function(input, index) { Object.values(inputs).forEach(function(input, index) {

View File

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