diff --git a/htdocs/lib/settings/BookmarkTable.js b/htdocs/lib/settings/BookmarkTable.js index a9e28f8..a311129 100644 --- a/htdocs/lib/settings/BookmarkTable.js +++ b/htdocs/lib/settings/BookmarkTable.js @@ -12,7 +12,8 @@ $.fn.bookmarktable = function() { var $cell = $(e.target); var html = $cell.html(); - var index = $cell.parent('tr').find('td').index($cell); + var $row = $cell.parent('tr'); + var index = $row.find('td').index($cell); var $input = inputs[index]; if (!$input) return; @@ -22,6 +23,12 @@ $.fn.bookmarktable = function() { $input.focus(); var submit = function() { + $.ajax(document.location.href + "/" + $row.data('id'), { + data: JSON.stringify(Object.fromEntries([[$input.prop('name'), $input.val()]])), + contentType: 'application/json', + method: 'POST' + }); + var $option = $input.find('option:selected') if ($option.length) { $cell.html($option.html()); diff --git a/owrx/bookmarks.py b/owrx/bookmarks.py index fe74365..211c13e 100644 --- a/owrx/bookmarks.py +++ b/owrx/bookmarks.py @@ -1,4 +1,5 @@ from datetime import datetime, timezone +from owrx.config.core import CoreConfig import json import os @@ -42,7 +43,7 @@ class Bookmarks(object): def __init__(self): self.file_modified = None self.bookmarks = [] - self.fileList = ["/etc/openwebrx/bookmarks.json", "bookmarks.json"] + self.fileList = [Bookmarks._getBookmarksFile(), "/etc/openwebrx/bookmarks.json", "bookmarks.json"] def _refresh(self): modified = self._getFileModifiedTimestamp() @@ -85,3 +86,13 @@ class Bookmarks(object): else: (lo, hi) = range return [b for b in self.bookmarks if lo <= b.getFrequency() <= hi] + + @staticmethod + def _getBookmarksFile(): + coreConfig = CoreConfig() + return "{data_directory}/bookmarks.json".format(data_directory=coreConfig.get_data_directory()) + + def store(self): + with open(Bookmarks._getBookmarksFile(), "w") as file: + json.dump([b.__dict__() for b in self.bookmarks], file, indent=4) + self.file_modified = self._getFileModifiedTimestamp() diff --git a/owrx/controllers/bookmarks.py b/owrx/controllers/bookmarks.py index 7c7d39d..5ed1608 100644 --- a/owrx/controllers/bookmarks.py +++ b/owrx/controllers/bookmarks.py @@ -2,6 +2,11 @@ from owrx.controllers.template import WebpageController from owrx.controllers.admin import AuthorizationMixin from owrx.bookmarks import Bookmark, Bookmarks from owrx.modes import Modes +import json + +import logging + +logger = logging.getLogger(__name__) class BookmarksController(AuthorizationMixin, WebpageController): @@ -67,5 +72,31 @@ class BookmarksController(AuthorizationMixin, WebpageController): modulation_name=bookmark.getModulation() if mode is None else mode.name, ) + def _findBookmark(self, bookmark_id): + bookmarks = Bookmarks.getSharedInstance() + try: + return next(b for b in bookmarks.getBookmarks() if id(b) == bookmark_id) + except StopIteration: + return None + + def update(self): + bookmark_id = int(self.request.matches.group(1)) + bookmark = self._findBookmark(bookmark_id) + if bookmark is None: + self.send_response("{}", content_type="application/json", code=404) + return + try: + data = json.loads(self.get_body()) + for key in ["name", "frequency", "modulation"]: + if key in data: + value = data[key] + if key == "frequency": + value = int(value) + setattr(bookmark, key, value) + Bookmarks.getSharedInstance().store() + self.send_response("{}", content_type="application/json", code=200) + except json.JSONDecodeError: + self.send_response("{}", content_type="application/json", code=400) + def indexAction(self): self.serve_template("settings/bookmarks.html", **self.template_variables()) diff --git a/owrx/http.py b/owrx/http.py index 1f816fa..96f54ee 100644 --- a/owrx/http.py +++ b/owrx/http.py @@ -110,6 +110,7 @@ class Router(object): ), StaticRoute("/settings/sdr", SdrSettingsController), StaticRoute("/settings/bookmarks", BookmarksController), + RegexRoute("/settings/bookmarks/(.+)", BookmarksController, method="POST", options={"action": "update"}), StaticRoute("/login", SessionController, options={"action": "loginAction"}), StaticRoute("/login", SessionController, method="POST", options={"action": "processLoginAction"}), StaticRoute("/logout", SessionController, options={"action": "logoutAction"}),