def pay(request): ''' 支付订单 :param request: POST { 'preplot_id': 'pay': } :return: ''' if request.method == 'GET': pass elif request.method == 'POST': preplot_id = request.POST.get('preplot_id') pay = request.POST.get('pay') # 设置订单已支付 preplot = models.preplot.objects.get_or_none(preplot_id=preplot_id) if preplot.is_paid: return JsonResponse({'status': 202, 'message': '订单已支付。'}) else: preplot.is_paid = True preplot.save() user = models.userInfo.objects.get_or_none( user_id=userSystem(request).getUsername()) user.money = user.money - pay user.save() return JsonResponse({ 'status': 200, 'message': 'OK', })
def recharge(request): ''' :param request: POST { 'money':xxx } :return: ''' if request.method == 'GET': pass elif request.method == 'POST': cash = request.POST.get('money') user = models.userInfo.objects.get_or_none( user_id=userSystem(request).getUsername()) user.money += int(cash) user.save() return JsonResponse({ 'status': 200, 'message': 'SUCCESS', 'data': { 'money': user.money, } })
def refund(request): ''' 订单退款 :param request: :return: ''' if request.method == 'GET': pass elif request.method == 'POST': preplot_id = request.POST.get('preplot_id') ticket_id = request.POST.get('ticket_id') refund = int(request.POST.get('pay')) * 0.5 user = models.userInfo.objects.get_or_none( user_id=userSystem(request).getUsername()) ticket = models.ticketInfo.objects.get_or_none(ticket_id=ticket_id) print('------------------') preplot = models.preplot.objects.get_or_none(preplot_id=preplot_id) print('-----------') ticketpreplot = models.ticketPreplot.objects.get_or_none( ticket_id=ticket) if ticketpreplot.is_refund: return JsonResponse({'status': 202, 'message': '您已经办理退款手续。'}) else: ticketpreplot.is_refund = True ticketpreplot.save() user.money = user.money + refund user.save() models.ticketRefund.ticketRefundObject(preplot, ticket, refund, True) return JsonResponse({'status': 200, 'message': 'SUCCESS'})
def userPOST(request): ''' 修改用户信息 :param request: POST :return: ''' # 错误信息列表 error = '' assert request.method == 'POST' user = models.userInfo.objects.get_or_none( user_id=userSystem(request).getUsername()) if user: user.user_name = request.POST.get('user_name', user.user_name) user.phone = request.POST.get('phone', user.phone) user.email = request.POST.get('email', user.email) user.id_num = request.POST.get('id_num', user.id_num) else: error = 'user is not exist.' return JsonResponse({ 'status': 202, 'message': error, }) # 保存 修改 user.save() return JsonResponse({'status': 200, 'message': 'SUCCESS'})
def userGET(request): ''' 显示用户信息 :param request: :return: ''' # 错误信息列表 error = '' assert request.method == 'GET' # 获取用户对象 user_id = request.GET.get('user_id') if user_id: user = models.userInfo.objects.get_or_none(user_id=user_id) else: user = models.userInfo.objects.get_or_none( user_id=userSystem(request).getUsername()) if user is not None: return JsonResponse({ 'status': 200, 'message': 'SUCCESS', 'data': { 'user_id': user.user_id, 'user_name': user.user_name, 'id_num': user.id_num, 'email': user.email, 'phone': user.phone, 'money': user.money, } }) else: error = 'user is not exist' return JsonResponse({'status': 202, 'message': error})
def process_request(self, request): ''' Request 预处理函数 :param request: :return: ''' # 错误信息 context = '' print(request.path) # 限制访问url列表,需要权限或者处于登陆状态 if 'xadmin' not in request.path \ and ('user' in request.path \ or 'order' in request.path \ or 'refund' in request.path \ or 'search' in request.path)\ : print('进入验证') # 如果用户没有认证,限制访问 if not request.session.has_key('sessionID') \ and not request.session.has_key('token'): # 用户不具有登陆认证口令 context = 'Please login' return JsonResponse({ 'status': 202, 'message': context, }) # 如果用户拥有口令,但口令过期 elif request.session.has_key('sessionID') \ and request.session.has_key('token'): #用户拥有session,登陆验证 user = userSystem(request) if not user.getUserObject(): #用户登出 logout(request) context = 'your authentication exceed the time limit or you has logged in another place.' return JsonResponse({ 'status': 202, 'message': context, }) ''' 渲染模板 ''' '''
def process_request(self, request): ''' Request 预处理函数 :param request: :return: ''' #错误信息 context = [] if request.method == 'GET': requestData = request.GET else: requestData = request.POST if 'admin' not in request.path: # 如果用户没有认证,限制访问 if not request.session.has_key('sessionID') and not request.session.has_key('token') \ and 'register' not in request.path and 'login' not in request.path: context.append('Please login') return JsonResponse({ 'status': 403, 'message': context, }) elif request.session.has_key('sessionID') and request.session.has_key('token') \ and 'register' not in request.path and 'logout' not in request.path: try: #用户拥有session,登陆验证 user = userSystem(request) if not user.getUserObject(): context.append( 'your authentication exceed the time limit') return JsonResponse({ 'status': 403, 'message': context, }) ''' 权限管理 pass ''' except Exception as e: context.append('somthing is wrong') return JsonResponse({ 'status': 202, 'message': context, })
def prelot(request): ''' 获取 订单信息 :param request: GET :return: { 'status': 'message': 'data':[ { 'preplot_id': 'total_pay': 'is_paid': 'date': 'ticket':[ { 'ticket_id': 'train_id': 'startstation': 'endstation': 'departuretime': 'arrivaltime': 'passenger': 'pay': 'is_valid': 'is_refund': }, ....... ] } ] } ''' if request.method == 'GET': user = models.userInfo.objects.get_or_none( user_id=userSystem(request).getUsername()) preplot_list = affair.get_preplot(user) return JsonResponse({ 'status': 200, 'message': 'OK', 'data': preplot_list, })
def login(request): ''' 登陆函数 :param request: POST { ’user_id': 'passwd': } :return: { 'status': 'message': } ''' # 判断request.method if request.method == 'GET': # 重定向到 登陆界面 return redirect('/api/v1/login/') elif request.method == 'POST': # 获取用户 账户 和 密码 user_id = request.POST.get('user_id') passwd = request.POST.get('passwd') print(user_id) print(passwd) # 获取user对象 user = userSystem(request) # user登陆 认证 # 判定新用户登陆 顶替旧的用户 if user.getUsername() != user_id: user.delCache() # 清理 session request.session.flush() error = user.authentication(user_id=user_id, passwd=passwd) # error为空 则登陆成功 # error不为空 则登陆不成功 if not error: return JsonResponse({'status': 200, 'message': 'OK', 'data': {}}) else: return JsonResponse({'status': 202, 'message': error, 'data': {}})
def logout(request): ''' 账号 登出 :param request: :return: { 'status': 'message': } ''' # 清理缓存 user = userSystem(request) user.delCache() # 清理 session request.session.flush() return JsonResponse({ 'status': 200, 'message': 'OK', })
def change(request): ''' 改签车票 :param request: POST :return: ''' if request.method == 'GET': pass elif request.method == 'POST': # 获取票的详细信息 train_id = request.POST.get('train_id') passenger_id = request.POST.get('passenger_id') passenger = models.userInfo.objects.get_or_none(user_id=passenger_id) startstation = models.stationInfo.objects.get_or_none( station_name=request.POST.get('startstation')) endstation = models.stationInfo.objects.get_or_none( station_name=request.POST.get('endstation')) pay = request.POST.get('pay') date = request.POST.get('date') departuretime = date + request.POST.get('departuretime') arrivaltime = date + request.POST.get('arrivaltime') print('---------------') print(startstation) print(endstation) if passenger and startstation and endstation and date and pay and departuretime and arrivaltime and train_id: # 形成订单 owner = models.userInfo.objects.get_or_none( user_id=userSystem(request).getUsername()) preplot_id = md5(time.strftime('%M:%S')) preplot = models.preplot.preplotObject(preplot_id, owner, date) carriage_id = request.POST.get('carriage_id') seat = request.POST.get('seat') # 形成票 train = models.trainInfo.objects.get_or_none(train_id=train_id) carriage = models.carriageInfo.objects.get_or_none( carriage_id=carriage_id) # carriage_num = models.trainCarriage.objects.filter(Q(train_id=train) & Q(carriage_id=carriage))[0].carriage_num ticket_id = md5(time.strftime('%M:%S')) ticket = models.ticketInfo.ticketInfoObject( ticket_id, train, carriage, seat, pay, startstation, endstation, datetime.strptime(departuretime, '%Y-%m-%d%H:%M:%S'), datetime.strptime(arrivaltime, '%Y-%m-%d%H:%M:%S')) # 形成绑定信息 models.ticketPreplot.ticketPreplotObject(preplot, ticket, passenger) old_preplot_id = request.POST.get('old_preplot_id') old_ticke_id = request.POST.get('old_ticket_id') old_ticke = models.ticketInfo.objects.get_or_none( ticket_id=old_ticke_id) print(old_ticke) print(old_ticke_id) old_ticke.is_valid = False old_ticke.save() old_preplot = models.ticketPreplot.objects.get_or_none( ticket_id=old_ticke) old_preplot.is_refund = True old_preplot.save() user = models.userInfo.objects.get_or_none( user_id=userSystem(request).getUsername()) user.money = user.money + old_ticke.pay user.save() preplot_info = models.preplot.objects.get_or_none( preplot_id=old_preplot_id) models.ticketRefund.ticketRefundObject(preplot_info, old_ticke, old_ticke.pay, True) return JsonResponse({ 'status': 200, 'message': 'SUCCESS', }) else: return JsonResponse({ 'status': 202, 'message': 'user is not exist.' })
def order(request): ''' 如果时间更改的话 就需要加个字段,来计算时间 ticket_id train_id startstation endstation carriage_id site pay departuretime arrivaltime is_valid preplot_id user_id date = mod is_paid :param request: POST { 'date':2018-5-16, 'train_id':G3 'startstation':2 'endstation':8 'departuretime':05:06 'arrivaltime':17:56 'pay':5000 'data':[ { 'carriage_id':21, 'seat':0, 'passenger_id_num':130521199803077773, }, ] } :return: ''' if request.method == 'GET': pass elif request.method == 'POST': # 获取票的详细信息 train_id = request.POST.get('train_id') startstation = models.stationInfo.objects.get_or_none( station_name=request.POST.get('startstation')) endstation = models.stationInfo.objects.get_or_none( station_name=request.POST.get('endstation')) pay = request.POST.get('pay') date = request.POST.get('date') data = eval(request.POST.get('data')) print(type(data)) print(data) print('-----------------------') departuretime = date + request.POST.get('departuretime') arrivaltime = date + request.POST.get('arrivaltime') print(data[0]) passenger_id_num = data[0]['passenger_id_num'] print(passenger_id_num) carriage_id = data[0]['carriage'] seat = data[0]['seat'] passenger = models.userInfo.objects.get_or_none( id_num=passenger_id_num) if passenger: # 形成订单 owner = models.userInfo.objects.get_or_none( user_id=userSystem(request).getUsername()) preplot_id = md5(time.strftime('%M:%S')) preplot = models.preplot.preplotObject(preplot_id, owner, date) #形成票 train = models.trainInfo.objects.get_or_none(train_id=train_id) carriage = models.carriageInfo.objects.get_or_none( carriage_id=carriage_id) # carriage_num = models.trainCarriage.objects.filter(Q(train_id=train) & Q(carriage_id=carriage))[0].carriage_num ticket_id = md5(time.strftime('%M:%S')) ticket = models.ticketInfo.ticketInfoObject( ticket_id, train, carriage, seat, pay, startstation, endstation, datetime.strptime(departuretime, '%Y-%m-%d%H:%M:%S'), datetime.strptime(arrivaltime, '%Y-%m-%d%H:%M:%S')) #形成绑定信息 models.ticketPreplot.ticketPreplotObject(preplot, ticket, passenger) return JsonResponse({ 'status': 200, 'message': 'SUCCESS', }) else: return JsonResponse({ 'status': 202, 'message': 'user is not exist.' })