Compare commits

...
Sign in to create a new pull request.

8 commits

8 changed files with 63 additions and 13 deletions

View file

@ -10,7 +10,20 @@ Use `gunicorn -w 2 'flaskr:create_app()'` to run app. Increase the number of wor
## Initializing database
The first time you install the app in each environment, you need to initialize database using `flask --app flaskr init-db`. This only needs to be run once per environment, and **will delete existing database if run again**.
## Secret key
## Config file
The config file is located at `<python_environment>/var/flaskr-instance/config.py` in production, and in `instance/` in development. The instance folder is created when the database is initialized.
### Secret Key
Every website with login needs a secret key to hash passwords with.
`<python_environment>/var/flaskr-instance/config.py` must contain a line `SECRET_KEY = '<secret_key>`, which must be randomly generated.
The config file must contain a line `SECRET_KEY = '<secret_key>`, which must be randomly generated.
Suggested way of generating the key is `python -c 'import secrets; print(secrets.token_hex())'`, which returns a hexadecimal string with length 64. You may choose to randomly generate a key using a different method, but ensure that it is resistant to brute-force attacks.
### Registration
Since this blog is meant to be updated by a limited number of people, registration is forbidden (403) by default. In addition, registration (/auth/register) and login (/auth/login) URLs are not hyperlinked anywhere. Registration can be opened by including `REGISTER = True`, and is closed by default.
### Name
The default app name is "Flaskr", and it is visible on the header bar as well as the page title. Including a line `NAME = '<name>'` in the config file replaces "Flaskr" with your chosen name.
### Static folder
The default static folder is the one included in the repository. You can use a separate static folder to use your own assets by including a line `STATIC_FOLDER = '<absolute/path/to/folder>'` in the config file.

View file

@ -8,6 +8,8 @@ def create_app(test_config=None):
app.config.from_mapping(
SECRET_KEY='dev',
DATABASE=os.path.join(app.instance_path, 'flaskr.sqlite'),
REGISTER=False,
NAME='Flaskr'
)
app.wsgi_app = ProxyFix(
@ -21,6 +23,9 @@ def create_app(test_config=None):
# load the test config if passed in
app.config.from_mapping(test_config)
if app.config.get('STATIC_FOLDER') is not None:
app.static_folder = app.config.get('STATIC_FOLDER')
# ensure the instance folder exists
try:
os.makedirs(app.instance_path)

View file

@ -1,7 +1,7 @@
import functools
from flask import (
Blueprint, flash, g, redirect, render_template, request, session, url_for
Blueprint, flash, g, redirect, render_template, request, session, url_for, current_app, abort
)
from werkzeug.security import check_password_hash, generate_password_hash
@ -11,6 +11,8 @@ bp = Blueprint('auth', __name__, url_prefix='/auth')
@bp.route('/register', methods=('GET', 'POST'))
def register():
if not current_app.config['REGISTER']:
abort(403)
if request.method == 'POST':
username = request.form['username']
password = request.form['password']

View file

@ -66,6 +66,13 @@ def get_post(id, check_author=True):
return post
@bp.route('/<int:id>')
def post(id):
post = get_post(id, check_author=False)
post = dict(post)
post['body'] = markdown.markdown(post['body'])
return render_template('blog/post.html', post=post)
@bp.route('/<int:id>/update', methods=('GET', 'POST'))
@login_required
def update(id):
@ -101,3 +108,7 @@ def delete(id):
db.execute('DELETE FROM post WHERE id = ?',(id,))
db.commit()
return redirect(url_for('blog.index'))
@bp.route('/temp')
def temp():
return render_template('temp.html')

View file

@ -1,19 +1,19 @@
<!doctype html>
<title>{% block title %}{% endblock %} - Flaskr</title>
<title>{% block title %}{% endblock %} - {{ config['NAME'] }}</title>
<link rel="stylesheet" href="{{ url_for('static', filename='style.css') }}">
<link rel="icon" type="image/png" href="{{ url_for('static', filename='favicon.png') }}">
<nav>
<h1>Flaskr</h1>
<h1>{{ config['NAME'] }}</h1>
<ul>
{% if g.user %}
<li><span>{{ g.user['username'] }}</span>
<li><a href="{{ url_for('auth.logout') }}">Log Out</a>
{% else %}
<li><a href="{{ url_for('auth.register') }}">Register</a>
<li><a href="{{ url_for('auth.login') }}">Log In</a>
{% endif %}
</ul>
</nav>
</br>
</br>
</br>
<section class="content">
<header>
{% block header %}{% endblock %}

View file

@ -12,14 +12,13 @@
<article class="post">
<header>
<div>
<h1>{{ post['title'] }}</h1>
<h1><a href="{{ url_for('blog.post', id=post['id']) }}">{{ post['title'] }}</a></h1>
<div class="about">by {{ post['username'] }} on {{ post['created'].strftime('%Y-%m-%d') }}</div>
</div>
{% if g.user['id'] == post['author_id'] %}
<a class="action" href="{{ url_for('blog.update', id=post['id']) }}">Edit</a>
{% endif %}
<a href="{{ url_for('blog.update', id=post['id']) }}">Edit</a>
{% endif %}
</header>
<p class="body">{{ post['body']|safe }}</p>
</article>
{% if not loop.last %}
<hr>

View file

@ -0,0 +1,20 @@
{% extends 'base.html' %}
{% block header %}
<h1>{% block title %}{{ post['title']}}{% endblock %}</h1>
{% endblock %}
{% block content %}
<article class="post">
<header>
<div>
<h1>{{ post['title'] }}</h1>
<div class="about">by {{ post['username'] }} on {{ post['created'].strftime('%Y-%m-%d') }}</div>
</div>
{% if g.user['id'] == post['author_id'] %}
<a class="action" href="{{ url_for('blog.update', id=post['id']) }}">Edit</a>
{% endif %}
</header>
<p class="body">{{ post['body']|safe }}</p>
</article>
{% endblock %}

View file

@ -8,7 +8,7 @@ dnspython==2.3.0
email-validator==2.0.0.post2
exceptiongroup==1.1.1
Flask==2.3.2
-e git+https://gitlab.com/pvtejas/based4tech.git@4be89bd767a7c5a84ab62fbf4ad924ae1af077f1#egg=flaskr
-e git+https://gitlab.com/pvtejas/based4tech.git@bda624e4dc1cf97ba2b5b3fcb66a5b28398307bc#egg=flaskr
gunicorn==20.1.0
h11==0.14.0
httpcore==0.17.0