add the "add bookmarks" function

This commit is contained in:
Jakob Ketterl
2021-02-14 16:21:09 +01:00
parent 9b1659d3dd
commit 29a161b7b7
7 changed files with 117 additions and 11 deletions

View File

@ -96,3 +96,6 @@ class Bookmarks(object):
with open(Bookmarks._getBookmarksFile(), "w") as file:
json.dump([b.__dict__() for b in self.bookmarks], file, indent=4)
self.file_modified = self._getFileModifiedTimestamp()
def addBookmark(self, bookmark: Bookmark):
self.bookmarks.append(bookmark)

View File

@ -32,7 +32,7 @@ class BookmarksController(AuthorizationMixin, WebpageController):
)
return """
<table class="table bookmarks">
<table class="table">
<tr>
<th>Name</th>
<th class="frequency">Frequency</th>
@ -97,5 +97,19 @@ class BookmarksController(AuthorizationMixin, WebpageController):
except json.JSONDecodeError:
self.send_response("{}", content_type="application/json", code=400)
def new(self):
bookmarks = Bookmarks.getSharedInstance()
try:
data = json.loads(self.get_body())
# sanitize
data = {k: data[k] for k in ["name", "frequency", "modulation"]}
bookmark = Bookmark(data)
bookmarks.addBookmark(bookmark)
bookmarks.store()
self.send_response(json.dumps({"bookmark_id": id(bookmark)}), 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),
StaticRoute("/settings/bookmarks", BookmarksController, method="POST", options={"action": "new"}),
RegexRoute("/settings/bookmarks/(.+)", BookmarksController, method="POST", options={"action": "update"}),
StaticRoute("/login", SessionController, options={"action": "loginAction"}),
StaticRoute("/login", SessionController, method="POST", options={"action": "processLoginAction"}),