Skip to content

Commit

Permalink
Add html export option. Closes #11
Browse files Browse the repository at this point in the history
  • Loading branch information
Mozzo1000 committed Dec 26, 2024
1 parent caedd49 commit f6e68ff
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 0 deletions.
3 changes: 3 additions & 0 deletions web/src/components/Data/RequestData.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ function RequestData() {
taskType = "csv_export"
}else if (dataFormat == "json") {
taskType = "json_export"
}else if (dataFormat == "html") {
taskType = "html_export"
}
TasksService.create(taskType, {}).then(
response => {
Expand Down Expand Up @@ -44,6 +46,7 @@ function RequestData() {
<Select id="data-format" required value={dataFormat} onChange={(e) => setDataFormat(e.target.value)}>
<option value="csv">CSV</option>
<option value="json">JSON</option>
<option value="html">HTML</option>
</Select>
</div>
<Button onClick={() => requestData()}>Request data</Button>
Expand Down
3 changes: 3 additions & 0 deletions workers/manage.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

from src.export_csv import CSVWorker
from src.export_json import JSONWorker
from src.export_html import HTMLWorker

def main():
load_dotenv()
Expand Down Expand Up @@ -72,6 +73,8 @@ def process_task(data, cursor):
CSVWorker(cursor).pickup_task(data["id"], data)
elif data["type"] == "json_export":
JSONWorker(cursor).pickup_task(data["id"], data)
elif data["type"] == "html_export":
HTMLWorker(cursor).pickup_task(data["id"], data)

if __name__ == '__main__':
main()
43 changes: 43 additions & 0 deletions workers/src/export_html.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import time
from datetime import datetime
from dotenv import load_dotenv
import os
import string
import random
import json
from jinja2 import Environment, FileSystemLoader

load_dotenv()

if not os.path.exists(os.getenv("EXPORT_FOLDER")):
os.makedirs(os.getenv("EXPORT_FOLDER"))

class HTMLWorker:
def __init__(self, cursor):
self.WORKER_ID = "html_export:97305e39-86fb-4433-8220-4a756a33b4e7"
self.cursor = cursor

self.env = Environment(loader=FileSystemLoader('src/html_templates'))


def pickup_task(self, id, data):
self.cursor.execute(f"UPDATE tasks SET status='started', worker='{self.WORKER_ID}', updated_on='{time.strftime('%Y-%m-%d %H:%M:%S')}' WHERE id={id}")

self.create_html(data["created_by"])
self.cursor.execute(f"UPDATE tasks SET status='success', updated_on='{time.strftime('%Y-%m-%d %H:%M:%S')}' WHERE id={id}")


def create_html(self, owner_id):
self.cursor.execute(f"SELECT title, isbn, description, reading_status, current_page, total_pages, author, rating FROM books WHERE owner_id={owner_id}")
rows = self.cursor.fetchall()

random_string = "".join(random.choice(string.ascii_letters + string.digits) for _ in range(8))
filename = f"export_{datetime.now().strftime("%y%m%d")}_{random_string}.html"

template = self.env.get_template("all_books.html")
output = template.render(data=rows)

with open(os.path.join(os.getenv("EXPORT_FOLDER"), filename), "w") as f:
f.write(output)

self.cursor.execute("INSERT INTO files (filename, owner_id) VALUES (%s, %s)", (filename, owner_id))
51 changes: 51 additions & 0 deletions workers/src/html_templates/all_books.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<!DOCTYPE html>
<html lang="en">
<head>
<title>Exported books</title>
<style>
table {
font-family: arial, sans-serif;
border-collapse: collapse;
width: 100%;
}

td, th {
border: 1px solid #dddddd;
text-align: left;
padding: 8px;
}

tr:nth-child(even) {
background-color: #dddddd;
}
</style>

</head>
<body>
<h1>All books ({{ data|length }})</h1>
<table>
<tr>
<th>Title</th>
<th>Author</th>
<th>Description</th>
<th>ISBN</th>
<th>Reading status</th>
<th>Total pages</th>
<th>Current page</th>
<th>Rating</th>
</tr>
{% for item in data %}
<tr>
<td>{{ item.title }}</td>
<td>{{ item.author }}</td>
<td>{{ item.description }}</td>
<td>{{ item.isbn }}</td>
<td>{{ item.reading_status }}</td>
<td>{{ item.total_pages }}</td>
<td>{{ item.current_page }}</td>
<td>{{ item.rating }}</td>
</tr>
{% endfor %}
</table>
</body>
</html>

0 comments on commit f6e68ff

Please sign in to comment.