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
48 changes: 47 additions & 1 deletion main.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import functools
from flask import Flask, jsonify, request, abort
from flask import Flask, jsonify, request, abort,Response, make_response, render_template_string
from sqlalchemy import func, or_
from werkzeug.exceptions import HTTPException
import csv
from io import StringIO

from weasyprint import HTML

app = Flask(__name__)
app.config.from_object("config.Config")
Expand Down Expand Up @@ -120,5 +124,47 @@ def api_hadiths_random():
return Hadith.query.filter_by(collection="riyadussalihin").order_by(func.rand())



#CSV EXPORT
@app.route("/v1/collections/export/csv", methods=["GET"])
def export_collections_csv():
collections = HadithCollection.query.all()
si = StringIO()
writer = csv.writer(si)
writer.writerow(["Collection ID", "Name", "Description"])
for collection in collections:
writer.writerow([collection.collectionID, collection.name, collection.description])

output = make_response(si.getvalue())
output.headers["Content-Disposition"] = "attachment; filename=collections.csv"
output.headers["Content-Type"] = "text/csv"
return output

#PDF export
@app.route("/v1/collections/export/pdf", methods=["GET"])
def export_collections_pdf():
collections = HadithCollection.query.all()
html_content = render_template_string(
"""
<!DOCTYPE html>
<html>
<head><title>Collections</title></head>
<body>
<h1>Hadith Collections</h1>
<ul>
{% for collection in collections %}
<li>{{ collection.name }}: {{ collection.description }}</li>
{% endfor %}
</ul>
</body>
</html>
""", collections=collections
)
pdf = HTML(string=html_content).write_pdf()
response = make_response(pdf)
response.headers["Content-Type"] = "application/pdf"
response.headers["Content-Disposition"] = "inline; filename=collections.pdf"
return response

if __name__ == "__main__":
app.run(host="0.0.0.0")
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,4 @@ virtualenv==20.0.25
Werkzeug==3.0.6
wrapt==1.12.1
zipp==3.19.1
weasyprint == 63.0