reverted
This commit is contained in:
@@ -28,725 +28,6 @@ data:
|
|||||||
|
|
||||||
APP = FastAPI()
|
APP = FastAPI()
|
||||||
|
|
||||||
# ================================
|
|
||||||
# Simple Notes Widget - Multi-user
|
|
||||||
# ================================
|
|
||||||
|
|
||||||
def get_notes_file(user: str) -> str:
|
|
||||||
"""Get notes file path for a user, with validation."""
|
|
||||||
# Sanitize username: only allow alphanumeric, dash, underscore
|
|
||||||
safe_user = re.sub(r'[^a-zA-Z0-9_-]', '', user)
|
|
||||||
if not safe_user:
|
|
||||||
safe_user = "default"
|
|
||||||
data_dir = os.environ.get("DATA_DIR", "/data")
|
|
||||||
return os.path.join(data_dir, f"notes_{safe_user}.txt")
|
|
||||||
|
|
||||||
def load_notes(user: str) -> str:
|
|
||||||
"""Load notes from file for a specific user."""
|
|
||||||
notes_file = get_notes_file(user)
|
|
||||||
try:
|
|
||||||
if os.path.exists(notes_file):
|
|
||||||
with open(notes_file, "r", encoding="utf-8") as f:
|
|
||||||
return f.read()
|
|
||||||
except Exception as e:
|
|
||||||
print(f"Error loading notes for {user}: {e}")
|
|
||||||
return ""
|
|
||||||
|
|
||||||
def save_notes(user: str, content: str) -> bool:
|
|
||||||
"""Save notes to file for a specific user."""
|
|
||||||
notes_file = get_notes_file(user)
|
|
||||||
try:
|
|
||||||
with open(notes_file, "w", encoding="utf-8") as f:
|
|
||||||
f.write(content)
|
|
||||||
return True
|
|
||||||
except Exception as e:
|
|
||||||
print(f"Error saving notes for {user}: {e}")
|
|
||||||
return False
|
|
||||||
|
|
||||||
@APP.get("/notes")
|
|
||||||
def notes_widget(key: str = "", user: str = "default", accent: str = "ffffff", bgcolor: str = ""):
|
|
||||||
"""Serve the notes widget HTML page for a specific user with optional theme."""
|
|
||||||
expected_key = os.environ.get("GLANCE_HELPER_KEY", "")
|
|
||||||
if key != expected_key:
|
|
||||||
return Response(content="Unauthorized", status_code=401)
|
|
||||||
|
|
||||||
safe_user = re.sub(r'[^a-zA-Z0-9_-]', '', user) or "default"
|
|
||||||
# Sanitize accent color (hex only, default to white)
|
|
||||||
safe_accent = re.sub(r'[^a-fA-F0-9]', '', accent)[:6] or "ffffff"
|
|
||||||
# Sanitize bgcolor (hex only, default to dark purple matching Orsi's theme)
|
|
||||||
safe_bgcolor = re.sub(r'[^a-fA-F0-9]', '', bgcolor)[:6] or "2d1f3d"
|
|
||||||
|
|
||||||
current_notes = load_notes(safe_user)
|
|
||||||
escaped_notes = current_notes.replace("&", "&").replace("<", "<").replace(">", ">").replace('"', """)
|
|
||||||
|
|
||||||
html = f"""<!DOCTYPE html>
|
|
||||||
<html>
|
|
||||||
<head>
|
|
||||||
<meta charset="UTF-8">
|
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
||||||
<style>
|
|
||||||
:root {{
|
|
||||||
--accent: #{safe_accent};
|
|
||||||
--accent-20: #{safe_accent}33;
|
|
||||||
--accent-40: #{safe_accent}66;
|
|
||||||
--accent-60: #{safe_accent}99;
|
|
||||||
--bgcolor: #{safe_bgcolor};
|
|
||||||
}}
|
|
||||||
* {{ margin: 0; padding: 0; box-sizing: border-box; }}
|
|
||||||
html, body {{
|
|
||||||
background: var(--bgcolor);
|
|
||||||
height: 100%;
|
|
||||||
}}
|
|
||||||
body {{
|
|
||||||
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
|
|
||||||
}}
|
|
||||||
.container {{
|
|
||||||
display: flex;
|
|
||||||
flex-direction: column;
|
|
||||||
height: 100%;
|
|
||||||
padding: 6px;
|
|
||||||
}}
|
|
||||||
.status {{
|
|
||||||
font-size: 11px;
|
|
||||||
color: var(--accent-60);
|
|
||||||
padding: 4px 8px;
|
|
||||||
text-align: right;
|
|
||||||
min-height: 20px;
|
|
||||||
}}
|
|
||||||
.status.saving {{ color: rgba(255, 200, 100, 0.9); }}
|
|
||||||
.status.saved {{ color: rgba(150, 255, 180, 0.9); }}
|
|
||||||
.status.error {{ color: rgba(255, 120, 120, 0.9); }}
|
|
||||||
textarea {{
|
|
||||||
flex: 1;
|
|
||||||
width: 100%;
|
|
||||||
background: rgba(255, 255, 255, 0.06);
|
|
||||||
border: 1px solid var(--accent-20);
|
|
||||||
border-radius: 8px;
|
|
||||||
color: rgba(255, 255, 255, 0.9);
|
|
||||||
font-size: 14px;
|
|
||||||
line-height: 1.5;
|
|
||||||
padding: 12px;
|
|
||||||
resize: none;
|
|
||||||
outline: none;
|
|
||||||
}}
|
|
||||||
textarea:focus {{
|
|
||||||
border-color: var(--accent-40);
|
|
||||||
background: rgba(255, 255, 255, 0.1);
|
|
||||||
}}
|
|
||||||
textarea::placeholder {{
|
|
||||||
color: var(--accent-40);
|
|
||||||
}}
|
|
||||||
textarea::-webkit-scrollbar {{ width: 6px; }}
|
|
||||||
textarea::-webkit-scrollbar-track {{ background: transparent; }}
|
|
||||||
textarea::-webkit-scrollbar-thumb {{ background: var(--accent-40); border-radius: 3px; }}
|
|
||||||
textarea::-webkit-scrollbar-thumb:hover {{ background: var(--accent-60); }}
|
|
||||||
</style>
|
|
||||||
</head>
|
|
||||||
<body>
|
|
||||||
<div class="container">
|
|
||||||
<textarea id="notes" placeholder="Write your notes here...">{escaped_notes}</textarea>
|
|
||||||
<div class="status" id="status"></div>
|
|
||||||
</div>
|
|
||||||
<script>
|
|
||||||
const textarea = document.getElementById('notes');
|
|
||||||
const status = document.getElementById('status');
|
|
||||||
const apiKey = '{expected_key}';
|
|
||||||
const user = '{safe_user}';
|
|
||||||
let saveTimeout = null;
|
|
||||||
let lastSaved = textarea.value;
|
|
||||||
|
|
||||||
function updateStatus(text, className) {{
|
|
||||||
status.textContent = text;
|
|
||||||
status.className = 'status ' + (className || '');
|
|
||||||
}}
|
|
||||||
|
|
||||||
async function saveNotes() {{
|
|
||||||
const content = textarea.value;
|
|
||||||
if (content === lastSaved) return;
|
|
||||||
|
|
||||||
updateStatus('Saving...', 'saving');
|
|
||||||
try {{
|
|
||||||
const response = await fetch('/notes/save?key=' + apiKey + '&user=' + user, {{
|
|
||||||
method: 'POST',
|
|
||||||
headers: {{ 'Content-Type': 'application/json' }},
|
|
||||||
body: JSON.stringify({{ content: content }})
|
|
||||||
}});
|
|
||||||
if (response.ok) {{
|
|
||||||
lastSaved = content;
|
|
||||||
updateStatus('Saved ✓', 'saved');
|
|
||||||
setTimeout(() => updateStatus(''), 2000);
|
|
||||||
}} else {{
|
|
||||||
updateStatus('Save failed', 'error');
|
|
||||||
}}
|
|
||||||
}} catch (e) {{
|
|
||||||
updateStatus('Save failed', 'error');
|
|
||||||
}}
|
|
||||||
}}
|
|
||||||
|
|
||||||
textarea.addEventListener('input', () => {{
|
|
||||||
if (saveTimeout) clearTimeout(saveTimeout);
|
|
||||||
saveTimeout = setTimeout(saveNotes, 1000);
|
|
||||||
}});
|
|
||||||
|
|
||||||
textarea.addEventListener('blur', saveNotes);
|
|
||||||
window.addEventListener('beforeunload', saveNotes);
|
|
||||||
</script>
|
|
||||||
</body>
|
|
||||||
</html>"""
|
|
||||||
return Response(content=html, media_type="text/html")
|
|
||||||
|
|
||||||
@APP.post("/notes/save")
|
|
||||||
async def save_notes_api(key: str = "", user: str = "default", content: dict = None):
|
|
||||||
"""API endpoint to save notes for a specific user."""
|
|
||||||
expected_key = os.environ.get("GLANCE_HELPER_KEY", "")
|
|
||||||
if key != expected_key:
|
|
||||||
return Response(content="Unauthorized", status_code=401)
|
|
||||||
|
|
||||||
safe_user = re.sub(r'[^a-zA-Z0-9_-]', '', user) or "default"
|
|
||||||
|
|
||||||
if content and "content" in content:
|
|
||||||
if save_notes(safe_user, content["content"]):
|
|
||||||
return {"status": "ok", "user": safe_user}
|
|
||||||
return Response(content="Failed to save", status_code=500)
|
|
||||||
|
|
||||||
# ================================
|
|
||||||
# Unified User Data System (Notes, Todo, Motivation)
|
|
||||||
# ================================
|
|
||||||
# File format:
|
|
||||||
# [Notes]
|
|
||||||
# Free-form notes text...
|
|
||||||
#
|
|
||||||
# [Todo]
|
|
||||||
# - [ ] Uncompleted task
|
|
||||||
# - [x] Completed task
|
|
||||||
#
|
|
||||||
# [Motivation]
|
|
||||||
# Quote 1
|
|
||||||
# Quote 2
|
|
||||||
|
|
||||||
def get_userdata_file(user: str) -> str:
|
|
||||||
"""Get user data file path, with validation."""
|
|
||||||
safe_user = re.sub(r'[^a-zA-Z0-9_-]', '', user) or "default"
|
|
||||||
data_dir = os.environ.get("DATA_DIR", "/data")
|
|
||||||
return os.path.join(data_dir, f"userdata_{safe_user}.txt")
|
|
||||||
|
|
||||||
def load_userdata(user: str) -> dict:
|
|
||||||
"""Load and parse user data file into sections."""
|
|
||||||
filepath = get_userdata_file(user)
|
|
||||||
sections = {"Notes": "", "Todo": [], "Motivation": []}
|
|
||||||
try:
|
|
||||||
if os.path.exists(filepath):
|
|
||||||
with open(filepath, "r", encoding="utf-8") as f:
|
|
||||||
content = f.read()
|
|
||||||
current_section = None
|
|
||||||
section_lines = []
|
|
||||||
for line in content.split('\n'):
|
|
||||||
stripped = line.strip()
|
|
||||||
if stripped.startswith('[') and stripped.endswith(']'):
|
|
||||||
# Save previous section
|
|
||||||
if current_section == "Notes":
|
|
||||||
sections["Notes"] = '\n'.join(section_lines).strip()
|
|
||||||
elif current_section == "Todo":
|
|
||||||
for l in section_lines:
|
|
||||||
l = l.strip()
|
|
||||||
if l.startswith('- [x] '):
|
|
||||||
sections["Todo"].append({"text": l[6:], "done": True})
|
|
||||||
elif l.startswith('- [ ] '):
|
|
||||||
sections["Todo"].append({"text": l[6:], "done": False})
|
|
||||||
elif current_section == "Motivation":
|
|
||||||
for l in section_lines:
|
|
||||||
l = l.strip()
|
|
||||||
if l:
|
|
||||||
sections["Motivation"].append(l)
|
|
||||||
# Start new section
|
|
||||||
current_section = stripped[1:-1]
|
|
||||||
section_lines = []
|
|
||||||
else:
|
|
||||||
section_lines.append(line)
|
|
||||||
# Handle last section
|
|
||||||
if current_section == "Notes":
|
|
||||||
sections["Notes"] = '\n'.join(section_lines).strip()
|
|
||||||
elif current_section == "Todo":
|
|
||||||
for l in section_lines:
|
|
||||||
l = l.strip()
|
|
||||||
if l.startswith('- [x] '):
|
|
||||||
sections["Todo"].append({"text": l[6:], "done": True})
|
|
||||||
elif l.startswith('- [ ] '):
|
|
||||||
sections["Todo"].append({"text": l[6:], "done": False})
|
|
||||||
elif current_section == "Motivation":
|
|
||||||
for l in section_lines:
|
|
||||||
l = l.strip()
|
|
||||||
if l:
|
|
||||||
sections["Motivation"].append(l)
|
|
||||||
except Exception as e:
|
|
||||||
print(f"Error loading userdata for {user}: {e}")
|
|
||||||
return sections
|
|
||||||
|
|
||||||
def save_userdata(user: str, sections: dict) -> bool:
|
|
||||||
"""Save sections back to user data file."""
|
|
||||||
filepath = get_userdata_file(user)
|
|
||||||
try:
|
|
||||||
lines = []
|
|
||||||
lines.append("[Notes]")
|
|
||||||
lines.append(sections.get("Notes", ""))
|
|
||||||
lines.append("")
|
|
||||||
lines.append("[Todo]")
|
|
||||||
for item in sections.get("Todo", []):
|
|
||||||
mark = "x" if item.get("done") else " "
|
|
||||||
lines.append(f"- [{mark}] {item.get('text', '')}")
|
|
||||||
lines.append("")
|
|
||||||
lines.append("[Motivation]")
|
|
||||||
for quote in sections.get("Motivation", []):
|
|
||||||
lines.append(quote)
|
|
||||||
with open(filepath, "w", encoding="utf-8") as f:
|
|
||||||
f.write('\n'.join(lines))
|
|
||||||
return True
|
|
||||||
except Exception as e:
|
|
||||||
print(f"Error saving userdata for {user}: {e}")
|
|
||||||
return False
|
|
||||||
|
|
||||||
# --------------------------------
|
|
||||||
# Todo Widget
|
|
||||||
# --------------------------------
|
|
||||||
@APP.get("/userdata/todo")
|
|
||||||
def todo_widget(key: str = "", user: str = "default", accent: str = "5ac8d8", bgcolor: str = "transparent"):
|
|
||||||
"""Serve the Todo widget HTML page."""
|
|
||||||
expected_key = os.environ.get("GLANCE_HELPER_KEY", "")
|
|
||||||
if key != expected_key:
|
|
||||||
return Response(content="Unauthorized", status_code=401)
|
|
||||||
|
|
||||||
safe_user = re.sub(r'[^a-zA-Z0-9_-]', '', user) or "default"
|
|
||||||
safe_accent = re.sub(r'[^a-fA-F0-9]', '', accent)[:6] or "5ac8d8"
|
|
||||||
# Handle transparent background
|
|
||||||
if bgcolor == "transparent" or not bgcolor:
|
|
||||||
bg_style = "transparent"
|
|
||||||
else:
|
|
||||||
safe_bgcolor = re.sub(r'[^a-fA-F0-9]', '', bgcolor)[:6]
|
|
||||||
bg_style = f"#{safe_bgcolor}" if safe_bgcolor else "transparent"
|
|
||||||
|
|
||||||
sections = load_userdata(safe_user)
|
|
||||||
todos = sections.get("Todo", [])
|
|
||||||
|
|
||||||
# Build todo items HTML
|
|
||||||
todo_html = ""
|
|
||||||
for i, item in enumerate(todos):
|
|
||||||
checked = "checked" if item.get("done") else ""
|
|
||||||
text_escaped = item.get("text", "").replace("&", "&").replace("<", "<").replace(">", ">").replace('"', """)
|
|
||||||
done_class = "done" if item.get("done") else ""
|
|
||||||
todo_html += f'''<div class="todo-item {done_class}" data-index="{i}">
|
|
||||||
<input type="checkbox" class="todo-check" {checked} onchange="toggleTodo({i})">
|
|
||||||
<span class="todo-text">{text_escaped}</span>
|
|
||||||
<button class="todo-delete" onclick="deleteTodo({i})" title="Delete">🗑</button>
|
|
||||||
</div>'''
|
|
||||||
|
|
||||||
html = f"""<!DOCTYPE html>
|
|
||||||
<html>
|
|
||||||
<head>
|
|
||||||
<meta charset="UTF-8">
|
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
||||||
<style>
|
|
||||||
:root {{
|
|
||||||
--accent: #{safe_accent};
|
|
||||||
--accent-20: #{safe_accent}33;
|
|
||||||
--accent-40: #{safe_accent}66;
|
|
||||||
--accent-60: #{safe_accent}99;
|
|
||||||
}}
|
|
||||||
* {{ margin: 0; padding: 0; box-sizing: border-box; }}
|
|
||||||
html, body {{ background: {bg_style}; height: 100%; }}
|
|
||||||
body {{ font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif; color: rgba(255,255,255,0.9); }}
|
|
||||||
.container {{ display: flex; flex-direction: column; height: 100%; padding: 8px; gap: 8px; }}
|
|
||||||
.todo-list {{ flex: 1; overflow-y: auto; display: flex; flex-direction: column; gap: 4px; }}
|
|
||||||
.todo-item {{
|
|
||||||
display: flex; align-items: center; gap: 8px; padding: 8px 10px;
|
|
||||||
background: rgba(255,255,255,0.04); border-radius: 6px;
|
|
||||||
transition: background 0.15s;
|
|
||||||
}}
|
|
||||||
.todo-item:hover {{ background: rgba(255,255,255,0.08); }}
|
|
||||||
.todo-item:hover .todo-delete {{ opacity: 1; }}
|
|
||||||
.todo-item.done .todo-text {{ opacity: 0.5; text-decoration: line-through; }}
|
|
||||||
.todo-check {{
|
|
||||||
width: 18px; height: 18px; cursor: pointer; accent-color: var(--accent);
|
|
||||||
flex-shrink: 0;
|
|
||||||
}}
|
|
||||||
.todo-text {{ flex: 1; font-size: 14px; line-height: 1.4; word-break: break-word; color: rgba(255,255,255,0.85); }}
|
|
||||||
.todo-delete {{
|
|
||||||
opacity: 0; background: none; border: none; cursor: pointer;
|
|
||||||
font-size: 14px; padding: 4px; transition: opacity 0.15s;
|
|
||||||
}}
|
|
||||||
.todo-delete:hover {{ transform: scale(1.1); }}
|
|
||||||
.add-form {{ display: flex; gap: 8px; }}
|
|
||||||
.add-input {{
|
|
||||||
flex: 1; padding: 10px 12px; border: 1px solid var(--accent-20);
|
|
||||||
background: rgba(255,255,255,0.06); border-radius: 6px;
|
|
||||||
color: rgba(255,255,255,0.9); font-size: 14px; outline: none;
|
|
||||||
}}
|
|
||||||
.add-input:focus {{ border-color: var(--accent-40); background: rgba(255,255,255,0.1); }}
|
|
||||||
.add-input::placeholder {{ color: rgba(255,255,255,0.4); }}
|
|
||||||
.add-btn {{
|
|
||||||
padding: 10px 16px; background: var(--accent-20); border: none;
|
|
||||||
border-radius: 6px; color: var(--accent); font-size: 14px; cursor: pointer;
|
|
||||||
transition: background 0.15s;
|
|
||||||
}}
|
|
||||||
.add-btn:hover {{ background: var(--accent-40); }}
|
|
||||||
.status {{ font-size: 11px; text-align: center; min-height: 16px; opacity: 0.6; }}
|
|
||||||
.status.error {{ color: #f87171; opacity: 1; }}
|
|
||||||
.todo-list::-webkit-scrollbar {{ width: 6px; }}
|
|
||||||
.todo-list::-webkit-scrollbar-track {{ background: transparent; }}
|
|
||||||
.todo-list::-webkit-scrollbar-thumb {{ background: var(--accent-40); border-radius: 3px; }}
|
|
||||||
</style>
|
|
||||||
</head>
|
|
||||||
<body>
|
|
||||||
<div class="container">
|
|
||||||
<div class="add-form">
|
|
||||||
<input type="text" class="add-input" id="newTodo" placeholder="+ Add a task" onkeypress="if(event.key==='Enter')addTodo()">
|
|
||||||
<button class="add-btn" onclick="addTodo()">Add</button>
|
|
||||||
</div>
|
|
||||||
<div class="todo-list" id="todoList">{todo_html if todo_html else '<div style="opacity:0.5;text-align:center;padding:20px;">No tasks yet</div>'}</div>
|
|
||||||
<div class="status" id="status"></div>
|
|
||||||
</div>
|
|
||||||
<script>
|
|
||||||
const apiKey = '{expected_key}';
|
|
||||||
const user = '{safe_user}';
|
|
||||||
const baseUrl = '/userdata/todo';
|
|
||||||
|
|
||||||
function showStatus(msg, isError) {{
|
|
||||||
const s = document.getElementById('status');
|
|
||||||
s.textContent = msg;
|
|
||||||
s.className = 'status' + (isError ? ' error' : '');
|
|
||||||
if (!isError) setTimeout(() => s.textContent = '', 2000);
|
|
||||||
}}
|
|
||||||
|
|
||||||
async function addTodo() {{
|
|
||||||
const input = document.getElementById('newTodo');
|
|
||||||
const text = input.value.trim();
|
|
||||||
if (!text) return;
|
|
||||||
try {{
|
|
||||||
const r = await fetch(baseUrl + '/add?key=' + apiKey + '&user=' + user, {{
|
|
||||||
method: 'POST',
|
|
||||||
headers: {{ 'Content-Type': 'application/json' }},
|
|
||||||
body: JSON.stringify({{ text: text }})
|
|
||||||
}});
|
|
||||||
if (r.ok) {{ input.value = ''; location.reload(); }}
|
|
||||||
else showStatus('Failed to add', true);
|
|
||||||
}} catch(e) {{ showStatus('Error: ' + e.message, true); }}
|
|
||||||
}}
|
|
||||||
|
|
||||||
async function toggleTodo(index) {{
|
|
||||||
try {{
|
|
||||||
const r = await fetch(baseUrl + '/toggle?key=' + apiKey + '&user=' + user + '&index=' + index, {{ method: 'POST' }});
|
|
||||||
if (!r.ok) showStatus('Failed to update', true);
|
|
||||||
}} catch(e) {{ showStatus('Error: ' + e.message, true); }}
|
|
||||||
}}
|
|
||||||
|
|
||||||
async function deleteTodo(index) {{
|
|
||||||
try {{
|
|
||||||
const r = await fetch(baseUrl + '/delete?key=' + apiKey + '&user=' + user + '&index=' + index, {{ method: 'POST' }});
|
|
||||||
if (r.ok) location.reload();
|
|
||||||
else showStatus('Failed to delete', true);
|
|
||||||
}} catch(e) {{ showStatus('Error: ' + e.message, true); }}
|
|
||||||
}}
|
|
||||||
</script>
|
|
||||||
</body>
|
|
||||||
</html>"""
|
|
||||||
return Response(content=html, media_type="text/html")
|
|
||||||
|
|
||||||
@APP.post("/userdata/todo/add")
|
|
||||||
async def todo_add(key: str = "", user: str = "default", content: dict = None):
|
|
||||||
expected_key = os.environ.get("GLANCE_HELPER_KEY", "")
|
|
||||||
if key != expected_key:
|
|
||||||
return Response(content="Unauthorized", status_code=401)
|
|
||||||
safe_user = re.sub(r'[^a-zA-Z0-9_-]', '', user) or "default"
|
|
||||||
if content and content.get("text"):
|
|
||||||
sections = load_userdata(safe_user)
|
|
||||||
sections["Todo"].append({"text": content["text"].strip(), "done": False})
|
|
||||||
if save_userdata(safe_user, sections):
|
|
||||||
return {"status": "ok"}
|
|
||||||
return Response(content="Failed", status_code=500)
|
|
||||||
|
|
||||||
@APP.post("/userdata/todo/toggle")
|
|
||||||
async def todo_toggle(key: str = "", user: str = "default", index: int = 0):
|
|
||||||
expected_key = os.environ.get("GLANCE_HELPER_KEY", "")
|
|
||||||
if key != expected_key:
|
|
||||||
return Response(content="Unauthorized", status_code=401)
|
|
||||||
safe_user = re.sub(r'[^a-zA-Z0-9_-]', '', user) or "default"
|
|
||||||
sections = load_userdata(safe_user)
|
|
||||||
if 0 <= index < len(sections["Todo"]):
|
|
||||||
sections["Todo"][index]["done"] = not sections["Todo"][index]["done"]
|
|
||||||
if save_userdata(safe_user, sections):
|
|
||||||
return {"status": "ok"}
|
|
||||||
return Response(content="Failed", status_code=500)
|
|
||||||
|
|
||||||
@APP.post("/userdata/todo/delete")
|
|
||||||
async def todo_delete(key: str = "", user: str = "default", index: int = 0):
|
|
||||||
expected_key = os.environ.get("GLANCE_HELPER_KEY", "")
|
|
||||||
if key != expected_key:
|
|
||||||
return Response(content="Unauthorized", status_code=401)
|
|
||||||
safe_user = re.sub(r'[^a-zA-Z0-9_-]', '', user) or "default"
|
|
||||||
sections = load_userdata(safe_user)
|
|
||||||
if 0 <= index < len(sections["Todo"]):
|
|
||||||
sections["Todo"].pop(index)
|
|
||||||
if save_userdata(safe_user, sections):
|
|
||||||
return {"status": "ok"}
|
|
||||||
return Response(content="Failed", status_code=500)
|
|
||||||
|
|
||||||
# --------------------------------
|
|
||||||
# Motivation Widget
|
|
||||||
# --------------------------------
|
|
||||||
import random as _random
|
|
||||||
|
|
||||||
@APP.get("/userdata/motivation")
|
|
||||||
def motivation_widget(key: str = "", user: str = "default", accent: str = "5ac8d8", bgcolor: str = "transparent"):
|
|
||||||
"""Serve the Motivation widget HTML page with random quote."""
|
|
||||||
expected_key = os.environ.get("GLANCE_HELPER_KEY", "")
|
|
||||||
if key != expected_key:
|
|
||||||
return Response(content="Unauthorized", status_code=401)
|
|
||||||
|
|
||||||
safe_user = re.sub(r'[^a-zA-Z0-9_-]', '', user) or "default"
|
|
||||||
safe_accent = re.sub(r'[^a-fA-F0-9]', '', accent)[:6] or "5ac8d8"
|
|
||||||
if bgcolor == "transparent" or not bgcolor:
|
|
||||||
bg_style = "transparent"
|
|
||||||
else:
|
|
||||||
safe_bgcolor = re.sub(r'[^a-fA-F0-9]', '', bgcolor)[:6]
|
|
||||||
bg_style = f"#{safe_bgcolor}" if safe_bgcolor else "transparent"
|
|
||||||
|
|
||||||
sections = load_userdata(safe_user)
|
|
||||||
quotes = sections.get("Motivation", [])
|
|
||||||
|
|
||||||
# Pick random quote
|
|
||||||
current_quote = _random.choice(quotes) if quotes else "Add your first motivational quote!"
|
|
||||||
current_quote_escaped = current_quote.replace("&", "&").replace("<", "<").replace(">", ">").replace('"', """)
|
|
||||||
|
|
||||||
# Build manage URL (opens in new tab)
|
|
||||||
manage_url = f"/userdata/motivation/manage?key={expected_key}&user={safe_user}&accent={safe_accent}"
|
|
||||||
|
|
||||||
html = f"""<!DOCTYPE html>
|
|
||||||
<html>
|
|
||||||
<head>
|
|
||||||
<meta charset="UTF-8">
|
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
||||||
<style>
|
|
||||||
:root {{
|
|
||||||
--accent: #{safe_accent};
|
|
||||||
}}
|
|
||||||
* {{ margin: 0; padding: 0; box-sizing: border-box; }}
|
|
||||||
html, body {{ background: {bg_style}; height: 100%; }}
|
|
||||||
body {{ font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif; color: rgba(255,255,255,0.85); }}
|
|
||||||
.container {{ display: flex; flex-direction: column; height: 100%; padding: 8px 12px; position: relative; }}
|
|
||||||
.settings-btn {{
|
|
||||||
position: absolute; top: 4px; right: 4px;
|
|
||||||
background: none; border: none; cursor: pointer;
|
|
||||||
font-size: 14px; opacity: 0.3; transition: opacity 0.15s;
|
|
||||||
text-decoration: none; color: inherit;
|
|
||||||
}}
|
|
||||||
.settings-btn:hover {{ opacity: 0.7; }}
|
|
||||||
.quote-display {{
|
|
||||||
flex: 1; display: flex; align-items: center; justify-content: center;
|
|
||||||
text-align: center; padding: 4px 24px;
|
|
||||||
}}
|
|
||||||
.quote-text-main {{
|
|
||||||
font-size: 14px; line-height: 1.5; font-style: italic;
|
|
||||||
color: rgba(255,255,255,0.8);
|
|
||||||
}}
|
|
||||||
.quote-footer {{
|
|
||||||
font-size: 11px; opacity: 0.4; text-align: center; padding-top: 4px;
|
|
||||||
}}
|
|
||||||
</style>
|
|
||||||
</head>
|
|
||||||
<body>
|
|
||||||
<div class="container">
|
|
||||||
<a class="settings-btn" href="{manage_url}" target="_blank" title="Manage quotes">⚙️</a>
|
|
||||||
<div class="quote-display">
|
|
||||||
<div class="quote-text-main">"{current_quote_escaped}"</div>
|
|
||||||
</div>
|
|
||||||
<div class="quote-footer">{len(quotes)} quote{'s' if len(quotes) != 1 else ''}</div>
|
|
||||||
</div>
|
|
||||||
</body>
|
|
||||||
</html>"""
|
|
||||||
return Response(content=html, media_type="text/html")
|
|
||||||
|
|
||||||
@APP.get("/userdata/motivation/manage")
|
|
||||||
def motivation_manage(key: str = "", user: str = "default", accent: str = "5ac8d8"):
|
|
||||||
"""Full-page management interface for motivation quotes."""
|
|
||||||
expected_key = os.environ.get("GLANCE_HELPER_KEY", "")
|
|
||||||
if key != expected_key:
|
|
||||||
return Response(content="Unauthorized", status_code=401)
|
|
||||||
|
|
||||||
safe_user = re.sub(r'[^a-zA-Z0-9_-]', '', user) or "default"
|
|
||||||
safe_accent = re.sub(r'[^a-fA-F0-9]', '', accent)[:6] or "5ac8d8"
|
|
||||||
|
|
||||||
sections = load_userdata(safe_user)
|
|
||||||
quotes = sections.get("Motivation", [])
|
|
||||||
|
|
||||||
# Build quotes list HTML
|
|
||||||
quotes_html = ""
|
|
||||||
for i, q in enumerate(quotes):
|
|
||||||
q_escaped = q.replace("&", "&").replace("<", "<").replace(">", ">").replace('"', """)
|
|
||||||
quotes_html += f'''<div class="quote-item" data-index="{i}">
|
|
||||||
<span class="quote-num">{i+1}.</span>
|
|
||||||
<span class="quote-text">{q_escaped}</span>
|
|
||||||
<button class="quote-delete" onclick="deleteQuote({i})" title="Delete">×</button>
|
|
||||||
</div>'''
|
|
||||||
|
|
||||||
html = f"""<!DOCTYPE html>
|
|
||||||
<html>
|
|
||||||
<head>
|
|
||||||
<meta charset="UTF-8">
|
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
||||||
<title>Manage Motivation Quotes</title>
|
|
||||||
<style>
|
|
||||||
:root {{
|
|
||||||
--accent: #{safe_accent};
|
|
||||||
--accent-20: #{safe_accent}33;
|
|
||||||
--accent-40: #{safe_accent}66;
|
|
||||||
}}
|
|
||||||
* {{ margin: 0; padding: 0; box-sizing: border-box; }}
|
|
||||||
html, body {{ background: #0d1117; min-height: 100vh; }}
|
|
||||||
body {{ font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif; color: rgba(255,255,255,0.9); }}
|
|
||||||
.container {{ max-width: 600px; margin: 0 auto; padding: 24px 16px; }}
|
|
||||||
.header {{ margin-bottom: 24px; }}
|
|
||||||
.header h1 {{ font-size: 20px; font-weight: 600; margin-bottom: 4px; color: var(--accent); }}
|
|
||||||
.header p {{ font-size: 13px; opacity: 0.6; }}
|
|
||||||
.add-form {{ display: flex; gap: 8px; margin-bottom: 20px; }}
|
|
||||||
.add-input {{
|
|
||||||
flex: 1; padding: 12px 14px; border: 1px solid var(--accent-20);
|
|
||||||
background: rgba(255,255,255,0.06); border-radius: 8px;
|
|
||||||
color: rgba(255,255,255,0.9); font-size: 14px; outline: none;
|
|
||||||
}}
|
|
||||||
.add-input:focus {{ border-color: var(--accent-40); background: rgba(255,255,255,0.1); }}
|
|
||||||
.add-input::placeholder {{ color: rgba(255,255,255,0.4); }}
|
|
||||||
.add-btn {{
|
|
||||||
padding: 12px 20px; background: var(--accent-20); border: none;
|
|
||||||
border-radius: 8px; color: var(--accent); font-size: 14px; cursor: pointer;
|
|
||||||
transition: background 0.15s; white-space: nowrap;
|
|
||||||
}}
|
|
||||||
.add-btn:hover {{ background: var(--accent-40); }}
|
|
||||||
.quotes-list {{ display: flex; flex-direction: column; gap: 8px; }}
|
|
||||||
.quote-item {{
|
|
||||||
display: flex; align-items: flex-start; gap: 10px; padding: 12px 14px;
|
|
||||||
background: rgba(255,255,255,0.04); border-radius: 8px;
|
|
||||||
transition: background 0.15s;
|
|
||||||
}}
|
|
||||||
.quote-item:hover {{ background: rgba(255,255,255,0.08); }}
|
|
||||||
.quote-item:hover .quote-delete {{ opacity: 1; }}
|
|
||||||
.quote-num {{ font-size: 12px; opacity: 0.4; min-width: 24px; padding-top: 2px; }}
|
|
||||||
.quote-text {{ flex: 1; font-size: 14px; line-height: 1.5; word-break: break-word; }}
|
|
||||||
.quote-delete {{
|
|
||||||
opacity: 0; background: none; border: none; cursor: pointer;
|
|
||||||
font-size: 20px; color: rgba(255,255,255,0.4); padding: 0 4px;
|
|
||||||
transition: opacity 0.15s, color 0.15s;
|
|
||||||
}}
|
|
||||||
.quote-delete:hover {{ color: #f87171; }}
|
|
||||||
.empty-state {{ text-align: center; padding: 40px 20px; opacity: 0.5; font-size: 14px; }}
|
|
||||||
.status {{ font-size: 12px; text-align: center; padding: 8px; opacity: 0.7; }}
|
|
||||||
.status.error {{ color: #f87171; opacity: 1; }}
|
|
||||||
.status.success {{ color: #4ade80; opacity: 1; }}
|
|
||||||
.close-hint {{ text-align: center; margin-top: 24px; font-size: 12px; opacity: 0.4; }}
|
|
||||||
</style>
|
|
||||||
</head>
|
|
||||||
<body>
|
|
||||||
<div class="container">
|
|
||||||
<div class="header">
|
|
||||||
<h1>Manage Motivation Quotes</h1>
|
|
||||||
<p>User: {safe_user} • {len(quotes)} quote{'s' if len(quotes) != 1 else ''}</p>
|
|
||||||
</div>
|
|
||||||
<div class="add-form">
|
|
||||||
<input type="text" class="add-input" id="newQuote" placeholder="Add a new motivational quote..." onkeypress="if(event.key==='Enter')addQuote()">
|
|
||||||
<button class="add-btn" onclick="addQuote()">Add Quote</button>
|
|
||||||
</div>
|
|
||||||
<div class="status" id="status"></div>
|
|
||||||
<div class="quotes-list" id="quotesList">
|
|
||||||
{quotes_html if quotes_html else '<div class="empty-state">No quotes yet. Add your first motivational quote above!</div>'}
|
|
||||||
</div>
|
|
||||||
<div class="close-hint">Close this tab when done. Refresh your dashboard to see changes.</div>
|
|
||||||
</div>
|
|
||||||
<script>
|
|
||||||
const apiKey = '{expected_key}';
|
|
||||||
const user = '{safe_user}';
|
|
||||||
const baseUrl = '/userdata/motivation';
|
|
||||||
|
|
||||||
function showStatus(msg, type) {{
|
|
||||||
const s = document.getElementById('status');
|
|
||||||
s.textContent = msg;
|
|
||||||
s.className = 'status ' + (type || '');
|
|
||||||
if (type !== 'error') setTimeout(() => {{ s.textContent = ''; s.className = 'status'; }}, 2000);
|
|
||||||
}}
|
|
||||||
|
|
||||||
async function addQuote() {{
|
|
||||||
const input = document.getElementById('newQuote');
|
|
||||||
const text = input.value.trim();
|
|
||||||
if (!text) return;
|
|
||||||
try {{
|
|
||||||
const r = await fetch(baseUrl + '/add?key=' + apiKey + '&user=' + user, {{
|
|
||||||
method: 'POST',
|
|
||||||
headers: {{ 'Content-Type': 'application/json' }},
|
|
||||||
body: JSON.stringify({{ text: text }})
|
|
||||||
}});
|
|
||||||
if (r.ok) {{
|
|
||||||
input.value = '';
|
|
||||||
showStatus('Quote added!', 'success');
|
|
||||||
setTimeout(() => location.reload(), 500);
|
|
||||||
}} else {{
|
|
||||||
showStatus('Failed to add quote', 'error');
|
|
||||||
}}
|
|
||||||
}} catch(e) {{ showStatus('Error: ' + e.message, 'error'); }}
|
|
||||||
}}
|
|
||||||
|
|
||||||
async function deleteQuote(index) {{
|
|
||||||
if (!confirm('Delete this quote?')) return;
|
|
||||||
try {{
|
|
||||||
const r = await fetch(baseUrl + '/delete?key=' + apiKey + '&user=' + user + '&index=' + index, {{ method: 'POST' }});
|
|
||||||
if (r.ok) {{
|
|
||||||
showStatus('Quote deleted', 'success');
|
|
||||||
setTimeout(() => location.reload(), 500);
|
|
||||||
}} else {{
|
|
||||||
showStatus('Failed to delete', 'error');
|
|
||||||
}}
|
|
||||||
}} catch(e) {{ showStatus('Error: ' + e.message, 'error'); }}
|
|
||||||
}}
|
|
||||||
</script>
|
|
||||||
</body>
|
|
||||||
</html>"""
|
|
||||||
return Response(content=html, media_type="text/html")
|
|
||||||
|
|
||||||
@APP.post("/userdata/motivation/add")
|
|
||||||
async def motivation_add(key: str = "", user: str = "default", content: dict = None):
|
|
||||||
expected_key = os.environ.get("GLANCE_HELPER_KEY", "")
|
|
||||||
if key != expected_key:
|
|
||||||
return Response(content="Unauthorized", status_code=401)
|
|
||||||
safe_user = re.sub(r'[^a-zA-Z0-9_-]', '', user) or "default"
|
|
||||||
if content and content.get("text"):
|
|
||||||
sections = load_userdata(safe_user)
|
|
||||||
sections["Motivation"].append(content["text"].strip())
|
|
||||||
if save_userdata(safe_user, sections):
|
|
||||||
return {"status": "ok"}
|
|
||||||
return Response(content="Failed", status_code=500)
|
|
||||||
|
|
||||||
@APP.post("/userdata/motivation/delete")
|
|
||||||
async def motivation_delete(key: str = "", user: str = "default", index: int = 0):
|
|
||||||
expected_key = os.environ.get("GLANCE_HELPER_KEY", "")
|
|
||||||
if key != expected_key:
|
|
||||||
return Response(content="Unauthorized", status_code=401)
|
|
||||||
safe_user = re.sub(r'[^a-zA-Z0-9_-]', '', user) or "default"
|
|
||||||
sections = load_userdata(safe_user)
|
|
||||||
if 0 <= index < len(sections["Motivation"]):
|
|
||||||
sections["Motivation"].pop(index)
|
|
||||||
if save_userdata(safe_user, sections):
|
|
||||||
return {"status": "ok"}
|
|
||||||
return Response(content="Failed", status_code=500)
|
|
||||||
|
|
||||||
@APP.get("/userdata/motivation/random")
|
|
||||||
def motivation_random(key: str = "", user: str = "default"):
|
|
||||||
"""Get a random motivation quote as JSON."""
|
|
||||||
expected_key = os.environ.get("GLANCE_HELPER_KEY", "")
|
|
||||||
if key != expected_key:
|
|
||||||
return Response(content="Unauthorized", status_code=401)
|
|
||||||
safe_user = re.sub(r'[^a-zA-Z0-9_-]', '', user) or "default"
|
|
||||||
sections = load_userdata(safe_user)
|
|
||||||
quotes = sections.get("Motivation", [])
|
|
||||||
if quotes:
|
|
||||||
return {"quote": _random.choice(quotes), "total": len(quotes)}
|
|
||||||
return {"quote": "", "total": 0}
|
|
||||||
|
|
||||||
# ================================
|
# ================================
|
||||||
# Időkép configuration
|
# Időkép configuration
|
||||||
# ================================
|
# ================================
|
||||||
|
|||||||
@@ -764,11 +764,7 @@ data:
|
|||||||
- type: calendar
|
- type: calendar
|
||||||
first-day-of-week: monday
|
first-day-of-week: monday
|
||||||
# Tasks (persistent, file-based)
|
# Tasks (persistent, file-based)
|
||||||
- type: iframe
|
- type: to-do
|
||||||
css-class: iframe-no-tint
|
|
||||||
source: https://glance-helper.dooplex.hu/userdata/todo?key=oplQqnLnJK2vErRVYJpvVUcSDBOSbCHZSbsYY2bwSifgTMfT&user=kisfenyo&accent=5ac8d8&bgcolor=0f1c24
|
|
||||||
height: 200
|
|
||||||
title: Tasks
|
|
||||||
# Outline Notes iframe
|
# Outline Notes iframe
|
||||||
- type: iframe
|
- type: iframe
|
||||||
source: https://outline.dooplex.hu/collection/dooplex-server-iTAZn04AaR/recent
|
source: https://outline.dooplex.hu/collection/dooplex-server-iTAZn04AaR/recent
|
||||||
@@ -852,12 +848,6 @@ data:
|
|||||||
</div>
|
</div>
|
||||||
{{ end }}
|
{{ end }}
|
||||||
</div>
|
</div>
|
||||||
# Motivation Quote
|
|
||||||
- type: iframe
|
|
||||||
css-class: iframe-no-tint
|
|
||||||
source: https://glance-helper.dooplex.hu/userdata/motivation?key=oplQqnLnJK2vErRVYJpvVUcSDBOSbCHZSbsYY2bwSifgTMfT&user=kisfenyo&accent=5ac8d8&bgcolor=0f1c24
|
|
||||||
height: 80
|
|
||||||
title: Motivation
|
|
||||||
|
|
||||||
# Calendar Events Widget (Családi only)
|
# Calendar Events Widget (Családi only)
|
||||||
- type: custom-api
|
- type: custom-api
|
||||||
|
|||||||
Reference in New Issue
Block a user