Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 22 additions & 2 deletions app/routes.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
from flask import Blueprint, render_template, request, redirect, url_for, flash
from flask import Blueprint, render_template, request, redirect, url_for, flash, Response
from app.db import snippets_collection
from app.forms import SnippetForm
from app.models import make_snippet
from bson import ObjectId
from app.utils import parse_tags
from datetime import datetime
import json as _json

main = Blueprint("main", __name__)

Expand Down Expand Up @@ -56,6 +57,25 @@ def add_snippet():
return render_template("add.html", form=form)


@main.route("/export")
def export_snippets():
snapshots = list(snippets_collection.find().sort("created_at", -1))
payload = []
for snip in snapshots:
snip["_id"] = str(snip.get("_id"))
payload.append({
"title": snip.get("title"),
"language": snip.get("language"),
"code": snip.get("code"),
"description": snip.get("description") or "",
"tags": snip.get("tags") or [],
})
filename = f"stashsnip-export-{datetime.utcnow().strftime('%Y%m%d-%H%M%S')}.json"
return Response(
_json.dumps(payload, ensure_ascii=False, indent=2),
mimetype='application/json',
headers={'Content-Disposition': f'attachment; filename="{filename}"'},
)

@main.route("/snippet/<id>")
def view_snippet(id):
Expand Down Expand Up @@ -101,4 +121,4 @@ def edit_snippet(id):
form.description.data = snippet.get("description", "")
form.tags.data = ", ".join(snippet.get("tags", []))

return render_template("edit.html", form=form, snippet=snippet)
return render_template("edit.html", form=form, snippet=snippet)
3 changes: 2 additions & 1 deletion app/templates/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
<nav>
<a href="/" class="logo">Stash<span>Snip</span></a>
<a href="/add" class="btn-add">+ New Snip</a>
<a href="/export" class="btn-add">Export JSON</a>
</nav>

<main>
Expand All @@ -29,4 +30,4 @@
<script src="{{ url_for('static', filename='main.js') }}"></script>
<script>hljs.highlightAll();</script>
</body>
</html>
</html>