def get_code_by_sms(request, tel): """获取短信验证码""" if check_tel(tel): if caches['default'].get(tel): resp = DefaultResponse(*CODE_TOO_FREQUENCY) else: code = gen_mobile_code() message = f'您的短信验证码是{code},打死也不能告诉别人哟!【Python小课】' # 通过异步化函数的delay方法让函数异步化的执行,这个地方就相当于是消息的生产者 # 如果要完成这个任务还需要消息的消费者,需要其他的进程来处理掉这条消息 # 消费者跟生产者可以是不同的机器(通常情况下也是如此) # celery -A zufang worker -l debug # send_sms_by_luosimao.delay(tel, message) # task = send_sms_by_luosimao.s(countdown=10, expires=60) # task.delay(tel, message) send_sms_by_luosimao.apply_async( (tel, message), # {'tel': tel, 'message': message}, queue='queue1', countdown=10, # retry_policy={}, # expires=60, # compression='zlib', ) caches['default'].set(tel, code, timeout=120) resp = DefaultResponse(*MOBILE_CODE_SUCCESS) else: resp = DefaultResponse(*INVALID_TEL_NUM) return resp
def get_code_by_sms(request, tel): """获取短信验证码""" if check_tel(tel): if caches['default'].get(f'{tel}:block'): resp = DefaultResponse(*CODE_TOO_FREQUENCY) else: code = gen_mobile_code() message = f'您的短信验证码是{code},打死也不能告诉别人哟!【Python小课】' send_sms_by_luosimao.apply_async((tel, message), countdown=random.random() * 5) caches['default'].set(f'{tel}:block', code, timeout=120) caches['default'].set(f'{tel}:valid', code, timeout=1800) resp = DefaultResponse(*MOBILE_CODE_SUCCESS) else: resp = DefaultResponse(*INVALID_TEL_NUM) return resp
def get_bar_data(request): queryset = Emp.objects.values('dept__name').annotate(avgsal=Avg('sal')) names, sals = [], [] for result in queryset: names.append(result['dept__name']) sals.append('%.2f' % float(result['avgsal'])) return DefaultResponse( data={ 'names': names, 'sals': [ sals, [ '%.2f' % (random.randint(-1000, 1000) + float(sal)) for sal in sals ], [ '%.2f' % (random.randint(-1000, 1000) + float(sal)) for sal in sals ], [ '%.2f' % (random.randint(-1000, 1000) + float(sal)) for sal in sals ], ] })
def upload_house_photo(request): file_obj = request.FILES.get('mainphoto') if file_obj and len(file_obj) < MAX_PHOTO_SIZE: prefix = to_md5_hex(file_obj.file) filename = f'{prefix}{os.path.splitext(file_obj.name)[1]}' upload_stream_to_qiniu.delay(file_obj, filename, len(file_obj)) # photo = HousePhoto() # photo.path = f'http://q69nr46pe.bkt.clouddn.com/{filename}' # photo.ismain = True # photo.save() resp = DefaultResponse( *FILE_UPLOAD_SUCCESS, data={'url': f'http://q6i8nba3h.bkt.clouddn.com/{filename}'}) else: resp = DefaultResponse(*FILE_SIZE_EXCEEDED) return resp
def login(request): """登录(获取用户身份令牌)""" username = request.data.get('username') password = request.data.get('password') if (check_tel(username) or check_email(username) or check_username(username)) and len(password) >= 6: password = to_md5_hex(password) q = Q(username=username, password=password) | \ Q(tel=username, password=password) | \ Q(email=username, password=password) user = User.objects.filter(q)\ .only('username', 'realname').first() if user: if user.status: # 用户登录成功通过JWT生成用户身份令牌 payload = { 'exp': datetime.datetime.utcnow() + datetime.timedelta(days=1), 'data': { 'userid': user.userid, 'realname': user.realname, 'roleid': user.roles.get_queryset().first().roleid } } token = jwt.encode(payload, SECRET_KEY, algorithm='HS256').decode() with atomic(): current_time = timezone.now() if not user.lastvisit or \ (current_time - user.lastvisit).days >= 1: user.point += 2 user.lastvisit = current_time user.save() loginlog = LoginLog() loginlog.user = user loginlog.logdate = current_time loginlog.ipaddr = get_ip_address(request) loginlog.save() resp = DefaultResponse(*USER_LOGIN_SUCCESS, data={'token': token}) else: resp = DefaultResponse(*USER_LOGIN_STATUS) else: resp = DefaultResponse(*USER_LOGIN_FAILED) else: resp = DefaultResponse(*INVALID_LOGIN_INFO) return resp
def get_code_by_sms(request, tel): if check_tel(tel): if caches['default'].get(f'{tel}:block'): res = DefaultResponse(*CODE_TOO_FREQUENCY) else: code = gen_mobile_code() time.sleep(random.random() * 10) message = f'您的短信验证码为{code}, 如非本人操作请忽略【铁壳网络】' print(message) # 成为消息的生产者,并将任务加入队列 # send_sms_by_luosimao.apply_async((message, tel), countdown=random.random() * 10) caches['default'].set(f'{tel}:block', code, timeout=120) caches['default'].set(f'{tel}:valid', code, timeout=1800) res = DefaultResponse(*MOBILE_CODE_SUCCESS) else: res = DefaultResponse(*INVALID_TEL_NUM) return res
def get_payment_page(request, houseid): # https://opendocs.alipay.com/apis/api_1/alipay.trade.page.pay model = AlipayTradePagePayModel() # 产品订单号 model.out_trade_no = '202003051646520001' # 订单总金额 model.total_amount = 200 # 订单主题 model.subject = '租房订金' # model.body = '支付宝测试' # 销售产品码,与支付宝签约的产品码名称。 注:目前仅支持FAST_INSTANT_TRADE_PAY model.product_code = 'FAST_INSTANT_TRADE_PAY' # 结算详细信息 settle_detail_info = SettleDetailInfo() settle_detail_info.amount = 200 # 结算收款方的账户类型 settle_detail_info.trans_in_type = 'cardAliasNo' # 结算收款方 settle_detail_info.trans_in = '6216603100004601162' settle_detail_infos = list() settle_detail_infos.append(settle_detail_info) # 结算信息 settle_info = SettleInfo() settle_info.settle_detail_infos = settle_detail_infos model.settle_info = settle_info # sub_merchant = SubMerchant() # sub_merchant.merchant_id = '2088102180149774' # model.sub_merchant = sub_merchant request = AlipayTradePagePayRequest(biz_model=model) url = alipay_client.page_execute(request, http_method='GET') # 此处应该补充生成交易流水的代码 ---> Model # 将用户信息、房源信息、交易编号、交易金额、交易时间、交易状态、……写入数据库 # 还需要一个设计一个接口作为接收通知的接口其中包括对订单交易结果的查询 # 最后还要更新之前的交易流水信息 # request = AlipayTradeQueryRequest(biz_model=model) # content = alipay_client.page_execute(request) return DefaultResponse(data={'url': url})