-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
100 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |