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

import itertools

from group.models import GroupPhotoInfo


def get_current_photos(group_id, user_id, current_id):
    # 获取从 current_id 到 now 的群组照片列表
    group_photos = GroupPhotoInfo.objects.filter(
        group_id=group_id,
        status=True,
        pk__gt=current_id,
    ).order_by(
        '-session_id',
        '-pk'
    )
    # 最新照片
    latest_photo = group_photos.first()
    # 照片按照 session_id 分组
    group_photos = map(lambda x: {'session_id': x[0], 'photos': [y.photo_info(user_id) for y in x[1]]},
                       itertools.groupby(group_photos, lambda x: x.session_id))
    return {
        'current_id': latest_photo and latest_photo.pk or current_id,
        'photos': group_photos,
    }
