Skip to content

Commit 3c7f326

Browse files
committed
Merge pull request #107 from python-cn/react
plim -> jinja2
2 parents 2cb413b + 07c9bb9 commit 3c7f326

29 files changed

+880
-713
lines changed

firefly/app.py

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
1-
from __future__ import absolute_import
21
# coding=utf-8
2+
from __future__ import absolute_import
33
import os
44

5-
from flask import Flask, g, request
6-
from flask_mako import render_template
5+
from flask import Flask, g, request, send_from_directory
76
from flask_security import MongoEngineUserDatastore
87
from flask_social_blueprint.core import SocialBlueprint
8+
from flask_wtf.csrf import CsrfProtect
99

1010
from firefly import config as _config
1111
from firefly.ext import (
12-
api, babel, cache, db, login_manager, mail, mako,
13-
redis_store, security
12+
api, babel, cache, db, login_manager, mail, redis_store, security
1413
)
1514
from firefly.models.user import User, SocialConnection, Role
1615
from firefly.utils import send_mail
16+
from firefly.libs.template import render_template
1717

1818

1919
def create_app(config):
@@ -34,18 +34,24 @@ def create_app(config):
3434
cache.init_app(app)
3535
db.init_app(app)
3636
mail.init_app(app)
37-
mako.init_app(app)
3837
redis_store.init_app(app)
38+
CsrfProtect(app)
3939

4040
register_auth(app)
4141
register_hooks(app)
42+
register_static(app)
4243
register_blueprints(app)
4344
configure_error_handles(app)
4445
plug_to_db(db)
4546

4647
return app
4748

4849

50+
def register_static(app):
51+
app.route('/static/<path:filename>')(
52+
lambda filename: send_from_directory('static', filename))
53+
54+
4955
def register_auth(app):
5056
def load_user(user_id):
5157
return User.objects(_id=user_id)
@@ -57,7 +63,6 @@ def load_user(user_id):
5763
# Setup Flask-Security
5864
security.init_app(app, MongoEngineUserDatastore(db, User, Role))
5965
state = app.extensions['security']
60-
state.render_template = render_template
6166
state.send_mail_task(send_mail)
6267
app.extensions['security'] = state
6368

@@ -68,16 +73,16 @@ def configure_error_handles(app):
6873

6974
@app.errorhandler(403)
7075
def forbidden_page(error):
71-
return render_template('403.html')
76+
return render_template('403.html'), 403
7277

7378
@app.errorhandler(404)
7479
def not_found_page(error):
75-
return render_template('404.html')
80+
return render_template('404.html'), 404
7681

7782

7883
def register_blueprints(app):
79-
from firefly.views import (home, post, category, api, keyboard, user)
80-
for i in (home, post, category, api, keyboard, user):
84+
from firefly.views import (home, post, category, api, keyboard, user, auth)
85+
for i in (home, post, category, api, keyboard, user, auth):
8186
app.register_blueprint(i.bp)
8287

8388

@@ -92,6 +97,13 @@ def get_locale():
9297
def before_request():
9398
g.locale = get_locale()
9499

100+
@app.context_processor
101+
def inject_builtin():
102+
return {
103+
'hasattr': hasattr,
104+
'len': len
105+
}
106+
95107

96108
def plug_to_db(db):
97109
from firefly.models.utils import dict_filter

