Skip to content

Commit 31e8e77

Browse files
committed
posts/detail.html use jinja2
1 parent 31b08a6 commit 31e8e77

File tree

3 files changed

+164
-110
lines changed

3 files changed

+164
-110
lines changed

firefly/templates/base.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,9 @@
4949
</div>
5050
<div class="panel">
5151
{% if current_user.is_authenticated() %}
52-
Hi {{current_user.username if current_user.username else current_user.cn}}
52+
Hi {{(current_user.username if current_user.username else current_user.cn).decode('utf-8')}}
5353
<a title="退出" class="btn-primary btn-small btn sign-out-button" href="{{url_for('security.logout')}}">
54+
<i class="fa fa-sign-out"></i>
5455
</a>
5556
{% else %}
5657
<button title="注册" class="btn-primary btn-small btn sign-up-button">注册</button>
Lines changed: 137 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -1,116 +1,145 @@
1-
-py!
2-
from firefly.libs.markdown import Markdown
3-
from firefly.views.utils import short_timesince
4-
-inherit /base.html
5-
- namespace name='forms' /_forms.html
6-
-namespace name="widgets_editor" /widgets/markdown_editor.html
1+
{% extends "base.html" %}
2+
{% import 'widgets/markdown_editor.html' as markdown_editor %}
73

