Ejemplo n.º 1
0
 def get_comments_by_content(cls, content_id, start, count):
     cache = CacheManager().cache
     cache_key = CONTENT_COMMENT_CACHE_KEY % content_id
     end = start + count - 1
     comment_ids = cache.lrange(cache_key, start, end)
     comments = [cls.get(comment_id) for comment_id in comment_ids]
     return comments
Ejemplo n.º 2
0
 def left_change_times(self):
     cache = CacheManager().cache
     redis_key = flower_user_name_update_limit_key % self.user_id
     if cache.exists(redis_key):
         update_count = cache.get(redis_key)
         return 3 - int(update_count)
     else:
         return 3
Ejemplo n.º 3
0
 def get_contents_by_user(cls, user_id, start, count):
     cache = CacheManager().cache
     end = start + count - 1
     content_ids = cache.zrevrange(USER_CREATED_CONTENT_KEY % user_id,
                                   start, end)
     return content_ids and [
         Content.get(content_id) for content_id in content_ids
     ]
Ejemplo n.º 4
0
 def get_user_reward_rank(cls, start=0, count=10):
     cache = CacheManager().cache
     end = start + count - 1
     rst = cache.zrevrange(USER_REWARD_RANK_CACHE_KEY, start, end, True)
     if rst:
         return [{'user_id': d[0], 'amount': d[1]} for d in rst]
     else:
         return []
Ejemplo n.º 5
0
 def created_rooms(self):
     from starmachine.model.room import Room
     cache = CacheManager().cache
     cache_key = USER_CREATED_ROOM_KEY % self.id
     room_ids = list(cache.smembers(cache_key))
     return room_ids and [
         Room.get(room_id).jsonify(self) for room_id in room_ids
     ]
Ejemplo n.º 6
0
 def get_reward_rank(self, start, count):
     cache = CacheManager().cache
     redis_key = ROOM_USER_REWARD_CACHE_KEY % self.id
     end = start + count - 1
     rst = cache.zrevrange(redis_key, start, end, True)
     if rst:
         return [{'user_id': info[0], 'amount': info[1]} for info in rst]
     else:
         return []
Ejemplo n.º 7
0
 def get_user_amount_by_room(cls, room_id):
     cache = CacheManager().cache
     cache_key = ROOM_USER_CACHE_KEY % room_id
     if cache.exists(cache_key):
         return cache.zcard(cache_key)
     else:
         db = DbManager().db
         sql = 'select count(id) from {table} where room_id=%s'.format(table=cls.table)
         return db.get(sql, room_id).get('count(id)')
Ejemplo n.º 8
0
 def get_rooms_increase_rank(cls, year, month, start=0, count=10):
     redis_key = ROOM_INCREASE_RANK_CACHE_KEY % (year, month)
     cache = CacheManager().cache
     end = start + count - 1
     rst = cache.zrevrange(redis_key, start, end, True)
     if rst:
         return [{'room_id': d[0], 'increase': d[1]} for d in rst]
     else:
         return []
Ejemplo n.º 9
0
 def get_follows_by_user(cls, user_id, start=0, count=10):
     cache = CacheManager().cache
     end = start + count - 1
     user_follow_key = USER_FOLLOW_KEY % user_id
     rst = cache.zrevrange(user_follow_key, start, end, True)
     if rst:
         return [{'user_id': d[0], 'follow_time': d[1]} for d in rst]
     else:
         return []
Ejemplo n.º 10
0
 def flush_reward_cache(self):
     cache = CacheManager().cache
     now = datetime.now()
     for delta in xrange(1, 5):
         month = now.month - 1 + delta
         year = now.year + month / 12
         month = month % 12 + 1
         max_day = calendar.monthrange(year, month)[1]
         end_day = (datetime(year=year, month=month, day=max_day) + timedelta(days=1)).strftime('%Y-%m-%d')
         user_cache_key = USER_DATE_REWARD_CACHE % (self.room_id, end_day)
         cache.delete(user_cache_key)
Ejemplo n.º 11
0
 def get_users_by_room(cls, room_id):
     cache = CacheManager().cache
     cache_key = ROOM_USER_CACHE_KEY % room_id
     if cache.exists(cache_key):
         user_ids = cache.zrevrange(cache_key, 0, -1)
         return user_ids and [User.get(user_id) for user_id in user_ids]
     else:
         db = DbManager().db
         sql = 'select user_id from {table} where room_id=%s'.format(table=cls.table)
         user_ids_dict = db.query(sql, room_id)
         return user_ids_dict and [User.get(user_id_dict.get('user_id')) for user_id_dict in user_ids_dict]
Ejemplo n.º 12
0
 def has_followed(cls, user_id, follow_id):
     cache = CacheManager().cache
     cache_key = USER_FOLLOW_KEY % user_id
     if cache.exists(cache_key):
         return cache.zscore(cache_key, follow_id)
     else:
         db = DbManager().db
         sql = 'select id from {table} where user_id=%s and follow_id=%s'.format(table=cls.table)
         try:
             return db.query(sql, user_id, follow_id)[0]
         except:
             return None
Ejemplo n.º 13
0
 def room_exists_user(cls, room_id, user_id):
     cache = CacheManager().cache
     cache_key = ROOM_USER_CACHE_KEY % room_id
     if cache.exists(cache_key):
         return cache.zscore(cache_key, user_id)
     else:
         db = DbManager().db
         sql = 'select id from {table} where room_id=%s and user_id=%s'.format(table=cls.table)
         try:
             return db.query(sql, room_id, user_id)[0]
         except:
             return None
Ejemplo n.º 14
0
    def post(self):
        try:
            user_id = self.get_argument('user_id')
            name = self.get_argument('name')
        except MissingArgumentError:
            return self.error(MISSING_PARAMS)

        if FlowerUser.exists_name(name):
            return self.error(FLOWER_USER_NAME_EXISTS)

        if get_str_length(name) < 2:
            return self.error(FLOWER_NAME_LESS_MINIMUM_LIMIT)

        if get_str_length(name) > 20:
            return self.error(FLOWER_NAME_OVER_MAXIMUM_LIMIT)

        group_user_info = FlowerUser.get_by_user(user_id)
        if not group_user_info:
            try:
                random_name = random.choice(FLOWER_AVATAR_ARR)
                avatar_uri = 'static/avatar/flower/image_huaming_%[email protected]' % random_name
                group_user_info = FlowerUser.add(user_id, name, avatar_uri)
            except Exception as e:
                logger.error(u'添加用户花名成功。Error:[%s]' % e)
                return self.error(SYSTEM_ERROR)
        else:
            try:
                cache = CacheManager().cache
                redis_key = flower_user_name_update_limit_key % user_id
                if cache.exists(redis_key):
                    update_count = cache.get(redis_key)
                    if int(update_count) >= 3:
                        return self.error(FLOWER_USER_NAME_UPDATE_OVER_LIMIT)
                    else:
                        group_user_info.update_name(name)
                        cache.incr(redis_key, 1)
                else:
                    group_user_info.update_name(name)
                    cache.set(redis_key, 1)
                    cache.expire(redis_key, flower_user_name_update_limit_time)
            except Exception as e:
                logger.error(u'更新用户花名失败。Error:[%s]', e)
                return self.error(SYSTEM_ERROR)

        if UserChatStatus.is_flower_identity(user_id):
            rong_client.user_refresh(user_id, name=name)

        return self.render({
            'status': 0,
            'data': {
                'left_change_times': group_user_info.left_change_times(),
            }
        })
