# -*- coding: utf-8 -*-

from __future__ import division

from django.conf import settings
from django.db import connection, transaction
from django_curtail_uuid import CurtailUUID
from django_logit import logit
from django_response import response
from paginator import pagination
from rest_framework import viewsets
from TimeConvert import TimeConvert as tc

from account.models import UserInfo
from group.models import GroupInfo, GroupPhotoInfo, GroupUserInfo, PhotoCommentInfo, PhotoThumbUpInfo
from group.serializers import GroupInfoSerializer, GroupPhotoInfoSerializer, GroupUserInfoSerializer
from message.models import UserMessageInfo
from utils.error.errno_utils import GroupPhotoStatusCode, GroupStatusCode, GroupUserStatusCode, UserStatusCode
from utils.group_photo_utils import get_current_photos
from utils.price_utils import get_group_photo_price
from utils.qiniucdn import qiniu_file_url
from utils.redis.connect import r
from utils.redis.rgroup import (del_group_photo_thumbup_flag, get_group_info, get_group_photo_comment_list,
                                get_group_photo_data, get_group_photo_thumbup_flag, get_group_photo_thumbup_list,
                                get_group_photo_watchers, get_group_users_info, set_group_info, set_group_info_by_id,
                                set_group_photo_comment_list, set_group_photo_data, set_group_photo_thumbup_flag,
                                set_group_photo_thumbup_list, set_group_users_info)
from utils.redis.rkeys import GROUP_LAST_PHOTO_PK, GROUP_PHOTO_WATCHER_SET, GROUP_USERS_PASSED_SET
from utils.redis.rlock import upload_lock
from utils.redis.rorder import get_lensman_order_record
from utils.sql.raw import PAI2_HOME_API, PAI2_HOME_WX_API
from utils.storage_qiniu_utils import file_save
from utils.time_utils import origin_expired_stamps
from utils.url_utils import share_url


@logit
@transaction.atomic
def group_create_api(request):
    """ 群组创建 """
    user_id = request.POST.get('user_id', '')
    group_name = request.POST.get('group_name', '')
    group_default_avatar = int(request.POST.get('group_default_avatar', 0))

    # 用户校验
    try:
        user = UserInfo.objects.get(user_id=user_id)
    except UserInfo.DoesNotExist:
        return response(UserStatusCode.USER_NOT_FOUND)

    # 群组唯一标识
    group_id = CurtailUUID.uuid(GroupInfo, 'group_id')

    # 群组记录创建
    group = GroupInfo.objects.create(
        group_id=group_id,
        admin_id=user_id,
        group_name=group_name,
        group_default_avatar=group_default_avatar,
        group_from=GroupInfo.APP_GROUP,
    )

    # Redis 群组数据缓存
    group_info = set_group_info(group)

    # 群组用户记录创建
    GroupUserInfo.objects.create(
        group_id=group_id,
        user_id=user_id,
        nickname=user.final_nickname,
        avatar=user.avatar,
        admin=True,
        user_status=GroupUserInfo.PASSED,
        passed_at=tc.utc_datetime(),
    )

    # Redis 群组用户数据缓存
    group_users = set_group_users_info(group)

    # Redis 群组通过集合缓存
    r.sadd(GROUP_USERS_PASSED_SET % group_id, user_id)

    return response(200, 'Create Group Success', u'群组创建成功', {
        'group_id': group_id,
        'group': group_info,
        'users': group_users,
    })


@logit
def group_detail_api(request):
    """ 群组详情 """
    group_id = request.POST.get('group_id', '')
    user_id = request.POST.get('user_id', '')

    return response(200, 'Get Group Detail Info Success', u'获取群组详情成功', {
        'group_id': group_id,
        'group': get_group_info(group_id),
        'users': get_group_users_info(group_id, user_id),
    })


@logit
def group_update_api(request):
    """ 群组更新 """
    group_id = request.POST.get('group_id', '')
    admin_id = request.POST.get('admin_id', '') or request.POST.get('user_id', '')
    group_name = request.POST.get('group_name', '')
    group_desc = request.POST.get('group_desc', '')

    group_avatar = request.FILES.get('group_avatar', '')

    # 群组校验
    try:
        group = GroupInfo.objects.get(group_id=group_id)
    except GroupInfo.DoesNotExist:
        return response(GroupStatusCode.GROUP_NOT_FOUND)

    # 权限校验
    if group.admin_id != admin_id:
        return response(GroupStatusCode.NOT_GROUP_ADMIN)

    # 群组名称更新
    if group_name:
        group.group_name = group_name
    # 群组描述更新
    if group_desc:
        group.group_desc = group_desc
    # 群组头像更新
    if group_avatar:
        group.group_avatar = file_save(group_avatar, prefix='group', ext='.jpeg').photo_path
    group.save()

    # Redis 群组数据缓存更新
    group_info = set_group_info(group)

    return response(200, 'Update Group Success', u'群组更新成功', {
        'group_id': group_id,
        'group': group_info,
        'users': get_group_users_info(group_id, admin_id),
    })


@logit
def group_delete_api(request):
    """ 群组删除 """
    group_id = request.POST.get('group_id', '')
    admin_id = request.POST.get('admin_id', '') or request.POST.get('user_id', '')

    # 群组校验
    try:
        group = GroupInfo.objects.get(group_id=group_id)
    except GroupInfo.DoesNotExist:
        return response(GroupStatusCode.GROUP_NOT_FOUND)

    # 权限校验
    if group.admin_id != admin_id:
        return response(GroupStatusCode.NOT_GROUP_ADMIN)

    # 照片数量校验
    if GroupPhotoInfo.objects.filter(group_id=group_id, status=True).count():
        return response(GroupStatusCode.GROUP_PHOTO_NOT_EMPTY)

    # 删除群组
    group.status = False
    group.save()

    # 删除群组用户
    GroupUserInfo.objects.filter(group_id=group_id).update(status=False)

    return response(200, 'Delete Group Success', u'群组删除成功')


@logit
def group_list_api(request):
    """ 群组列表 """
    user_id = request.POST.get('user_id', '')
    page = int(request.POST.get('page', 1))
    num = int(request.POST.get('num', settings.GROUP_NUM_PER_PAGE))

    group_users = GroupUserInfo.objects.filter(user_id=user_id, user_status=GroupUserInfo.PASSED, status=True).order_by('-pk')
    group_users, left = pagination(group_users, page, num)

    groups = []
    for group_user in group_users:
        group_info = get_group_info(group_user.group_id)
        groups.append(group_info) if group_info else None

    return response(200, 'Get Group List Success', u'获取群组列表成功', {
        'groups': groups,
        'left': left,
    })


@logit
def group_lock_api(request):
    """ 群组锁定 """
    group_id = request.POST.get('group_id', '')
    admin_id = request.POST.get('admin_id', '') or request.POST.get('user_id', '')

    # 群组校验
    try:
        group = GroupInfo.objects.get(group_id=group_id)
    except GroupInfo.DoesNotExist:
        return response(GroupStatusCode.GROUP_NOT_FOUND)

    # 权限校验
    if group.admin_id != admin_id:
        return response(GroupStatusCode.NOT_GROUP_ADMIN)

    # 群组锁定
    group.group_lock = True
    group.save()

    # Redis 群组数据缓存更新
    set_group_info(group)

    return response(200, 'Lock Success', u'锁定成功')


@logit
def group_unlock_api(request):
    """ 群组解锁 """
    group_id = request.POST.get('group_id', '')
    admin_id = request.POST.get('admin_id', '') or request.POST.get('user_id', '')

    # 群组校验
    try:
        group = GroupInfo.objects.get(group_id=group_id)
    except GroupInfo.DoesNotExist:
        return response(GroupStatusCode.GROUP_NOT_FOUND)

    # 权限校验
    if group.admin_id != admin_id:
        return response(GroupStatusCode.NOT_GROUP_ADMIN)

    # 群组解锁
    group.group_lock = False
    group.save()

    # Redis 群组数据缓存更新
    set_group_info(group)

    return response(200, 'Unlock Success', u'解锁成功')


@logit
def group_data_api(request):
    """ 群组数据，评论数，点赞数 """
    group_id = request.POST.get('group_id', '')

    return response(200, 'Get Group Data Success', u'获取群组数据成功', {
        'photo_datas': get_group_photo_data(group_id),
    })