8-
-def outlet()
9-
div#topic-title
10-
div.container
4+
{% block outlet %}
5+
<div id="topic-title">
6+
<div class="container">
117
div.title-wrapper
12-
h1
13-
a.fancy-title href="${post.url()}"
14-
${post.title}
15-
- if post.category
16-
div.category-title
17-
a.badge-wrapper.bullet href="/c/${post.category.id}"
18-
span.badge-category-bg style="background-color: ${post.category.color if post.category else '#999'}"
19-
span.badge-category.clear-badge style="vertical-align: middle;" title="${post.category.description}"
20-
${post.category.name}
21-
div.container.posts
22-
div.row
23-
section#topic.topic-area data-topic-id="${post.id}"
24-
div.posts-wrapper
25-
- for index, p in enumerate([post] + post.comments)
26-
-py
27-
cloak_id = 'post-cloak-{}'.format(index)
28-
class c(object):
29-
id = 100001
30-
name = 'test1'
31-
avatar = lambda x:'https://meta-discourse.global.ssl.fastly.net/user_avatar/meta.discourse.org/codinghorror/90/5297.png'
32-
cn = 'Test'
33-
def url(self):
34-
return '/user/1000001'
35-
author = p.author if p.author else c()
36-
author_name = author.username if hasattr(author, 'username') and author.username else author.cn
37-
div#${cloak_id}.post-cloak data-comment-id=${p.id} data-order-id=${index}
38-
div.topic-post.clearfix.regular
39-
article.boxed data-user-id="${author.id}" data-post-id="${p.id}"
40-
div.row
41-
div.topic-avatar
42-
div.contents
43-
a href="${author.url()}" data-user-card="${author_name}"
44-
img.avatar alt="" width="45" height="45" src="${author.avatar()}" title="${author_name}"
45-
div.topic-body
46-
div.topic-meta-data
47-
div.names.trigger-user-card
48-
span.username.staff
49-
a href="${author.url()}" data-user-card="${author_name}"
50-
${author_name}
51-
i.fa.fa-shield title="This user is a moderator" alt="This user is a moderator"
52-
span.full-name
53-
a href="${author.url()}" data-user-card="${author_name}"
54-
${author.cn}
55-
span.user-title
56-
a.user-group href="/groups/discourse"
57-
co-founder
58-
div.post-info
59-
a.post-date href="/t/quickly-tell-someone-why-they-should-look-at-a-topic/27503/2" data-bindattr-1516="1516"
60-
span.relative-date title="${p.created_at.strftime('%c %p')}" data-time="1428885788713" data-format="tiny"
61-
${short_timesince(p.created_at)}
62-
div.contents.regular
63-
div.cooked
64-
- if p.content
65-
-py
66-
content = Markdown.render(p.content)
67-
- if p.post_type == 'Quote'
68-
blockquote
69-
${content|n}
70-
- else
71-
${content|n}
72-
- if hasattr(p, 'embed_code') and p.embed_code
73-
${p.embed_code}
74-
- if hasattr(p, 'image_url') and p.image_url
75-
p
76-
img" src=${p.image_url}"
77-
p
78-
${p.created_at.strftime('%H:%M %Y-%m-%d')}
79-
section.post-menu-area.clearfix
80-
nav.post-controls
81-
-py
82-
coments = p.get_replies() if p.post_type == 'Comment' else []
83-
-if coments:
84-
button.show-replies
85-
span.badge-posts
86-
${len(coments)}
87-
Replies
88-
i.fa.fa-chevron-down
89-
div.actions
90-
- if current_user.is_authenticated()
91-
button.like title='喜欢这个主题?'
92-
i.fa.fa-heart
93-
button title='分享' data-share-url="/t/dd"
94-
i.fa.fa-link
95-
- if current_user.is_authenticated()
96-
button.bookmark title='标记'
97-
div.read-icon
98-
button.create title='评论'
99-
i.fa.fa-reply
100-
评论
101-
section.embedded-posts.bottom.hide
8+
<h1>
9+
<a class="fancy-title" href="{{post.url()}}">{{post.title}}</a>
10+
</h1>
11+
{% if post.category %}
12+
<div class="category-title">
13+
<a class="badge-wrapper bullet" href="/c/{{post.category.id}}">
14+
<span class="badge-category-bg" style="background-color: {{post.category.color if post.category else '#999'}}"></span>
15+
<span class="badge-category clear-badge" style="vertical-align: middle;" title="{{post.category.description}}">{{post.category.name}}</span>
16+
</a>
17+
{% endif %}
18+
</div>
19+
</div>
20+
</div>
21+
<div class="container posts">
22+
<div class="row">
23+
<section id="topic" class="topic-area" data-topic-id="{{post.id}}">
24+
<div class="posts-wrapper">
25+
{% for p in [post] + post.comments %}
26+
{% set author = gen_author(p) %}
27+
{% set author_name = gen_author_name(p).decode('utf-8') %}
28+
{% set index = loop.index %}
29+
<div id="{{'post-cloak-{}'.format(index)}}" class="post-cloak" data-comment-id={{p.id}} data-order-id={{index}}>
30+
<div class="topic-post clearfix regular">
31+
<article class="boxed" data-user-id="{{author.id}}" data-post-id="{{p.id}}">
32+
<div class="row">
33+
<div class="topic-avatar">
34+
<div class="contents">
35+
<a href="{{author.url()}}" data-user-card="{{author_name}}" >
36+
<img class="avatar" alt="" src="{{author.avatar()}}" width="45" height="45" title="{{author_name}}"/>
37+
</a>
38+
</div>
39+
</div>
40+
<div class="topic-body">
41+
<div class="topic-meta-data">
42+
<div class="names trigger-user-card">
43+
<span class="username staff">
44+
<a href="{{author.url()}}" data-user-card="{{author_name}}">
45+
{{author_name}}
46+
</a>
47+
<i class="fa fa-shield" title="This user is a moderator" alt="This user is a moderator"></i>
48+
</span>
49+
<span class="full-name">
50+
<a href="{{author.url()}}" data-user-card="{{author_name}}">
51+
{{author.cn.decode('utf-8')}}
52+
</a>
53+
</span>
54+
<span class="user-title">
55+
<a class="user-group" href="/groups/discourse">
56+
co-founder
57+
</a>
58+
</span>
59+
</div>
60+
<div class="post-info">
61+
<a class="post-date" href="/t/quickly-tell-someone-why-they-should-look-at-a-topic/27503/2" data-bindattr-1516="1516">
62+
<span class="relative-date" title="{{p.created_at.strftime('%c %p')}}" data-time="1428885788713" data-format="tiny" >
63+
{{short_timesince(p.created_at)}}
64+
</span>
65+
</a>
66+
</div>
67+
<div class="contents regular">
68+
<div class="cooked">
69+
{% if p.content %}
70+
{% autoescape false %}
71+
{{Markdown.render(p.content)}}
72+
{% endautoescape %}
73+
{% endif %}
74+
{% if hasattr(p, 'embed_code') and p.embed_code %}
75+
{{p.embed_code}}
76+
{% endif %}
77+
{% if hasattr(p, 'image_url') and p.image_url %}
78+
<p><img src={{p.image_url}}></p>
79+
<p>{{p.created_at.strftime('%H:%M %Y-%m-%d')}}</p>
80+
{% endif %}
81+
</div>
82+
<section class="post-menu-area clearfix">
83+
<nav class="post-controls">
84+
{% set coments = p.get_replies() if p.post_type == 'Comment' else [] %}
85+
{% if coments %}
86+
<button class="show-replies">
87+
<span class="badge-posts">{{len(coments)}}</span>
88+
Replies
89+
<i class="fa fa-chevron-down"></i>
90+
</button>
91+
{% endif %}
92+
<div class="actions">
93+
{% if current_user.is_authenticated() %}
94+
<button class="like" title='喜欢这个主题?'>
95+
<i class="fa fa-heart"></i>
96+
</button>
97+
{% endif %}
98+
<button title='分享' data-share-url="/t/dd">
99+
<i class="fa fa-link"></i>
100+
</button>
101+
{% if current_user.is_authenticated() %}
102+
<button class="bookmark" title='标记'>
103+
<div class="read-icon"></div>
104+
</button>
105+
<button class="create" title='评论'>
106+
<i class="fa fa-reply">评论</i>
107+
</button>
108+
{% endif %}
109+
</div>
110+
</nav>
111+
</section>
112+
<section class="embedded-posts bottom hide"></section>
113+
</div>
114+
</div>
115+
</div>
116+
</div>
117+
</article>
118+
</div>
119+
</div>
120+
{% endfor %}
121+
</div>
122+
</section>
123+
</div>
124+
</div>
125+
{% endblock %}
102126

