Skip to content

Commit

Permalink
增加文章置顶功能
Browse files Browse the repository at this point in the history
  • Loading branch information
时雨 committed May 6, 2015
1 parent 3596dab commit c5fb337
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 204 deletions.
25 changes: 9 additions & 16 deletions controller/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,29 +19,13 @@ def prepare(self):
if self.power != "admin":
self.redirect("/")

def get(self, *args, **kwargs):
self.home_action(*args, **kwargs)

def post(self, *args, **kwargs):
method = ("%s_action" % args[0]) if len(args) > 0 else "home_action"
if hasattr(self, method):
getattr(self, method)(*args, **kwargs)
else:
self.home_action(*args, **kwargs)

@tornado.web.asynchronous
@gen.coroutine
def home_action(self, *args, **kwargs):
codes = []
cursor = self.db.invite.find({
"used": {"$eq": False},
"time": {"$gt": (time.time() - self.settings["invite_expire"])}
})
while (yield cursor.fetch_next):
codes.append(cursor.next_object()["code"])
codes = "\n".join(codes)
self.render("admin.htm", flash = self.flash, codes = codes)

@tornado.web.asynchronous
@gen.coroutine
def register_action(self, *args, **kwargs):
Expand Down Expand Up @@ -112,6 +96,15 @@ def article_action(self, *args, **kwargs):
"open": open
}
})
elif method in ("top", "notop"):
top = True if method == "top" else False
post = yield self.db.article.find_and_modify({
"_id": ObjectId(id)
}, {
"$set": {
"top": top
}
})
elif method == "del":
post = yield self.db.article.find_and_modify({
"_id": ObjectId(id)
Expand Down
2 changes: 1 addition & 1 deletion controller/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def get(self, *args, **kwargs):
page = intval(args[1])
if not page or page <= 0 : page = 1
cursor = self.db.article.find()
cursor.sort([('time', pymongo.DESCENDING)]).limit(limit).skip((page - 1) * limit)
cursor.sort([('top', pymongo.DESCENDING), ('time', pymongo.DESCENDING)]).limit(limit).skip((page - 1) * limit)
count = yield cursor.count()
posts = yield cursor.to_list(length = limit)
sorts = yield self.get_sort()
Expand Down
3 changes: 2 additions & 1 deletion controller/publish.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,8 @@ def post(self, *args, **kwargs):
"star": False,
"rank": 0,
"comment": [],
"open": False
"open": False,
"top": False
}
model = ArticleModel()
if not model(article):
Expand Down
10 changes: 10 additions & 0 deletions static/assets/js/post.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,16 @@ $(document).ready(function(){
}
return false;
})
$("#top a").on("click", function(){
if (this.hash == "#top") {
$("#open [name='method']").val('notop');
$("#open").submit();
}else if(this.hash == "#notop") {
$("#open [name='method']").val('top');
$("#open").submit();
}
return false;
})
$("#rank a").on("click", function(){
var rank = prompt("输入奖励或惩罚的分数");
if (!rank || parseInt(rank) == 0){
Expand Down
186 changes: 0 additions & 186 deletions templates/admin.htm

This file was deleted.

8 changes: 8 additions & 0 deletions templates/post.htm
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,14 @@
{% raw xsrf_form_html() %}
</form>
</li>
<li>
<form action="/admin/article" method="post" id="top">
<input name="id" value="{{ post['_id'] }}" type="hidden">
<input name="method" type="hidden">
<a class="am-text-sm" href="#{{ 'top' if post.get('top') else 'notop' }}"><span class="am-icon-thumb-tack"></span>&ensp;{% if post.get('top') %}&nbsp;不置顶{% else %}&nbsp;置顶{% end %}</a>
{% raw xsrf_form_html() %}
</form>
</li>
{% end %}
</ul>
</div>
Expand Down

0 comments on commit c5fb337

Please sign in to comment.