Compare commits
4 commits
production
...
individual
| Author | SHA1 | Date | |
|---|---|---|---|
| f2fb55ea33 | |||
| 39243beff4 | |||
| 67953ac42e | |||
| ed71714a79 |
10 changed files with 101 additions and 10 deletions
|
|
@ -8,6 +8,7 @@ def create_app(test_config=None):
|
||||||
app.config.from_mapping(
|
app.config.from_mapping(
|
||||||
SECRET_KEY='dev',
|
SECRET_KEY='dev',
|
||||||
DATABASE=os.path.join(app.instance_path, 'flaskr.sqlite'),
|
DATABASE=os.path.join(app.instance_path, 'flaskr.sqlite'),
|
||||||
|
ALLOW_REGISTER=True,
|
||||||
)
|
)
|
||||||
|
|
||||||
app.wsgi_app = ProxyFix(
|
app.wsgi_app = ProxyFix(
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,7 @@
|
||||||
import functools
|
import functools
|
||||||
|
|
||||||
from flask import (
|
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
|
||||||
)
|
)
|
||||||
from werkzeug.security import check_password_hash, generate_password_hash
|
from werkzeug.security import check_password_hash, generate_password_hash
|
||||||
|
|
||||||
|
|
@ -11,7 +11,8 @@ bp = Blueprint('auth', __name__, url_prefix='/auth')
|
||||||
|
|
||||||
@bp.route('/register', methods=('GET', 'POST'))
|
@bp.route('/register', methods=('GET', 'POST'))
|
||||||
def register():
|
def register():
|
||||||
return "Admin only", 403
|
if not current_app.config["ALLOW_REGISTER"]:
|
||||||
|
return "Admin only", 403
|
||||||
if request.method == 'POST':
|
if request.method == 'POST':
|
||||||
username = request.form['username']
|
username = request.form['username']
|
||||||
password = request.form['password']
|
password = request.form['password']
|
||||||
|
|
|
||||||
|
|
@ -28,6 +28,23 @@ def index():
|
||||||
posts.append(post)
|
posts.append(post)
|
||||||
return render_template('blog/index.html', posts=posts)
|
return render_template('blog/index.html', posts=posts)
|
||||||
|
|
||||||
|
@bp.route('/firehose')
|
||||||
|
def firehose():
|
||||||
|
db = get_db()
|
||||||
|
db_posts = db.execute(
|
||||||
|
'SELECT p.id, title, body, created, author_id, username'
|
||||||
|
' FROM post p JOIN user u ON p.author_id = u.id'
|
||||||
|
' ORDER BY created DESC'
|
||||||
|
).fetchall()
|
||||||
|
posts = []
|
||||||
|
for post in db_posts:
|
||||||
|
if post['created'] > datetime.datetime.utcnow():
|
||||||
|
continue
|
||||||
|
post = dict(post)
|
||||||
|
post['body'] = markdown.markdown(post['body'])
|
||||||
|
posts.append(post)
|
||||||
|
return render_template('blog/firehose.html', posts=posts)
|
||||||
|
|
||||||
@bp.route('/create',methods=('GET', 'POST'))
|
@bp.route('/create',methods=('GET', 'POST'))
|
||||||
@login_required
|
@login_required
|
||||||
def create():
|
def create():
|
||||||
|
|
@ -69,6 +86,11 @@ def get_post(id, check_author=True):
|
||||||
|
|
||||||
return post
|
return post
|
||||||
|
|
||||||
|
@bp.route('/<int:id>')
|
||||||
|
def individual_post(id):
|
||||||
|
post = get_post(id, False)
|
||||||
|
return render_template('blog/post.html', post=post)
|
||||||
|
|
||||||
@bp.route('/<int:id>/update', methods=('GET', 'POST'))
|
@bp.route('/<int:id>/update', methods=('GET', 'POST'))
|
||||||
@login_required
|
@login_required
|
||||||
def update(id):
|
def update(id):
|
||||||
|
|
|
||||||
|
|
@ -8,9 +8,6 @@
|
||||||
{% if g.user %}
|
{% if g.user %}
|
||||||
<li><span>{{ g.user['username'] }}</span>
|
<li><span>{{ g.user['username'] }}</span>
|
||||||
<li><a href="{{ url_for('auth.logout') }}">Log Out</a>
|
<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 %}
|
{% endif %}
|
||||||
</ul>
|
</ul>
|
||||||
</nav>
|
</nav>
|
||||||
|
|
|
||||||
28
flaskr/templates/blog/firehose.html
Normal file
28
flaskr/templates/blog/firehose.html
Normal file
|
|
@ -0,0 +1,28 @@
|
||||||
|
{% extends 'base.html' %}
|
||||||
|
|
||||||
|
{% block header %}
|
||||||
|
<h1>{% block title %}Posts{% endblock %}</h1>
|
||||||
|
{% if g.user %}
|
||||||
|
<a class="action" href="{{ url_for('blog.create') }}">New</a>
|
||||||
|
{% endif %}
|
||||||
|
{% endblock %}
|
||||||
|
|
||||||
|
{% block content %}
|
||||||
|
{% for post in posts %}
|
||||||
|
<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>
|
||||||
|
{% if not loop.last %}
|
||||||
|
<hr>
|
||||||
|
{% endif %}
|
||||||
|
{% endfor %}
|
||||||
|
{% endblock %}
|
||||||
|
|
@ -19,7 +19,6 @@
|
||||||
<a class="action" href="{{ url_for('blog.update', id=post['id']) }}">Edit</a>
|
<a class="action" href="{{ url_for('blog.update', id=post['id']) }}">Edit</a>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
</header>
|
</header>
|
||||||
<p class="body">{{ post['body']|safe }}</p>
|
|
||||||
</article>
|
</article>
|
||||||
{% if not loop.last %}
|
{% if not loop.last %}
|
||||||
<hr>
|
<hr>
|
||||||
|
|
|
||||||
19
flaskr/templates/blog/post.html
Normal file
19
flaskr/templates/blog/post.html
Normal file
|
|
@ -0,0 +1,19 @@
|
||||||
|
{% extends 'base.html' %}
|
||||||
|
|
||||||
|
{% block header %}
|
||||||
|
<h1>{% block title %}{{ post['title'] }}{% endblock %}</h1>
|
||||||
|
{% endblock %}
|
||||||
|
|
||||||
|
{% block content %}
|
||||||
|
<article class="post">
|
||||||
|
<header>
|
||||||
|
<div>
|
||||||
|
<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 %}
|
||||||
|
|
@ -15,6 +15,7 @@ def app():
|
||||||
app = create_app({
|
app = create_app({
|
||||||
'TESTING': True,
|
'TESTING': True,
|
||||||
'DATABASE': db_path,
|
'DATABASE': db_path,
|
||||||
|
'ALLOW_REGISTER': True,
|
||||||
})
|
})
|
||||||
|
|
||||||
with app.app_context():
|
with app.app_context():
|
||||||
|
|
|
||||||
|
|
@ -14,6 +14,10 @@ def test_register(client, app):
|
||||||
"SELECT * FROM user WHERE USERNAME = 'a'",
|
"SELECT * FROM user WHERE USERNAME = 'a'",
|
||||||
).fetchone() is not None
|
).fetchone() is not None
|
||||||
|
|
||||||
|
app.config["ALLOW_REGISTER"] = False
|
||||||
|
response = client.get('/auth/register')
|
||||||
|
assert b"Admin only" in response.data
|
||||||
|
|
||||||
@pytest.mark.parametrize(('username', 'password', 'message'), (
|
@pytest.mark.parametrize(('username', 'password', 'message'), (
|
||||||
('', '', b'Username is required.'),
|
('', '', b'Username is required.'),
|
||||||
('a', '', b'Password is required.'),
|
('a', '', b'Password is required.'),
|
||||||
|
|
|
||||||
|
|
@ -3,8 +3,21 @@ from flaskr.db import get_db
|
||||||
|
|
||||||
def test_index(client, auth):
|
def test_index(client, auth):
|
||||||
response = client.get('/')
|
response = client.get('/')
|
||||||
assert b"Log In" in response.data
|
assert b"Log In" not in response.data
|
||||||
assert b"Register" in response.data
|
assert b"Register" not in response.data
|
||||||
|
|
||||||
|
auth.login()
|
||||||
|
response = client.get('/')
|
||||||
|
assert b'Log Out' in response.data
|
||||||
|
assert b'test title' in response.data
|
||||||
|
assert b'by test on 2018-01-01' in response.data
|
||||||
|
assert b'test\nbody' not in response.data
|
||||||
|
assert b'href="/1/update"' in response.data
|
||||||
|
|
||||||
|
def test_firehose(client, auth):
|
||||||
|
response = client.get('/')
|
||||||
|
assert b"Log In" not in response.data
|
||||||
|
assert b"Register" not in response.data
|
||||||
|
|
||||||
auth.login()
|
auth.login()
|
||||||
response = client.get('/')
|
response = client.get('/')
|
||||||
|
|
@ -14,6 +27,12 @@ def test_index(client, auth):
|
||||||
assert b'test\nbody' in response.data
|
assert b'test\nbody' in response.data
|
||||||
assert b'href="/1/update"' in response.data
|
assert b'href="/1/update"' in response.data
|
||||||
|
|
||||||
|
def test_individual_page(client, auth):
|
||||||
|
response = client.get('/1')
|
||||||
|
assert b'test title' in response.data
|
||||||
|
assert b'by test on 2018-01-01' in response.data
|
||||||
|
assert b'test\nbody' in response.data
|
||||||
|
|
||||||
@pytest.mark.parametrize('path', (
|
@pytest.mark.parametrize('path', (
|
||||||
'/create',
|
'/create',
|
||||||
'/1/update',
|
'/1/update',
|
||||||
|
|
@ -58,7 +77,7 @@ def test_create(client, auth, app):
|
||||||
def test_update(client, auth, app):
|
def test_update(client, auth, app):
|
||||||
auth.login()
|
auth.login()
|
||||||
assert client.get('/1/update').status_code == 200
|
assert client.get('/1/update').status_code == 200
|
||||||
client.post('/1/update', data={'title': 'updated', 'body': ''})
|
client.post('/1/update', data={'title': 'updated', 'body': '', 'created': '1970-01-01 00:00:00'})
|
||||||
|
|
||||||
with app.app_context():
|
with app.app_context():
|
||||||
db = get_db()
|
db = get_db()
|
||||||
|
|
@ -71,7 +90,7 @@ def test_update(client, auth, app):
|
||||||
))
|
))
|
||||||
def test_create_update_validate(client, auth, path):
|
def test_create_update_validate(client, auth, path):
|
||||||
auth.login()
|
auth.login()
|
||||||
response = client.post(path, data={'title': '', 'body': ''})
|
response = client.post(path, data={'title': '', 'body': '', 'created': '1970-01-01 00:00:00'})
|
||||||
assert b'Title is required.' in response.data
|
assert b'Title is required.' in response.data
|
||||||
|
|
||||||
def test_delete(client, auth, app):
|
def test_delete(client, auth, app):
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue