Skip to content
Browse files

count hit for topic

  • Loading branch information...
1 parent fd42ce6 commit aeef854793dfea8a274ebf9e0d7f5b6cd677145a lepture committed Mar 9, 2012
Showing with 28 additions and 2 deletions.
  1. +24 −1 june/handlers/topic.py
  2. +1 −1 june/locale/zh_CN.csv
  3. +3 −0 june/templates/topic.html
View
25 june/handlers/topic.py
@@ -149,7 +149,7 @@ def head(self, id):
pass
def get(self, id):
- topic = self.get_topic_by_id(id)
+ topic = self._hit_topic(id)
if not topic:
self.send_error(404)
return
@@ -164,6 +164,29 @@ def get(self, id):
return
self.render('topic.html', topic=topic, node=node, users=users)
+ def _hit_topic(self, id):
+ key = 'hit$topic:%s' % str(id)
+ count = self.cache.get(key)
+ if count is None:
+ count = 1
+ self.cache.set(key, 1)
+ else:
+ self.cache.incr(key)
+ if count > 10:
+ topic = self.db.query(Topic).filter_by(id=id).first()
+ if not topic:
+ return None
+ self.cache.set(key, 1)
+ topic.hits += 10
+ topic.impact += 10
+ self.db.add(topic)
+ self.db.commit()
+ self.cache.delete('topic:%s' % str(id))
+ else:
+ topic = self.get_topic_by_id(id)
+ topic.hits = topic.hits + count
+ return topic
+
@require_user
def post(self, id):
# for topic reply
View
2 june/locale/zh_CN.csv
@@ -62,4 +62,4 @@
"Flush Cache", 清空缓存
"Member Manage", 会员管理
-"You have %s unread message", 你有%s条未读消息
+"Hits", 点击
View
3 june/templates/topic.html
@@ -20,7 +20,10 @@ <h1 class="entry-title">{{escape(topic.title)}}</h1>
<a href="/member/{{escape(user.username)}}">{{escape(user.username)}}</a>
<time class="updated" datetime="{{xmldatetime(topic.created)}}">{{topic.created.strftime("%b %d, %Y %H:%M")}}</time>
+ • {{topic.hits }} {{_("Hits")}}
+ {% if current_user and current_user.role > 9 %}
• {{topic.impact}}
+ {% end %}
</div>
</div>
{% if current_user %}

0 comments on commit aeef854

Please sign in to comment.
Something went wrong with that request. Please try again.