implement bookmark storage
This commit is contained in:
		@@ -12,7 +12,8 @@ $.fn.bookmarktable = function() {
 | 
				
			|||||||
            var $cell = $(e.target);
 | 
					            var $cell = $(e.target);
 | 
				
			||||||
            var html = $cell.html();
 | 
					            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];
 | 
					            var $input = inputs[index];
 | 
				
			||||||
            if (!$input) return;
 | 
					            if (!$input) return;
 | 
				
			||||||
@@ -22,6 +23,12 @@ $.fn.bookmarktable = function() {
 | 
				
			|||||||
            $input.focus();
 | 
					            $input.focus();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            var submit = function() {
 | 
					            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')
 | 
					                var $option = $input.find('option:selected')
 | 
				
			||||||
                if ($option.length) {
 | 
					                if ($option.length) {
 | 
				
			||||||
                    $cell.html($option.html());
 | 
					                    $cell.html($option.html());
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -1,4 +1,5 @@
 | 
				
			|||||||
from datetime import datetime, timezone
 | 
					from datetime import datetime, timezone
 | 
				
			||||||
 | 
					from owrx.config.core import CoreConfig
 | 
				
			||||||
import json
 | 
					import json
 | 
				
			||||||
import os
 | 
					import os
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -42,7 +43,7 @@ class Bookmarks(object):
 | 
				
			|||||||
    def __init__(self):
 | 
					    def __init__(self):
 | 
				
			||||||
        self.file_modified = None
 | 
					        self.file_modified = None
 | 
				
			||||||
        self.bookmarks = []
 | 
					        self.bookmarks = []
 | 
				
			||||||
        self.fileList = ["/etc/openwebrx/bookmarks.json", "bookmarks.json"]
 | 
					        self.fileList = [Bookmarks._getBookmarksFile(), "/etc/openwebrx/bookmarks.json", "bookmarks.json"]
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def _refresh(self):
 | 
					    def _refresh(self):
 | 
				
			||||||
        modified = self._getFileModifiedTimestamp()
 | 
					        modified = self._getFileModifiedTimestamp()
 | 
				
			||||||
@@ -85,3 +86,13 @@ class Bookmarks(object):
 | 
				
			|||||||
        else:
 | 
					        else:
 | 
				
			||||||
            (lo, hi) = range
 | 
					            (lo, hi) = range
 | 
				
			||||||
            return [b for b in self.bookmarks if lo <= b.getFrequency() <= hi]
 | 
					            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()
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -2,6 +2,11 @@ from owrx.controllers.template import WebpageController
 | 
				
			|||||||
from owrx.controllers.admin import AuthorizationMixin
 | 
					from owrx.controllers.admin import AuthorizationMixin
 | 
				
			||||||
from owrx.bookmarks import Bookmark, Bookmarks
 | 
					from owrx.bookmarks import Bookmark, Bookmarks
 | 
				
			||||||
from owrx.modes import Modes
 | 
					from owrx.modes import Modes
 | 
				
			||||||
 | 
					import json
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import logging
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					logger = logging.getLogger(__name__)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
class BookmarksController(AuthorizationMixin, WebpageController):
 | 
					class BookmarksController(AuthorizationMixin, WebpageController):
 | 
				
			||||||
@@ -67,5 +72,31 @@ class BookmarksController(AuthorizationMixin, WebpageController):
 | 
				
			|||||||
            modulation_name=bookmark.getModulation() if mode is None else mode.name,
 | 
					            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):
 | 
					    def indexAction(self):
 | 
				
			||||||
        self.serve_template("settings/bookmarks.html", **self.template_variables())
 | 
					        self.serve_template("settings/bookmarks.html", **self.template_variables())
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -110,6 +110,7 @@ class Router(object):
 | 
				
			|||||||
            ),
 | 
					            ),
 | 
				
			||||||
            StaticRoute("/settings/sdr", SdrSettingsController),
 | 
					            StaticRoute("/settings/sdr", SdrSettingsController),
 | 
				
			||||||
            StaticRoute("/settings/bookmarks", BookmarksController),
 | 
					            StaticRoute("/settings/bookmarks", BookmarksController),
 | 
				
			||||||
 | 
					            RegexRoute("/settings/bookmarks/(.+)", BookmarksController, method="POST", options={"action": "update"}),
 | 
				
			||||||
            StaticRoute("/login", SessionController, options={"action": "loginAction"}),
 | 
					            StaticRoute("/login", SessionController, options={"action": "loginAction"}),
 | 
				
			||||||
            StaticRoute("/login", SessionController, method="POST", options={"action": "processLoginAction"}),
 | 
					            StaticRoute("/login", SessionController, method="POST", options={"action": "processLoginAction"}),
 | 
				
			||||||
            StaticRoute("/logout", SessionController, options={"action": "logoutAction"}),
 | 
					            StaticRoute("/logout", SessionController, options={"action": "logoutAction"}),
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user