123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420 |
- from __future__ import division
- from django.conf import settings
- from django.db import transaction
- from django_logit import logit
- from django_query import get_query_value
- from django_response import response
- from paginator import pagination
- from TimeConvert import TimeConvert as tc
- from account.models import UserInfo
- from coupon.models import UserCouponInfo
- from member.models import (GoodsInfo, GoodsOrderInfo, MemberActivityInfo, MemberActivitySigninInfo,
- MemberActivitySignupInfo, RightInfo)
- from utils.error.errno_utils import (MemberActivityStatusCode, MemberCouponStatusCode, MemberGoodStatusCode, MemberRightStatusCode,
- UserStatusCode)
- from utils.redis.connect import r
- from utils.redis.rkeys import MEMBER_SEND_COUPON_LIST, MEMBER_UPGRADE_INFO
- from utils.redis.rshot import get_member_shot_data
- @logit
- def member(request):
- brand_id = request.POST.get('brand_id', settings.KODO_DEFAULT_BRAND_ID)
- user_id = request.POST.get('user_id', '')
-
- try:
- user = UserInfo.objects.get(user_id=user_id)
- except UserInfo.DoesNotExist:
- return response(UserStatusCode.USER_NOT_FOUND)
- rights = RightInfo.objects.filter(status=True).order_by('position')
- rights = [right.data for right in rights]
- goods = GoodsInfo.objects.filter(only_for_member=False, left_num__gt=0, status=True).order_by('position')
- goods = [good.data(user_id) for good in goods][:2]
- member_goods = GoodsInfo.objects.filter(only_for_member=True, left_num__gt=0, minlevel__lte=user.level, status=True).order_by('position')
- member_goods = [good.data(user_id) for good in member_goods]
- member_goods = [good for good in member_goods if not good['has_member_exchange']]
- upgrade_info, _ = r.getdel(MEMBER_UPGRADE_INFO % (brand_id, user_id))
- return response(200, data={
- 'nickname': user.final_nickname,
- 'avatar': user.avatar,
- 'integral': user.integral,
- 'freeze_integral': user.freeze_integral,
- 'final_integral': user.final_integral,
- 'shots_num': user.shots_num,
- 'level': user.level,
- 'rights': rights,
- 'goods': goods,
- 'member_goods': member_goods,
- 'has_upgrade': bool(upgrade_info),
- })
- @logit
- def rights(request):
- brand_id = request.POST.get('brand_id', settings.KODO_DEFAULT_BRAND_ID)
- user_id = request.POST.get('user_id', '')
- level = request.POST.get('level', '')
-
- try:
- user = UserInfo.objects.get(user_id=user_id)
- except UserInfo.DoesNotExist:
- return response(UserStatusCode.USER_NOT_FOUND)
- rights = RightInfo.objects.filter(status=True).order_by('position')
- rights = [right.data for right in rights]
- return response(200, data={
- 'nickname': user.final_nickname,
- 'avatar': user.avatar,
- 'integral': user.integral,
- 'freeze_integral': user.freeze_integral,
- 'final_integral': user.final_integral,
- 'shots_num': user.shots_num,
- 'level': user.level,
- 'rights': rights,
- })
- @logit
- def right_detail(request):
- brand_id = request.POST.get('brand_id', settings.KODO_DEFAULT_BRAND_ID)
- user_id = request.POST.get('user_id', '')
- right_id = request.POST.get('right_id', '')
- try:
- right = RightInfo.objects.get(right_id=right_id)
- except RightInfo.DoesNotExist:
- return response(MemberRightStatusCode.RIGHT_NOT_FOUND)
- return response(200, data={
- 'right': right.data,
- })
- @logit
- def goods(request):
- brand_id = request.POST.get('brand_id', settings.KODO_DEFAULT_BRAND_ID)
- user_id = request.POST.get('user_id', '')
-
- try:
- user = UserInfo.objects.get(user_id=user_id)
- except UserInfo.DoesNotExist:
- return response(UserStatusCode.USER_NOT_FOUND)
- raw_goods = GoodsInfo.objects.filter(only_for_member=False, left_num__gt=0, status=True).order_by('position', '-pk')
- banners = goods = []
- for good in raw_goods:
- if good.is_slider:
- banners.append(good.data(user_id))
- else:
- goods.append(good.data(user_id))
- return response(200, data={
- 'nickname': user.final_nickname,
- 'avatar': user.avatar,
- 'integral': user.integral,
- 'freeze_integral': user.freeze_integral,
- 'final_integral': user.final_integral,
- 'shots_num': user.shots_num,
- 'level': user.level,
- 'banners': banners,
- 'goods': goods,
- })
- @logit
- def good_detail(request):
- brand_id = request.POST.get('brand_id', settings.KODO_DEFAULT_BRAND_ID)
- user_id = request.POST.get('user_id', '')
- good_id = request.POST.get('good_id', '')
-
- try:
- user = UserInfo.objects.get(user_id=user_id)
- except UserInfo.DoesNotExist:
- return response(UserStatusCode.USER_NOT_FOUND)
- try:
- good = GoodsInfo.objects.get(good_id=good_id)
- except GoodsInfo.DoesNotExist:
- return response(MemberGoodStatusCode.GOOD_NOT_FOUND)
- return response(200, data={
- 'nickname': user.final_nickname,
- 'avatar': user.avatar,
- 'integral': user.integral,
- 'freeze_integral': user.freeze_integral,
- 'final_integral': user.final_integral,
- 'shots_num': user.shots_num,
- 'level': user.level,
- 'good': good.details(user_id),
- })
- @logit
- @transaction.atomic
- def good_exchange(request):
- brand_id = request.POST.get('brand_id', settings.KODO_DEFAULT_BRAND_ID)
- user_id = request.POST.get('user_id', '')
- good_id = request.POST.get('good_id', '')
- good_ids = get_query_value(request, 'good_ids', val_cast_type='listjson')
- name = request.POST.get('name', '')
- phone = request.POST.get('phone', '')
- address = request.POST.get('address', '')
-
- try:
- user = UserInfo.objects.select_for_update().get(user_id=user_id)
- except UserInfo.DoesNotExist:
- return response(UserStatusCode.USER_NOT_FOUND)
- if good_id:
- good_ids.append(good_id)
- goods = []
- for good_id in good_ids:
- try:
- good = GoodsInfo.objects.select_for_update().get(good_id=good_id)
- except GoodsInfo.DoesNotExist:
- return response(MemberGoodStatusCode.GOOD_NOT_FOUND)
- if good.left_num <= 0:
- return response(MemberGoodStatusCode.GOOD_STOCK_NOT_ENOUGH)
- if user.level < good.minlevel:
- return response(MemberGoodStatusCode.GOOD_NO_EXCHANGE_PERMISSION)
- if user.integral < good.integral:
- return response(MemberGoodStatusCode.GOOD_INTEGRAL_NOT_ENOUGH)
- user.integral -= good.integral
- user.save()
- good.left_num -= 1
- good.save()
- GoodsOrderInfo.objects.create(
- user_id=user_id,
- good_id=good_id,
- good_type=good.good_type,
- name=name,
- phone=phone,
- address=address,
- )
- if good.good_type == GoodsInfo.PHYSICAL:
-
- pass
- else:
-
- if good.coupon_id:
-
- r.rpushjson(MEMBER_SEND_COUPON_LIST, {
- 'brand_id': brand_id,
- 'user_id': user_id,
- 'coupon_id': good.coupon_id,
- })
- else:
- pass
- goods.append(good.data(user_id))
- return response(200, data={
- 'nickname': user.final_nickname,
- 'avatar': user.avatar,
- 'integral': user.integral,
- 'freeze_integral': user.freeze_integral,
- 'final_integral': user.final_integral,
- 'shots_num': user.shots_num,
- 'level': user.level,
- 'goods': goods,
- })
- @logit
- def coupons(request):
- brand_id = request.POST.get('brand_id', settings.KODO_DEFAULT_BRAND_ID)
- user_id = request.POST.get('user_id', '')
- page = request.POST.get('page', 1)
- num = request.POST.get('num', 20)
- coupons = UserCouponInfo.objects.filter(user_id=user_id, status=True).order_by('-pk')
- coupons, left = pagination(coupons, page, num)
- coupons = [coupon.data for coupon in coupons]
- return response(200, data={
- 'coupons': coupons,
- 'left': left,
- })
- @logit
- def user_coupon_detail(request):
- brand_id = request.POST.get('brand_id', settings.KODO_DEFAULT_BRAND_ID)
- user_coupon_id = request.POST.get('user_coupon_id', '')
- try:
- coupon = UserCouponInfo.objects.get(user_coupon_id=user_coupon_id, status=True)
- except UserCouponInfo.DoesNotExist:
- return response(MemberCouponStatusCode.USER_COUPON_NOT_FOUND)
- return response(200, data=coupon.data)
- @logit
- @transaction.atomic
- def user_coupon_use(request):
- brand_id = request.POST.get('brand_id', settings.KODO_DEFAULT_BRAND_ID)
- admin_id = request.POST.get('admin_id', '')
- user_coupon_id = request.POST.get('user_coupon_id', '')
- try:
- coupon = UserCouponInfo.objects.select_for_update().get(user_coupon_id=user_coupon_id, status=True)
- except UserCouponInfo.DoesNotExist:
- return response(MemberCouponStatusCode.USER_COUPON_NOT_FOUND)
- if not coupon.has_actived:
- return response(MemberCouponStatusCode.USER_COUPON_NOT_ACTIVED)
- if coupon.has_expired:
- return response(MemberCouponStatusCode.USER_COUPON_HAS_EXPIRED)
- if coupon.has_used:
- return response(MemberCouponStatusCode.USER_COUPON_HAS_USED)
- coupon.has_used = True
- coupon.admin_id = admin_id
- coupon.used_at = tc.utc_datetime()
- coupon.save()
- return response(200, data=coupon.data)
- @logit
- def integrals(request):
- brand_id = request.POST.get('brand_id', settings.KODO_DEFAULT_BRAND_ID)
- return response(200, data={
- 'shots_types': get_member_shot_data(),
- 'enable_photo_upvote_integral': True,
- 'mp_url': 'https://mp.weixin.qq.com/s/2K6PAnf3KrxtrP40-DBuww',
- 'photo_upvote_integrals': {
- 'headers': [u'排名', u'日', u'周', u'月'],
- 'ranks': [
- [u'第1名', '10', '20', '30'],
- [u'第2名', '5', '10', '15'],
- [u'第3名', '3', '6', '9'],
- [u'第4-10名', '1', '2', '3'],
- ]
- },
- 'enable_activity_integral': True,
- })
- @logit
- def activity_list(request):
- brand_id = request.POST.get('brand_id', settings.KODO_DEFAULT_BRAND_ID)
- user_id = request.POST.get('user_id', '')
- raw_activitys = MemberActivityInfo.objects.filter(status=True).order_by('-date')
- banners = []
- activitys = []
- for act in raw_activitys:
- if act.is_slider:
- banners.append(act.data(user_id))
- else:
- activitys.append(act.data(user_id))
- return response(200, data={
- 'banners': banners,
- 'activitys': activitys,
- })
- @logit
- def activity_detail(request):
- brand_id = request.POST.get('brand_id', settings.KODO_DEFAULT_BRAND_ID)
- user_id = request.POST.get('user_id', '')
- activity_id = request.POST.get('activity_id')
- try:
- act = MemberActivityInfo.objects.get(activity_id=activity_id, status=True)
- except MemberActivityInfo.DoesNotExist:
- return response(MemberActivityStatusCode.ACTIVITY_NOT_FOUND)
- return response(200, data={
- 'activity': act.details(user_id),
- })
- @logit
- def activity_signup(request):
- brand_id = request.POST.get('brand_id', settings.KODO_DEFAULT_BRAND_ID)
- user_id = request.POST.get('user_id', '')
- activity_id = request.POST.get('activity_id')
- name = request.POST.get('name', '')
- phone = request.POST.get('phone', '')
- try:
- act = MemberActivityInfo.objects.get(activity_id=activity_id, status=True)
- except MemberActivityInfo.DoesNotExist:
- return response(MemberActivityStatusCode.ACTIVITY_NOT_FOUND)
- MemberActivitySignupInfo.objects.update_or_create(user_id=user_id, activity_id=activity_id, defaults={
- 'title': act.title,
- 'name': name,
- 'phone': phone,
- })
-
-
- return response(200, data={
- 'activity': act.data(user_id),
- })
- @logit
- @transaction.atomic
- def activity_signin(request):
- brand_id = request.POST.get('brand_id', settings.KODO_DEFAULT_BRAND_ID)
- user_id = request.POST.get('user_id', '')
- activity_id = request.POST.get('activity_id')
-
- try:
- user = UserInfo.objects.select_for_update().get(user_id=user_id)
- except UserInfo.DoesNotExist:
- return response(UserStatusCode.USER_NOT_FOUND)
- try:
- act = MemberActivityInfo.objects.get(activity_id=activity_id, status=True)
- except MemberActivityInfo.DoesNotExist:
- return response(MemberActivityStatusCode.ACTIVITY_NOT_FOUND)
- MemberActivitySigninInfo.objects.update_or_create(user_id=user_id, activity_id=activity_id, defaults={
- 'title': act.title,
- })
- user.integral += act.integral
- user.save()
-
- return response(200, data={
- 'activity': act.data(user_id),
- })
|