def sort_optional(request): sort_type = request.POST.get('sort_type', '1') page = request.POST.get('page', str(OPTINOAL_PAGE)) page_num = request.POST.get('page_num', str(OPTINOAL_PAGE_NUM)) if not (page.isdigit() and page_num.isdigit()): return JsonResponse(get_response_data('000002')) page = int(page) page_num = int(page_num) # 判断用户是否登录 rval, user_id = is_auth(request.META) _logger.info('user is auth? {0}, user id is {1}'.format(rval, user_id)) if rval: jh_user = Jh_User.objects.get(user_id=user_id) # 已注册用户 elif user_id is not None: jh_user = Jh_User.objects.get(id=user_id) # 游客 else: return JsonResponse(get_response_data('000007')) # jh_user = Jh_User.objects.get(user=request.user) personal_select = jh_user.personal_select if sort_type == '1': # 移动单个选项 wine_code = request.POST.get('code', '') sort_no = request.POST.get('sort_no', '') if wine_code and sort_no.isdigit(): sort_no = int(sort_no) personal_select_list = personal_select.split(';') if (sort_no >= len(personal_select_list)) or ( wine_code not in personal_select_list): # 移到末尾 tmp = ';'.join(personal_select.split(wine_code + ';')) jh_user.personal_select = tmp + ';' + wine_code jh_user.save() else: origin_no = personal_select_list.index(wine_code) if origin_no < sort_no: # 往后移 for i in range(origin_no, sort_no): personal_select_list[i] = personal_select_list[i + 1] else: # 往前移 for j in range(origin_no, sort_no, -1): personal_select_list[j] = personal_select_list[j - 1] personal_select_list[sort_no] = wine_code jh_user.personal_select = ';'.join(personal_select_list) jh_user.save() data = get_wine_list(jh_user.personal_select, (page - 1) * page_num, page * page_num) return JsonResponse(get_response_data('000000', data)) else: return JsonResponse(get_response_data('000002')) else: # 全局排序 sort_codes = request.POST.get('sort_codes') if not sort_codes: return JsonResponse(get_response_data('000002')) data = get_wine_list(sort_codes, (page - 1) * page_num, page * page_num) return JsonResponse(get_response_data('000000', data))
def search_wine(request): key = request.POST.get('key') try: page = int(request.POST.get('page', OPTINOAL_PAGE)) page_num = int(request.POST.get('page_num', OPTINOAL_PAGE_NUM)) except Exception as e: _logger.info('error msg is {0}'.format(e)) return JsonResponse(get_response_data('000002')) # 判断用户是否登录 rval, user_id = is_auth(request.META) _logger.info('user is auth? {0}, user id is {1}'.format(rval, user_id)) if rval: jh_user = Jh_User.objects.get(user_id=user_id) # 已注册用户 elif user_id is not None: jh_user = Jh_User.objects.get(id=user_id) # 游客 else: return JsonResponse(get_response_data('000007')) select_codes = jh_user.personal_select.split(';') data = [] start = (page - 1) * page_num end = page * page_num if not key: wine_info = WineInfo.objects.all()[start:end] for wine in wine_info: wine_json = wine.to_json() last_price, ratio = last_price_ratio(wine.code) wine_json['last_price'] = last_price if wine.code in select_codes: wine_json['is_select'] = True else: wine_json['is_select'] = False data.append(wine_json) else: # wine_info = WineInfo.objects.filter(name__contains=key)[start:end] wine_info = WineInfo.objects.filter( Q(name__contains=key) | Q(code__contains=key))[start:end] for wine in wine_info: wine_json = wine.to_json() last_price, ratio = last_price_ratio(wine.code) wine_json['last_price'] = last_price if wine.code in select_codes: wine_json['is_select'] = True else: wine_json['is_select'] = False data.append(wine_json) res = get_response_data('000000', data) return JsonResponse(res)
def del_optional(request): codes = request.POST.get('code') if not codes: return get_response_data('000002') # 判断用户是否登录 rval, user_id = is_auth(request.META) _logger.info('user is auth? {0}, user id is {1}'.format(rval, user_id)) if rval: jh_user = Jh_User.objects.get(user_id=user_id) # 已注册用户 elif user_id is not None: jh_user = Jh_User.objects.get(id=user_id) # 游客 else: return JsonResponse(get_response_data('000007')) # jh_user = Jh_User.objects.get(user=request.user) personal_select = jh_user.personal_select.split(';') del_codes = list(set(codes.split(';'))) new_codes = [code for code in personal_select if code not in del_codes] data = [] start = (OPTINOAL_PAGE - 1) * OPTINOAL_PAGE_NUM end = OPTINOAL_PAGE * OPTINOAL_PAGE_NUM for wine_code in new_codes[start:end]: try: wine = WineInfo.objects.get(code=wine_code, is_delete=False) except Exception as e: _logger.info('wine {0} not found'.format(wine_code)) continue wine_json = wine.to_json() wine_json['quote_change'] = '0.00%' # 待后续补充计算方法 data.append(wine_json) new_codes = ';'.join(new_codes) jh_user.personal_select = new_codes jh_user.save() # 推送最新自选信息 for del_code in del_codes: rval = select_emit(del_code, 'delete') if rval: _logger.info('code: {0}, 推送最新自选信息<<<成功>>>!'.format(del_code)) else: _logger.info('code: {0}, 推送最新自选信息<<<失败>>>!'.format(del_code)) res = get_response_data('000000', data) return JsonResponse(res)
def get_optional(request): # 判断用户是否登录 rval, user_id = is_auth(request.META) _logger.info('user is auth? {0}, user id is {1}'.format(rval, user_id)) if rval: jh_user = Jh_User.objects.get(user_id=user_id) # 已注册用户 elif user_id is not None: jh_user = Jh_User.objects.get(id=user_id) # 游客 else: return JsonResponse(get_response_data('000007')) # jh_user = Jh_User.objects.get(user=request.user) personal_select = jh_user.personal_select options = personal_select.split(';') try: page = int(request.GET.get('page', OPTINOAL_PAGE)) page_num = int(request.GET.get('page_num', OPTINOAL_PAGE_NUM)) except Exception as e: _logger.info('error msg is {0}'.format(e)) return JsonResponse(get_response_data('000002')) data = [] start = (page - 1) * page_num end = page * page_num sort_no = start for wine_code in options[start:end]: try: wine = WineInfo.objects.get(code=wine_code, is_delete=False) except Exception as e: _logger.info('wine {0} not found'.format(wine_code)) continue wine_json = wine.to_json() last_price, ratio = last_price_ratio(wine_code) wine_json['quote_change'] = ratio # 待后续补充计算方法 wine_json['last_price'] = last_price # wine_json['proposed_price'] = last_price wine_json['sort_no'] = sort_no data.append(wine_json) sort_no += 1 res = get_response_data('000000', data) return JsonResponse(res)
def set_optional(request): wine_code = request.POST.get('code') if not wine_code: res = get_response_data('000002') return JsonResponse(res) # 判断用户是否登录 rval, user_id = is_auth(request.META) _logger.info('user is auth? {0}, user id is {1}'.format(rval, user_id)) if rval: jh_user = Jh_User.objects.get(user_id=user_id) elif user_id is not None: jh_user = Jh_User.objects.get(id=user_id) else: return JsonResponse(get_response_data('000007')) # jh_user = Jh_User.objects.get(user=request.user) personal_select = jh_user.personal_select personal_select_list = personal_select.split(';') # personal_select_list = list(set(personal_select_list)) if wine_code in personal_select_list: res = get_response_data('100001') return JsonResponse(res) if personal_select: personal_select += ';' + wine_code else: personal_select = wine_code # personal_select += wine_code + ';' jh_user.personal_select = personal_select jh_user.save() # 推送最新自选信息 rval = select_emit(wine_code, 'add') if rval: _logger.info('推送最新自选信息<<<成功>>>!') else: _logger.info('推送最新自选信息<<<失败>>>!') res = get_response_data('000000') return JsonResponse(res)