firefly/config.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,11 @@
44
'''
55
from __future__ import print_function
66
from __future__ import absolute_import
7-
from plim import preprocessor
87

98
SECRET_KEY = 'you need modify this into local_settings.py'
109

1110
DEBUG = False
1211

13-
# plim
14-
MAKO_DEFAULT_FILTERS = ['decode.utf_8', 'h']
15-
MAKO_PREPROCESSOR = preprocessor
16-
MAKO_TRANSLATE_EXCEPTIONS = False
17-
1812
MAX_CONTENT_LENGTH = 16 * 1024 * 1024
1913

2014
# available languages

firefly/ext.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1-
from __future__ import absolute_import
21
# coding=utf-8
2+
from __future__ import absolute_import
33
from flask import current_app
44
from flask_babel import Babel
55
from flask_cache import Cache
66
from flask_login import LoginManager
77
from flask_mail import Mail
8-
from flask_mako import MakoTemplates
98
from flask_mongoengine import MongoEngine
109
from flask_redis import FlaskRedis
1110
from flask_restful import Api
@@ -18,7 +17,6 @@
1817
db = MongoEngine()
1918
login_manager = LoginManager()
2019
mail = Mail()
21-
mako = MakoTemplates()
2220
redis_store = FlaskRedis()
2321
security = Security()
2422

firefly/libs/template.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# coding=utf-8
2+
from flask import render_template as _render_template
3+
4+
from flask_login import current_user
5+
from flask_security.utils import url_for_security
6+
7+
8+
def render_template(template_name_or_list, **context):
9+
context.update({
10+
'current_user': current_user,
11+
'url_for_security': url_for_security
12+
})
13+
return _render_template(template_name_or_list, **context)

firefly/models/topic.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,3 +109,29 @@ def post_type(self):
109109

110110
def get_replies(self):
111111
return Comment.objects.filter(ref_id=self.id)
112+
113+
114+
def get_all_posts():
115+
posts = Post.objects.all()
116+
for post in posts:
117+
yield get_post(post)
118+
119+
120+
def get_post(post):
121+
id = post.id
122+
# author = post.author
123+
category = post.category
124+
category_name = ''
125+
category_slug = ''
126+
category_color = '#999'
127+
if category is not None:
128+
category_name = category.name
129+
category_slug = category.slug
130+
category_color = category.color
131+
title = post.title
132+
replies = len(post.comments)
133+
views = post.views
134+
created_at = post.created_at
135+
activity = post.recent_activity_time
136+
return (post, id, category, category_name, category_slug, category_color,
137+
title, replies, views, created_at, activity)

firefly/static/Gruntfile.js

Lines changed: 55 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -3,57 +3,67 @@
33
module.exports = function (grunt) {
44

55
grunt.initConfig({
6-
// autoprefixer: {
7-
// dist: {
8-
// files: {
9-
// 'stylesheets/style.css': 'stylesheets/_style.css'
10-
// }
11-
// }
12-
// },
13-
watch: {
14-
sass: {
15-
files: "stylesheets/scss/{,*/}*.scss",
16-
tasks: ["sass_globbing", "sass"]
17-
},
18-
css: {
19-
files: 'stylesheets/*.css',
20-
options: {
21-
livereload: true
22-
}
23-
},
24-
html: {
25-
files: '../templates/**/*.html',
26-
options: {
27-
livereload: true
28-
}
6+
react: {
7+
dynamic_mappings: {
8+
files: [
9+
{
10+
expand: true,
11+
cwd: 'javascripts/src/',
12+
src: ['*.jsx'],
13+
dest: 'javascripts',
14+
ext: '.js'
2915
}
16+
]
17+
}
18+
},
19+
watch: {
20+
sass: {
21+
files: "stylesheets/scss/{,*/}*.scss",
22+
tasks: ["sass_globbing", "sass"]
3023
},
31-
sass_globbing: {
32-
sass: {
33-
files: {
34-
'stylesheets/scss/common.scss': 'stylesheets/scss/common/*.scss',
35-
'stylesheets/scss/topic.scss': 'stylesheets/scss/topic/*.scss',
36-
'stylesheets/scss/home.scss': 'stylesheets/scss/home/*.scss',
37-
},
38-
options: {
39-
useSingleQuoates: false
40-
}
41-
}
24+
css: {
25+
files: 'stylesheets/*.css',
26+
options: {
27+
livereload: true
28+
}
29+
},
30+
html: {
31+
files: '../templates/**/*.html',
32+
options: {
33+
livereload: true
34+
}
4235
},
36+
react: {
37+
files: 'javascripts/src/*.jsx',
38+
tasks: ['react']
39+
}
40+
},
41+
sass_globbing: {
4342
sass: {
44-
dev: {
45-
files: {
46-
'stylesheets/index.css': 'stylesheets/scss/index.scss',
47-
'stylesheets/post.css': 'stylesheets/scss/post.scss',
48-
}
49-
}
43+
files: {
44+
'stylesheets/scss/common.scss': 'stylesheets/scss/common/*.scss',
45+
'stylesheets/scss/topic.scss': 'stylesheets/scss/topic/*.scss',
46+
'stylesheets/scss/home.scss': 'stylesheets/scss/home/*.scss',
47+
},
48+
options: {
49+
useSingleQuoates: false
50+
}
51+
}
52+
},
53+
sass: {
54+
dev: {
55+
files: {
56+
'stylesheets/index.css': 'stylesheets/scss/index.scss',
57+
'stylesheets/post.css': 'stylesheets/scss/post.scss',
58+
}
5059
}
60+
}
5161
});
5262

53-
grunt.loadNpmTasks('grunt-contrib-watch');
54-
grunt.loadNpmTasks('grunt-contrib-sass');
55-
grunt.loadNpmTasks('grunt-sass-globbing');
56-
// grunt.loadNpmTasks('grunt-autoprefixer');
63+
grunt.loadNpmTasks('grunt-contrib-watch');
64+
grunt.loadNpmTasks('grunt-contrib-sass');
65+
grunt.loadNpmTasks('grunt-sass-globbing');
66+
grunt.loadNpmTasks('grunt-react');
5767

58-
grunt.registerTask('default', ['sass_globbing', 'sass', 'watch']);
68+
grunt.registerTask('default', ['sass_globbing', 'sass', 'watch', 'react']);
5969
};

firefly/static/javascripts/libs/react-0.13.0.min.js

Lines changed: 15 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

firefly/static/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
"grunt-contrib-sass": "^0.9.2",
2121
"grunt-contrib-uglify": "^0.8.1",
2222
"grunt-contrib-watch": "^0.6.1",
23+
"grunt-react": "^0.12.2",
2324
"grunt-sass-globbing": "^1.2.0"
2425
}
2526
}

firefly/templates/403.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
h1 对不起,您没有权限访问这个页面
1+
<h1>对不起,您没有权限访问这个页面</h1>

firefly/templates/404.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
h1 这里没有你要找的内容
1+
<h1>这里没有你要找的内容</h1>

0 commit comments

Comments
 (0)