Ejemplo n.º 15
0
    def remove_and_black_user(cls, room_id, user_id):
        remove_time = datetime.now()
        db = DbManager().db
        db.execute('begin;')
        try:
            sql = 'delete from {table} where room_id=%s and user_id=%s'.format(table=cls.table)
            db.execute(sql, room_id, user_id)
            sql = 'insert into {table} (room_id, user_id, create_time) values (%s, %s, %s)'.format(table=RoomBlackUser.table)
            db.execute(sql, room_id, user_id, remove_time)
            db.execute('commit;')
            logger.info(u'在mysql中从房间中移除用户成功。Room:[%s], User:[%s]' % (room_id, user_id))
        except:
            db.execute('rollback;')
            logger.error(u'在mysql中从房间中移除用户失败。Room:[%s], User:[%s]' % (room_id, user_id))
            raise

        cache = CacheManager().cache
        room_user_key = ROOM_USER_CACHE_KEY % room_id
        user_room_key = USER_ROOM_CACHE_KEY % user_id
        try:
            cache.zrem(room_user_key, user_id)
            cache.zrem(user_room_key, room_id)
            logger.info(u'在redis中从房间中移除用户成功。Room:[%s], User:[%s]' % (room_id, user_id))
        except Exception as e:
            logger.error(u'从redis中删除房间用户关系失败。Error:[%s]' % e)
            raise

        cache.zincrby(ROOM_USER_COUNT_KEY, room_id, -1)
        room_increase_rank_key = ROOM_INCREASE_RANK_CACHE_KEY % (remove_time.year, remove_time.month)
        cache.zincrby(room_increase_rank_key, room_id, -1)
Ejemplo n.º 16
0
    def add(cls,
            creator_id,
            name,
            intro,
            tag_ids,
            avatar,
            limit_user_number,
            create_time,
            status=ROOM_PUBLIC,
            question_info=None):
        from starmachine.model.room_tag import RoomTag
        from starmachine.model.room_user import RoomUser
        db = DbManager().db
        cache = CacheManager().cache
        db.execute('begin;')
        try:
            sql = 'insert into {table} (creator_id, name, intro, avatar, limit_user_number, status, create_time) ' \
                'values (%s, %s, %s, %s, %s, %s, %s)'.format(table=cls.table)
            room_id = db.execute(sql, creator_id, name, intro, avatar,
                                 limit_user_number, status, create_time)
            if int(status) == ROOM_PRIVATE_NEED_VERIFY and question_info:
                question_name = question_info.get('question_name')
                options = question_info.get('options')
                sql = 'insert into {table} (room_id, name, create_time) values (%s, %s, %s)'.format(
                    table=RoomQuestion.table)
                room_question_id = db.execute(sql, room_id, question_name,
                                              create_time)
                for option in options:
                    text = option.get('text')
                    is_right_answer = option.get('is_right_answer')
                    sql = 'insert into {table} (question_id, text, is_right_answer) values (%s, %s, %s)'.format(
                        table=RoomQuestionOption.table)
                    db.execute(sql, room_question_id, text, is_right_answer)

            for tag_id in tag_ids:
                sql = 'insert into {table} (room_id, tag_id) values (%s, %s)'.format(
                    table=RoomTag.table)
                db.execute(sql, room_id, tag_id)
            sql = 'insert into {table} (room_id, balance) values (%s, %s)'.format(
                table=StarFund.table)
            db.execute(sql, room_id, '0.00')
            db.execute('commit;')
        except:
            db.execute('rollback;')
            raise
        cache_key = USER_CREATED_ROOM_KEY % creator_id
        cache.sadd(cache_key, room_id)
        RoomUser.add(room_id, creator_id, ROOM_USER_CREATOR)
        return room_id
Ejemplo n.º 17
0
 def add(cls, user_id, reply_user_id, content_id, text):
     db = DbManager().db
     create_time = datetime.now()
     cache = CacheManager().cache
     cache_key = CONTENT_COMMENT_CACHE_KEY % content_id
     sql = 'insert into {table} (user_id, reply_user_id, content_id, text, create_time) values ' \
         '(%s, %s, %s, %s, %s)'.format(table=cls.table)
     comment_id = db.execute(sql, user_id, reply_user_id, content_id, text,
                             create_time)
     if comment_id:
         content = Content.get(content_id)
         content.update(last_comment_time=create_time)
         cache.lpush(cache_key, comment_id)
         return cls(comment_id, user_id, reply_user_id, content_id, text,
                    create_time)
