diff --git a/app/routes.py b/app/routes.py index 9ffbaa8..24b9541 100644 --- a/app/routes.py +++ b/app/routes.py @@ -1,8 +1,9 @@ from datetime import datetime, UTC from math import ceil +import json from bson import ObjectId -from flask import Blueprint, render_template, request, redirect, url_for, flash +from flask import Blueprint, render_template, request, redirect, url_for, flash, jsonify from app.db import snippets_collection from app.forms import SnippetForm @@ -136,3 +137,25 @@ def edit_snippet(id): form.tags.data = ", ".join(snippet.get("tags", [])) return render_template("edit.html", form=form, snippet=snippet) + + +@main.route("/export") +def export_snippets(): + """Export all snippets as downloadable JSON file""" + # Get all snippets from database + snippets = list(snippets_collection.find()) + + # Convert ObjectId to string for JSON compatibility + for snippet in snippets: + snippet["_id"] = str(snippet["_id"]) + # Convert datetime objects to ISO format strings + if "created_at" in snippet: + snippet["created_at"] = snippet["created_at"].isoformat() + if "updated_at" in snippet: + snippet["updated_at"] = snippet["updated_at"].isoformat() + + # Return JSON with download headers + return jsonify(snippets), 200, { + "Content-Disposition": "attachment; filename=stash_snip_backup.json", + "Content-Type": "application/json" + } diff --git a/app/static/style.css b/app/static/style.css index 6d75d30..809b879 100644 --- a/app/static/style.css +++ b/app/static/style.css @@ -501,3 +501,34 @@ main { max-width: 1000px; margin: 0 auto; padding: 2rem 1.5rem; } border-color: var(--amber); color: var(--amber); } + +.export-section { + margin: 20px 0; + display: flex; + justify-content: flex-start; +} + +.export-btn { + display: inline-flex; + align-items: center; + gap: 8px; + padding: 10px 16px; + background-color: #4CAF50; + color: white; + text-decoration: none; + border-radius: 4px; + font-size: 14px; + font-weight: 500; + transition: background-color 0.3s ease; + border: none; + cursor: pointer; +} + +.export-btn:hover { + background-color: #45a049; +} + +.export-btn svg { + width: 16px; + height: 16px; +} diff --git a/app/templates/index.html b/app/templates/index.html index 92146b6..efeb2dd 100644 --- a/app/templates/index.html +++ b/app/templates/index.html @@ -6,6 +6,17 @@
{{ total_snippets }} snippet{{ 's' if total_snippets != 1 else '' }} found
+ +