日期:2014-05-16 浏览次数:20388 次
class UserTag(mongopie.Model): ''' 用户的标签云 ''' user = mongopie.StringField() tag = mongopie.StringField() count = mongopie.IntegerField(default=0) class Vote(mongopie.Model): ''' 一次投票,voter在created_at时间给votee的tag标签投了一票''' voter = mongopie.StringField() votee = mongopie.StringField() tag = mongopie.StringField() created_at = mongopie.DateTimeField(auto_now=True)
class UserTag(mongopie.Model): ...... @classmethod def add_tag(cls, vote): ut = cls.find_one(user=vote.votee, tag=vote.tag) if not ut: ut = cls() ut.user = vote.votee ut.tag = vote.tag ut.count += 1 ut.save() return ut class Vote(mongopie.Model): ...... def on_created(self): UserTag.add_tag(self)
def make_vote(voter, votee, tag): v = Vote() v.voter = voter v.votee = votee v.tag = tag v.save() return v def test(): # Tom 给Jack的hacking 投了一票 make_vote('Tom', 'Jack', 'Hacking') # Jerry 给Jack的Food 投了一票 make_vote('Jerry', 'Jack', 'Food') # Jerry 也给Jack的Hacking投了一票 make_vote('Jerry', 'Jack', 'Hacking') for ut in UserTag.find(user='Jack'): # 找出Jack所有的投票 print ut.get_dict() #输出结果是 {'count': 2, '_id': ObjectId('4eecbce5347307034e000001'), 'tag': u'Hacking', 'user': u'Jack'} {'count': 1, '_id': ObjectId('4eecbce5347307034e000003'), 'tag': u'Food', 'user': u'Jack'}