No Description

rgroup.py 4.8KB

    # -*- coding: utf-8 -*- import json from django.conf import settings from django.core.serializers.json import DjangoJSONEncoder from utils.redis.rkeys import (GROUP_INFO, GROUP_PHOTO_COMMENT_LIST, GROUP_PHOTO_DATA, GROUP_PHOTO_THUMB_UP, GROUP_PHOTO_THUMB_UP_LIST, GROUP_PHOTO_WATCHER_SET, GROUP_USERS_INFO) r = settings.REDIS_CACHE # 群组信息相关 def set_group_info(group): """ 设置群组信息 """ group_data = group.data r.setex(GROUP_INFO % group.group_id, settings.REDIS_EXPIRED_MONTH, json.dumps(group_data, cls=DjangoJSONEncoder)) return group_data def set_group_info_by_id(group_id): """ 设置群组信息 """ from group.models import GroupInfo try: group = GroupInfo.objects.get(group_id=group_id) except GroupInfo.DoesNotExist: return {} return set_group_info(group) def get_group_info(group_id): """ 获取群组信息 """ return json.loads(r.get(GROUP_INFO % group_id) or '{}') or set_group_info_by_id(group_id) # 群组用户信息相关 def set_group_users_info(group): """ 设置群组用户信息 """ group_users = group.users(admin=False) r.setex(GROUP_USERS_INFO % group.group_id, settings.REDIS_EXPIRED_MONTH, json.dumps({ 'group_users': group_users, 'admin_id': group.admin_id, }, cls=DjangoJSONEncoder)) return group_users def set_group_users_info_by_id(group_id): """ 设置群组用户信息 """ from group.models import GroupInfo try: group = GroupInfo.objects.get(group_id=group_id) except GroupInfo.DoesNotExist: return {} return set_group_users_info(group) def get_group_users_info(group_id, user_id): """ 获取群组用户信息 """ group_users_data = json.loads(r.get(GROUP_USERS_INFO % group_id) or '{}') or set_group_users_info_by_id(group_id) group_users, admin_id = group_users_data.get('group_users', {}), group_users_data.get('admin_id', '') if group_users and user_id != admin_id: [group_users.pop(k) for k in ['applying_count', 'applying']] return group_users # 群组照片相关 def set_group_photo_data(group_id): """ 设置群组照片数据信息 """ from group.models import GroupPhotoInfo group_photos = GroupPhotoInfo.objects.filter(group_id=group_id, status=True) group_photos = [photo.photo_data for photo in group_photos] r.set(GROUP_PHOTO_DATA % group_id, json.dumps(group_photos)) return group_photos def get_group_photo_data(group_id): """ 获取群组照片数据信息 """ return json.loads(r.get(GROUP_PHOTO_DATA % group_id) or '[]') or set_group_photo_data(group_id) def set_group_photo_thumbup_flag(photo_id, user_id): """ 设置群组照片用户点赞标识信息 """ r.setex(GROUP_PHOTO_THUMB_UP % (photo_id, user_id), settings.REDIS_EXPIRED_MONTH, True) def del_group_photo_thumbup_flag(photo_id, user_id): """ 删除群组照片用户点赞标识信息 """ r.delete(GROUP_PHOTO_THUMB_UP % (photo_id, user_id)) def get_group_photo_thumbup_flag(photo_id, user_id): """ 获取群组照片用户点赞标识信息 """ if r.exists(GROUP_PHOTO_THUMB_UP % (photo_id, user_id)): return True else: from group.models import PhotoThumbUpInfo if PhotoThumbUpInfo.objects.filter(photo_id=photo_id, user_id=user_id, thumbup=True).exists(): set_group_photo_thumbup_flag(photo_id, user_id) return True else: return False def set_group_photo_comment_list(photo_id): """ 设置群组照片用户评论列表 """ from group.models import PhotoCommentInfo photo_comments = PhotoCommentInfo.objects.filter(photo_id=photo_id) photo_comments = [comment.comment_info for comment in photo_comments] r.set(GROUP_PHOTO_COMMENT_LIST % photo_id, json.dumps(photo_comments, cls=DjangoJSONEncoder)) return photo_comments def get_group_photo_comment_list(photo_id): """ 获取群组照片用户评论列表 """ return json.loads(r.get(GROUP_PHOTO_COMMENT_LIST % photo_id) or '[]') or set_group_photo_comment_list(photo_id) def set_group_photo_thumbup_list(photo_id): """ 设置群组照片用户点赞列表 """ from group.models import PhotoThumbUpInfo photo_thumbups = PhotoThumbUpInfo.objects.filter(photo_id=photo_id, thumbup=True) photo_thumbups = [thumbup.thumbup_info for thumbup in photo_thumbups] r.set(GROUP_PHOTO_THUMB_UP_LIST % photo_id, json.dumps(photo_thumbups, cls=DjangoJSONEncoder)) return photo_thumbups def get_group_photo_thumbup_list(photo_id): """ 获取群组照片用户点赞列表 """ return json.loads(r.get(GROUP_PHOTO_THUMB_UP_LIST % photo_id) or '[]') or set_group_photo_thumbup_list(photo_id) def get_group_photo_watchers(photo_id): """ 获取群组照片用户关注列表 """ return r.smembers(GROUP_PHOTO_WATCHER_SET % photo_id)