implement bookmark storage

This commit is contained in:
Jakob Ketterl 2021-02-14 00:41:03 +01:00
parent 8ea4d11e9c
commit 3d97d362b5
4 changed files with 52 additions and 2 deletions

View File

@ -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());

View File

@ -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()

View File

@ -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())

View File

@ -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"}),