|
|
|
import os
|
|
|
|
from datetime import datetime
|
|
|
|
from zoneinfo import ZoneInfo
|
|
|
|
|
|
|
|
import markdown
|
|
|
|
from app import db
|
|
|
|
from models.postmodel import Post
|
|
|
|
from sqlalchemy.exc import (DatabaseError, DataError, IntegrityError,
|
|
|
|
InterfaceError, InvalidRequestError)
|
|
|
|
|
|
|
|
|
|
|
|
def schedule_post(request):
|
|
|
|
print("Posted file: {}".format(request.files["file"]))
|
|
|
|
print(request.headers)
|
|
|
|
if "file" in request.files:
|
|
|
|
print("file found")
|
|
|
|
return _schedule_file(request)
|
|
|
|
if request.form["text"]:
|
|
|
|
print("text found")
|
|
|
|
return _schedule_text(request)
|
|
|
|
|
|
|
|
return "no correct header found"
|
|
|
|
|
|
|
|
|
|
|
|
def _schedule_file(request):
|
|
|
|
file = request.files["file"]
|
|
|
|
file_str_contents = file.read().decode("utf-8")
|
|
|
|
html = markdown.markdown(file_str_contents)
|
|
|
|
_save_to_database(html)
|
|
|
|
return "file post scheduled"
|
|
|
|
|
|
|
|
|
|
|
|
def _schedule_text(request):
|
|
|
|
print(request.form["text"])
|
|
|
|
text = request.form["text"]
|
|
|
|
content = f"<p>{text}</p>"
|
|
|
|
_save_to_database(content)
|
|
|
|
return "text post scheduled"
|
|
|
|
|
|
|
|
|
|
|
|
def _save_to_database(content):
|
|
|
|
try:
|
|
|
|
zoneinfo = ZoneInfo("Europe/Berlin")
|
|
|
|
post = Post(
|
|
|
|
content=content,
|
|
|
|
last_updated=datetime.now().astimezone(tz=zoneinfo),
|
|
|
|
scheduled_time=datetime.now().astimezone(tz=zoneinfo),
|
|
|
|
)
|
|
|
|
db.session.add(post)
|
|
|
|
db.session.commit()
|
|
|
|
|
|
|
|
except (
|
|
|
|
IntegrityError,
|
|
|
|
InvalidRequestError,
|
|
|
|
DataError,
|
|
|
|
InterfaceError,
|
|
|
|
DatabaseError,
|
|
|
|
):
|
|
|
|
db.session.rollback()
|