Browse Source

ability to upload text files

main
crunk 1 year ago
parent
commit
a8462bc720
  1. 1
      .gitignore
  2. 2
      app.py
  3. 36
      scheduler/schedule_text.py

1
.gitignore

@ -11,3 +11,4 @@ test/*
*.db *.db
instance/* instance/*
static/* static/*
tmpupload/*

2
app.py

@ -10,6 +10,8 @@ def create_app():
APP = Flask(__name__) APP = Flask(__name__)
APP.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:///schedule.db" APP.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:///schedule.db"
APP.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False APP.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False
APP.config["MAX_CONTENT_LENGTH"] = 16 * 1000
APP.config["UPLOAD_FOLDER"] = "tmpupload"
db.init_app(APP) db.init_app(APP)
migrate.init_app(APP, db, render_as_batch=True) migrate.init_app(APP, db, render_as_batch=True)

36
scheduler/schedule_text.py

@ -1,6 +1,8 @@
import os
from datetime import datetime from datetime import datetime
from zoneinfo import ZoneInfo from zoneinfo import ZoneInfo
import markdown
from app import db from app import db
from models.postmodel import Post from models.postmodel import Post
from sqlalchemy.exc import (DatabaseError, DataError, IntegrityError, from sqlalchemy.exc import (DatabaseError, DataError, IntegrityError,
@ -8,24 +10,36 @@ from sqlalchemy.exc import (DatabaseError, DataError, IntegrityError,
def schedule_post(request): 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"]: if request.form["text"]:
print("text found")
return _schedule_text(request) return _schedule_text(request)
if request.form["file"]:
return _schedule_file(request)
return "no correct header found" return "no correct header found"
def _schedule_file(request): def _schedule_file(request):
print(request.form["file"]) file = request.files["file"]
return "not yet implemented" 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): def _schedule_text(request):
print(request.form["text"]) 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: try:
text = request.form["text"]
content = f"<p>{text}</p>"
zoneinfo = ZoneInfo("Europe/Berlin") zoneinfo = ZoneInfo("Europe/Berlin")
post = Post( post = Post(
content=content, content=content,
@ -35,7 +49,11 @@ def _schedule_text(request):
db.session.add(post) db.session.add(post)
db.session.commit() db.session.commit()
except DatabaseError: except (
IntegrityError,
InvalidRequestError,
DataError,
InterfaceError,
DatabaseError,
):
db.session.rollback() db.session.rollback()
return "post scheduled"

Loading…
Cancel
Save