103-
-def head_script()
104-
link href="${url_for('static', filename='stylesheets/base16-light.css')}" rel="stylesheet"
105-
link href="${url_for('static', filename='stylesheets/codemirror.css')}" rel="stylesheet"
106-
link href="${url_for('static', filename='stylesheets/post.css')}" rel="stylesheet" media="all"
127+
{% block head_script %}
128+
<link href="{{url_for('static', filename='stylesheets/base16-light.css')}}" rel="stylesheet"/>
129+
<link href="{{url_for('static', filename='stylesheets/codemirror.css')}}" rel="stylesheet"/>
130+
<link href="{{url_for('static', filename='stylesheets/post.css')}}" rel="stylesheet" media="all"/>
131+
{% endblock %}
107132

108-
-def footer_script()
109-
script
133+
{% block footer_script %}
134+
<script>
110135
require(['../../static/javascripts/common'], function (common) {
111136
require(['post']);
112137
});
138+
</script>
139+
{% endblock %}
113140

114-
- if current_user.is_authenticated()
115-
- def others()
116-
${widgets_editor.topic()}
141+
{% block others %}
142+
{% if current_user.is_authenticated() %}
143+
{{ markdown_editor.topic() }}
144+
{% endif %}
145+
{% endblock %}

firefly/views/post.py

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,42 @@
66

77
from firefly.models.topic import Post, Comment
88
from firefly.libs.template import render_template
9+
from firefly.libs.markdown import Markdown
10+
from firefly.views.utils import short_timesince
911

1012

1113
bp = Blueprint("post", __name__, url_prefix="/post")
1214

1315

16+
def gen_author(p):
17+
class c(object):
18+
id = 100001
19+
name = 'test1'
20+
avatar = lambda x: 'https://meta-discourse.global.ssl.fastly.net/user_avatar/meta.discourse.org/codinghorror/90/5297.png' # noqa
21+
cn = 'Test'
22+
23+
def url(self):
24+
return '/user/1000001'
25+
author = p.author if p.author else c()
26+
return author
27+
28+
29+
def gen_author_name(p):
30+
author = gen_author(p)
31+
return author.username if hasattr(author, 'username') and \
32+
author.username else author.cn
33+
34+
1435
class DetailView(MethodView):
1536

1637
form = model_form(Comment, exclude=['created_at', 'author', 'id'])
1738

1839
def get(self, id):
1940
post = Post.objects.get_or_404(id=id)
2041
Post.objects(id=id).update_one(inc__views=1)
21-
return render_template('posts/detail.html', post=post)
42+
return render_template('posts/detail.html', post=post, hasattr=hasattr,
43+
Markdown=Markdown, gen_author=gen_author,
44+
gen_author_name=gen_author_name,
45+
short_timesince=short_timesince)
2246

2347
bp.add_url_rule('/<int:id>/', view_func=DetailView.as_view('detail'))

0 commit comments

Comments
 (0)