Skip to content

Commit 2d34a42

Browse files
committed
Merge pull request #83 from mozillazg/login-required
apply login_required
2 parents c04abb7 + 20a66f8 commit 2d34a42

File tree

7 files changed

+82
-47
lines changed

7 files changed

+82
-47
lines changed

conftest.py

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
1+
# coding=utf-8
12
from __future__ import absolute_import
2-
# -*- coding: utf-8 -*-
33

4+
from flask import url_for
5+
from flask_login import current_user
46
import pytest
57

68
from firefly.app import create_app
79
from firefly.ext import db
10+
from firefly.models.user import User
811

912

1013
@pytest.fixture
@@ -16,3 +19,29 @@ def cleanup():
1619
db.connection.drop_database(db_name)
1720
request.addfinalizer(cleanup)
1821
return app
22+
23+
24+
@pytest.fixture
25+
def client_class(request, client):
26+
def login(cls):
27+
user = User.objects.filter(email='foo@bar.com').first()
28+
if user is None:
29+
user = User.create_user('foo', 'foo@bar.com', 'foobar')
30+
else:
31+
user.set_password('foobar')
32+
user.save()
33+
34+
form = {
35+
'email': 'foo@bar.com',
36+
'password': 'foobar',
37+
}
38+
rv = client.post(
39+
url_for('home.login'), data=form,
40+
follow_redirects=True
41+
)
42+
assert current_user.is_authenticated()
43+
assert url_for('security.logout') in rv.data
44+
45+
if request.cls is not None:
46+
request.cls.client = client
47+
request.cls._login = login

firefly/models/user.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,9 @@ def create_user(cls, username, email, password, **kwargs):
9090
username=username, email=email, password=password, **kwargs
9191
)
9292

93+
def set_password(self, password):
94+
self.password = self.generate_password(password)
95+
9396
def check_password(self, password):
9497
return security.check_password_hash(
9598
self.password,

firefly/templates/index.html

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,10 @@
2525
li title="all topics grouped by category"
2626
a href="/categories"
2727
Categories
28-
button#create-topic.btn.btn-default.right
29-
i.fa.fa-plus
30-
New Topic
28+
- if current_user.is_authenticated()
29+
button#create-topic.btn.btn-default.right
30+
i.fa.fa-plus
31+
New Topic
3132
div.container.list-container
3233
div#list-area
3334
div.contents
@@ -49,8 +50,9 @@
4950
-for post in posts
5051
${topic_item.main(post)}
5152

52-
-def others()
53-
${widgets_editor.index()}
53+
- if current_user.is_authenticated()
54+
-def others()
55+
${widgets_editor.index()}
5456

5557
-def head_script()
5658
link href="${url_for('static', filename='stylesheets/base16-light.css')}" rel="stylesheet"

firefly/templates/posts/detail.html

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -87,15 +87,17 @@
8787
Replies
8888
i.fa.fa-chevron-down
8989
div.actions
90-
button.like title='喜欢这个主题?'
91-
i.fa.fa-heart
90+
- if current_user.is_authenticated()
91+
button.like title='喜欢这个主题?'
92+
i.fa.fa-heart
9293
button title='分享' data-share-url="/t/dd"
9394
i.fa.fa-link
94-
button.bookmark title='标记'
95-
div.read-icon
96-
button.create title='评论'
97-
i.fa.fa-reply
98-
评论
95+
- if current_user.is_authenticated()
96+
button.bookmark title='标记'
97+
div.read-icon
98+
button.create title='评论'
99+
i.fa.fa-reply
100+
评论
99101
section.embedded-posts.bottom.hide
100102

101103
-def head_script()
@@ -109,5 +111,6 @@
109111
require(['post']);
110112
});
111113

112-
- def others()
113-
${widgets_editor.topic()}
114+
- if current_user.is_authenticated()
115+
- def others()
116+
${widgets_editor.topic()}

firefly/views/home.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from flask.views import MethodView
55
from flask.blueprints import Blueprint
66
from flask_mako import render_template, render_template_def
7-
from flask_login import login_user, current_user
7+
from flask_login import login_user, current_user, login_required
88

