Skip to content

Commit 6494267

Browse files
committed
widgets/topic_item.html use jinja2
1 parent 6b2a3a9 commit 6494267

File tree

6 files changed

+119
-49
lines changed

6 files changed

+119
-49
lines changed

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/templates/categories/list.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
{% extends "base.html" %}
12
-inherit /base.html
23
- block content
34
h1

firefly/templates/index.html

Lines changed: 84 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,90 @@
44
首页
55
{% endblock %}
66

7-
{% if current_user.is_authenticated() %}
8-
head_script
9-
{% endif %}
7+
{% block outlet %}
8+
<div class="list-controls">
9+
<div class="container list-control">
10+
<ol class="category-breadcrumb">
11+
<li class="bullet">
12+
<a class="badge-category" href="" style="background-color: #eee; color: #333">
13+
all categories
14+
</a>
15+
<a class="badge-category category-dropdown-button" href="" style="background-color: #eee; color: #333">
16+
<i class="fa fa-caret-right"></i>
17+
</a>
18+
<section class="category-dropdown-menu hidden">
19+
<div class="cat">
20+
<a class="badge-category" href="/">
21+
all categories
22+
</a>
23+
</div>
24+
</section>
25+
</li>
26+
</ol>
27+
<ul id="navigation-bar" class="nav nav-pills">
28+
<li class="active" title="topics with recent posts">
29+
<a href="/latest">Latest</a>
30+
</li>
31+
<li title="all topics grouped by category">
32+
<a href="/categories">Categories</a>
33+
</li>
34+
</ul>
35+
{% if current_user.is_authenticated() %}
36+
<button id="create-topic" class="btn btn-default right">
37+
<i class="fa fa-plus">New Topic</i>
38+
</button>
39+
{% endif %}
40+
</div>
41+
<div class="container list-container">
42+
<div id="list-area">
43+
<div class="contents">
44+
<table class="topic-list">
45+
<thead>
46+
<tr>
47+
<th class="default">主题</th>
48+
<th class="category">分类
49+
<i class="fa fa-chevron-down"></i>
50+
</th>
51+
<th class="posts num">回应数</th>
52+
<th class="views num">阅读量</th>
53+
<th class="activity num">最近活跃时间</th>
54+
</tr>
55+
</thead>
56+
<tbody class="effeckt-list" data-effeckt-type="pop-in">
57+
{% for post, id, category, category_name, category_slug, category_color, title, replies, views, created_at, activity in posts %}
58+
<tr class={{"topic-list-item new-item" if is_new else "topic-list-item"}}>
59+
<td class="main-link clearfix">
60+
<a class="title" href="{{url_for('post.detail', id=id)}}">
61+
{{title}}
62+
</a>
63+
<span class="topic-post-badges"></span>
64+
</td>
65+
<td class="category">
66+
<a class="badge-wrapper bullet" href="/category/{{category_slug}}">
67+
<span class="badge-category-parent-bg" style="background-color: {{category_color}}"></span>
68+
<span class="badge-category clear-badge">{{category_name}}</span>
69+
</a>
70+
</td>
71+
<td class="num posts-map post">
72+
<a class="posts-map badge-posts" href="">{{replies}}</a>
73+
</td>
74+
<td class="num views">
75+
<span class="number">{{views}}</span>
76+
</td>
77+
<td class="num age activity">
78+
<a href="">
79+
<span class="relative-date" title="{{activity}}">{{activity}}</span>
80+
</a>
81+
</td>
82+
</tr>
83+
{% endfor %}
84+
</tbody>
85+
</table>
86+
</div>
87+
</div>
88+
</div>
89+
</div>
90+
{% endblock %}
1091

1192
{% block head_script %}
1293
<link href="{{url_for('static', filename='stylesheets/base16-light.css')}}" rel="stylesheet"/>

firefly/templates/widgets/topic_item.html

Lines changed: 0 additions & 38 deletions
This file was deleted.

firefly/views/home.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
# coding=utf-8
22
from __future__ import absolute_import
3-
from flask import request, jsonify, redirect, url_for, render_template
3+
from flask import request, jsonify, redirect, url_for
44
from flask.views import MethodView
55
from flask.blueprints import Blueprint
6-
from flask_mako import render_template_def
76
from flask_login import login_user, current_user, login_required
87

98
from firefly.forms.user import LoginForm, RegisterForm
10-
from firefly.models.topic import Category, Post, Comment
9+
from firefly.models.topic import (Category, Post, Comment, get_all_posts,
10+
get_post)
1111
from firefly.models.user import User
12+
from firefly.libs.template import render_template
1213

1314

1415
bp = Blueprint("home", __name__, url_prefix="/")
@@ -17,7 +18,7 @@
1718
class HomeView(MethodView):
1819

1920
def get(self):
20-
posts = Post.objects.all()
21+
posts = get_all_posts()
2122
return render_template('index.html', posts=posts)
2223

2324

@@ -35,10 +36,8 @@ def post(self):
3536
post = Post(title=title, content=content, category=category,
3637
author=User.objects.get_or_404(id=author_id))
3738
post.save()
38-
html = render_template_def(
39-
'/widgets/topic_item.html', 'main', post=post, is_new=True)
40-
41-
return jsonify(ok=0, html=html)
39+
res = get_post(post)
40+
return jsonify(ok=0, res=res)
4241

4342

4443
class CreateCommentView(MethodView):

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ flask-restful
1111
MarkupSafe
1212
mistune
1313
pygments
14+
plim
1415
arrow
1516
Flask_Security
1617
facebook-sdk

0 commit comments

Comments
 (0)