Ejemplo n.º 18
0
    def delete(self):
        db = DbManager().db
        content_id = self.id
        db.execute('begin;')
        try:
            sql = 'delete from {table} where id=%s'.format(table=self.table)
            db.execute(sql, content_id)
            db.execute('commit;')
        except:
            db.execute('rollback;')
            raise

        cache = CacheManager().cache
        posts_cache_key = POSTS_CACHE_KEY % content_id
        cache.delete(posts_cache_key)
Ejemplo n.º 19
0
    def get_liked_message_by_group(cls, group_id, start=0, count=10):
        cache = CacheManager().cache
        group_message_list_key = GROUP_MESSAGE_LIKED_LIST % group_id
        end = start + count - 1
        rst = cache.zrevrange(group_message_list_key, start, end, True)
        data = []
        for d in rst:
            message_id = d[0]
            amount = d[1]
            message = GroupMessage.get(message_id)
            if message:
                data.append({
                    'message_id': message.id,
                    'content': message.content,
                    'amount': amount,
                })

        return data
Ejemplo n.º 20
0
    def get_liked_message_amount_by_group(cls, group_id):
        cache = CacheManager().cache
        group_message_list_key = GROUP_MESSAGE_LIKED_LIST % group_id
        rst = cache.zrevrange(group_message_list_key, 0, -1, True)
        data = []
        for d in rst:
            message_id = d[0]
            amount = d[1]
            message = GroupMessage.get(message_id)
            if message:
                message_creator_id = message.creator_id
                data.append({
                    'message_id': message_id,
                    'amount': amount,
                    'creator_id': message_creator_id,
                })

        return data
Ejemplo n.º 21
0
 def add(cls, user_id, vote_id, option_id):
     db = DbManager().db
     cache = CacheManager().cache
     create_time = datetime.now()
     date = create_time.date()
     vote_cache_key = VOTE_PEOPLE_CACHE_KEY % vote_id
     option_cache_key = VOTE_OPTION_PEOPLE_CACHE_KEY % option_id
     vote_daily_count_redis_key = USER_DAILY_VOTE_COUNT % (user_id, date)
     sql = 'insert into {table} (user_id, vote_id, option_id, create_time) values (%s, %s, %s, %s)'.format(
         table=cls.table)
     db.execute(sql, user_id, vote_id, option_id, create_time)
     cache.zadd(vote_cache_key, get_int_date(create_time), user_id)
     cache.zadd(option_cache_key, get_int_date(create_time), user_id)
     cache.incr(vote_daily_count_redis_key, 1)
Ejemplo n.º 22
0
 def add_verify_code(cls, verify_type, telephone, verify_code):
     cache = CacheManager().cache
     key = VERIFY_CODE_CACHE_KEY.get(verify_type) % telephone
     cache.mset({
         key: verify_code,
     })
     cache.expire(key, settings.VERIFY_CODE_EXPIRE_TIME)
Ejemplo n.º 23
0
    def delete_room_user(cls, room_id, user_id):
        cache = CacheManager().cache
        delete_time = datetime.now()
        room_user_key = ROOM_USER_CACHE_KEY % room_id
        user_room_key = USER_ROOM_CACHE_KEY % user_id
        try:
            cache.zrem(room_user_key, user_id)
            cache.zrem(user_room_key, room_id)
            logger.info(u'从redis中删除房间用户关系成功。')
        except Exception as e:
            logger.error(u'从redis中删除房间用户关系失败。Error:[%s]' % e)
            raise

        job_delete_room_user.delay(room_id, user_id)
        cache.zincrby(ROOM_USER_COUNT_KEY, room_id, -1)
        room_increase_rank_key = ROOM_INCREASE_RANK_CACHE_KEY % (delete_time.year, delete_time.month)
        cache.zincrby(room_increase_rank_key, room_id, -1)
        return
