123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368 |
- from __future__ import division
- import json
- import shortuuid
- from curtail_uuid import CurtailUUID
- from django.conf import settings
- from django.core.serializers.json import DjangoJSONEncoder
- from logit import logit
- from TimeConvert import TimeConvert as tc
- from account.models import UserInfo
- from group.models import GroupInfo, GroupUserInfo
- from utils.error.errno_utils import GroupStatusCode, GroupUserStatusCode, TokenStatusCode, UserStatusCode
- from utils.error.response_utils import response
- from utils.redis.connect import r
- from utils.redis.rgroup import get_group_info, get_group_users_info, set_group_info, set_group_users_info
- from utils.redis.rkeys import TOUR_GUIDE_GROUP_CUR_GATHER_INFO, TOUR_GUIDE_GROUP_CUR_SESSION
- from utils.redis.rtourguide import get_tour_guide_own_group, set_tour_guide_own_group
- from utils.redis.rtouruser import get_tour_user_belong_group
- from utils.storage_utils import file_save
- @logit
- def tg_group_create_api(request):
- """ 旅行团创建 """
- user_id = request.POST.get('user_id', '')
- group_name = request.POST.get('group_name', '')
- group_default_avatar = int(request.POST.get('group_default_avatar', 0))
- started_at = tc.utc_string_to_utc_datetime(request.POST.get('started_at', ''), format='%Y-%m-%dT%H:%M:%SZ')
- ended_at = tc.utc_string_to_utc_datetime(request.POST.get('ended_at', ''), format='%Y-%m-%dT%H:%M:%SZ')
- total_persons = int(request.POST.get('total_persons', 1))
-
- try:
- user = UserInfo.objects.get(user_id=user_id)
- except UserInfo.DoesNotExist:
- return response(UserStatusCode.USER_NOT_FOUND)
-
- if not user.istourguide:
- return response(GroupStatusCode.NOT_GROUP_ADMIN)
-
- if GroupInfo.objects.filter(
- admin_id=user_id,
- group_closed=False,
- status=True,
- ended_at__gt=tc.utc_datetime(),
- ).exists():
- return response(GroupStatusCode.COULD_HAVE_ONLY_ONE_ACTIVE_GROUP)
-
- group_id = CurtailUUID.uuid(GroupInfo, 'group_id')
-
- group = GroupInfo.objects.create(
- group_id=group_id,
- admin_id=user_id,
- group_name=group_name,
- group_default_avatar=group_default_avatar,
- group_from=GroupInfo.TOURGUIDE_GROUP,
- name=user.name,
- phone=user.phone,
- started_at=started_at,
- ended_at=ended_at,
- total_persons=total_persons,
- )
-
- group_info = set_group_info(group)
-
- GroupUserInfo.objects.create(
- group_id=group_id,
- user_id=user_id,
- nickname=user.final_nickname,
- avatar=user.avatar,
- admin=True,
- user_status=GroupUserInfo.PASSED,
- passed_at=tc.utc_datetime(),
- subadmin=True,
- name=user.name,
- phone=user.phone,
- )
-
- group_users = set_group_users_info(group)
-
- set_tour_guide_own_group(user_id, group_id)
- return response(200, 'Create Tour Guide Group Success', u'旅行团创建成功', {
- 'group_id': group_id,
- 'group': group_info,
- 'users': group_users,
- })
- @logit
- def tg_group_detail_api(request):
- """ 旅行团详情 """
- group_id = request.POST.get('group_id', '')
- user_id = request.POST.get('user_id', '')
- group_users_info = get_group_users_info(group_id, user_id)
-
- group_passed_users = [uinfo for uinfo in group_users_info['passed'] if not uinfo['subadmin']]
-
- group_users_info['passed'] = group_passed_users
-
- group_users_info['passed_count'] = len(group_passed_users)
-
- group_users_info['relative_persons'] = sum([user['relative_persons'] for user in group_passed_users])
- return response(200, 'Get Tour Guide Group Detail Info Success', u'获取旅行团详情成功', {
- 'group_id': group_id,
- 'group': get_group_info(group_id),
- 'users': group_users_info,
- })
- @logit
- def pai2_tginfo_api(request):
- """ 首页旅行团信息 """
- user_id = request.POST.get('user_id', '')
-
- group_id = get_tour_user_belong_group(user_id)
- if not group_id:
- return response(GroupUserStatusCode.USER_HAS_NOT_JOIN_GROUP)
- group_info = get_group_info(group_id)
-
- ended_at = group_info.get('ended_at', '')
- 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):
- return response(GroupStatusCode.GROUP_HAS_ENDED)
-
- gather_at = group_info.get('gather_at', '')
- 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):
- group_info['gather_at'] = ''
- group_info['gather_lon'] = ''
- group_info['gather_lat'] = ''
- group_info['gather_location'] = ''
- return response(200, 'Get Tour Guide Group Detail Info Success', u'获取旅行团详情成功', {
- 'group_id': group_id,
- 'group': group_info,
- 'users': get_group_users_info(group_id, user_id),
- })
- @logit(body=settings.LOGIT_BODY_FLAG, res=settings.LOGIT_RES_FLAG)
- def tg_group_update_api(request):
- """ 旅行团更新 """
- group_id = request.POST.get('group_id', '')
- admin_id = request.POST.get('admin_id', '') or request.POST.get('user_id', '')
- group_name = request.POST.get('group_name', '')
- group_desc = request.POST.get('group_desc', '')
- group_avatar = request.FILES.get('group_avatar', '')
- started_at = tc.utc_string_to_utc_datetime(request.POST.get('started_at', ''), format='%Y-%m-%dT%H:%M:%SZ')
- ended_at = tc.utc_string_to_utc_datetime(request.POST.get('ended_at', ''), format='%Y-%m-%dT%H:%M:%SZ')
- total_persons = int(request.POST.get('total_persons', 0))
- attentions = request.FILES.get('attentions', '')
- schedules = request.FILES.get('schedules', '')
-
- try:
- group = GroupInfo.objects.get(group_id=group_id)
- except GroupInfo.DoesNotExist:
- return response(GroupStatusCode.GROUP_NOT_FOUND)
-
- if group.admin_id != admin_id:
- return response(GroupStatusCode.NO_UPDATE_PERMISSION)
-
- if group_name:
- group.group_name = group_name
-
- if group_desc:
- group.group_desc = group_desc
-
- if group_avatar:
- group.group_avatar = file_save(group_avatar, prefix='group', ext='jpeg')[0]
-
- if started_at:
- group.started_at = started_at
- if ended_at:
- group.ended_at = ended_at
-
- if total_persons:
- group.total_persons = total_persons
-
- if attentions:
- group.attentions_path = file_save(attentions, prefix='tour', ext='jpeg')[0]
-
- if schedules:
- group.schedules_path = file_save(schedules, prefix='tour', ext='jpeg')[0]
- group.save()
-
- group_info = set_group_info(group)
- return response(200, 'Update Group Success', u'群组更新成功', {
- 'group_id': group_id,
- 'group': group_info,
- 'users': get_group_users_info(group_id, admin_id),
- })
- @logit
- def tg_group_close_api(request):
- """ 旅行团关闭 """
- group_id = request.POST.get('group_id', '')
- admin_id = request.POST.get('admin_id', '') or request.POST.get('user_id', '')
-
- try:
- group = GroupInfo.objects.get(group_id=group_id)
- except GroupInfo.DoesNotExist:
- return response(GroupStatusCode.GROUP_NOT_FOUND)
-
- if group.admin_id != admin_id:
- return response(GroupStatusCode.NO_CLOSE_PERMISSION)
-
- group.group_closed = True
- group.closed_at = tc.utc_datetime()
- group.save()
-
- set_group_info(group)
- return response(200, 'Close Tour Guide Group Success', u'旅行团关闭成功')
- @logit
- def tg_group_gather_start_api(request):
- """ 旅行团设置集合时间和地点 """
- group_id = request.POST.get('group_id', '')
- admin_id = request.POST.get('admin_id', '') or request.POST.get('user_id', '')
- gather_at = tc.utc_string_to_utc_datetime(request.POST.get('gather_at', ''), format='%Y-%m-%dT%H:%M:%SZ')
- gather_lon = request.POST.get('lon', '')
- gather_lat = request.POST.get('lat', '')
- gather_location = request.POST.get('gather_location', '')
-
- try:
- group = GroupInfo.objects.get(group_id=group_id)
- except GroupInfo.DoesNotExist:
- return response(GroupStatusCode.GROUP_NOT_FOUND)
-
- if not GroupUserInfo.objects.filter(group_id=group_id, user_id=admin_id, subadmin=True, status=True).exists():
- return response(GroupStatusCode.NO_UPDATE_PERMISSION)
-
- group.gather_at = gather_at
- group.gather_lon = gather_lon
- group.gather_lat = gather_lat
- group.gather_location = gather_location
- group.save()
-
- set_group_info(group)
-
- r.pipeline().set(
- TOUR_GUIDE_GROUP_CUR_SESSION % group_id, shortuuid.uuid()
- ).set(
- TOUR_GUIDE_GROUP_CUR_GATHER_INFO % group_id, json.dumps({
- 'gather_at': gather_at,
- 'gather_lon': gather_lon,
- 'gather_lat': gather_lat,
- 'gather_location': gather_location,
- }, cls=DjangoJSONEncoder)
- ).execute()
- return response(200, 'Set Tour Guide Group Gather Info Success', u'设置旅行团集合信息成功')
- @logit
- def tg_group_token_api(request):
- """ 旅行团权限管理票据 """
- admin_id = request.POST.get('admin_id', '') or request.POST.get('user_id', '')
-
- group_id = get_tour_guide_own_group(admin_id)
- return response(200, 'Generate Token Success', u'生成票据成功', {
- 'token': r.token(group_id + admin_id, time=180)
- })
- @logit
- def tg_group_transfer_api(request):
- """ 旅行团权限管理转移 """
- admin_id = request.POST.get('admin_id', '')
- user_id = request.POST.get('user_id', '')
- token = request.POST.get('token', '')
-
- group_id = get_tour_guide_own_group(admin_id)
-
- if not r.token_exists(group_id + admin_id, token):
- return response(TokenStatusCode.TOKEN_NOT_FOUND)
-
- try:
- user = UserInfo.objects.get(user_id=user_id)
- except UserInfo.DoesNotExist:
- return response(UserStatusCode.USER_NOT_FOUND)
-
- try:
- group = GroupInfo.objects.get(group_id=group_id)
- except GroupInfo.DoesNotExist:
- return response(GroupStatusCode.GROUP_NOT_FOUND)
-
- if not GroupUserInfo.objects.filter(group_id=group_id, user_id=admin_id, admin=True, status=True).exists():
- return response(GroupStatusCode.NO_TRANSFER_PERMISSION)
-
- group_user, created = GroupUserInfo.objects.get_or_create(
- group_id=group_id,
- user_id=user_id,
- defaults={
- 'nickname': user.final_nickname,
- 'avatar': user.avatar,
- 'user_status': GroupUserInfo.PASSED,
- 'passed_at': tc.utc_datetime(),
- 'subadmin': True,
- 'name': user.name,
- 'phone': user.phone,
- }
- )
- if not created:
- group_user.current_id = -1
- group_user.nickname = user.final_nickname
- group_user.avatar = user.avatar
- group_user.user_status = GroupUserInfo.PASSED
- group_user.passed_at = tc.utc_datetime()
- group_user.subadmin = True
- group_user.name = user.name
- group_user.phone = user.phone
- group_user.save()
-
- group_users = set_group_users_info(group)
- return response(200, 'Create Tour Guide Group Success', u'旅行团创建成功', {
- 'group_id': group_id,
- 'group': group.data,
- 'users': group_users,
- })
|