backend/app/utils.py
dm1sh 87e5a16a06
Fixed empty cover error
Updated deployment instructions
Switched to alpine python image
2023-09-17 16:40:26 +03:00

59 lines
1.1 KiB
Python

"""
Utils for publite_backend module
"""
import re
from hashlib import sha256
from typing import Optional, Union
from pydantic import BaseModel # pylint: disable=no-name-in-module
DocumentTokens = dict[str, Union[str, dict[str, str]]]
class HTMLBook(BaseModel): # pylint: disable=too-few-public-methods
"""Transitional model for returned book data"""
title: str
author: str
cover: Optional[str] = None
content: str
class HashedHTMLBook(HTMLBook): # pylint: disable=too-few-public-methods
"""Model for returned book data with content hash"""
hash: str
replacements = [
("
", ""),
("", ""),
(r">\s+?<", "><"),
]
def strip_whitespace(string: bytes) -> str:
"""Removes"""
res = string.decode()
for old, new in replacements:
res = re.sub(old, new, res)
return res.strip()
def add_hash(content: HTMLBook) -> HashedHTMLBook:
"""
Adds hash of book content
"""
h_content: HashedHTMLBook = content.copy()
h_content["hash"] = sha256(content["content"].encode()).hexdigest()
return h_content