Ejemplo n.º 24
0
    def add(cls, room_id, user_id, status=ROOM_USER_NORMAL):
        join_time = datetime.now()
        cache = CacheManager().cache
        room_user_key = ROOM_USER_CACHE_KEY % room_id
        user_room_key = USER_ROOM_CACHE_KEY % user_id
        score = get_int_date(join_time)
        try:
            cache.zadd(room_user_key, score, user_id)
            cache.zadd(user_room_key, score, room_id)
            logger.info(u'添加房间用户关系到redis成功。')
        except Exception as e:
            logger.error(u'添加房间关系用户到redis失败。Error:[%s]' % e)
            raise

        add_room_user.delay(room_id, user_id, join_time, status)
        cache.zincrby(ROOM_USER_COUNT_KEY, room_id)
        room_increase_rank_key = ROOM_INCREASE_RANK_CACHE_KEY % (join_time.year, join_time.month)
        cache.zincrby(room_increase_rank_key, room_id, 1)
        return
Ejemplo n.º 25
0
 def add(cls, creator_id, proverbs, status=CHECK_STATUS_PENDING):
     create_time = datetime.now()
     db = DbManager().db
     cache = CacheManager().cache
     sql = 'insert into {table} (creator_id, proverbs, status, create_time) values (%s, %s, %s, %s)'.format(
         table=cls.table)
     proverbs_id = db.execute(sql, creator_id, proverbs, status,
                              create_time)
     if proverbs_id:
         user_tag_proverbs_limit_key = user_tag_proverbs_24_hours_limit % creator_id
         cache.set(user_tag_proverbs_limit_key, 'true')
         cache.expire(user_tag_proverbs_limit_key, LIMIT_SECONDS)
         return cls(proverbs_id, creator_id, proverbs, status, create_time,
                    None, None)
Ejemplo n.º 26
0
    def post(self):
        try:
            user_id = self.get_argument('user_id')
            room_id = self.get_argument('room_id')
            question_id = self.get_argument('question_id')
            option_id = self.get_argument('option_id')
        except MissingArgumentError:
            return self.error(MISSING_PARAMS)

        user = User.get(user_id)
        room = Room.get(room_id)
        question = RoomQuestion.get(question_id)
        option = RoomQuestionOption.get(option_id)
        if not question:
            return self.error(ROOM_QUESTION_NOT_FOUND)

        if int(question.room_id) != int(room_id) or option.question_id != int(question_id):
            return self.error(ACCESS_NOT_ALLOWED)

        user_room_question_24_hours_limit = 'user:%s:room:%s:question:24:limit'
        LIMIT_SECONDS = 24 * 60 * 60
        redis_key = user_room_question_24_hours_limit % (user_id, room_id)
        cache = CacheManager().cache
        if cache.exists(redis_key):
            return self.error(ROOM_QUESTION_ANSWER_LIMIT)

        cache.set(redis_key, 'true')
        cache.expire(redis_key, LIMIT_SECONDS)
        if option.is_right_answer:
            try:
                RoomUser.add(room_id, user_id, ROOM_USER_NORMAL)
                return self.render({
                    "status": 0,
                    "data": room.jsonify(user),
                })
            except Exception as e:
                logger.error(u'用户加入房间失败。User:[%s] Room:[%s] Error:[%s]' % (user_id, room_id, e))
                return self.error(SYSTEM_ERROR)
        else:
            return self.error(ROOM_QUESTION_ANSWER_WRONG)
