拍爱

tourguidegroup_views.py 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. # -*- coding: utf-8 -*-
  2. from __future__ import division
  3. import json
  4. import shortuuid
  5. from curtail_uuid import CurtailUUID
  6. from django.conf import settings
  7. from django.core.serializers.json import DjangoJSONEncoder
  8. from logit import logit
  9. from TimeConvert import TimeConvert as tc
  10. from account.models import UserInfo
  11. from group.models import GroupInfo, GroupUserInfo
  12. from utils.error.errno_utils import GroupStatusCode, GroupUserStatusCode, TokenStatusCode, UserStatusCode
  13. from utils.error.response_utils import response
  14. from utils.redis.connect import r
  15. from utils.redis.rgroup import get_group_info, get_group_users_info, set_group_info, set_group_users_info
  16. from utils.redis.rkeys import TOUR_GUIDE_GROUP_CUR_GATHER_INFO, TOUR_GUIDE_GROUP_CUR_SESSION
  17. from utils.redis.rtourguide import get_tour_guide_own_group, set_tour_guide_own_group
  18. from utils.redis.rtouruser import get_tour_user_belong_group
  19. from utils.storage_utils import file_save
  20. @logit
  21. def tg_group_create_api(request):
  22. """ 旅行团创建 """
  23. user_id = request.POST.get('user_id', '')
  24. group_name = request.POST.get('group_name', '')
  25. group_default_avatar = int(request.POST.get('group_default_avatar', 0))
  26. started_at = tc.utc_string_to_utc_datetime(request.POST.get('started_at', ''), format='%Y-%m-%dT%H:%M:%SZ') # UTC, %Y-%m-%dT%H:%M:%SZ
  27. ended_at = tc.utc_string_to_utc_datetime(request.POST.get('ended_at', ''), format='%Y-%m-%dT%H:%M:%SZ') # UTC, %Y-%m-%dT%H:%M:%SZ
  28. total_persons = int(request.POST.get('total_persons', 1))
  29. # 用户校验
  30. try:
  31. user = UserInfo.objects.get(user_id=user_id)
  32. except UserInfo.DoesNotExist:
  33. return response(UserStatusCode.USER_NOT_FOUND)
  34. # 权限校验
  35. if not user.istourguide:
  36. return response(GroupStatusCode.NOT_GROUP_ADMIN)
  37. # 旅行团校验
  38. if GroupInfo.objects.filter(
  39. admin_id=user_id,
  40. group_closed=False,
  41. status=True,
  42. ended_at__gt=tc.utc_datetime(),
  43. ).exists():
  44. return response(GroupStatusCode.COULD_HAVE_ONLY_ONE_ACTIVE_GROUP)
  45. # 群组唯一标识
  46. group_id = CurtailUUID.uuid(GroupInfo, 'group_id')
  47. # 群组记录创建
  48. group = GroupInfo.objects.create(
  49. group_id=group_id,
  50. admin_id=user_id,
  51. group_name=group_name,
  52. group_default_avatar=group_default_avatar,
  53. group_from=GroupInfo.TOURGUIDE_GROUP,
  54. name=user.name,
  55. phone=user.phone,
  56. started_at=started_at,
  57. ended_at=ended_at,
  58. total_persons=total_persons,
  59. )
  60. # Redis 群组数据缓存
  61. group_info = set_group_info(group)
  62. # 群组用户记录创建
  63. GroupUserInfo.objects.create(
  64. group_id=group_id,
  65. user_id=user_id,
  66. nickname=user.final_nickname,
  67. avatar=user.avatar,
  68. admin=True,
  69. user_status=GroupUserInfo.PASSED,
  70. passed_at=tc.utc_datetime(),
  71. subadmin=True,
  72. name=user.name,
  73. phone=user.phone,
  74. )
  75. # Redis 群组用户数据缓存
  76. group_users = set_group_users_info(group)
  77. # Redis 设置导游拥有的旅行团
  78. set_tour_guide_own_group(user_id, group_id)
  79. return response(200, 'Create Tour Guide Group Success', u'旅行团创建成功', {
  80. 'group_id': group_id,
  81. 'group': group_info,
  82. 'users': group_users,
  83. })
  84. @logit
  85. def tg_group_detail_api(request):
  86. """ 旅行团详情 """
  87. group_id = request.POST.get('group_id', '')
  88. user_id = request.POST.get('user_id', '')
  89. group_users_info = get_group_users_info(group_id, user_id)
  90. # Remove tourguide
  91. group_passed_users = [uinfo for uinfo in group_users_info['passed'] if not uinfo['subadmin']]
  92. # Update passed users
  93. group_users_info['passed'] = group_passed_users
  94. # Update passed count
  95. group_users_info['passed_count'] = len(group_passed_users)
  96. # Sum(relative_persons)
  97. group_users_info['relative_persons'] = sum([user['relative_persons'] for user in group_passed_users])
  98. return response(200, 'Get Tour Guide Group Detail Info Success', u'获取旅行团详情成功', {
  99. 'group_id': group_id,
  100. 'group': get_group_info(group_id),
  101. 'users': group_users_info,
  102. })
  103. @logit
  104. def pai2_tginfo_api(request):
  105. """ 首页旅行团信息 """
  106. user_id = request.POST.get('user_id', '')
  107. # 获取用户当前所处旅行团
  108. group_id = get_tour_user_belong_group(user_id)
  109. if not group_id:
  110. return response(GroupUserStatusCode.USER_HAS_NOT_JOIN_GROUP)
  111. group_info = get_group_info(group_id)
  112. # Check whether ended
  113. ended_at = group_info.get('ended_at', '')
  114. if ended_at and tc.utc_datetime() > (tc.utc_string_to_utc_datetime(ended_at, format='%Y-%m-%dT%H:%M:%SZ') if isinstance(ended_at, basestring) else ended_at):
  115. return response(GroupStatusCode.GROUP_HAS_ENDED)
  116. # Check gather info
  117. gather_at = group_info.get('gather_at', '')
  118. if gather_at and tc.utc_datetime() > (tc.utc_string_to_utc_datetime(gather_at, format='%Y-%m-%dT%H:%M:%SZ') if isinstance(gather_at, basestring) else gather_at):
  119. group_info['gather_at'] = ''
  120. group_info['gather_lon'] = ''
  121. group_info['gather_lat'] = ''
  122. group_info['gather_location'] = ''
  123. return response(200, 'Get Tour Guide Group Detail Info Success', u'获取旅行团详情成功', {
  124. 'group_id': group_id,
  125. 'group': group_info,
  126. 'users': get_group_users_info(group_id, user_id),
  127. })
  128. @logit(body=settings.LOGIT_BODY_FLAG, res=settings.LOGIT_RES_FLAG)
  129. def tg_group_update_api(request):
  130. """ 旅行团更新 """
  131. group_id = request.POST.get('group_id', '')
  132. admin_id = request.POST.get('admin_id', '') or request.POST.get('user_id', '')
  133. group_name = request.POST.get('group_name', '')
  134. group_desc = request.POST.get('group_desc', '')
  135. group_avatar = request.FILES.get('group_avatar', '')
  136. started_at = tc.utc_string_to_utc_datetime(request.POST.get('started_at', ''), format='%Y-%m-%dT%H:%M:%SZ') # UTC, %Y-%m-%dT%H:%M:%SZ
  137. ended_at = tc.utc_string_to_utc_datetime(request.POST.get('ended_at', ''), format='%Y-%m-%dT%H:%M:%SZ') # UTC, %Y-%m-%dT%H:%M:%SZ
  138. total_persons = int(request.POST.get('total_persons', 0))
  139. attentions = request.FILES.get('attentions', '')
  140. schedules = request.FILES.get('schedules', '')
  141. # 群组校验
  142. try:
  143. group = GroupInfo.objects.get(group_id=group_id)
  144. except GroupInfo.DoesNotExist:
  145. return response(GroupStatusCode.GROUP_NOT_FOUND)
  146. # 权限校验
  147. if group.admin_id != admin_id:
  148. return response(GroupStatusCode.NO_UPDATE_PERMISSION)
  149. # 群组名称更新
  150. if group_name:
  151. group.group_name = group_name
  152. # 群组描述更新
  153. if group_desc:
  154. group.group_desc = group_desc
  155. # 群组头像更新
  156. if group_avatar:
  157. group.group_avatar = file_save(group_avatar, prefix='group', ext='jpeg')[0]
  158. # 起止时间更新
  159. if started_at:
  160. group.started_at = started_at
  161. if ended_at:
  162. group.ended_at = ended_at
  163. # 旅行团总人数更新
  164. if total_persons:
  165. group.total_persons = total_persons
  166. # 注意事项更新
  167. if attentions:
  168. group.attentions_path = file_save(attentions, prefix='tour', ext='jpeg')[0]
  169. # 行程安排更新
  170. if schedules:
  171. group.schedules_path = file_save(schedules, prefix='tour', ext='jpeg')[0]
  172. group.save()
  173. # Redis 群组数据缓存更新
  174. group_info = set_group_info(group)
  175. return response(200, 'Update Group Success', u'群组更新成功', {
  176. 'group_id': group_id,
  177. 'group': group_info,
  178. 'users': get_group_users_info(group_id, admin_id),
  179. })
  180. @logit
  181. def tg_group_close_api(request):
  182. """ 旅行团关闭 """
  183. group_id = request.POST.get('group_id', '')
  184. admin_id = request.POST.get('admin_id', '') or request.POST.get('user_id', '')
  185. # 群组校验
  186. try:
  187. group = GroupInfo.objects.get(group_id=group_id)
  188. except GroupInfo.DoesNotExist:
  189. return response(GroupStatusCode.GROUP_NOT_FOUND)
  190. # 权限校验
  191. if group.admin_id != admin_id:
  192. return response(GroupStatusCode.NO_CLOSE_PERMISSION)
  193. # 群组解锁
  194. group.group_closed = True
  195. group.closed_at = tc.utc_datetime()
  196. group.save()
  197. # Redis 群组数据缓存更新
  198. set_group_info(group)
  199. return response(200, 'Close Tour Guide Group Success', u'旅行团关闭成功')
  200. @logit
  201. def tg_group_gather_start_api(request):
  202. """ 旅行团设置集合时间和地点 """
  203. group_id = request.POST.get('group_id', '')
  204. admin_id = request.POST.get('admin_id', '') or request.POST.get('user_id', '')
  205. gather_at = tc.utc_string_to_utc_datetime(request.POST.get('gather_at', ''), format='%Y-%m-%dT%H:%M:%SZ') # UTC, %Y-%m-%dT%H:%M:%SZ
  206. gather_lon = request.POST.get('lon', '') # 经度
  207. gather_lat = request.POST.get('lat', '') # 纬度
  208. gather_location = request.POST.get('gather_location', '') # 地点
  209. # 群组校验
  210. try:
  211. group = GroupInfo.objects.get(group_id=group_id)
  212. except GroupInfo.DoesNotExist:
  213. return response(GroupStatusCode.GROUP_NOT_FOUND)
  214. # 权限校验
  215. if not GroupUserInfo.objects.filter(group_id=group_id, user_id=admin_id, subadmin=True, status=True).exists():
  216. return response(GroupStatusCode.NO_UPDATE_PERMISSION)
  217. # 集合信息设置
  218. group.gather_at = gather_at
  219. group.gather_lon = gather_lon
  220. group.gather_lat = gather_lat
  221. group.gather_location = gather_location
  222. group.save()
  223. # Redis 群组数据缓存更新
  224. set_group_info(group)
  225. # 更新Session
  226. r.pipeline().set(
  227. TOUR_GUIDE_GROUP_CUR_SESSION % group_id, shortuuid.uuid()
  228. ).set(
  229. TOUR_GUIDE_GROUP_CUR_GATHER_INFO % group_id, json.dumps({
  230. 'gather_at': gather_at,
  231. 'gather_lon': gather_lon,
  232. 'gather_lat': gather_lat,
  233. 'gather_location': gather_location,
  234. }, cls=DjangoJSONEncoder)
  235. ).execute()
  236. return response(200, 'Set Tour Guide Group Gather Info Success', u'设置旅行团集合信息成功')
  237. @logit
  238. def tg_group_token_api(request):
  239. """ 旅行团权限管理票据 """
  240. admin_id = request.POST.get('admin_id', '') or request.POST.get('user_id', '')
  241. # 获取旅行团唯一标识
  242. group_id = get_tour_guide_own_group(admin_id)
  243. return response(200, 'Generate Token Success', u'生成票据成功', {
  244. 'token': r.token(group_id + admin_id, time=180)
  245. })
  246. @logit
  247. def tg_group_transfer_api(request):
  248. """ 旅行团权限管理转移 """
  249. admin_id = request.POST.get('admin_id', '') # 导游唯一标识,识别二维码获取
  250. user_id = request.POST.get('user_id', '')
  251. token = request.POST.get('token', '')
  252. # 获取旅行团唯一标识
  253. group_id = get_tour_guide_own_group(admin_id)
  254. # 票据校验
  255. if not r.token_exists(group_id + admin_id, token):
  256. return response(TokenStatusCode.TOKEN_NOT_FOUND)
  257. # 用户校验
  258. try:
  259. user = UserInfo.objects.get(user_id=user_id)
  260. except UserInfo.DoesNotExist:
  261. return response(UserStatusCode.USER_NOT_FOUND)
  262. # 群组校验
  263. try:
  264. group = GroupInfo.objects.get(group_id=group_id)
  265. except GroupInfo.DoesNotExist:
  266. return response(GroupStatusCode.GROUP_NOT_FOUND)
  267. # 权限校验
  268. if not GroupUserInfo.objects.filter(group_id=group_id, user_id=admin_id, admin=True, status=True).exists():
  269. return response(GroupStatusCode.NO_TRANSFER_PERMISSION)
  270. # 群组用户记录创建,若记录不存在,则创建,若记录已存在,则更新
  271. group_user, created = GroupUserInfo.objects.get_or_create(
  272. group_id=group_id,
  273. user_id=user_id,
  274. defaults={
  275. 'nickname': user.final_nickname,
  276. 'avatar': user.avatar,
  277. 'user_status': GroupUserInfo.PASSED,
  278. 'passed_at': tc.utc_datetime(),
  279. 'subadmin': True,
  280. 'name': user.name,
  281. 'phone': user.phone,
  282. }
  283. )
  284. if not created:
  285. group_user.current_id = -1
  286. group_user.nickname = user.final_nickname
  287. group_user.avatar = user.avatar
  288. group_user.user_status = GroupUserInfo.PASSED
  289. group_user.passed_at = tc.utc_datetime()
  290. group_user.subadmin = True
  291. group_user.name = user.name
  292. group_user.phone = user.phone
  293. group_user.save()
  294. # Redis 群组用户数据缓存
  295. group_users = set_group_users_info(group)
  296. return response(200, 'Create Tour Guide Group Success', u'旅行团创建成功', {
  297. 'group_id': group_id,
  298. 'group': group.data,
  299. 'users': group_users,
  300. })