diff options
-rw-r--r-- | .gitignore | 117 | ||||
-rw-r--r-- | app.py | 39 | ||||
-rw-r--r-- | config.py | 12 | ||||
-rw-r--r-- | forms.py | 7 | ||||
-rw-r--r-- | models.py | 14 | ||||
-rw-r--r-- | static/style.css | 34 | ||||
-rw-r--r-- | templates/base.html | 17 | ||||
-rw-r--r-- | templates/index.html | 11 | ||||
-rw-r--r-- | templates/post.html | 8 |
9 files changed, 259 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..df09c56 --- /dev/null +++ b/.gitignore @@ -0,0 +1,117 @@ +# Byte-compiled / optimized / DLL files +__pycache__/ +*.py[cod] +*$py.class + +# C extensions +*.so + +# Distribution / packaging +.Python +env/ +venv/ +ENV/ +env.bak/ +venv.bak/ +*.egg-info/ +.dist/ +build/ +develop-eggs/ +.eggs/ +lib/ +lib64/ +parts/ +sdist/ +var/ +wheels/ +*.egg +.installed.cfg +*.egg-info/ +.eggs/ + +# PyInstaller +# Usually these files are written by a python script from a template +# before PyInstaller builds the exe, so as to inject date/other infos into it. +*.manifest +*.spec + +# Installer logs +pip-log.txt +pip-delete-this-directory.txt + +# Unit test / coverage reports +htmlcov/ +.tox/ +.nox/ +.coverage +.coverage.* +.cache +nosetests.xml +coverage.xml +*.cover +*.py,cover +.hypothesis/ + +# Translations +*.mo +*.pot + +# Flask stuff +instance/ +.webassets-cache + +# Scrapy stuff: +.scrapy + +# Jupyter Notebook +.ipynb_checkpoints + +# pyenv +.python-version + +# celery beat schedule file +celerybeat-schedule +celerybeat-schedule.* + +# SageMath parsed files +*.sage.py + +# Environment variables +.env +.env.* + +# Environments +.env +.venv +env/ +venv/ +ENV/ +env.bak/ +venv.bak/ + +# Spyder project settings +.spyderproject +.spyproject + +# Rope project settings +.ropeproject + +# mkdocs documentation +/site + +# mypy +.mypy_cache/ +.dmypy.json +dmypy.json + +# Pyre type checker +.pyre/ + +# pytype static type analyzer +.pytype/ + +# Cython debug symbols +cython_debug/ + +# Local configuration files +config.py diff --git a/app.py b/app.py new file mode 100644 index 0000000..7a1b07f --- /dev/null +++ b/app.py @@ -0,0 +1,39 @@ +from flask import Flask, render_template, request, redirect, url_for +from peewee import SqliteDatabase +from models import Post +from forms import PostForm + +app = Flask(__name__) +app.config.from_object('config') + +db = SqliteDatabase('blog.db') + +@app.before_request +def before_request(): + db.connect() + +@app.after_request +def after_request(response): + db.close() + return response + +@app.route('/') +def index(): + posts = Post.select().order_by(Post.created_at.desc()) + return render_template('index.html', posts=posts) + +@app.route('/post/<int:post_id>') +def post(post_id): + post = Post.get(Post.id == post_id) + return render_template('post.html', post=post) + +@app.route('/create', methods=['GET', 'POST']) +def create(): + form = PostForm() + if form.validate_on_submit(): + Post.create(title=form.title.data, content=form.content.data) + return redirect(url_for('index')) + return render_template('create.html', form=form) + +if __name__ == '__main__': + app.run(debug=True) \ No newline at end of file diff --git a/config.py b/config.py new file mode 100644 index 0000000..9416e9c --- /dev/null +++ b/config.py @@ -0,0 +1,12 @@ +import os + +class Config: + SECRET_KEY = os.urandom(24) + + @staticmethod + def init_app(app): + pass + +config = { + 'default': Config +} \ No newline at end of file diff --git a/forms.py b/forms.py new file mode 100644 index 0000000..c66954c --- /dev/null +++ b/forms.py @@ -0,0 +1,7 @@ +from flask_wtf import FlaskForm +from wtforms import StringField, TextAreaField +from wtforms.validators import DataRequired + +class PostForm(FlaskForm): + title = StringField('Title', validators=[DataRequired()]) + content = TextAreaField('Content', validators=[DataRequired()]) \ No newline at end of file diff --git a/models.py b/models.py new file mode 100644 index 0000000..955a70a --- /dev/null +++ b/models.py @@ -0,0 +1,14 @@ +from peewee import Model, CharField, TextField, DateTimeField +from datetime import datetime +from app import db + +class BaseModel(Model): + class Meta: + database = db + +class Post(BaseModel): + title = CharField() + content = TextField() + created_at = DateTimeField(default=datetime.now) + +db.create_tables([Post]) \ No newline at end of file diff --git a/static/style.css b/static/style.css new file mode 100644 index 0000000..8e258e5 --- /dev/null +++ b/static/style.css @@ -0,0 +1,34 @@ +body { + font-family: Arial, sans-serif; + margin: 0; + padding: 0; + background-color: #f4f4f4; +} + +header { + background-color: #333; + color: #fff; + padding: 1rem; +} + +header h1 { + margin: 0; +} + +header a { + color: #fff; + text-decoration: none; +} + +main { + padding: 1rem; +} + +a { + color: #333; + text-decoration: none; +} + +a:hover { + text-decoration: underline; +} \ No newline at end of file diff --git a/templates/base.html b/templates/base.html new file mode 100644 index 0000000..48b4d9c --- /dev/null +++ b/templates/base.html @@ -0,0 +1,17 @@ +<!DOCTYPE html> +<html lang="en"> +<head> + <meta charset="UTF-8"> + <meta name="viewport" content="width=device-width, initial-scale=1.0"> + <title>Blog</title> + <link rel="stylesheet" href="{{ url_for('static', filename='style.css') }}"> +</head> +<body> + <header> + <h1><a href="{{ url_for('index') }}">My Blog</a></h1> + </header> + <main> + {% block content %}{% endblock %} + </main> +</body> +</html> \ No newline at end of file diff --git a/templates/index.html b/templates/index.html new file mode 100644 index 0000000..9a05e7a --- /dev/null +++ b/templates/index.html @@ -0,0 +1,11 @@ +{% extends 'base.html' %} + +{% block content %} + <h2>Posts</h2> + <ul> + {% for post in posts %} + <li><a href="{{ url_for('post', post_id=post.id) }}">{{ post.title }}</a> - {{ post.created_at.strftime('%Y-%m-%d') }}</li> + {% endfor %} + </ul> + <a href="{{ url_for('create') }}">Create a new post</a> +{% endblock %} \ No newline at end of file diff --git a/templates/post.html b/templates/post.html new file mode 100644 index 0000000..5a28b2b --- /dev/null +++ b/templates/post.html @@ -0,0 +1,8 @@ +{% extends 'base.html' %} + +{% block content %} + <h2>{{ post.title }}</h2> + <p>{{ post.created_at.strftime('%Y-%m-%d %H:%M') }}</p> + <div>{{ post.content }}</div> + <a href="{{ url_for('index') }}">Back to posts</a> +{% endblock %} \ No newline at end of file |