Ejemplo n.º 27
0
 def unfollow(cls, user_id, follow_id):
     cache = CacheManager().cache
     db = DbManager().db
     user_follow_key = USER_FOLLOW_KEY % user_id
     user_fans_key = USER_FANS_KEY % follow_id
     cache.zrem(user_follow_key, follow_id)
     cache.zrem(user_fans_key, user_id)
     db.execute('begin;')
     try:
         sql = 'select follow_type from {table} where user_id=%s and follow_id=%s'.format(table=cls.table)
         follow_type = int(db.get(sql, user_id, follow_id).get('follow_type'))
         sql = 'delete from {table} where user_id=%s and follow_id=%s'.format(table=cls.table)
         db.execute(sql, user_id, follow_id)
         sql = 'delete from {table} where user_id=%s and fans_id=%s'.format(table=UserFans.table)
         db.execute(sql, follow_id, user_id)
         if follow_type == FOLLOW_BOTH_TYPE:
             sql = 'update {table} set type=%s where user_id=%s and follow_id=%s'.format(table=cls.table)
             db.execute(sql, FOLLOW_SINGLE_TYPE, follow_id, user_id)
         db.execute('commit;')
     except:
         db.execute('rollback;')
         raise
Ejemplo n.º 28
0
 def follow(cls, user_id, follow_id, follow_type):
     db = DbManager().db
     cache = CacheManager().cache
     create_time = datetime.now()
     score = get_int_date(create_time)
     follow_type = int(follow_type)
     user_follow_key = USER_FOLLOW_KEY % user_id
     user_fans_key = USER_FANS_KEY % follow_id
     cache.zadd(user_follow_key, score, follow_id)
     cache.zadd(user_fans_key, score, user_id)
     db.execute('begin;')
     try:
         sql = 'insert into {table} (user_id, follow_id, follow_type, create_time) values (%s, %s, %s, %s)'.format(table=cls.table)
         db.execute(sql, user_id, follow_id, follow_type, create_time)
         sql = 'insert into {table} (follow_id, user_id, create_time) values (%s, %s, %s)'.format(table=UserFans.table)
         db.execute(sql, follow_id, user_id, create_time)
         if follow_type == FOLLOW_BOTH_TYPE:
             sql = 'update {table} set follow_type=%s where user_id=%s and follow_id=%s'.format(table=cls.table)
             db.execute(sql, FOLLOW_BOTH_TYPE, follow_id, user_id)
         db.execute('commit;')
     except:
         db.execute('rollback;')
         raise
Ejemplo n.º 29
0
    def delete(self):
        db = DbManager().db
        content_id = self.id
        db.execute('begin;')
        try:
            sql = 'delete from {table} where id=%s'.format(table=self.table)
            db.execute(sql, content_id)
            sql = 'delete from comment where content_id=%s'
            db.execute(sql, content_id)
            sql = 'delete from content_liked where content_id=%s'
            db.execute(sql, content_id)
            db.execute('commit;')
        except:
            db.execute('rollback;')
            raise

        cache = CacheManager().cache
        cache.zrem(USER_CREATED_CONTENT_KEY % self.creator_id, content_id)
        # 删除内容的评论和点赞记录
        content_comment_key = CONTENT_COMMENT_CACHE_KEY % content_id
        comment_ids = cache.lrange(content_comment_key, 0, -1)
        with cache.pipeline() as pipe:
            for comment_id in comment_ids:
                comment_cache_key = COMMENT_CACHE_KEY % comment_id
                pipe.delete(comment_cache_key)

            pipe.delete(content_comment_key)
            content_liked_key = CONTENT_LIKED_CACHE_KEY % content_id
            pipe.delete(content_liked_key)
            pipe.zrem(CONTENT_LIKED_RANK_KEY, content_id)
            pipe.execute()

        ContentTypeMap = getContentTypeMap()
        content_model = ContentTypeMap.get(self.content_type)
        content_obj = content_model.get(self.id)
        content_obj.delete()
Ejemplo n.º 30
0
 def flush_comment_cache(self):
     cache = CacheManager().cache
     cache_obj_key = COMMENT_CACHE_KEY % self.id
     cache_list_key = CONTENT_COMMENT_CACHE_KEY % self.content_id
     cache.delete(cache_obj_key)
     cache.lrem(cache_list_key, 0, self.id)