拍爱

models.py 19KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. # -*- coding: utf-8 -*-
  2. from django.db import models
  3. from django.utils.translation import ugettext_lazy as _
  4. from shortuuidfield import ShortUUIDField
  5. from TimeConvert import TimeConvert as tc
  6. from pai2.basemodels import CreateUpdateMixin
  7. from utils.redis.rgroup import get_group_photo_thumbup_flag
  8. from utils.redis.rorder import get_lensman_order_record
  9. from utils.time_utils import origin_expired_stamps
  10. from utils.url_utils import img_url, share_url
  11. class GroupInfo(CreateUpdateMixin):
  12. APP_GROUP = 0
  13. SESSION_GROUP = 1
  14. TOURGUIDE_GROUP = 10
  15. GROUP_FROM = (
  16. (APP_GROUP, u'APP 建群'),
  17. (SESSION_GROUP, u'SESSION 建群'),
  18. (TOURGUIDE_GROUP, u'导游建群'),
  19. )
  20. group_id = models.CharField(_(u'group_id'), max_length=255, blank=True, null=True, help_text=u'群组唯一标识', db_index=True, unique=True)
  21. admin_id = models.CharField(_(u'admin_id'), max_length=255, blank=True, null=True, help_text=u'用户唯一标识', db_index=True)
  22. group_name = models.CharField(_(u'group_name'), max_length=255, blank=True, null=True, help_text=u'群组名称')
  23. group_default_avatar = models.IntegerField(_(u'group_default_avatar'), default=0, help_text=u'群组默认头像,0 - 255,水果头像')
  24. group_avatar = models.CharField(_(u'group_avatar'), max_length=255, blank=True, null=True, help_text=u'群组头像')
  25. group_desc = models.TextField(_(u'group_desc'), blank=True, null=True, help_text=u'群组描述')
  26. group_from = models.IntegerField(_(u'group_from'), choices=GROUP_FROM, default=APP_GROUP, help_text=u'群组来源')
  27. session_id = models.CharField(_(u'session_id'), max_length=255, blank=True, null=True, help_text=u'照片组唯一标识', db_index=True)
  28. group_lock = models.BooleanField(_(u'group_lock'), default=False, help_text=u'群组锁定')
  29. group_initio = models.BooleanField(_(u'group_initio'), default=False, help_text=u'群组查看照片从头开始')
  30. # 旅行团
  31. name = models.CharField(_(u'name'), max_length=255, blank=True, null=True, help_text=u'导游姓名')
  32. phone = models.CharField(_(u'phone'), max_length=255, blank=True, null=True, help_text=u'导游电话')
  33. started_at = models.DateTimeField(_(u'started_at'), blank=True, null=True, help_text=_(u'旅游团开始时间'))
  34. ended_at = models.DateTimeField(_(u'ended_at'), blank=True, null=True, help_text=_(u'旅游团结束时间'))
  35. total_persons = models.IntegerField(_(u'total_persons'), default=1, help_text=u'旅游团总人数')
  36. group_closed = models.BooleanField(_(u'group_closed'), default=False, help_text=u'旅游团关闭')
  37. gather_at = models.DateTimeField(_(u'gather_at'), blank=True, null=True, help_text=_(u'旅游团集合时间'))
  38. gather_lon = models.FloatField(_(u'gather_lon'), blank=True, null=True, help_text=_(u'旅游团集合经度'))
  39. gather_lat = models.FloatField(_(u'gather_lat'), blank=True, null=True, help_text=_(u'旅游团集合纬度'))
  40. gather_location = models.CharField(_(u'gather_location'), max_length=255, blank=True, null=True, help_text=u'旅游团集合地点')
  41. gather_screenshot = models.CharField(_(u'gather_screenshot'), max_length=255, blank=True, null=True, help_text=u'旅游团集合地点截图')
  42. attentions_path = models.CharField(_(u'attentions_path'), max_length=255, blank=True, null=True, help_text=u'注意事项照片存放路径')
  43. schedules_path = models.CharField(_(u'schedules_path'), max_length=255, blank=True, null=True, help_text=u'行程安排照片存放路径')
  44. class Meta:
  45. verbose_name = _(u'groupinfo')
  46. verbose_name_plural = _(u'groupinfo')
  47. def __unicode__(self):
  48. return unicode(self.group_id)
  49. @property
  50. def group_avatar_url(self):
  51. return img_url(self.group_avatar)
  52. @property
  53. def group_attentions_url(self):
  54. return img_url(self.attentions_path)
  55. @property
  56. def group_schedules_url(self):
  57. return img_url(self.schedules_path)
  58. @property
  59. def gather_screenshot_url(self):
  60. return img_url(self.gather_screenshot)
  61. @property
  62. def group_photo_num(self):
  63. return GroupPhotoInfo.objects.filter(group_id=self.group_id, status=True).count()
  64. @property
  65. def data(self):
  66. return {
  67. 'group_id': self.group_id,
  68. 'admin_id': self.admin_id,
  69. 'group_name': self.group_name,
  70. 'group_default_avatar': self.group_default_avatar,
  71. 'group_avatar': self.group_avatar_url,
  72. 'group_desc': self.group_desc,
  73. 'group_from': self.group_from,
  74. 'group_lock': self.group_lock,
  75. 'group_initio': self.group_initio,
  76. 'group_photo_num': self.group_photo_num,
  77. 'name': self.name,
  78. 'phone': self.phone,
  79. 'started_at': tc.remove_microsecond(self.started_at),
  80. 'ended_at': tc.remove_microsecond(self.ended_at),
  81. 'total_persons': self.total_persons,
  82. 'gather_at': tc.remove_microsecond(self.gather_at),
  83. 'gather_lon': self.gather_lon,
  84. 'gather_lat': self.gather_lat,
  85. 'gather_location': self.gather_location,
  86. 'gather_screenshot': self.gather_screenshot_url,
  87. 'created_at': tc.remove_microsecond(self.created_at),
  88. 'banners': {
  89. 'attentions': self.group_attentions_url,
  90. 'schedules': self.group_schedules_url,
  91. },
  92. }
  93. def users(self, admin=True, user_id=None):
  94. all_users = GroupUserInfo.objects.filter(group_id=self.group_id, user_status__in=[GroupUserInfo.APPLYING, GroupUserInfo.PASSED], status=True)
  95. passed_users = all_users.filter(user_status=GroupUserInfo.PASSED)
  96. passed_count = passed_users.count()
  97. passed = [passed.user_info for passed in passed_users]
  98. if admin and self.admin_id != user_id:
  99. return {
  100. 'passed_count': passed_count,
  101. 'passed': passed,
  102. }
  103. applying_users = all_users.filter(user_status=GroupUserInfo.APPLYING)
  104. applying_count = applying_users.count()
  105. applying = [applying.user_info for applying in applying_users]
  106. return {
  107. 'applying_count': applying_count,
  108. 'passed_count': passed_count,
  109. 'applying': applying,
  110. 'passed': passed,
  111. }
  112. class GroupUserInfo(CreateUpdateMixin):
  113. APPLYING = 0
  114. PASSED = 1
  115. REFUSED = 2
  116. DELETED = 3
  117. QUIT = 4
  118. USER_STATUS = (
  119. (APPLYING, u'申请中'),
  120. (PASSED, u'已通过'),
  121. (REFUSED, u'已拒绝'),
  122. (DELETED, u'已删除'),
  123. (QUIT, u'已退出'),
  124. )
  125. group_id = models.CharField(_(u'group_id'), max_length=255, blank=True, null=True, help_text=u'群组唯一标识', db_index=True)
  126. user_id = models.CharField(_(u'user_id'), max_length=255, blank=True, null=True, help_text=u'用户唯一标识', db_index=True)
  127. current_id = models.IntegerField(_(u'current_id'), default=-1, help_text=u'当前群组照片ID')
  128. nickname = models.CharField(_(u'nickname'), max_length=255, blank=True, null=True, help_text=u'用户群组昵称')
  129. avatar = models.CharField(_(u'avatar'), max_length=255, blank=True, null=True, help_text=u'用户头像')
  130. admin = models.BooleanField(_(u'admin'), default=False, help_text=u'群组管理员')
  131. user_status = models.IntegerField(_(u'user_status'), choices=USER_STATUS, default=APPLYING)
  132. passed_at = models.DateTimeField(_(u'passed_at'), blank=True, null=True, help_text=_(u'通过时间'))
  133. refused_at = models.DateTimeField(_(u'refused_at'), blank=True, null=True, help_text=_(u'拒绝时间'))
  134. deleted_at = models.DateTimeField(_(u'deleted_at'), blank=True, null=True, help_text=_(u'删除时间'))
  135. quit_at = models.DateTimeField(_(u'quit_at'), blank=True, null=True, help_text=_(u'退出时间'))
  136. # 旅行团相关
  137. subadmin = models.BooleanField(_(u'subadmin'), default=False, help_text=u'副群组管理员')
  138. name = models.CharField(_(u'name'), max_length=255, blank=True, null=True, help_text=u'用户姓名')
  139. phone = models.CharField(_(u'phone'), max_length=255, blank=True, null=True, help_text=u'用户电话')
  140. relative_persons = models.IntegerField(_(u'relative_persons'), default=1, help_text=u'关联人数')
  141. authority = models.BooleanField(_(u'authority'), default=True, help_text=u'是否有定位权限')
  142. remark = models.CharField(_(u'remark'), max_length=255, blank=True, null=True, help_text=u'备注')
  143. admin_status = models.BooleanField(_(u'admin_status'), default=True, help_text=_(u'群组管理员状态'))
  144. class Meta:
  145. verbose_name = _(u'groupuserinfo')
  146. verbose_name_plural = _(u'groupuserinfo')
  147. unique_together = (('group_id', 'user_id'),)
  148. def __unicode__(self):
  149. return unicode(self.pk)
  150. @property
  151. def user_info(self):
  152. return {
  153. 'user_id': self.user_id,
  154. 'nickname': self.nickname,
  155. 'avatar': self.avatar,
  156. 'admin': self.admin,
  157. 'subadmin': self.subadmin,
  158. 'name': self.name,
  159. 'phone': self.phone,
  160. 'relative_persons': self.relative_persons,
  161. 'authority': self.authority,
  162. 'remark': self.remark,
  163. }
  164. @property
  165. def admin_info(self):
  166. return {
  167. 'user_id': self.user_id,
  168. 'nickname': self.nickname,
  169. 'avatar': self.avatar,
  170. 'admin': self.admin,
  171. 'subadmin': self.subadmin,
  172. 'name': self.name,
  173. 'phone': self.phone,
  174. 'relative_persons': self.relative_persons,
  175. 'authority': self.authority,
  176. 'remark': self.remark,
  177. 'status': self.admin_status,
  178. }
  179. class GroupPhotoInfo(CreateUpdateMixin):
  180. APP_GROUP = 0
  181. SESSION_GROUP = 1
  182. PHOTO_FROM = (
  183. (APP_GROUP, u'APP 建群'),
  184. (SESSION_GROUP, u'SESSION 建群'),
  185. )
  186. photo_id = ShortUUIDField(_(u'photo_id'), max_length=255, help_text=u'照片唯一标识', db_index=True)
  187. group_id = models.CharField(_(u'group_id'), max_length=255, blank=True, null=True, help_text=u'群组唯一标识', db_index=True)
  188. user_id = models.CharField(_(u'user_id'), max_length=255, blank=True, null=True, help_text=u'用户唯一标识', db_index=True)
  189. nickname = models.CharField(_(u'nickname'), max_length=255, blank=True, null=True, help_text=u'用户群组昵称')
  190. avatar = models.CharField(_(u'avatar'), max_length=255, blank=True, null=True, help_text=u'用户头像')
  191. photo_md5 = models.CharField(_(u'photo_md5'), max_length=255, blank=True, null=True, help_text=u'照片 MD5', db_index=True)
  192. photo_path = models.CharField(_(u'photo_path'), max_length=255, blank=True, null=True, help_text=u'照片存放路径')
  193. photo_w = models.IntegerField(_(u'photo_w'), default=0, help_text=u'照片宽度')
  194. photo_h = models.IntegerField(_(u'photo_h'), default=0, help_text=u'照片高度')
  195. # 双列: 540, 40-50K
  196. photo_thumbnail_path = models.CharField(_(u'photo_thumbnail_path'), max_length=255, blank=True, null=True, help_text=u'照片缩略图存放路径')
  197. photo_thumbnail_w = models.IntegerField(_(u'photo_thumbnail_w'), default=0, help_text=u'照片缩略图宽度')
  198. photo_thumbnail_h = models.IntegerField(_(u'photo_thumbnail_h'), default=0, help_text=u'照片缩略图高度')
  199. # 单列: 1080, xx-100K
  200. photo_thumbnail2_path = models.CharField(_(u'photo_thumbnail2_path'), max_length=255, blank=True, null=True, help_text=u'照片缩略图存放路径')
  201. photo_thumbnail2_w = models.IntegerField(_(u'photo_thumbnail2_w'), default=0, help_text=u'照片缩略图宽度')
  202. photo_thumbnail2_h = models.IntegerField(_(u'photo_thumbnail2_h'), default=0, help_text=u'照片缩略图高度')
  203. comment_num = models.IntegerField(_(u'comment_num'), default=0, help_text=u'照片评论数量')
  204. thumbup_num = models.IntegerField(_(u'thumbup_num'), default=0, help_text=u'照片点赞数量')
  205. photo_from = models.IntegerField(_(u'photo_from'), choices=PHOTO_FROM, default=APP_GROUP, help_text=u'照片来源')
  206. session_id = models.CharField(_(u'session_id'), max_length=255, blank=True, null=True, help_text=u'照片组唯一标识,同 PhotosInfo 表', db_index=True)
  207. lensman_id = models.CharField(_(u'lensman_id'), max_length=255, blank=True, null=True, help_text=u'摄影师唯一标识,同 PhotosInfo 表', db_index=True)
  208. lensman_photo_id = models.CharField(_(u'lensman_photo_id'), max_length=255, blank=True, null=True, help_text=u'摄影师照片唯一标识,同 PhotosInfo 表', db_index=True)
  209. nomark = models.IntegerField(_(u'nomark'), default=299, help_text=u'摄影师照片无水印价格(分)')
  210. origin = models.IntegerField(_(u'origin'), default=999, help_text=u'摄影师照片高清图价格(分)')
  211. class Meta:
  212. verbose_name = _(u'groupphotoinfo')
  213. verbose_name_plural = _(u'groupphotoinfo')
  214. def __unicode__(self):
  215. return unicode(self.pk)
  216. @property
  217. def photo_url(self):
  218. return img_url(self.photo_path)
  219. @property
  220. def photo_thumbnail_url(self):
  221. return img_url(self.photo_thumbnail_path)
  222. @property
  223. def photo_thumbnail2_url(self):
  224. return img_url(self.photo_thumbnail2_path)
  225. @property
  226. def photo_share_url(self):
  227. return share_url(self.photo_id)
  228. @property
  229. def photo_data(self):
  230. return {
  231. 'photo_id': self.photo_id,
  232. 'comment_num': self.comment_num,
  233. 'thumbup_num': self.thumbup_num,
  234. }
  235. def photo_info(self, user_id):
  236. try:
  237. group = GroupInfo.objects.get(group_id=self.group_id)
  238. except GroupInfo.DoesNotExist:
  239. group = None
  240. porder = get_lensman_order_record(self.photo_id, user_id) if self.photo_from == GroupPhotoInfo.SESSION_GROUP else {}
  241. return {
  242. 'group_id': group and group.group_id,
  243. 'group_name': group and group.group_name,
  244. 'group_default_avatar': group and group.group_default_avatar,
  245. 'group_avatar': group and group.group_avatar_url,
  246. 'group_from': group and group.group_from,
  247. 'photo_id': self.photo_id,
  248. 'photo_url': self.photo_url,
  249. 'photo_w': self.photo_w,
  250. 'photo_h': self.photo_h,
  251. 'photo_thumbnail_url': self.photo_thumbnail_url,
  252. 'photo_thumbnail_w': self.photo_thumbnail_w,
  253. 'photo_thumbnail_h': self.photo_thumbnail_h,
  254. 'photo_thumbnail2_url': self.photo_thumbnail2_url,
  255. 'photo_thumbnail2_w': self.photo_thumbnail2_w,
  256. 'photo_thumbnail2_h': self.photo_thumbnail2_h,
  257. 'photo_share_url': self.photo_share_url,
  258. 'user_id': self.user_id,
  259. 'nickname': self.nickname,
  260. 'avatar': self.avatar,
  261. 'comment_num': self.comment_num,
  262. 'thumbup': get_group_photo_thumbup_flag(self.pk, user_id),
  263. 'thumbup_num': self.thumbup_num,
  264. 'photo_from': self.photo_from,
  265. 'session_id': self.session_id,
  266. 'nomark': self.nomark,
  267. 'origin': self.origin,
  268. 'porder': porder,
  269. 'created_at': self.created_at.replace(microsecond=0),
  270. 'origin_expired_stamps': origin_expired_stamps(self.lensman_photo_id, self.user_id)
  271. }
  272. class GroupPhotoOrderInfo(CreateUpdateMixin):
  273. group_id = models.CharField(_(u'group_id'), max_length=255, blank=True, null=True, help_text=u'群组唯一标识', db_index=True)
  274. session_id = models.CharField(_(u'session_id'), max_length=255, blank=True, null=True, help_text=u'照片组唯一标识,同 PhotosInfo 表', db_index=True)
  275. user_id = models.CharField(_(u'user_id'), max_length=255, blank=True, null=True, help_text=u'用户唯一标识', db_index=True)
  276. photo_id = models.CharField(_(u'photo_id'), max_length=255, blank=True, null=True, help_text=u'照片唯一标识', db_index=True)
  277. lensman_photo_id = models.CharField(_(u'lensman_photo_id'), max_length=255, blank=True, null=True, help_text=u'摄影师照片唯一标识,同 PhotosInfo 表', db_index=True)
  278. m_photo_path = models.CharField(_(u'm_photo_path'), max_length=255, blank=True, null=True, help_text=u'照片存放路径,Box上传,无水印')
  279. l_photo_path = models.CharField(_(u'l_photo_path'), max_length=255, blank=True, null=True, help_text=u'照片存放路径,美化大图')
  280. r_photo_path = models.CharField(_(u'r_photo_path'), max_length=255, blank=True, null=True, help_text=u'照片存放路径,高清大图')
  281. class Meta:
  282. verbose_name = _(u'groupphotoorderinfo')
  283. verbose_name_plural = _(u'groupphotoorderinfo')
  284. def __unicode__(self):
  285. return unicode(self.pk)
  286. @property
  287. def m_photo_url(self):
  288. return img_url(self.m_photo_path)
  289. @property
  290. def l_photo_url(self):
  291. return img_url(self.l_photo_path)
  292. @property
  293. def r_photo_url(self):
  294. return img_url(self.r_photo_path)
  295. @property
  296. def porder_info(self):
  297. return {
  298. 'm_photo_url': self.m_photo_url,
  299. 'l_photo_url': self.l_photo_url,
  300. 'r_photo_url': self.r_photo_url,
  301. }
  302. class PhotoCommentInfo(CreateUpdateMixin):
  303. photo_id = models.CharField(_(u'photo_id'), max_length=255, blank=True, null=True, help_text=u'飞图唯一标识', db_index=True)
  304. user_id = models.CharField(_(u'user_id'), max_length=255, blank=True, null=True, help_text=u'用户唯一标识', db_index=True)
  305. nickname = models.CharField(_(u'nickname'), max_length=255, blank=True, null=True, help_text=u'用户群组昵称')
  306. avatar = models.CharField(_(u'avatar'), max_length=255, blank=True, null=True, help_text=u'用户头像')
  307. to_uid = models.CharField(_(u'to_uid'), max_length=255, blank=True, null=True, help_text=u'被评论用户唯一标识', db_index=True)
  308. comment = models.TextField(_(u'comment'), blank=True, null=True, help_text=u'用户评论')
  309. class Meta:
  310. verbose_name = _(u'photocommentinfo')
  311. verbose_name_plural = _(u'photocommentinfo')
  312. def __unicode__(self):
  313. return unicode(self.pk)
  314. @property
  315. def comment_info(self):
  316. return {
  317. 'user_id': self.user_id,
  318. 'nickname': self.nickname,
  319. 'avatar': self.avatar,
  320. 'to_uid': self.to_uid,
  321. 'comment': self.comment,
  322. 'created_at': tc.remove_microsecond(self.created_at),
  323. }
  324. class PhotoThumbUpInfo(CreateUpdateMixin):
  325. photo_id = models.CharField(_(u'photo_id'), max_length=255, blank=True, null=True, help_text=u'飞图唯一标识', db_index=True)
  326. user_id = models.CharField(_(u'user_id'), max_length=255, blank=True, null=True, help_text=u'用户唯一标识', db_index=True)
  327. nickname = models.CharField(_(u'nickname'), max_length=255, blank=True, null=True, help_text=u'用户群组昵称')
  328. avatar = models.CharField(_(u'avatar'), max_length=255, blank=True, null=True, help_text=u'用户头像')
  329. thumbup = models.BooleanField(_(u'thumbup'), default=True, help_text=u'用户点赞', db_index=True)
  330. class Meta:
  331. verbose_name = _(u'photothumbupinfo')
  332. verbose_name_plural = _(u'photothumbupinfo')
  333. def __unicode__(self):
  334. return unicode(self.pk)
  335. @property
  336. def thumbup_info(self):
  337. return {
  338. 'user_id': self.user_id,
  339. 'nickname': self.nickname,
  340. 'avatar': self.avatar,
  341. }