@logit(body=settings.LOGIT_BODY_FLAG, res=settings.LOGIT_RES_FLAG)
def flyimg_upload_api(request):
    """ 飞图上传 """
    group_id = request.POST.get('group_id', '')
    user_id = request.POST.get('user_id', '')
    nickname = request.POST.get('nickname', '')

    photo = request.FILES.get('photo', '')

    current_id = int(request.POST.get('current_id', -1))

    # 用户校验
    try:
        user = UserInfo.objects.get(user_id=user_id)
    except UserInfo.DoesNotExist:
        return response(UserStatusCode.USER_NOT_FOUND)

    # 群组用户校验
    try:
        group_user = GroupUserInfo.objects.get(group_id=group_id, user_id=user_id, user_status=GroupUserInfo.PASSED)
    except GroupUserInfo.DoesNotExist:
        return response(GroupUserStatusCode.GROUP_USER_NOT_FOUND)

    if photo and upload_lock(group_id, user_id, photo):
        photo_info = file_save(photo, prefix='fly', ext='.jpeg', thumbnail=True)

        # 群组照片记录创建
        group_photo, created = GroupPhotoInfo.objects.get_or_create(
            group_id=group_id,
            user_id=user_id,
            photo_md5=photo_info.photo_md5,
            defaults={
                'nickname': nickname or user.final_nickname,
                'avatar': user.avatar,
                'photo_path': photo_info.photo_path,
                'has_watermark': False,
                'photo_w': photo_info.photo_w,
                'photo_h': photo_info.photo_h,
                'photo_thumbnail_path': photo_info.photo_thumbnail_path,
                'photo_thumbnail_w': photo_info.photo_thumbnail_w,
                'photo_thumbnail_h': photo_info.photo_thumbnail_h,
                'photo_thumbnail2_path': photo_info.photo_thumbnail2_path,
                'photo_thumbnail2_w': photo_info.photo_thumbnail2_w,
                'photo_thumbnail2_h': photo_info.photo_thumbnail2_h,
            }
        )

        if created:
            # 设置群组最后一张照片PK
            r.set(GROUP_LAST_PHOTO_PK % group_id, group_photo.pk)

            # Redis 群组数据缓存
            set_group_info_by_id(group_id)

    curinfo = get_current_photos(group_id, user_id, max(current_id, group_user.current_id), request=request)

    return response(200, 'Flyimg Upload Success', u'飞图上传成功', curinfo)


@logit
def flyimg_list_api(request):
    """ 飞图列表 """
    group_id = request.POST.get('group_id', '')
    user_id = request.POST.get('user_id', '')
    current_id = int(request.POST.get('current_id', -1))

    # 群组用户校验
    try:
        group_user = GroupUserInfo.objects.get(group_id=group_id, user_id=user_id, user_status=GroupUserInfo.PASSED)
    except GroupUserInfo.DoesNotExist:
        return response(GroupUserStatusCode.GROUP_USER_NOT_FOUND)

    curinfo = get_current_photos(group_id, user_id, max(current_id, group_user.current_id), request=request)

    return response(200, 'Get Flyimg List Success', u'获取飞图上传成功', curinfo)


@logit
def flyimg_detail_api(request):
    user_id = request.POST.get('user_id', '')
    photo_id = request.POST.get('photo_id', '')

    # 群组照片校验
    try:
        group_photo = GroupPhotoInfo.objects.get(photo_id=photo_id)
    except GroupPhotoInfo.DoesNotExist:
        return response(GroupPhotoStatusCode.GROUP_PHOTO_NOT_FOUND)

    return response(200, 'Get Flyimg Detail Success', u'获取飞图详情成功', {
        'photo_info': group_photo.photo_info(user_id),
        'comments': get_group_photo_comment_list(photo_id),
        'thumbup': get_group_photo_thumbup_flag(photo_id, user_id),  # user_id 是否点赞 photo_id
        'thumbups': get_group_photo_thumbup_list(photo_id),  # 群组照片点赞列表
    })


