def get_content_task(request): ret_dict = {} id = request.GET.get("id") id = int(id) news = None try: news = Task.objects.get(id=id) except Task.DoesNotExist: ret_dict['code'] = 1 ret_dict['msg'] = u"该任务不存在" else: update_view_count(news) ret_dict['code'] = 0 strategy = news.strategy.replace('"/media/', '"' + host + '/media/') rules = news.rules.replace('"/media/', '"' + host + '/media/') taskinfo = { 'is_forbidden': news.is_forbidden, 'left_num': news.left_num, 'rules': rules, 'strategy': strategy, 'url': news.exp_url_mobile, 'title': news.title, } ret_dict['taskinfo'] = taskinfo if is_authenticated_app(request): try: UserTask.objects.get(user=request.user, task=news) except UserTask.DoesNotExist: ret_dict.update(accepted=0) else: ret_dict.update(accepted=1) return JsonResponse(ret_dict)
def get_user_coupon_exchange_detail(request): if not request.user.is_authenticated() and not is_authenticated_app( request): res = { 'code': -1, } return JsonResponse(res) count = int(request.GET.get('count', 0)) type = request.GET.get("type", '') data = [] count = int(count) start = 12 * count item_list = UserEvent.objects.filter(user=request.user, event_type='4')[start:start + 12] data = [] for con in item_list: coupon = con.content_object reason = con.remark if con.audit_state == '2': log = con.audited_logs.first() if log: reason = log.reason i = { "title": coupon.project.title, "amount": coupon.project.amount, "account": con.invest_account, "state": con.get_audit_state_display(), 'remark': con.remark, 'time': con.time.strftime("%Y-%m-%d %H:%M:%S"), 'type': coupon.project.get_ctype_display(), 'state_int': con.audit_state, 'reason': reason } data.append(i) return JsonResponse(data, safe=False)
def get_lottery(request): if request.method != "POST" or not request.is_ajax(): logger.warning("Experience refused no-ajax request!!!") raise Http404 result = {} if not request.user.is_authenticated() and not is_authenticated_app( request): result['code'] = -1 result['url'] = reverse('login') + "?next=" + reverse( 'activity_lottery') return JsonResponse(result) if request.user.scores < 10: result['code'] = -2 return JsonResponse(result) trans = charge_score(request.user, '1', 10, u"积分抽奖") if not trans: result['code'] = -3 logger.error("lottery 10 scores charge error!") return JsonResponse(result) event = UserEvent.objects.create(user=request.user, event_type='7', audit_state='1') trans.user_event = event trans.save(update_fields=['user_event']) award_list = [ (1, 61), (2, 30), (3, 6), (4, 2), (5, 1), (6, 0), ] itemid = weighted_random(award_list) translist = None if itemid == 2: translist = charge_score(request.user, '0', 10, u'抽奖获奖') elif itemid == 3: translist = charge_score(request.user, '0', 50, u'抽奖获奖') elif itemid == 4: translist = charge_money(request.user, '0', 80, u'抽奖获奖') elif itemid == 5: translist = charge_money(request.user, '0', 200, u'抽奖获奖') if itemid != 1 and not translist: result['code'] = 4 logger.error("Get lottery award charge error!") result['msg'] = "记账失败!" else: result['code'] = 0 result['itemid'] = itemid if itemid != 1: LotteryRecord.objects.create(user=request.user, award=AwardTable.get(itemid, u'未知')) translist.user_event = event translist.save(update_fields=['user_event']) return JsonResponse(result)
def signin_record(request): if not request.user.is_authenticated() and not is_authenticated_app( request): raise Http404 today = date.today() first_day_of_month = today - timedelta(today.day - 1) sign_days = UserSignIn.objects.filter( user=request.user, date__gte=first_day_of_month).values('date') records = [] for day in sign_days: records.append(day.get('date').day) return JsonResponse(records, safe=False)
def useCoupon(request): if not request.user.is_authenticated() and not is_authenticated_app( request): res = {'code': -1, 'msg': u"尚未登录"} return JsonResponse(res) coupon_id = request.POST.get('id', None) telnum = request.POST.get('telnum', None) remark = request.POST.get('remark', '') coupon_id = int(coupon_id) coupon = Coupon.objects.get(pk=coupon_id) code = '' msg = '' if coupon.is_used: code = '2' msg = u'该优惠券已兑换,请查看兑换记录!' else: events = UserEvent.objects.filter( invest_account=telnum, event_type='4', ) if events.exists(): pro_list = [] project = coupon.project for eve in events: cou = eve.content_object pro_list.append(cou.project) if project in pro_list: code = '2' msg = u'该账号已领取奖励,请不要重复提交!' if code != '2': UserEvent.objects.create( user=request.user, event_type='4', invest_account=telnum, content_object=coupon, audit_state='1', remark=remark, ) code = '1' msg = u'提交成功,请查看兑换记录!' coupon.is_used = True coupon.save(update_fields=['is_used'], ) result = {'code': code, 'msg': msg} return JsonResponse(result)
def get_user_welfare_json(request): if not request.is_ajax(): raise Http404 res = { 'code': 0, } if not request.user.is_authenticated() and not is_authenticated_app( request): raise Http404 type = request.GET.get("type", 0) count = request.GET.get("count", 0) try: count = int(count) type = int(type) except ValueError: count = 0 type = 0 item_list = [] if type == 0: etype = ContentType.objects.get_for_model(Task) else: etype = ContentType.objects.get_for_model(Finance) start = 12 * count item_list = UserEvent.objects.filter(user=request.user, content_type=etype)[start:start + 12] data = [] for con in item_list: reason = con.remark if con.audit_state == '2': log = con.audited_logs.first() if log: reason = log.reason i = { "title": con.content_object.title, "username": con.invest_account, "time": con.time.strftime("%Y-%m-%d"), "state": con.get_audit_state_display(), "state_int": con.audit_state, "reason": reason } data.append(i) return JsonResponse(data, safe=False)
def user_coupon_json(request): user = request.user if not user.is_authenticated() and not is_authenticated_app(request): res = { 'code': -1, } return JsonResponse(res) count = int(request.GET.get('count', 0)) type = str(request.GET.get("type", '')) #移动端顺序是使用0、现金1、加息2,数据库是使用2、现金0、加息1 if type == '0': type = '2' elif type == '1': type = '0' elif type == '2': type = '1' data = [] count = int(count) start = 6 * count try: count = int(count) except ValueError: count = 0 item_list = Coupon.objects.filter( user=request.user, project__ctype=type).select_related( 'project').order_by('-time')[start:start + 6] for con in item_list: project = con.project i = { "title": project.title, "amount": project.amount, "introduction": project.introduction, "url": project.exp_url_mobile, 'endtime': project.endtime, 'id': con.id, 'code': con.exchange_code, 'imgurl': project.pic.url, 'is_used': con.is_used, } data.append(i) return JsonResponse(data, safe=False)
def charge_json(request): if not request.is_ajax(): raise Http404 res = { 'code': 0, } if not request.user.is_authenticated() and not is_authenticated_app( request): raise Http404 user = request.user count = int(request.GET.get('count', 0)) type = str(request.GET.get('type', '0')) start = 6 * count item_list = TransList.objects.filter(user=request.user, transType=type)[start:start + 6] data = [] for con in item_list: i = { "item": con.reason, "amount": con.transAmount, "date": con.time.strftime("%Y-%m-%d"), } if type == '1': event = con.user_event if event: reason = event.remark if event.audit_state == '2': log = event.audited_logs.first() if log: reason = log.reason state = event.get_audit_state_display() state_int = event.audit_state i.update(state_int=event.audit_state, reason=reason) else: state = u"无" i.update(state=state) data.append(i) return JsonResponse(data, safe=False)
def phoneImageV(request): if not request.is_ajax(): raise Http404 action = request.GET.get('action', None) result = {'code': -1, 'message': 'error!'} phone = '' if action == 'register': phone = request.GET.get('phone', None) hashkey = request.GET.get('hashkey', None) response = request.GET.get('response', None) if not (phone and hashkey): return JsonResponse(result) ret = imageV(hashkey, response) if ret != 0: result['code'] = 1 result['message'] = u'图形验证码输入错误!' result.update(generateCap()) return JsonResponse(result) users = MyUser.objects.filter(mobile=phone) if users.exists(): result['code'] = 1 result['message'] = u'该手机号码已被注册,请直接登录!' result.update(generateCap()) return JsonResponse(result) elif action == 'reset_password': phone = request.GET.get('phone', None) hashkey = request.GET.get('hashkey', None) response = request.GET.get('response', None) if not (phone and hashkey): return JsonResponse(result) ret = imageV(hashkey, response) if ret != 0: result['code'] = 1 result['message'] = u'图形验证码输入错误!' result.update(generateCap()) return JsonResponse(result) users = MyUser.objects.filter(mobile=phone) if not users.exists(): result['code'] = 1 result['message'] = u'该手机号码尚未注册!' result.update(generateCap()) return JsonResponse(result) elif action == "change_zhifubao": if not request.user.is_authenticated() and not is_authenticated_app( request): result['code'] = 1 result['message'] = u"尚未登录" return JsonResponse(result) phone = request.user.mobile elif action == "bind_weixin": phone = request.GET.get('phone', None) openid = request.session.get('openid', None) if not openid: result['code'] = 1 result['message'] = u"请在微信中打开网页" return JsonResponse(result) stamp = str(phone) lasttime = request.session.get(stamp, None) now = int(ttime.time()) if lasttime: dif = now - int(lasttime) if dif < 60: result['code'] = 2 result['message'] = u'请不要频繁提交!' result.update(generateCap()) return JsonResponse(result) today = date.today() remote_ip = get_client_ip(request) count_ip = MobileCode.objects.filter(remote_ip=remote_ip, create_at__gt=today).count() if count_ip >= 30: result['code'] = 3 result['message'] = u'该IP当日发送短信请求已超上限,请明日再来!' result.update(generateCap()) return JsonResponse(result) count_mobile = MobileCode.objects.filter(mobile=phone, create_at__gt=today).count() if count_mobile >= 5: result['code'] = 3 result['message'] = u'该手机号当日短信发送请求已超上限,请明日再来!' result.update(generateCap()) return JsonResponse(result) ret = sendmsg_bydhst(phone) if ret: logger.info('Varifing code has been send to:' + phone) result['code'] = 0 MobileCode.objects.create(mobile=phone, rand_code=ret, remote_ip=remote_ip) request.session[stamp] = now else: logger.error('Sending Varifing code to ' + phone + ' is failed!!!') result['code'] = 1 result['message'] = u"发送验证码失败!" result.update(generateCap()) return JsonResponse(result)