diff --git a/.gitignore b/.gitignore index 2b069c9..b4eda55 100644 --- a/.gitignore +++ b/.gitignore @@ -3,8 +3,9 @@ /lib64 /pyvenv.cfg +__pycache__/ + /.vscode/ /docker-compose.yml - -__pycache__/ +/data diff --git a/README.md b/README.md index a7d6605..0947fba 100644 --- a/README.md +++ b/README.md @@ -2,11 +2,17 @@ [![status-badge](https://ci.mylloon.fr/api/badges/71/status.svg)](https://ci.mylloon.fr/repos/71) -Personal diary with txt style, can be private! +personal diary with txt style, can be private! -## Environment Variables +## environment variables -| Name | Description | Required | +| name | description | required | | :-----------: | :-----------: | :------: | -| `TD_USERNAME` | user name | No | -| `TD_USERPASS` | user password | Yes | +| `TD_USERNAME` | user name | no | +| `TD_USERPASS` | user password | yes | + +## volumes (docker) + +| path | description | +| :--------------: | :--------------------: | +| `/txtdiary/data` | where posts are stored | diff --git a/src/app.py b/src/app.py index 5b91943..0c35280 100644 --- a/src/app.py +++ b/src/app.py @@ -1,7 +1,8 @@ from glob import glob -from os import urandom +from os import mkdir, urandom from flask import Flask +from config import Config app = Flask(__name__, static_url_path="/") @@ -12,3 +13,9 @@ for file in glob("src/routes/*.py"): exec(f"app.register_blueprint({module})") app.secret_key = urandom(12) + +# create data directory where posts are stored +try: + mkdir(Config.data_dir) +except FileExistsError: + pass diff --git a/src/config.py b/src/config.py index a69ec9b..196153a 100644 --- a/src/config.py +++ b/src/config.py @@ -31,6 +31,9 @@ class Config: # where is stored the password info _session_login = "logged_in" + # data location + data_dir = "data" + @staticmethod def is_logged() -> bool: """where the info about connection is stored""" diff --git a/src/routes/index.py b/src/routes/index.py index c1b1e0c..7d5a7e6 100644 --- a/src/routes/index.py +++ b/src/routes/index.py @@ -1,5 +1,7 @@ +from os import listdir, path as os_path +from werkzeug import Response from config import Config -from flask import Blueprint, render_template +from flask import Blueprint, flash, redirect, render_template, request name = __name__.split(".")[-1] router = Blueprint(name, __name__) @@ -9,3 +11,37 @@ router = Blueprint(name, __name__) def index() -> str: """index page""" return render_template("index.html", config=Config, page_name=name) + + +@router.route("/", methods=["POST"]) +def new_post() -> Response: + """create a new post""" + if Config.is_logged(): + content = request.form.get("p") + if content: + # finding all posts + paths = [ + os_path.join(Config.data_dir, basename) + for basename in listdir(Config.data_dir) + ] + + # finding an appropriate filename + filename = 0 + if len(paths) > filename: + filename = paths.index(max(iter(paths), key=os_path.getmtime)) + 1 + + # creating the post + with open( + f"{Config.data_dir}/{filename}.txt", + "w", + encoding="utf-8", + ) as f: + f.write(content) + + flash(f"post created.") + else: + flash(f"invalid post: {content}") + else: + flash("you can't do that.") + + return redirect("/") diff --git a/src/static/css/style.css b/src/static/css/style.css index db85a2a..0d9fb13 100644 --- a/src/static/css/style.css +++ b/src/static/css/style.css @@ -45,3 +45,27 @@ span#login-form input { display: block; margin-bottom: 1em; } + +/* new post area */ +aside { + float: right; +} + +aside button { + float: right; +} + +@media only screen and (max-width: 500px) { + aside { + float: unset; + } + + aside textarea { + width: 98%; + } +} + +aside textarea { + min-width: 20em; + min-height: 7em; +} diff --git a/src/templates/index.html b/src/templates/index.html index 4d73ce1..b9aaf5f 100644 --- a/src/templates/index.html +++ b/src/templates/index.html @@ -3,17 +3,13 @@ {% include "head.html" %} - {% include "header.html" %} - + {% include "post.html" %}
-

{{ "".join(get_flashed_messages()) }}

+

{{ "".join(get_flashed_messages()) | safe }}

posts

diff --git a/src/templates/post.html b/src/templates/post.html index dc1c977..162e718 100644 --- a/src/templates/post.html +++ b/src/templates/post.html @@ -1 +1,9 @@ - +{% if config.is_logged() -%} + +{% endif %}