99
from firefly.forms.user import LoginForm, RegisterForm
1010
from firefly.models.topic import Category, Post
@@ -21,15 +21,15 @@ def get(self):
2121

2222

2323
class CreateView(MethodView):
24+
decorators = [login_required]
25+
2426
def post(self):
2527
title = request.form.get('title')
2628
content = request.form.get('content')
2729
category_id = request.form.get('category', '')
28-
author_id = request.form.get('author', '')
2930
if category_id.isdigit():
3031
category_id = int(category_id)
31-
if not author_id:
32-
author_id = current_user.id
32+
author_id = current_user.id
3333
category = Category.objects.filter(id=category_id).first()
3434
post = Post(title=title, content=content, category=category,
3535
author=User.objects.get_or_404(id=author_id))

firefly/views/post.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1-
from __future__ import absolute_import
21
# coding=utf-8
3-
from flask import request, redirect, url_for
2+
from __future__ import absolute_import
3+
from flask import request, redirect, url_for, abort
44
from flask.views import MethodView
55
from flask.blueprints import Blueprint
66
from flask_mako import render_template
77
from flask_mongoengine.wtf import model_form
8+
from flask_login import current_user
89

10+
from firefly.models.user import User
911
from firefly.models.topic import Post, Comment
1012

1113

@@ -32,12 +34,16 @@ def get(self, id):
3234
return render_template('posts/detail.html', **context)
3335

3436
def post(self, id):
37+
if not current_user.is_authenticated():
38+
abort(403)
39+
3540
context = self.get_context(id)
3641
form = context.get('form')
3742

3843
if form.validate():
3944
comment = Comment()
4045
form.populate_obj(comment)
46+
comment.author = User.objects.get_or_404(id=current_user.id)
4147
comment.save()
4248

4349
post = context.get('post')

tests/test_post.py

Lines changed: 18 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -4,42 +4,33 @@
44
from flask import url_for
55
import pytest
66

7-
from firefly.models.user import User
8-
from firefly.models.topic import Category, Post
7+
from firefly.models.topic import Category, Post, Comment
98

109

1110
@pytest.mark.usefixtures('client_class')
1211
class TestPost:
1312

1413
def setup(self):
1514
c = Category.objects.create(
16-
name=u'python', description=u'描述', _slug=u'python-slug'
15+
name='python', description='描述', _slug='python-slug'
1716
)
1817
Post.objects.create(
19-
title=u'标题test', content=u'内容test', category=c
20-
)
21-
22-
# login user
23-
self.username = 'foo'
24-
self.password = 'foobar'
25-
self.email = 'foo@bar.com'
26-
self.user = User.create_user(
27-
username=self.username, password=self.password,
28-
email=self.email
18+
title='标题test', content='内容test', category=c
2919
)
20+
self._login()
3021

3122
def test_create(self):
23+
3224
category = Category.objects.first()
3325
url = url_for('home.create')
3426
form = {
3527
'title': '标题',
3628
'content': '内容喜喜喜喜喜喜',
3729
'category': category.id,
38-
'author': self.user.id
3930
}
4031
rv = self.client.post(url, data=form)
41-
assert rv.json['ok'] == 0
4232

33+
assert rv.json['ok'] == 0
4334
assert Post.objects.count() > 1
4435

4536
def test_detail(self):
@@ -50,14 +41,15 @@ def test_detail(self):
5041
assert post.title in data
5142
assert post.content in data
5243

53-
# def test_comment(self):
54-
# post = Post.objects.first()
55-
# url = url_for('post.detail', id=post.id)
56-
# form = {
57-
# 'content': u'评论测试',
58-
# }
59-
# rv = self.client.post(url, data=form, follow_redirects=False)
60-
61-
# assert rv.status_code == 302
62-
# assert Comment.objects.count() == 1
63-
# assert len(post.comments) == 1
44+
def test_comment(self):
45+
post = Post.objects.first()
46+
url = url_for('post.detail', id=post.id)
47+
form = {
48+
'content': '评论测试',
49+
'ref_id': 0,
50+
}
51+
self.client.post(url, data=form, follow_redirects=False)
52+
post.reload()
53+
54+
assert Comment.objects.count() == 1
55+
assert len(post.comments) == 1

0 commit comments

Comments
 (0)