feat: create a post
All checks were successful
ci/woodpecker/push/publish Pipeline was successful

This commit is contained in:
Mylloon 2023-12-22 02:11:53 +01:00
parent f9290c1664
commit 9b0d72f12f
Signed by: Anri
GPG key ID: A82D63DFF8D1317F
8 changed files with 97 additions and 16 deletions

5
.gitignore vendored
View file

@ -3,8 +3,9 @@
/lib64
/pyvenv.cfg
__pycache__/
/.vscode/
/docker-compose.yml
__pycache__/
/data

View file

@ -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 |

View file

@ -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

View file

@ -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"""

View file

@ -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"<a href='/read/{filename}'>post created</a>.")
else:
flash(f"invalid post: {content}")
else:
flash("you can't do that.")
return redirect("/")

View file

@ -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;
}

View file

@ -3,17 +3,13 @@
<head>
{% include "head.html" %}
</head>
<!-- TODO: icon of login in -->
{% include "header.html" %}
<body>
<aside>
<!-- TODO: side block for posting something new when connected -->
{% include "post.html" %}
</aside>
<main>
<p>{{ "".join(get_flashed_messages()) }}</p>
<p>{{ "".join(get_flashed_messages()) | safe }}</p>
<h3>posts</h3>
<!-- TODO: list of clickable posts -->

View file

@ -1 +1,9 @@
<span></span>
{% if config.is_logged() -%}
<aside>
<form action="/" method="post">
<textarea name="p"></textarea>
<br />
<button type="submit">send</button>
</form>
</aside>
{% endif %}