Yang Zhang avatar Yang Zhang committed b6054c0

add vote feature on main article

Comments (0)

Files changed (5)

sohuone/templates/article.html

 {%extends 'base.html'%}
 
 {%block singlepage%}
-<div class="offset2 span8">
+<div class="offset2 span8" articleid='{{article.id}}'>
     <div class="page-header">
       <h3>{{article.title}} <small>{{article.create_date.date()}}日{{article.create_date.hour}}时 | 作者{{article.poster.nickname}}</small></h3>
       字体调整 <button onclick='increase_fontsize()'class='btn btn-small'><i class="icon-plus"></i></button>
             <button onclick='decrease_fontsize()' class='btn btn-small'><i class="icon-minus"></i></button>
+        <div class='span2 pull-right' >
+            {%include 'include/vote.html'%}
+        </div>
+        
+            
     </div>
     
     <div class='content fontchangeable' style='line-height: 1.5em;'>

sohuone/templates/include/show_article_title.html

 <div class='artitcle  row-fluid' articleid='{{article.id}}'>
     <div class='span2'>
-        <a href='javascript:void(0);' class='thumbs thumbs-up' onclick='thumb_up("{{article.id}}")'>
-        <i class="icon-thumbs-up"></i>
-        <span class="badge badge-important up_counter">{{article.up_counter}}</span>
-        </a>
-        
-        <a href='javascript:void(0);' class='thumbs muted' onclick='thumb_down("{{article.id}}")'>
-        <i class="icon-thumbs-down"></i>
-        <span class="badge down_counter">{{article.down_counter}}</span>
-        </a>
-        {%if user%}
-            {%if article.id in like_articles%}
-                <div class="label label-info like-info" style='display:block'>你喜欢了这篇文章</div>
-                <div class="label muted dislike-info">你不喜欢这篇文章</div>
-            {%elif article.id in dislike_articles%}
-                <div class="label label-info like-info">你喜欢了这篇文章</div>
-                <div class="label muted dislike-info" style='display:block'>你不喜欢这篇文章</div>
-            {%else%}
-                <div class="label label-info like-info">你喜欢了这篇文章</div>
-                <div class="label muted dislike-info">你不喜欢这篇文章</div>
-            {%endif%}
-        {%else%}
-            <div class="label label-info like-info">你喜欢了这篇文章</div>
-            <div class="label muted dislike-info">你不喜欢这篇文章</div>
-        {%endif%}
+        {%include 'include/vote.html'%}
     </div>
     
     <div class='span10'>

sohuone/templates/include/vote.html

+<a href='javascript:void(0);' class='thumbs thumbs-up' onclick='thumb_up("{{article.id}}")'>
+<i class="icon-thumbs-up"></i>
+<span class="badge badge-important up_counter">{{article.up_counter}}</span>
+</a>
+
+<a href='javascript:void(0);' class='thumbs muted' onclick='thumb_down("{{article.id}}")'>
+<i class="icon-thumbs-down"></i>
+<span class="badge down_counter">{{article.down_counter}}</span>
+</a>
+{%if user%}
+    {%if article.id in like_articles or is_liked == 1%}
+        <div class="label label-info like-info" style='display:block'>你喜欢了这篇文章</div>
+        <div class="label muted dislike-info">你不喜欢这篇文章</div>
+    {%elif article.id in dislike_articles or is_liked ==-1%}
+        <div class="label label-info like-info">你喜欢了这篇文章</div>
+        <div class="label muted dislike-info" style='display:block'>你不喜欢这篇文章</div>
+    {%else%}
+        <div class="label label-info like-info">你喜欢了这篇文章</div>
+        <div class="label muted dislike-info">你不喜欢这篇文章</div>
+    {%endif%}
+{%else%}
+    <div class="label label-info like-info">你喜欢了这篇文章</div>
+    <div class="label muted dislike-info">你不喜欢这篇文章</div>
+{%endif%}

sohuone/test1.sql

     articleid                   INTEGER,
     like_degree                 INTEGER DEFAULT 0, -- 1, like, 0 soso, -1 dislike
     FOREIGN KEY(userid) REFERENCES user(id),
-    FOREIGN KEY(articleid) REFERENCES article(id)
+    FOREIGN KEY(articleid) REFERENCES article(id),
+    PRIMARY KEY(userid, articleid)
 );
 
 

sohuone/views/fontend.py

         like_articles = Article.user_like(session['user'].id)
         dislike_articles = set([x['articleid'] for x in like_articles if x['like_degree'] == -1])
         like_articles = set([x['articleid'] for x in like_articles if x['like_degree'] == 1])
-        return render_template('main.html', 
-                                articles = articles, 
+        return render_template('main.html',
+                                articles = articles,
                                 categories=categories,
                                 dislike_articles = dislike_articles,
                                 like_articles=like_articles)
-    
-    
+
+
     articles = Article.get_post_title_list()
     categories = Article.get_categories()
     return render_template('main.html', articles = articles, categories=categories)
 
 @frontend.route("/article/<int:articleid>")
 def show_article(articleid):
+    if('user' in session):
+        article = Article.get_post_byid(articleid)
+        categories = Article.get_categories()
+        is_liked = session['user'].like_degree(articleid)
+        return render_template('article.html',\
+                                article = article,\
+                                categories=categories,\
+                                is_liked = is_liked)
+
     article = Article.get_post_byid(articleid)
     categories = Article.get_categories()
     return render_template('article.html', article = article, categories=categories)
-    
+
 
 
 @frontend.route("/login", methods = ['GET' ,'POST'])
     else:
         flash(u'登入失败, 请重试','error')
         return redirect(url_for('login'))
-        
+
 @frontend.route("/login_require", methods = ['GET'])
 def login_require():
     flash(u"你需要先登入!","error")
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.