def get_appointment_detail_by_id(request, appointment_id): try: appointment = UserAppointment.objects.get( appointment_id=appointment_id) except UserAppointment.DoesNotExist: return JsonResponse(retcode({}, "9999", "预约单不存在"), safe=True, status=status.HTTP_404_NOT_FOUND) tmp_list = [] res_app_list = [] detail_list = AppointmentDetail.objects.filter(appointment_id=appointment) for detail in detail_list: if detail.site_id.id in tmp_list: res_app_list[tmp_list.index(detail.site_id.id)]['box_info'].append( AppointmentDetailSerializer(detail).data) else: tmp_list.append(detail.site_id.id) res_app_list.append({ 'site': SiteInfoSerializer(detail.site_id).data, 'box_info': [AppointmentDetailSerializer(detail).data] }) ret = { 'appointment': UserAppointmentSerializer(appointment).data, 'info': res_app_list } return JsonResponse(retcode(ret, "0000", "Success"), safe=True, status=status.HTTP_200_OK)
def get_enterprise_appointment(request): data = JSONParser().parse(request) try: enterprise_id = data['enterprise_id'] except Exception: return JsonResponse(retcode({}, "9999", "请输入企业用户id"), safe=True, status=status.HTTP_400_BAD_REQUEST) try: keyword = data['keyword'] except Exception: return JsonResponse(retcode({}, "9999", "请输入keyword"), safe=True, status=status.HTTP_400_BAD_REQUEST) try: enterprise = EnterpriseInfo.objects.get(enterprise_id=enterprise_id) except EnterpriseInfo.DoesNotExist: return JsonResponse(retcode({}, "9999", "企业信息不存在"), safe=True, status=status.HTTP_404_NOT_FOUND) pagination_class = api_settings.DEFAULT_PAGINATION_CLASS paginator = pagination_class() _appointment = UserAppointment.objects if keyword != "": _appointment = _appointment.filter(appointment_code__contains=keyword) ret = _appointment.filter( user_id__enterprise=enterprise).order_by("-appointment_code") page = paginator.paginate_queryset(ret, request) ret_ser = UserAppointmentSerializer(page, many=True) return paginator.get_paginated_response(ret_ser.data)
def set_notify_read_flag(request): data = JSONParser().parse(request) try: notify_id = data['notify_id'] except Exception: return JsonResponse(retcode({}, "9999", "请输入消息id"), safe=True, status=status.HTTP_400_BAD_REQUEST) try: read_flag = data['read_flag'] except Exception: return JsonResponse(retcode({}, "9999", "请输入已读标志"), safe=True, status=status.HTTP_400_BAD_REQUEST) try: notify = NotifyMessage.objects.get(notify_id=notify_id) except NotifyMessage.DoesNotExist: return JsonResponse(retcode({}, "9999", "消息不存在"), safe=True, status=status.HTTP_500_INTERNAL_SERVER_ERROR) notify.read_flag = read_flag notify.save() return JsonResponse(retcode( NotifyMessageSerializer(notify).data, "0000", "Success"), safe=True, status=status.HTTP_200_OK)
def delete_notify(request, notify_id): try: NotifyMessage.objects.get(notify_id=notify_id).delete() except NotifyMessage.DoesNotExist: return JsonResponse(retcode({}, "9999", "消息不存在"), safe=True, status=status.HTTP_500_INTERNAL_SERVER_ERROR) return JsonResponse(retcode({}, "0000", "消息删除成功"), safe=True, status=status.HTTP_200_OK)
def get_param(request, param_key): try: param = Param.objects.get(param_key=param_key) except Param.DoesNotExist: return JsonResponse(retcode({}, "9999", "参数不存在"), safe=True, status=status.HTTP_400_BAD_REQUEST) return JsonResponse(retcode( ParamSerializer(param).data, "0000", "Success"), safe=True, status=status.HTTP_200_OK)
def get_city_list(request, province_id): try: province = Province.objects.get(province_id=province_id) except Province.DoesNotExist: return JsonResponse(retcode({}, "9999", "省信息不存在"), safe=True, status=status.HTTP_404_NOT_FOUND) city_list = City.objects.filter(province=province).order_by('id') return JsonResponse(retcode( CitySerializer(city_list, many=True).data, "0000", "Success"), safe=True, status=status.HTTP_200_OK)
def ble_decrypt(request): data = JSONParser().parse(request) try: dec_data = data['data'] except Exception: return JsonResponse(retcode({}, "9999", "请输入待解密的数据"), safe=True, status=status.HTTP_400_BAD_REQUEST) aes = cryptoutils.aescrypt() return JsonResponse(retcode({"dec_data": str.upper(aes.decrypt(dec_data))}, "0000", "Success"), safe=True, status=status.HTTP_200_OK)
def get_enterprise_appointment_detail(request, appointment_id): try: appointment = UserAppointment.objects.get( appointment_id=appointment_id) except UserAppointment.DoesNotExist: return JsonResponse(retcode({}, "9999", "预约单不存在"), safe=True, status=status.HTTP_404_NOT_FOUND) ret = AppointmentDetail.objects.filter(appointment_id=appointment) return JsonResponse(retcode( AppointmentDetailSerializer(ret, many=True).data, "0000", "Success"), safe=True, status=status.HTTP_200_OK)
def get_box_stat(request, box_id): try: box = BoxInfo.objects.get(deviceid=box_id) except BoxInfo.DoesNotExist: return JsonResponse(retcode({}, "9999", "云箱不存在"), safe=True, status=status.HTTP_500_INTERNAL_SERVER_ERROR) # 获取周区间 today = datetime.datetime.today() week_start_tmp = today - datetime.timedelta(days=today.weekday()) week_start = datetime.datetime(week_start_tmp.year,week_start_tmp.month, week_start_tmp.day, 0, 0, 0) week_end = today + datetime.timedelta(days=7 - today.weekday()) week_queryset = RentLeaseInfo.objects.filter(rent_status=1, lease_start_time__gte=week_start, lease_start_time__lte=week_end, box=box) week_count = week_queryset.count() week_time = 0 for week in week_queryset: if week.lease_start_time is not None and week.lease_end_time is not None: week_time += float((week.lease_end_time - week.lease_start_time).days) * 24 + float( (week.lease_end_time - week.lease_start_time).seconds) / 3600 # 获取月区间 y = today.year m = today.month month_start_dt = datetime.date(y, m, 1) if m == 12: month_end_dt = datetime.date(y + 1, 1, 1) # month_end_dt = datetime.date(y + 1, 1, 1) - datetime.timedelta(days=1) else: month_end_dt = datetime.date(y, m + 1, 1) # month_end_dt = datetime.date(y, m + 1, 1) - datetime.timedelta(days=1) month_queryset = RentLeaseInfo.objects.filter(rent_status=1, lease_start_time__gte=month_start_dt, lease_start_time__lte=month_end_dt, box=box) month_count = month_queryset.count() month_time = 0 for month in month_queryset: if month.lease_start_time is not None and month.lease_end_time is not None: month_time += float((month.lease_end_time - month.lease_start_time).days) * 24 + float( (month.lease_end_time - month.lease_start_time).seconds) / 3600 # 获取年区间 year_start = datetime.date(y, 1, 1) year_end = datetime.date(y + 1, 1, 1) year_queryset = RentLeaseInfo.objects.filter(rent_status=1, lease_start_time__gte=year_start, lease_start_time__lte=year_end, box=box) year_count = year_queryset.count() year_time = 0 for year in year_queryset: if year.lease_start_time is not None and year.lease_end_time is not None: year_time += float((year.lease_end_time - year.lease_start_time).days) * 24 + float( (year.lease_end_time - year.lease_start_time).seconds) / 3600 ret = {'week_count': week_count, 'week_time': week_time, 'month_count': month_count, 'month_time': month_time, 'year_count': year_count, 'year_time': year_time} return JsonResponse(retcode(ret, '0000', 'Success'), safe=True, status=status.HTTP_200_OK)
def get_site_by_filter(request): data = JSONParser().parse(request) try: site_name = data['site_name'] except Exception: return JsonResponse(retcode({}, "9999", "仓库参数必输"), safe=True, status=status.HTTP_400_BAD_REQUEST) pagination_class = api_settings.DEFAULT_PAGINATION_CLASS param = request.query_params if param: pagination_class.default_limit = int(param.get('limit')) paginator = pagination_class() site_list = SiteInfo.objects.filter( Q(name__contains=site_name) | Q(location__contains=site_name)) res_site = [] for item in site_list: # 获取每个堆场的各箱子类型的数目 site_box_num = SiteBoxStock.objects.filter(site=item) res_site.append({ 'id': item.id, 'location': item.location, 'latitude': item.latitude, 'longitude': item.longitude, 'site_code': item.site_code, 'city': item.city, 'nation': item.nation, 'province': item.province, 'name': item.name, 'box_num': site_box_num }) page = paginator.paginate_queryset(res_site, request) ret_ser = SiteInfoMoreSerializer(page, many=True) return paginator.get_paginated_response(ret_ser.data)
def get_site_detail(request, site_id): try: site_info = SiteInfo.objects.get(id=site_id) except SiteInfo.DoesNotExist: return JsonResponse(retcode({}, "9999", "堆场信息不存在"), safe=True, status=status.HTTP_404_NOT_FOUND) # 获取各种类型箱子的可用个数 box_num = SiteBoxStock.objects.filter(site=site_info) return JsonResponse(retcode( { 'site_info': SiteInfoSerializer(site_info).data, 'box_counts': SiteBoxStockSerializer(box_num, many=True).data }, "0000", "Success"), safe=True, status=status.HTTP_200_OK)
def get_site_stat(request, site_id): pagination_class = api_settings.DEFAULT_PAGINATION_CLASS paginator = pagination_class() try: site = SiteInfo.objects.get(id=site_id) except SiteInfo.DoesNotExist: return JsonResponse(retcode({}, "9999", "仓库信息不存在"), safe=True, status=status.HTTP_400_BAD_REQUEST) stat_list = SiteStat.objects.filter(site=site).order_by('-stat_day') print stat_list res_stat = [] for stat_item in stat_list: details = SiteStatDetail.objects.filter(sitestat=stat_item) res_stat.append({ 'stat_id': stat_item.stat_id, 'stat_day': stat_item.stat_day, 'total_in': stat_item.total_in, 'total_out': stat_item.total_out, 'site': stat_item.site, 'detail': details }) page = paginator.paginate_queryset(res_stat, request) ret_ser = SiteStatResSerializer(page, many=True) return paginator.get_paginated_response(ret_ser.data)
def get_box_detail(request, box_id): try: box_info = BoxInfo.objects.get(deviceid=box_id) except BoxInfo.DoesNotExist: return JsonResponse(retcode({}, "9999", "云箱不存在"), safe=True, status=status.HTTP_500_INTERNAL_SERVER_ERROR) sensor_data = SensorData.objects.filter(deviceid=box_id).order_by('-timestamp') if len(sensor_data) > 0: last_data = sensor_data[0] _location = SensorPathDataSerializer(last_data).data else: _sensor = SensorData(deviceid=box_id, timestamp=0, latitude='0', longitude='0') _location = SensorPathDataSerializer(_sensor).data return JsonResponse(retcode( {'box_info': BoxInfoResSerializer(box_info).data, 'location': _location}, "0000", "Success"), safe=True, status=status.HTTP_200_OK)
def set_param(request): data = JSONParser().parse(request) try: param = Param.objects.get(param_key=data['param_key']) except Param.DoesNotExist: param_insert = Param(param_key=data['param_key'], param_value=data['param_value'], param_desc=data['param_desc']) param_insert.save() return JsonResponse(retcode( ParamSerializer(param_insert).data, "0000", "Success"), safe=True, status=status.HTTP_201_CREATED) param.param_value = data['param_value'] param.param_desc = data['param_desc'] param.save() return JsonResponse(retcode( ParamSerializer(param).data, "0000", "Success"), safe=True, status=status.HTTP_200_OK)
def get_box_lease_list(request, box_id): pagination_class = api_settings.DEFAULT_PAGINATION_CLASS paginator = pagination_class() try: box = BoxInfo.objects.get(deviceid=box_id) except BoxInfo.DoesNotExist: return JsonResponse(retcode({}, '9999', '云箱信息不存在'), safe=True, status=status.HTTP_500_INTERNAL_SERVER_ERROR) lease_list = RentLeaseInfo.objects.filter(box=box).order_by('-lease_start_time') page = paginator.paginate_queryset(lease_list, request) ret_ser = RentLeaseBoxSerializer(page, many=True) return paginator.get_paginated_response(ret_ser.data)
def get_appointment_detail_by_site(request, appointment_code, site_id): try: appointment = UserAppointment.objects.get( appointment_code=appointment_code, flag=0) except UserAppointment.DoesNotExist: return JsonResponse(retcode({}, "9999", "预约单不存在"), safe=True, status=status.HTTP_404_NOT_FOUND) try: site = SiteInfo.objects.get(id=site_id) except SiteInfo.DoesNotExist: return JsonResponse(retcode({}, "9999", "仓库信息不存在"), safe=True, status=status.HTTP_500_INTERNAL_SERVER_ERROR) tmp_list = [] res_app_list = [] detail_list = AppointmentDetail.objects.filter(appointment_id=appointment, site_id=site) if len(detail_list) == 0: return JsonResponse(retcode({}, "9999", "未找到对应仓库的预约信息"), safe=True, status=status.HTTP_500_INTERNAL_SERVER_ERROR) for detail in detail_list: if detail.site_id.id in tmp_list: res_app_list[tmp_list.index(detail.site_id.id)]['box_info'].append( AppointmentDetailSerializer(detail).data) else: tmp_list.append(detail.site_id.id) res_app_list.append({ 'site': SiteInfoSerializer(detail.site_id).data, 'box_info': [AppointmentDetailSerializer(detail).data] }) ret = { 'appointment': UserAppointmentSerializer(appointment).data, 'info': res_app_list } return JsonResponse(retcode(ret, "0000", "Success"), safe=True, status=status.HTTP_200_OK)
def cancel_appointment(request): data = JSONParser().parse(request) try: appointment = UserAppointment.objects.get( appointment_id=data['appointment_id'], flag=0) except UserAppointment.DoesNotExist: return JsonResponse(retcode({}, "9999", "预约单不存在或已取消"), safe=True, status=status.HTTP_200_OK) # 获取预约单详情,并根据预约单详情将已经预留的数量回退 detail_list = AppointmentDetail.objects.select_related( 'appointment_id', 'box_type').filter(appointment_id=appointment, flag=0) redis_update_content = {} conn = get_connection_from_pool() try: with transaction.atomic(): # 根据detail更新site stock的数量 for detail in detail_list: # stock = SiteBoxStock.objects.select_for_update().get(site=detail.site_id, box_type=detail.box_type) # stock.reserve_num -= detail.box_num # stock.save() redis_update_content['site'] = detail.site_id_id redis_update_content['box_type'] = detail.box_type.id redis_update_content['box_num'] = detail.box_num conn.rpush(CANCEL_HASH, json.dumps(redis_update_content)) detail.flag = 1 detail.save() # 更新appointment的flag为2(已取消) appointment.flag = 2 appointment.save() except Exception as e: log.error(repr(e)) return JsonResponse(retcode({}, "9999", "取消预约失败"), safe=True, status=status.HTTP_500_INTERNAL_SERVER_ERROR) return JsonResponse(retcode( UserAppointmentSerializer(appointment).data, "0000", "Success"), safe=True, status=status.HTTP_200_OK)
def get_notify_list_by_user(request, user_id): pagination_class = api_settings.DEFAULT_PAGINATION_CLASS paginator = pagination_class() try: user = EnterpriseUser.objects.get(user_id=user_id) except EnterpriseUser.DoesNotExist: return JsonResponse(retcode({}, "9999", "用户信息不存在"), safe=True, status=status.HTTP_400_BAD_REQUEST) notify_list = NotifyMessage.objects.filter(user=user).order_by( '-notify_time', 'read_flag') page = paginator.paginate_queryset(notify_list, request) ret_ser = NotifyMessageSerializer(page, many=True) return paginator.get_paginated_response(ret_ser.data)
def get_dash_data(request, user_id): try: user = EnterpriseUser.objects.get(user_id=user_id) except EnterpriseUser.DoesNotExist: return JsonResponse(retcode({}, "9999", "运输用户不存在"), safe=True, status=status.HTTP_500_INTERNAL_SERVER_ERROR) # 获取未完成预约单数量 appointment_count = UserAppointment.objects.filter(user_id=user, flag=0).count() # 获取在运箱子数量 box_count = RentLeaseInfo.objects.filter(user_id=user, on_site__isnull=True).count() # 获取通知数量 notify_count = NotifyMessage.objects.filter(user=user, read_flag='N').count() return JsonResponse(retcode( { 'appointment_count': appointment_count, 'box_count': box_count, 'notify_count': notify_count }, '0000', 'Success'), safe=True, status=status.HTTP_200_OK)
def get_enterprise_lease_finish_list(request, enterprise_id): pagination_class = api_settings.DEFAULT_PAGINATION_CLASS paginator = pagination_class() try: user = EnterpriseUser.objects.get(user_id=enterprise_id) except EnterpriseUser.DoesNotExist: return JsonResponse(retcode({}, "9999", "企业用户不存在"), safe=True, status=status.HTTP_500_INTERNAL_SERVER_ERROR) # 获取所有企业下所有用户的在运的订单 process_list = RentLeaseInfo.objects.filter( on_site__isnull=False, user_id__enterprise=user.enterprise) page = paginator.paginate_queryset(process_list, request) ret_ser = RentLeaseInfoSerializer(page, many=True) return paginator.get_paginated_response(ret_ser.data)
def get_dash_data(request): # 获取运输企业个数 enterprise_count = EnterpriseInfo.objects.all().count() # 获取仓库的个数 site_count = SiteInfo.objects.all().count() # 获取在运箱子的总数 box_count = RentLeaseInfo.objects.filter(rent_status=0).count() ret = { 'enterprise_count': enterprise_count, 'site_count': site_count, 'box_count': box_count } return JsonResponse(retcode(ret, "0000", "Success"), safe=True, status=status.HTTP_200_OK)
def get_process_order_list(request, user_id): pagination_class = api_settings.DEFAULT_PAGINATION_CLASS paginator = pagination_class() try: user = EnterpriseUser.objects.get(user_id=user_id) except EnterpriseUser.DoesNotExist: return JsonResponse(retcode({}, "9999", "用户不存在"), safe=True, status=status.HTTP_404_NOT_FOUND) # 获取正在运行中的箱子 lease_list = RentLeaseInfo.objects.filter(rent_status=0, user_id=user) page = paginator.paginate_queryset(lease_list, request) ret_ser = RentLeaseInfoSerializer(page, many=True) return paginator.get_paginated_response(ret_ser.data)
def create_appointment(request): data = JSONParser().parse(request) try: user_id = data['user_id'] except Exception: return JsonResponse(retcode({}, "9999", "请输入承运用户id"), safe=True, status=status.HTTP_400_BAD_REQUEST) try: cancel_time = data['cancel_time'] except Exception: return JsonResponse(retcode({}, "9999", "请输入预约时间"), safe=True, status=status.HTTP_400_BAD_REQUEST) try: site_total = data['site_total'] if len(site_total) == 0: raise Exception except Exception: return JsonResponse(retcode({}, "9999", "堆场预约箱子数不能为空"), safe=True, status=status.HTTP_400_BAD_REQUEST) try: appointment_time = data['appointment_time'] except Exception: return JsonResponse(retcode({}, "9999", "预约时间不能为空"), safe=True, status=status.HTTP_400_BAD_REQUEST) try: user_model = EnterpriseUser.objects.get(user_id=user_id) except EnterpriseUser.DoesNotExist: return JsonResponse(retcode({}, "9999", "承运方用户不存在"), safe=True, status=status.HTTP_404_NOT_FOUND) if user_model.enterprise.enterprise_deposit_status == 0: return JsonResponse(retcode({}, "9999", "企业未缴押金,无法预约租箱"), safe=True, status=status.HTTP_200_OK) try: with transaction.atomic(): # 获取预约码 code = get_appointment_code() # 生成预约单 appointment_id = str(uuid.uuid1()) # 获取取消时间 _cancel_time = datetime.datetime.fromtimestamp(cancel_time) _appointment_time = datetime.datetime.fromtimestamp( appointment_time) appointment_model = UserAppointment( appointment_id=appointment_id, appointment_code=code, appointment_time=_appointment_time, user_id=user_model, cancel_time=_cancel_time) appointment_model.save() # 循环获取每个堆场的请求 detail_list = [] for site in site_total: try: site_id = site['site_id'] except Exception: raise Exception("9999", "堆场信息不能为空") site_model = SiteInfo.objects.get(id=site_id) # 获取堆场的可用数量信息 try: site_box_num = site['site_box_num'] except Exception: raise Exception("9999", "堆场可用箱子数量不能为空") for _site_num in site_box_num: _stock_model = SiteBoxStock.objects.select_for_update( ).get(site=site_model, box_type=_site_num['box_type']) # 根据可用数量-已经预约数量是否大于当前租借数量判断是否可租 if _site_num[ 'num'] > _stock_model.ava_num - _stock_model.reserve_num: raise Exception("9999", "预约数量大于堆场可用箱子数量") # 更新box_type&site_id预约数量 upd_res_num = _stock_model.reserve_num + _site_num['num'] _stock_model.reserve_num = upd_res_num _stock_model.save() # 获取box_type的model box_type_model = BoxTypeInfo.objects.get( id=_site_num['box_type']) # 构建appointment_detail appointment_detail = AppointmentDetail( detail_id=str(uuid.uuid1()), appointment_id=appointment_model, box_type=box_type_model, box_num=_site_num['num'], site_id=site_model, flag=0) detail_list.append(appointment_detail) # 批量insert appointment detail AppointmentDetail.objects.bulk_create(detail_list) except Exception as e: code, msg = e.args if code == '0000': return JsonResponse(retcode({}, code, msg), safe=True, status=status.HTTP_200_OK) else: return JsonResponse(retcode({}, code, msg), safe=True, status=status.HTTP_500_INTERNAL_SERVER_ERROR) message = u'您的租箱预约已经成功,请到指定仓库获取云箱' create_notify("云箱预约", message, user_id) conn = get_connection_from_pool() user_id = user_model.user_id # if user_model.user_alias_id is not None and user_model.user_alias_id != "": if conn.hexists(USER_ALIAS_ID_HASH, user_id): alias = [] # alias.append(user_model.user_alias_id) alias.append(conn.hget(USER_ALIAS_ID_HASH, user_id)) celery.send_push_message.delay(alias, message) return JsonResponse(retcode( UserAppointmentSerializer(appointment_model).data, "0000", "Success"), safe=True, status=status.HTTP_200_OK)
def get_box_info_list(request): pagination_class = api_settings.DEFAULT_PAGINATION_CLASS param = request.query_params if param: pagination_class.default_limit = int(param.get('limit')) paginator = pagination_class() data = JSONParser().parse(request) try: province_id = data['province_id'] except Exception: return JsonResponse(retcode({}, "9999", "province输入有误"), safe=True, status=status.HTTP_400_BAD_REQUEST) try: city_id = data['city_id'] except Exception: return JsonResponse(retcode({}, "9999", "city输入有误"), safe=True, status=status.HTTP_400_BAD_REQUEST) try: site_id = data['site_id'] except Exception: return JsonResponse(retcode({}, "9999", "site输入有误"), safe=True, status=status.HTTP_400_BAD_REQUEST) try: ava_flag = data['ava_flag'] except Exception: return JsonResponse(retcode({}, "9999", "可用标志输入有误"), safe=True, status=status.HTTP_400_BAD_REQUEST) try: box_id = data['box_id'] except Exception: return JsonResponse(retcode({}, "9999", "云箱id输入有误"), safe=True, status=status.HTTP_400_BAD_REQUEST) query_set = BoxInfo.objects condition = 'N' if province_id != 0: try: province = Province.objects.get(province_id=province_id) except Province.DoesNotExist: return JsonResponse(retcode({}, "9999", "省信息不存在"), safe=True, status=status.HTTP_500_INTERNAL_SERVER_ERROR) query_set = query_set.filter(siteinfo__province=province) condition = 'Y' if city_id != 0: try: city = City.objects.get(id=city_id) except City.DoesNotExist: return JsonResponse(retcode({}, "9999", "市信息不存在"), safe=True, status=status.HTTP_500_INTERNAL_SERVER_ERROR) query_set = query_set.filter(siteinfo__city=city) condition = 'Y' if site_id != 0: try: site = SiteInfo.objects.get(id=site_id) except SiteInfo.DoesNotExist: return JsonResponse(retcode({}, "9999", "仓库信息不存在"), safe=True, status=status.HTTP_500_INTERNAL_SERVER_ERROR) query_set = query_set.filter(siteinfo=site) condition = 'Y' if ava_flag != '': query_set = query_set.filter(ava_flag=ava_flag) condition = 'Y' if box_id != '': query_set = query_set.filter(deviceid__contains=box_id) condition = 'Y' if condition == 'N': query_set = query_set.all() page = paginator.paginate_queryset(query_set, request) ret_ser = BoxInfoListSerializer(page, many=True) offset = request.GET.get('offset') if offset is None: ret_full = BoxInfoListSerializer(query_set, many=True) return JsonResponse({'data': {'results': ret_full.data}, 'status': 'OK', 'msg': 'Get box list success.'}, safe=True, status=status.HTTP_200_OK) return paginator.get_paginated_response(ret_ser.data)
def get_user_finished_list(request, user_id): # pagination_class = api_settings.DEFAULT_PAGINATION_CLASS pagination_class = complex_page.ComplexPagination param = request.query_params _limit = 10 _offset = 0 if param: _limit = int(param.get('limit')) _offset = int(param.get('offset')) paginator = pagination_class() try: user = EnterpriseUser.objects.get(user_id=user_id) except EnterpriseUser.DoesNotExist: return JsonResponse(retcode({}, "9999", "用户不存在"), safe=True, status=status.HTTP_404_NOT_FOUND) # 获取预约单列表 # appointment_list = UserAppointment.objects.filter(user_id=user).exclude(flag=0).order_by('-appointment_time') _new_limit = _offset + _limit appointment_list = UserAppointment.objects.filter(user_id=user).exclude( flag=0).order_by('-appointment_time')[_offset:_new_limit] _count = UserAppointment.objects.filter(user_id=user).exclude( flag=0).count() conn = get_connection_from_pool() ret = [] for appointment_item in appointment_list: if conn.hexists(APPOINTMENT_HASH, appointment_item.appointment_id): detail_pickle = conn.hget(APPOINTMENT_HASH, appointment_item.appointment_id) detail_dict = pickle.loads(detail_pickle) else: tmp_list = [] res_app_list = [] detail_list = AppointmentDetail.objects.filter( appointment_id=appointment_item) for detail in detail_list: if detail.site_id.id in tmp_list: # res_app_list[tmp_list.index(detail.site_id.id)]['box_info'].append( # AppointmentDetailSerializer(detail).data) res_app_list[tmp_list.index( detail.site_id.id)]['box_info'].append(detail) else: tmp_list.append(detail.site_id.id) # res_app_list.append({'site': SiteInfoSerializer(detail.site_id).data, # 'box_info': [AppointmentDetailSerializer(detail).data]}) res_app_list.append({ 'id': detail.site_id.id, 'location': detail.site_id.location, 'latitude': detail.site_id.latitude, 'longitude': detail.site_id.longitude, 'site_code': detail.site_id.site_code, 'city': detail.site_id.city, 'province': detail.site_id.province, 'nation': detail.site_id.nation, 'volume': detail.site_id.volume, 'box_info': [detail] }) # ret.append({'appointment': UserAppointmentSerializer(appointment_item).data, 'info': res_app_list}) detail_dict = { 'appointment_id': appointment_item.appointment_id, 'user_id': appointment_item.user_id, 'appointment_time': appointment_item.appointment_time, 'appointment_code': appointment_item.appointment_code, 'flag': appointment_item.flag, 'info': res_app_list } detail_pickle = pickle.dumps(detail_dict) conn.hset(APPOINTMENT_HASH, appointment_item.appointment_id, detail_pickle) ret.append(detail_dict) paginator.paginate_queryset(ret, request, _count) ret_ser = AppointmentResSerializer(ret, many=True) # page = paginator.paginate_queryset(ret, request) # ret_ser = AppointmentResSerializer(page, many=True) return paginator.get_paginated_response(ret_ser.data)
def get_all_params(request): param_list = Param.objects.all() return JsonResponse(retcode( ParamSerializer(param_list, many=True).data, "0000", "Success"), safe=True, status=status.HTTP_200_OK)
def get_all_site(request): site_list = SiteInfo.objects.all() return JsonResponse(retcode( AllSiteSerializer(site_list, many=True).data, "0000", "Success"), safe=True, status=status.HTTP_200_OK)
def get_province_list(request): province_list = Province.objects.all().order_by('province_id') return JsonResponse(retcode( ProvinceSerializer(province_list, many=True).data, "0000", "Success"), safe=True, status=status.HTTP_200_OK)
def get_box_type_list(request): type_list = BoxTypeInfo.objects.all() return JsonResponse(retcode(BoxTypeInfoSerializer(type_list, many=True).data, "0000", "Success"), safe=True, status=status.HTTP_200_OK)