Added search to static reminders and templates

This commit is contained in:
CasVT
2023-06-08 14:34:02 +02:00
parent 9b716b80d9
commit cf69427e87
8 changed files with 167 additions and 67 deletions

View File

@@ -17,7 +17,6 @@ from backend.db import close_db, get_db
filter_function = lambda query, p: (
query in p["title"].lower()
or query in p["text"].lower()
or query in p["notification_service_title"].lower()
)
def _find_next_time(

View File

@@ -9,6 +9,10 @@ from backend.custom_exceptions import (NotificationServiceNotFound,
ReminderNotFound)
from backend.db import get_db
filter_function = lambda query, p: (
query in p["title"].lower()
or query in p["text"].lower()
)
class StaticReminder:
"""Represents a static reminder
@@ -147,7 +151,23 @@ class StaticReminders:
))
return reminders
def search(self, query: str) -> List[dict]:
"""Search for static reminders
Args:
query (str): The term to search for
Returns:
List[dict]: All static reminders that match. Similar output to self.fetchall
"""
query = query.lower()
reminders = list(filter(
lambda p: filter_function(query, p),
self.fetchall()
))
return reminders
def fetchone(self, id: int) -> StaticReminder:
"""Get one static reminder

View File

@@ -7,6 +7,10 @@ from backend.custom_exceptions import (NotificationServiceNotFound,
TemplateNotFound)
from backend.db import get_db
filter_function = lambda query, p: (
query in p["title"].lower()
or query in p["text"].lower()
)
class Template:
"""Represents a template
@@ -136,6 +140,22 @@ class Templates:
return templates
def search(self, query: str) -> List[dict]:
"""Search for templates
Args:
query (str): The term to search for
Returns:
List[dict]: All templates that match. Similar output to self.fetchall
"""
query = query.lower()
reminders = list(filter(
lambda p: filter_function(query, p),
self.fetchall()
))
return reminders
def fetchone(self, id: int) -> Template:
"""Get one template

View File

@@ -632,6 +632,29 @@ def api_get_templates():
color=color)
return return_api(result.get(), code=201)
@api.route('/templates/search', methods=['GET'])
@error_handler
@auth
def api_templates_query():
"""
Endpoint: /templates/search
Description: Search through the list of templates
Requires being logged in: Yes
Methods:
GET:
Parameters (url):
query (required): The search term
Returns:
200:
The search results, listed like GET /templates
400:
KeyNotFound: One of the required parameters was not given
"""
query = extract_key(request.values, 'query')
result = g.user_data.templates.search(query)
return return_api(result)
@api.route('/templates/<int:t_id>', methods=['GET', 'PUT', 'DELETE'])
@error_handler
@auth
@@ -746,6 +769,29 @@ def api_static_reminders_list():
color=color)
return return_api(result.get(), code=201)
@api.route('/staticreminders/search', methods=['GET'])
@error_handler
@auth
def api_static_reminders_query():
"""
Endpoint: /staticreminders/search
Description: Search through the list of staticreminders
Requires being logged in: Yes
Methods:
GET:
Parameters (url):
query (required): The search term
Returns:
200:
The search results, listed like GET /staticreminders
400:
KeyNotFound: One of the required parameters was not given
"""
query = extract_key(request.values, 'query')
result = g.user_data.static_reminders.search(query)
return return_api(result)
@api.route('/staticreminders/<int:r_id>', methods=['GET', 'POST', 'PUT', 'DELETE'])
@error_handler
@auth

View File

@@ -46,9 +46,9 @@
}
/* REMINDER LIST */
#reminder-list,
#static-reminder-list,
#template-list {
#reminder-tab,
#static-reminder-tab,
#template-tab {
--gap: 1rem;
--entry-width: 13rem;
max-width: 43rem;

View File

