拍爱

models.py 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. # -*- coding: utf-8 -*-
  2. from django.conf import settings
  3. from django.db import models
  4. from django.utils.translation import ugettext_lazy as _
  5. from pai2.basemodels import CreateUpdateMixin
  6. from utils.url_utils import img_url
  7. from utils.redis.rgroup import get_group_photo_thumbup_flag
  8. r = settings.REDIS_CACHE
  9. class GroupInfo(CreateUpdateMixin):
  10. APP_GROUP = 0
  11. SESSION_GROUP = 1
  12. GROUP_FROM = (
  13. (APP_GROUP, u'APP 建群'),
  14. (SESSION_GROUP, u'SESSION 建群'),
  15. )
  16. group_id = models.CharField(_(u'group_id'), max_length=255, blank=True, null=True, help_text=u'群组唯一标识', db_index=True, unique=True)
  17. admin_id = models.CharField(_(u'admin_id'), max_length=255, blank=True, null=True, help_text=u'用户唯一标识')
  18. group_name = models.CharField(_(u'group_name'), max_length=255, blank=True, null=True, help_text=u'群组名称')
  19. group_default_avatar = models.IntegerField(_(u'group_default_avatar'), default=0, help_text=u'群组默认头像,0 - 255,水果头像')
  20. group_avatar = models.CharField(_(u'group_avatar'), max_length=255, blank=True, null=True, help_text=u'群组头像')
  21. group_desc = models.TextField(_(u'group_desc'), blank=True, null=True, help_text=u'群组描述')
  22. group_from = models.IntegerField(_(u'group_from'), choices=GROUP_FROM, default=APP_GROUP, help_text=u'群组来源')
  23. session_id = models.CharField(_(u'session_id'), max_length=255, blank=True, null=True, help_text=u'照片组唯一标识', db_index=True)
  24. group_lock = models.BooleanField(_(u'group_lock'), default=False, help_text=u'群组锁定')
  25. class Meta:
  26. verbose_name = _(u'groupinfo')
  27. verbose_name_plural = _(u'groupinfo')
  28. def __unicode__(self):
  29. return unicode(self.group_id)
  30. @property
  31. def group_avatar_url(self):
  32. return img_url(self.group_avatar)
  33. @property
  34. def data(self):
  35. return {
  36. 'group_id': self.group_id,
  37. 'admin_id': self.admin_id,
  38. 'group_name': self.group_name,
  39. 'group_default_avatar': self.group_default_avatar,
  40. 'group_avatar': self.group_avatar_url,
  41. 'group_desc': self.group_desc,
  42. 'group_from': self.group_from,
  43. 'group_lock': self.group_lock,
  44. 'created_at': self.created_at.replace(microsecond=0),
  45. }
  46. def users(self, admin=True, user_id=None):
  47. all_users = GroupUserInfo.objects.filter(group_id=self.group_id)
  48. passed_users = all_users.filter(user_status=GroupUserInfo.PASSED)
  49. passed_count = passed_users.count()
  50. passed = [passed.user_info for passed in passed_users]
  51. if admin and self.admin_id != user_id:
  52. return {
  53. 'passed_count': passed_count,
  54. 'passed': passed,
  55. }
  56. applying_users = all_users.filter(user_status=GroupUserInfo.APPLYING)
  57. applying_count = applying_users.count()
  58. applying = [applying.user_info for applying in applying_users]
  59. return {
  60. 'applying_count': applying_count,
  61. 'passed_count': passed_count,
  62. 'applying': applying,
  63. 'passed': passed,
  64. }
  65. class GroupUserInfo(CreateUpdateMixin):
  66. APPLYING = 0
  67. PASSED = 1
  68. REFUSED = 2
  69. DELETED = 3
  70. QUIT = 4
  71. USER_STATUS = (
  72. (APPLYING, u'申请中'),
  73. (PASSED, u'已通过'),
  74. (REFUSED, u'已拒绝'),
  75. (DELETED, u'已删除'),
  76. (QUIT, u'已退出'),
  77. )
  78. group_id = models.CharField(_(u'group_id'), max_length=255, blank=True, null=True, help_text=u'群组唯一标识', db_index=True)
  79. user_id = models.CharField(_(u'user_id'), max_length=255, blank=True, null=True, help_text=u'用户唯一标识')
  80. current_id = models.IntegerField(_(u'current_id'), default=-1, help_text=u'当前群组照片ID')
  81. nickname = models.CharField(_(u'nickname'), max_length=255, blank=True, null=True, help_text=u'用户群组昵称')
  82. avatar = models.CharField(_(u'avatar'), max_length=255, blank=True, null=True, help_text=u'用户头像')
  83. admin = models.BooleanField(_(u'admin'), default=False, help_text=u'群组管理员')
  84. user_status = models.IntegerField(_(u'user_status'), choices=USER_STATUS, default=APPLYING)
  85. passed_at = models.DateTimeField(_(u'passed_at'), blank=True, null=True, help_text=_(u'通过时间'))
  86. refused_at = models.DateTimeField(_(u'refused_at'), blank=True, null=True, help_text=_(u'拒绝时间'))
  87. deleted_at = models.DateTimeField(_(u'deleted_at'), blank=True, null=True, help_text=_(u'删除时间'))
  88. quit_at = models.DateTimeField(_(u'quit_at'), blank=True, null=True, help_text=_(u'退出时间'))
  89. class Meta:
  90. verbose_name = _(u'groupuserinfo')
  91. verbose_name_plural = _(u'groupuserinfo')
  92. def __unicode__(self):
  93. return unicode(self.pk)
  94. @property
  95. def user_info(self):
  96. return {
  97. 'user_id': self.user_id,
  98. 'nickname': self.nickname,
  99. 'avatar': self.avatar,
  100. 'admin': self.admin,
  101. }
  102. class GroupPhotoInfo(CreateUpdateMixin):
  103. APP_GROUP = 0
  104. SESSION_GROUP = 1
  105. PHOTO_FROM = (
  106. (APP_GROUP, u'APP 建群'),
  107. (SESSION_GROUP, u'SESSION 建群'),
  108. )
  109. group_id = models.CharField(_(u'group_id'), max_length=255, blank=True, null=True, help_text=u'群组唯一标识', db_index=True)
  110. user_id = models.CharField(_(u'user_id'), max_length=255, blank=True, null=True, help_text=u'用户唯一标识')
  111. nickname = models.CharField(_(u'nickname'), max_length=255, blank=True, null=True, help_text=u'用户群组昵称')
  112. avatar = models.CharField(_(u'avatar'), max_length=255, blank=True, null=True, help_text=u'用户头像')
  113. photo_path = models.CharField(_(u'photo_path'), max_length=255, blank=True, null=True, help_text=u'照片存放路径')
  114. photo_w = models.IntegerField(_(u'photo_w'), default=0, help_text=u'照片宽度')
  115. photo_h = models.IntegerField(_(u'photo_h'), default=0, help_text=u'照片高度')
  116. # 双列: 540, 40-50K
  117. photo_thumbnail_path = models.CharField(_(u'photo_thumbnail_path'), max_length=255, blank=True, null=True, help_text=u'照片缩略图存放路径')
  118. photo_thumbnail_w = models.IntegerField(_(u'photo_thumbnail_w'), default=0, help_text=u'照片缩略图宽度')
  119. photo_thumbnail_h = models.IntegerField(_(u'photo_thumbnail_h'), default=0, help_text=u'照片缩略图高度')
  120. # 单列: 1080, xx-100K
  121. photo_thumbnail2_path = models.CharField(_(u'photo_thumbnail2_path'), max_length=255, blank=True, null=True, help_text=u'照片缩略图存放路径')
  122. photo_thumbnail2_w = models.IntegerField(_(u'photo_thumbnail2_w'), default=0, help_text=u'照片缩略图宽度')
  123. photo_thumbnail2_h = models.IntegerField(_(u'photo_thumbnail2_h'), default=0, help_text=u'照片缩略图高度')
  124. comment_num = models.IntegerField(_(u'comment_num'), default=0, help_text=u'照片评论数量')
  125. thumbup_num = models.IntegerField(_(u'thumbup_num'), default=0, help_text=u'照片点赞数量')
  126. photo_from = models.IntegerField(_(u'photo_from'), choices=PHOTO_FROM, default=APP_GROUP, help_text=u'照片来源')
  127. class Meta:
  128. verbose_name = _(u'groupphotoinfo')
  129. verbose_name_plural = _(u'groupphotoinfo')
  130. def __unicode__(self):
  131. return unicode(self.pk)
  132. @property
  133. def photo_url(self):
  134. return img_url(self.photo_path)
  135. @property
  136. def photo_thumbnail_url(self):
  137. return img_url(self.photo_thumbnail_path)
  138. @property
  139. def photo_thumbnail2_url(self):
  140. return img_url(self.photo_thumbnail2_path)
  141. def photo_info(self, user_id):
  142. try:
  143. group = GroupInfo.objects.get(group_id=self.group_id)
  144. except GroupInfo.DoesNotExist:
  145. group = None
  146. return {
  147. 'group_id': group and group.group_id,
  148. 'group_name': group and group.group_name,
  149. 'group_default_avatar': group and group.group_default_avatar,
  150. 'group_avatar': group and group.group_avatar_url,
  151. 'group_from': group and group.group_from,
  152. 'photo_id': self.pk,
  153. 'photo_url': self.photo_url,
  154. 'photo_w': self.photo_w,
  155. 'photo_h': self.photo_h,
  156. 'photo_thumbnail_url': self.photo_thumbnail_url,
  157. 'photo_thumbnail_w': self.photo_thumbnail_w,
  158. 'photo_thumbnail_h': self.photo_thumbnail_h,
  159. 'photo_thumbnail2_url': self.photo_thumbnail2_url,
  160. 'photo_thumbnail2_w': self.photo_thumbnail2_w,
  161. 'photo_thumbnail2_h': self.photo_thumbnail2_h,
  162. 'user_id': self.user_id,
  163. 'nickname': self.nickname,
  164. 'avatar': self.avatar,
  165. 'comment_num': self.comment_num,
  166. 'thumbup': get_group_photo_thumbup_flag(self.pk, user_id),
  167. 'thumbup_num': self.thumbup_num,
  168. 'photo_from': self.photo_from,
  169. 'created_at': self.created_at.replace(microsecond=0),
  170. }
  171. class PhotoCommentInfo(CreateUpdateMixin):
  172. photo_id = models.CharField(_(u'photo_id'), max_length=255, blank=True, null=True, help_text=u'飞图唯一标识', db_index=True)
  173. user_id = models.CharField(_(u'user_id'), max_length=255, blank=True, null=True, help_text=u'用户唯一标识')
  174. nickname = models.CharField(_(u'nickname'), max_length=255, blank=True, null=True, help_text=u'用户群组昵称')
  175. avatar = models.CharField(_(u'avatar'), max_length=255, blank=True, null=True, help_text=u'用户头像')
  176. comment = models.TextField(_(u'comment'), blank=True, null=True, help_text=u'用户评论')
  177. class Meta:
  178. verbose_name = _(u'photocommentinfo')
  179. verbose_name_plural = _(u'photocommentinfo')
  180. def __unicode__(self):
  181. return unicode(self.pk)
  182. @property
  183. def comment_info(self):
  184. return {
  185. 'user_id': self.user_id,
  186. 'nickname': self.nickname,
  187. 'avatar': self.avatar,
  188. 'comment': self.comment,
  189. 'created_at': self.created_at.replace(microsecond=0),
  190. }
  191. class PhotoThumbUpInfo(CreateUpdateMixin):
  192. photo_id = models.CharField(_(u'photo_id'), max_length=255, blank=True, null=True, help_text=u'飞图唯一标识', db_index=True)
  193. user_id = models.CharField(_(u'user_id'), max_length=255, blank=True, null=True, help_text=u'用户唯一标识')
  194. nickname = models.CharField(_(u'nickname'), max_length=255, blank=True, null=True, help_text=u'用户群组昵称')
  195. avatar = models.CharField(_(u'avatar'), max_length=255, blank=True, null=True, help_text=u'用户头像')
  196. thumbup = models.BooleanField(_(u'thumbup'), default=True, help_text=u'用户点赞', db_index=True)
  197. class Meta:
  198. verbose_name = _(u'photothumbupinfo')
  199. verbose_name_plural = _(u'photothumbupinfo')
  200. def __unicode__(self):
  201. return unicode(self.pk)
  202. @property
  203. def thumbup_info(self):
  204. return {
  205. 'user_id': self.user_id,
  206. 'nickname': self.nickname,
  207. 'avatar': self.avatar,
  208. }