@logit
def comment_submit_api(request):
    """ 飞图评论提交 """
    group_id = request.POST.get('group_id', '')
    user_id = request.POST.get('user_id', '')
    to_uid = request.POST.get('to_uid', '')
    photo_id = request.POST.get('photo_id', '')
    comment = request.POST.get('comment', '')

    # 群组用户校验
    try:
        group_user = GroupUserInfo.objects.get(group_id=group_id, user_id=user_id, user_status=GroupUserInfo.PASSED)
    except GroupUserInfo.DoesNotExist:
        return response(GroupUserStatusCode.GROUP_USER_NOT_FOUND)

    # 群组照片校验
    try:
        group_photo = GroupPhotoInfo.objects.get(photo_id=photo_id)
    except GroupPhotoInfo.DoesNotExist:
        return response(GroupPhotoStatusCode.GROUP_PHOTO_NOT_FOUND)

    # 评论内容校验
    if not comment:
        return response(GroupPhotoStatusCode.COMMENT_CONTENT_EMPTY)

    # 群组照片评论记录创建
    PhotoCommentInfo.objects.create(
        photo_id=photo_id,
        user_id=user_id,
        nickname=group_user.nickname,
        avatar=group_user.avatar,
        to_uid=to_uid,
        comment=comment,
    )

    # 群组照片评论数更新
    group_photo.comment_num += 1
    group_photo.save()

    # Redis 群组照片数据缓存
    set_group_photo_data(group_id)

    # Redis 群组照片评论列表缓存刷新
    set_group_photo_comment_list(photo_id)

    r.sadd(GROUP_PHOTO_WATCHER_SET % photo_id, user_id)

    # 判断群组照片发布者是否已经被管理员移除／主动退出，如若移除／退出，则不给发布者提醒
    if r.sismember(GROUP_USERS_PASSED_SET % group_photo.group_id, group_photo.user_id):
        UserMessageInfo.objects.create(
            from_uid=user_id,
            from_nickname=group_user.nickname,
            from_avatar=group_user.avatar,
            to_uid=group_photo.user_id,
            group_id=group_photo.group_id,
            photo_id=group_photo.photo_id,
            msg_type=UserMessageInfo.COMMENT,
            msg_title=u'评论',
            msg_content=comment,
        )

    # 给所有关注者(评论/点赞)发送提醒，移除(评论/点赞)者和照片所有者
    watchers = get_group_photo_watchers(photo_id, [user_id, group_photo.user_id])
    for watcher in watchers:
        UserMessageInfo.objects.create(
            from_uid=user_id,
            from_nickname=group_user.nickname,
            from_avatar=group_user.avatar,
            to_uid=watcher,
            group_id=group_photo.group_id,
            photo_id=group_photo.photo_id,
            msg_type=UserMessageInfo.COMMENT,
            msg_title=u'评论',
            msg_content=comment,
        )

    return response(200, 'Comment Success', u'评论成功', {
        'comments': get_group_photo_comment_list(photo_id),
    })


@logit
def comment_list_api(request):
    """ 飞图评论列表 """
    photo_id = request.POST.get('photo_id', '')

    # 群组照片校验
    if not GroupPhotoInfo.objects.filter(photo_id=photo_id).exists():
        return response(GroupPhotoStatusCode.GROUP_PHOTO_NOT_FOUND)

    return response(200, 'Get Comment List Success', u'获取评论列表成功', {
        'comments': get_group_photo_comment_list(photo_id),
    })


@logit
def thumbup_submit_api(request):
    """ 飞图点赞提交 """
    group_id = request.POST.get('group_id', '')
    user_id = request.POST.get('user_id', '')
    photo_id = request.POST.get('photo_id', '')

    # 群组用户校验
    try:
        group_user = GroupUserInfo.objects.get(group_id=group_id, user_id=user_id, user_status=GroupUserInfo.PASSED)
    except GroupUserInfo.DoesNotExist:
        return response(GroupUserStatusCode.GROUP_USER_NOT_FOUND)

    # 群组照片校验
    try:
        group_photo = GroupPhotoInfo.objects.get(photo_id=photo_id)
    except GroupPhotoInfo.DoesNotExist:
        return response(GroupPhotoStatusCode.GROUP_PHOTO_NOT_FOUND)

    # user_id 是否点赞 photo_id
    if get_group_photo_thumbup_flag(photo_id, user_id):
        return response(GroupPhotoStatusCode.DUPLICATE_THUMB_UP)

    # 群组照片点赞记录创建／更新
    photo_thumbup, created = PhotoThumbUpInfo.objects.get_or_create(
        photo_id=photo_id,
        user_id=user_id,
    )
    photo_thumbup.nickname = group_user.nickname
    photo_thumbup.avatar = group_user.avatar
    photo_thumbup.thumbup = True
    photo_thumbup.save()

    # Redis 群组照片点赞数据缓存
    set_group_photo_thumbup_flag(photo_id, user_id)

    # 群组照片点赞数更新
    group_photo.thumbup_num += 1
    group_photo.save()

    # Redis 群组照片数据缓存
    set_group_photo_data(group_id)

    # Redis 群组照片点赞列表缓存刷新
    set_group_photo_thumbup_list(photo_id)

    r.sadd(GROUP_PHOTO_WATCHER_SET % photo_id, user_id)

    # 判断群组照片发布者是否已经被管理员移除／主动退出，如若移除／退出，则不给发布者提醒
    if r.sismember(GROUP_USERS_PASSED_SET % group_photo.group_id, group_photo.user_id):
        UserMessageInfo.objects.create(
            from_uid=user_id,
            from_nickname=group_user.nickname,
            from_avatar=group_user.avatar,
            to_uid=group_photo.user_id,
            group_id=group_photo.group_id,
            photo_id=group_photo.photo_id,
            msg_type=UserMessageInfo.THUMBUP,
            msg_title=u'点赞',
            msg_content=u'点赞',
        )

    # 给所有关注者(评论/点赞)发送提醒，移除(评论/点赞)者和照片所有者
    watchers = get_group_photo_watchers(photo_id, [user_id, group_photo.user_id])
    for watcher in watchers:
        UserMessageInfo.objects.create(
            from_uid=user_id,
            from_nickname=group_user.nickname,
            from_avatar=group_user.avatar,
            to_uid=watcher,
            group_id=group_photo.group_id,
            photo_id=group_photo.photo_id,
            msg_type=UserMessageInfo.THUMBUP,
            msg_title=u'点赞',
            msg_content=u'点赞',
        )

    return response(200, 'Thumbup Success', u'点赞提交成功', {
        'thumbup': True,
        'thumbups': get_group_photo_thumbup_list(photo_id),
    })