@@ -1,7 +1,7 @@
const types = {
'reminder': document.getElementById('reminder-list'),
'static_reminder': document.getElementById('static-reminder-list'),
'template': document.getElementById('template-list')
'reminder': document.getElementById('reminder-tab'),
'static_reminder': document.getElementById('static-reminder-tab'),
'template': document.getElementById('template-tab')
};
const icons = {

View File

@@ -5,7 +5,7 @@ function showTab(button) {
);
// Show desired tab and hide all others
document.querySelectorAll('#home > div:not(.tab-selector)').forEach(
document.querySelectorAll('#home > div:not(.tab-selector):not(.search-container)').forEach(
e => e.classList.add('hidden')
);
document.getElementById(button.dataset.target).classList.remove('hidden');
@@ -87,13 +87,25 @@ function fillTemplates() {
// Library search
//
function searchLibrary() {
const query = document.querySelector('#search-input').value;
fetch(`${url_prefix}/api/reminders/search?api_key=${api_key}&query=${query}`)
const query = document.querySelector('#search-input').value,
tab = document.getElementById(
document.querySelector('.tab-selector > button[data-selected="true"]').dataset.target
)
let url;
if (tab === types.reminder)
url = `${url_prefix}/api/reminders/search?api_key=${api_key}&query=${query}`;
else if (tab === types.static_reminder)
url = `${url_prefix}/api/staticreminders/search?api_key=${api_key}&query=${query}`;
else if (tab === types.template)
url = `${url_prefix}/api/templates/search?api_key=${api_key}&query=${query}`;
else return;
fetch(url)
.then(response => {
if (!response.ok) return Promise.reject(response.status);
return response.json();
})
.then(json => fillTable(types.reminder, json.result))
.then(json => fillTable(tab, json.result))
.catch(e => {
if (e === 401)
window.location.href = `${url_prefix}/`;
@@ -104,7 +116,16 @@ function searchLibrary() {
function clearSearchLibrary() {
document.querySelector('#search-input').value = '';
fillReminders();
const tab = document.getElementById(
document.querySelector('.tab-selector > button[data-selected="true"]').dataset.target
)
if (tab === types.reminder)
fillReminders();
else if (tab === types.static_reminder)
fillStaticReminders();
else if (tab === types.template)
fillTemplates();
else return;
};
// code run on load

View File

@@ -81,71 +81,65 @@
<button data-target="static-reminder-tab">Static Reminders</button>
<button data-target="template-tab">Templates</button>
</div>
<div id="reminder-tab">
<div class="search-container">
<noscript>Javascript is disabled. The web-ui of MIND does not work with JavaScript disabled.</noscript>
<form id="search-form">
<div class="search-bar">
<button type="submit">
<svg xmlns="http://www.w3.org/2000/svg" version="1.1" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:svgjs="http://svgjs.com/svgjs" width="256" height="256" x="0" y="0" viewBox="0 0 513.749 513.749" style="enable-background:new 0 0 512 512" xml:space="preserve">
<g>
<g>
<path d="M504.352,459.061l-99.435-99.477c74.402-99.427,54.115-240.344-45.312-314.746S119.261-9.277,44.859,90.15 S-9.256,330.494,90.171,404.896c79.868,59.766,189.565,59.766,269.434,0l99.477,99.477c12.501,12.501,32.769,12.501,45.269,0 c12.501-12.501,12.501-32.769,0-45.269L504.352,459.061z M225.717,385.696c-88.366,0-160-71.634-160-160s71.634-160,160-160 s160,71.634,160,160C385.623,314.022,314.044,385.602,225.717,385.696z"></path>
</g>
</g>
</svg>
</button>
<input type="text" id="search-input" required placeholder="Search..." aria-placeholder="Search for reminders">
<button type="button" id="clear-button">
<svg xmlns="http://www.w3.org/2000/svg" version="1.1" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:svgjs="http://svgjs.com/svgjs" width="256" height="256" x="0" y="0" viewBox="0 0 512.021 512.021" style="enable-background:new 0 0 512 512" xml:space="preserve">
<g>
<g>
<path d="M301.258,256.01L502.645,54.645c12.501-12.501,12.501-32.769,0-45.269c-12.501-12.501-32.769-12.501-45.269,0l0,0 L256.01,210.762L54.645,9.376c-12.501-12.501-32.769-12.501-45.269,0s-12.501,32.769,0,45.269L210.762,256.01L9.376,457.376 c-12.501,12.501-12.501,32.769,0,45.269s32.769,12.501,45.269,0L256.01,301.258l201.365,201.387 c12.501,12.501,32.769,12.501,45.269,0c12.501-12.501,12.501-32.769,0-45.269L301.258,256.01z"></path>
</g>
</g>
</svg>
</button>
</div>
</form>
</div>
<div id="reminder-list">
<button class="entry add-entry" id="add-reminder" aria-label="Add reminder" title="Add reminder">
<svg xmlns="http://www.w3.org/2000/svg" version="1.1" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:svgjs="http://svgjs.com/svgjs" width="256" height="256" x="0" y="0" viewBox="0 0 512 512" style="enable-background:new 0 0 512 512" xml:space="preserve">
<g>
<div class="search-container">
<noscript>Javascript is disabled. The web-ui of MIND does not work with JavaScript disabled.</noscript>
<form id="search-form">
<div class="search-bar">
<button type="submit">
<svg xmlns="http://www.w3.org/2000/svg" version="1.1" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:svgjs="http://svgjs.com/svgjs" width="256" height="256" x="0" y="0" viewBox="0 0 513.749 513.749" style="enable-background:new 0 0 512 512" xml:space="preserve">
<g>
<path d="M480,224H288V32c0-17.673-14.327-32-32-32s-32,14.327-32,32v192H32c-17.673,0-32,14.327-32,32s14.327,32,32,32h192v192 c0,17.673,14.327,32,32,32s32-14.327,32-32V288h192c17.673,0,32-14.327,32-32S497.673,224,480,224z"></path>
<g>
<path d="M504.352,459.061l-99.435-99.477c74.402-99.427,54.115-240.344-45.312-314.746S119.261-9.277,44.859,90.15 S-9.256,330.494,90.171,404.896c79.868,59.766,189.565,59.766,269.434,0l99.477,99.477c12.501,12.501,32.769,12.501,45.269,0 c12.501-12.501,12.501-32.769,0-45.269L504.352,459.061z M225.717,385.696c-88.366,0-160-71.634-160-160s71.634-160,160-160 s160,71.634,160,160C385.623,314.022,314.044,385.602,225.717,385.696z"></path>
</g>
</g>
</svg>
</button>
<input type="text" id="search-input" required placeholder="Search..." aria-placeholder="Search for reminders">
<button type="button" id="clear-button">
<svg xmlns="http://www.w3.org/2000/svg" version="1.1" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:svgjs="http://svgjs.com/svgjs" width="256" height="256" x="0" y="0" viewBox="0 0 512.021 512.021" style="enable-background:new 0 0 512 512" xml:space="preserve">
<g>
<g>
<path d="M301.258,256.01L502.645,54.645c12.501-12.501,12.501-32.769,0-45.269c-12.501-12.501-32.769-12.501-45.269,0l0,0 L256.01,210.762L54.645,9.376c-12.501-12.501-32.769-12.501-45.269,0s-12.501,32.769,0,45.269L210.762,256.01L9.376,457.376 c-12.501,12.501-12.501,32.769,0,45.269s32.769,12.501,45.269,0L256.01,301.258l201.365,201.387 c12.501,12.501,32.769,12.501,45.269,0c12.501-12.501,12.501-32.769,0-45.269L301.258,256.01z"></path>
</g>
</g>
</svg>
</button>
</div>
</form>
</div>
<div id="reminder-tab">
<button class="entry add-entry" id="add-reminder" aria-label="Add reminder" title="Add reminder">
<svg xmlns="http://www.w3.org/2000/svg" version="1.1" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:svgjs="http://svgjs.com/svgjs" width="256" height="256" x="0" y="0" viewBox="0 0 512 512" style="enable-background:new 0 0 512 512" xml:space="preserve">
<g>
<g>
<path d="M480,224H288V32c0-17.673-14.327-32-32-32s-32,14.327-32,32v192H32c-17.673,0-32,14.327-32,32s14.327,32,32,32h192v192 c0,17.673,14.327,32,32,32s32-14.327,32-32V288h192c17.673,0,32-14.327,32-32S497.673,224,480,224z"></path>
</g>
</svg>
<p>Add a notification service first!</p>
</button>
</div>
</g>
</svg>
<p>Add a notification service first!</p>
</button>
</div>
<div id="static-reminder-tab" class="hidden">
<div id="static-reminder-list">
<button class="entry add-entry" id="add-static-reminder" aria-label="Add static reminder" title="Add static reminder">
<svg xmlns="http://www.w3.org/2000/svg" version="1.1" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:svgjs="http://svgjs.com/svgjs" width="256" height="256" x="0" y="0" viewBox="0 0 512 512" style="enable-background:new 0 0 512 512" xml:space="preserve">
<button class="entry add-entry" id="add-static-reminder" aria-label="Add static reminder" title="Add static reminder">
<svg xmlns="http://www.w3.org/2000/svg" version="1.1" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:svgjs="http://svgjs.com/svgjs" width="256" height="256" x="0" y="0" viewBox="0 0 512 512" style="enable-background:new 0 0 512 512" xml:space="preserve">
<g>
<g>
<g>
<path d="M480,224H288V32c0-17.673-14.327-32-32-32s-32,14.327-32,32v192H32c-17.673,0-32,14.327-32,32s14.327,32,32,32h192v192 c0,17.673,14.327,32,32,32s32-14.327,32-32V288h192c17.673,0,32-14.327,32-32S497.673,224,480,224z"></path>
</g>
<path d="M480,224H288V32c0-17.673-14.327-32-32-32s-32,14.327-32,32v192H32c-17.673,0-32,14.327-32,32s14.327,32,32,32h192v192 c0,17.673,14.327,32,32,32s32-14.327,32-32V288h192c17.673,0,32-14.327,32-32S497.673,224,480,224z"></path>
</g>
</svg>
</button>
</div>
</g>
</svg>
</button>
</div>
<div id="template-tab" class="hidden">
<div id="template-list">
<button class="entry add-entry" id="add-template" aria-label="Add template" title="Add template">
<svg xmlns="http://www.w3.org/2000/svg" version="1.1" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:svgjs="http://svgjs.com/svgjs" width="256" height="256" x="0" y="0" viewBox="0 0 512 512" style="enable-background:new 0 0 512 512" xml:space="preserve">
<button class="entry add-entry" id="add-template" aria-label="Add template" title="Add template">
<svg xmlns="http://www.w3.org/2000/svg" version="1.1" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:svgjs="http://svgjs.com/svgjs" width="256" height="256" x="0" y="0" viewBox="0 0 512 512" style="enable-background:new 0 0 512 512" xml:space="preserve">
<g>
<g>
<g>
<path d="M480,224H288V32c0-17.673-14.327-32-32-32s-32,14.327-32,32v192H32c-17.673,0-32,14.327-32,32s14.327,32,32,32h192v192 c0,17.673,14.327,32,32,32s32-14.327,32-32V288h192c17.673,0,32-14.327,32-32S497.673,224,480,224z"></path>
</g>
<path d="M480,224H288V32c0-17.673-14.327-32-32-32s-32,14.327-32,32v192H32c-17.673,0-32,14.327-32,32s14.327,32,32,32h192v192 c0,17.673,14.327,32,32,32s32-14.327,32-32V288h192c17.673,0,32-14.327,32-32S497.673,224,480,224z"></path>
</g>
</svg>
</button>
</div>
</g>
</svg>
</button>
</div>
</div>