拍爱

group_photo_utils.py 976B

123456789101112131415161718192021222324252627282930313233
  1. # -*- coding: utf-8 -*-
  2. import itertools
  3. from group.models import GroupPhotoInfo
  4. def get_current_photos(group_id, user_id, current_id, request=None):
  5. # 获取从 current_id 到 now 的群组照片列表
  6. group_photos = GroupPhotoInfo.objects.filter(
  7. group_id=group_id,
  8. status=True,
  9. pk__gt=current_id,
  10. )
  11. # 最新照片
  12. group_tmp_photos = group_photos.order_by(
  13. '-pk',
  14. )
  15. latest_photo = group_tmp_photos.first()
  16. # 照片按照 session_id 分组
  17. group_photos = group_photos.order_by(
  18. '-session_id',
  19. '-pk',
  20. )
  21. if request and request.weixin:
  22. group_photos = group_photos[:30]
  23. group_photos = map(lambda x: {'session_id': x[0], 'photos': [y.photo_info(user_id) for y in x[1]]},
  24. itertools.groupby(group_photos, lambda x: x.session_id))
  25. return {
  26. 'current_id': latest_photo and latest_photo.pk or current_id,
  27. 'photos': group_photos,
  28. }