@logit
def thumbup_list_api(request):
    """ 飞图点赞列表 """
    user_id = request.POST.get('user_id', '')
    photo_id = request.POST.get('photo_id', '')

    return response(200, 'Get Thumbup List Success', u'获取点赞列表成功', {
        'thumbup': get_group_photo_thumbup_flag(photo_id, user_id),  # user_id 是否点赞 photo_id
        'thumbups': get_group_photo_thumbup_list(photo_id),  # 群组照片点赞列表
    })


@logit
def thumbup_cancel_api(request):
    """ 飞图点赞取消 """
    group_id = request.POST.get('group_id', '')
    user_id = request.POST.get('user_id', '')
    photo_id = request.POST.get('photo_id', '')

    # 群组用户校验
    try:
        group_user = GroupUserInfo.objects.get(group_id=group_id, user_id=user_id, user_status=GroupUserInfo.PASSED)
    except GroupUserInfo.DoesNotExist:
        return response(GroupUserStatusCode.GROUP_USER_NOT_FOUND)

    # 群组照片校验
    try:
        group_photo = GroupPhotoInfo.objects.get(photo_id=photo_id)
    except GroupPhotoInfo.DoesNotExist:
        return response(GroupPhotoStatusCode.GROUP_PHOTO_NOT_FOUND)

    # user_id 是否点赞 photo_id
    if not get_group_photo_thumbup_flag(photo_id, user_id):
        return response(GroupPhotoStatusCode.THUMB_UP_NOT_FOUND)

    # 群组照片点赞取消
    photo_thumbup, created = PhotoThumbUpInfo.objects.get_or_create(
        photo_id=photo_id,
        user_id=user_id,
    )
    photo_thumbup.thumbup = False
    photo_thumbup.save()

    # Redis 群组照片点赞数据移除
    del_group_photo_thumbup_flag(photo_id, user_id)

    # 群组照片点赞数更新
    group_photo.thumbup_num -= 1
    group_photo.save()

    # Redis 群组照片数据缓存
    set_group_photo_data(group_id)

    # Redis 群组照片点赞列表缓存刷新
    set_group_photo_thumbup_list(photo_id)

    # 判断群组照片发布者是否已经被管理员移除／主动退出，如若移除／退出，则不给发布者提醒
    if r.sismember(GROUP_USERS_PASSED_SET % group_photo.group_id, group_photo.user_id):
        UserMessageInfo.objects.create(
            from_uid=user_id,
            from_nickname=group_user.nickname,
            from_avatar=group_user.avatar,
            to_uid=group_photo.user_id,
            group_id=group_photo.group_id,
            photo_id=group_photo.photo_id,
            msg_type=UserMessageInfo.THUMBUP,
            msg_title=u'取消点赞',
            msg_content=u'取消点赞',
        )

    # 群组照片点赞列表
    photo_thumbups = PhotoThumbUpInfo.objects.filter(photo_id=photo_id, thumbup=True, status=True)

    return response(200, 'Thumbup Cancel Success', u'点赞取消成功', {
        'thumbup': False,
        'thumbups': [thumbup.thumbup_info for thumbup in photo_thumbups],
    })


