Compare commits

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

5 commits

Author SHA1 Message Date
4927b6f2da Remove padding around title 2026-03-11 20:45:56 +05:30
44a49e5aee Add link to individual post in index 2026-03-11 20:27:32 +05:30
89622db6c9 End individual post url in / 2026-03-11 20:27:06 +05:30
13bdcaef9e Fix bad test 2026-03-11 20:25:52 +05:30
f2fb55ea33 Working code, failing test 2026-03-01 20:32:48 +05:30
7 changed files with 93 additions and 4 deletions

View file

@ -8,7 +8,7 @@ def create_app(test_config=None):
app.config.from_mapping(
SECRET_KEY='dev',
DATABASE=os.path.join(app.instance_path, 'flaskr.sqlite'),
ALLOW_REGISTER=False,
ALLOW_REGISTER=True,
)
app.wsgi_app = ProxyFix(

View file

@ -28,6 +28,23 @@ def index():
posts.append(post)
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'))
@login_required
def create():
@ -69,6 +86,12 @@ def get_post(id, check_author=True):
return post
@bp.route('/<int:id>/')
def individual_post(id):
post = dict(get_post(id, False))
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):

View file

@ -12,7 +12,7 @@ nav { background: lightgray; display: flex; align-items: center; padding: 0 0.5r
nav h1 { flex: auto; margin: 0; }
nav h1 a { text-decoration: none; padding: 0.25rem 0.5rem; }
nav ul { display: flex; list-style: none; margin: 0; padding: 0; }
nav ul li a, nav ul li span, header .action { display: block; padding: 0.5rem; }
nav ul li a, nav ul li span, header .action { display: block;}
.content { padding: 0 1rem 1rem; }
.content > header { border-bottom: 1px solid lightgray; display: flex; align-items: flex-end; }
.content > header h1 { flex: auto; margin: 1rem 0 0.25rem 0; }

View 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 %}

View file

@ -12,14 +12,13 @@
<article class="post">
<header>
<div>
<h1>{{ post['title'] }}</h1>
<h1><a class="action" href="{{ url_for('blog.individual_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 %}
</header>
<p class="body">{{ post['body']|safe }}</p>
</article>
{% if not loop.last %}
<hr>

View 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 %}

View file

@ -11,9 +11,29 @@ def test_index(client, auth):
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
assert b'href="/1/"' 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()
response = client.get('/firehose')
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' 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', (
'/create',
'/1/update',