コード例 #1
0
    def update_unread_count(self, user, code, operate="add"):
        """
        @note: 更新提醒未读数
        """

        if UnreadType.objects.filter(code=code).count() == 0:
            return False

        urc, created = self.get_or_create_count_info(user)
        count_info = json.loads(urc.count_info)

        if not count_info.has_key(code):
            count_info.setdefault(code, 0)
        # 加一或者重置
        if operate == 'add':
            count_info[code] += 1
        else:
            count_info[code] = 0

        urc.count_info = json.dumps(count_info)
        urc.save()
        # 操作缓存
        user_id = utils.get_uid(user)
        cache_key = u'%s_%s' % ('unread_count', user_id)
        self.cache_obj.set(cache_key, count_info, 3600 * 24)

        return True
コード例 #2
0
 def get_unread_count_info(self, user):
     """
     @note: 获取未读数
     """
     user_id = utils.get_uid(user)
     try:
         count_info = json.loads(UnreadCount.objects.get(user_id=user_id).count_info)
     except UnreadCount.DoesNotExist:
         count_info = self.init_count_info()  # 没有就不用自动创建,更新的时候进行创建
     return count_info
コード例 #3
0
 def get_or_create_count_info(self, user):
     """
     @note: 获取用户未读数对象
     """
     user_id = utils.get_uid(user)
     obj_urcs = UnreadCount.objects.filter(user_id=user_id)
     created = True
     if not obj_urcs:
         count_info = self.init_count_info()
         urc = UnreadCount.objects.create(user_id=user_id, count_info=json.dumps(count_info))
         created = False
     else:
         urc = obj_urcs[0]
     return urc, created