@logit
def kodo_home_api(request):
    """ 首页照片信息 """
    user_id = request.POST.get('user_id', '')
    page = int(request.POST.get('page', 1))
    num = int(request.POST.get('num', settings.PAI2_HOME_PER_PAGE))

    # 执行原生 SQL 语句，获取首页照片列表
    cursor = connection.cursor()
    cursor.execute((PAI2_HOME_WX_API if request.weixin else PAI2_HOME_API).format(
        user_id=user_id,
        offset=0,
        rows=settings.PAI2_HOME_MAX_ROWS,
    ))
    rows = cursor.fetchall()

    # 首页照片分页
    rows, left = pagination(rows, page, num)

    # 首页照片信息
    rows = [{
        'group_id': row[0],
        'group_name': row[1],
        'group_default_avatar': row[2],
        'group_avatar': row[3],
        'group_from': row[4],
        'photo_id': row[5],
        'photo_url': qiniu_file_url(row[6], bucket='watermark' if row[7] else 'photo'),
        'photo_w': row[8],
        'photo_h': row[9],
        'photo_thumbnail_url': qiniu_file_url(row[10], bucket='thumbnail'),
        'photo_thumbnail_w': row[11],
        'photo_thumbnail_h': row[12],
        'photo_thumbnail2_url': qiniu_file_url(row[13], bucket='thumbnail2'),
        'photo_thumbnail2_w': row[14],
        'photo_thumbnail2_h': row[15],
        'photo_share_url': share_url(row[5]),  # Warning: Index of This Line is 5
        'user_id': row[16],
        'nickname': row[17],
        'avatar': row[18],
        'comment_num': row[19],
        'thumbup_num': row[20],
        'photo_from': row[21],
        'session_id': row[22],
        'nomark': row[23],
        'origin': row[24],
        'created_at': row[25],
        'origin_expired_stamps': origin_expired_stamps(row[25], row[16]),
        'thumbup': get_group_photo_thumbup_flag(row[5], user_id),
        'porder': get_lensman_order_record(row[5], user_id) if row[21] == GroupPhotoInfo.SESSION_GROUP else {},
        'display_payment_btn': row[21] == GroupPhotoInfo.SESSION_GROUP and row[26] not in [GroupPhotoInfo.OUTTAKE],
    } for row in rows]

    return response(200, 'Get Home Data Success', u'获取首页数据成功', {
        'photos': rows,
        'left': left,
    })


@logit
def lensman_photo_price(request):
    """ 摄影师照片价格获取 """
    user_id = request.POST.get('user_id', '')
    photo_id = request.POST.get('photo_id', '')
    photo_type = request.POST.get('photo_type', 'nomark')  # nomark for 去除水印, origin for 获取高清图

    # 群组照片校验
    try:
        group_photo = GroupPhotoInfo.objects.get(photo_id=photo_id)
    except GroupPhotoInfo.DoesNotExist:
        return response(GroupPhotoStatusCode.GROUP_PHOTO_NOT_FOUND)

    return response(200, 'Get Price Success', u'获取价格成功', {
        'price': get_group_photo_price(group_photo, photo_type)
    })


@logit
def lensman_photo_bought(request):
    """ 摄影师照片已购买 """
    user_id = request.POST.get('user_id', '')
    photo_id = request.POST.get('photo_id', '')

    return response(200, 'Get Bought Data Success', u'获取购买数据成功', {
        'porder': get_lensman_order_record(photo_id, user_id)
    })


class GroupInfoViewSet(viewsets.ModelViewSet):
    queryset = GroupInfo.objects.all().order_by('-pk')
    serializer_class = GroupInfoSerializer


class GroupUserInfoViewSet(viewsets.ModelViewSet):
    queryset = GroupUserInfo.objects.all().order_by('-pk')
    serializer_class = GroupUserInfoSerializer


class GroupPhotoInfoViewSet(viewsets.ModelViewSet):
    queryset = GroupPhotoInfo.objects.all().order_by('-pk')
    serializer_class = GroupPhotoInfoSerializer
