def post(self, request): """ 干部or个人考核结果报表 :param request: :return: """ ret = BaseResponse() data = request.data condition = {} conditions = {} is_dep = 0 try: year = data['year'] # 年份(前台默认传过来当前年份) name = data['name'] # 姓名 department = data['department'] # 单位 category = data['category'] # 类别 flag = data['flag'] # 标识 if name: condition['name__icontains'] = name conditions['name__icontains'] = name if department: condition['department'] = department is_dep = 1 if flag: condition['beiKaoHe__contains'] = flag conditions['beiKaoHe__contains'] = flag if year: condition['year'] = year conditions['year'] = year elif not year: year = datetime.datetime.now().year # 当前年份 condition['year'] = year conditions['year'] = year if category: obj = Gbresult.objects.filter( **conditions).values().order_by('rankingofCategory') elif category == '' and is_dep == 1: obj = Gbresult.objects.filter( **condition).values().order_by('rankingOfDep') elif category == '' and is_dep == 0: obj = Gbresult.objects.filter( **condition).values().order_by('rankingOfAll') if not obj: table = {} verbosename = Gbresult._meta.fields # 获取被导入数据库的字段 for name in verbosename: table['{}'.format(name.name)] = '' ret.code = 208 ret.data = '没有对应内容' ret.info = [table] return Response(ret.dict) ret.code = 201 ret.data = '数据加载成功' ret.info = obj except Exception as e: ret.code = 202 ret.error = '数据加载失败' return Response(ret.dict) return Response(ret.dict)
def get(self, request): """ 干部考核汇总表 将三张干部考核结果表的数据汇总在一起,并计算排名 """ ret = BaseResponse() try: with transaction.atomic(): year = datetime.datetime.now().year # 当前年份 # 计算前,首先删除当前年份的数据 Gbresult.objects.filter(year=year).all().delete() xy_obj = xygbResult.objects.filter( level='全体').values() # 学院干部结果表 jg_obj = jggbResult.objects.filter( level='全体').values() # 机关干部结果表 zsdw_obj = zsdwgbResult.objects.filter( level='全体').values() # 直属单位干部结果表 # 处理机关干部结果表 for i in range(0, len(jg_obj)): each_obj = Beiceping.objects.filter(IDcard=jg_obj[i]['beiceping_id']).\ values('name', 'department_id', 'beiKaoHe') # 姓名, 单位名 # 将机关干部结果表的内容添加到干部结果汇总表中 Gbresult.objects.create( name=each_obj[0]['name'], IDcard=jg_obj[i]['beiceping_id'], department=each_obj[0]['department_id'], category='机关', beiKaoHe=each_obj[0]['beiKaoHe'], year=year, index1=jg_obj[i]['index1'], index2=jg_obj[i]['index2'], index3=jg_obj[i]['index3'], index4=jg_obj[i]['index4'], index5=jg_obj[i]['index5'], index6=jg_obj[i]['index6'], index7=jg_obj[i]['index7'], index8=jg_obj[i]['index8'], index9=jg_obj[i]['index9'], index10=jg_obj[i]['index10'], index11=jg_obj[i]['index11'], index12=jg_obj[i]['index12'], index13=jg_obj[i]['index13'], index14=jg_obj[i]['index14'], index15=jg_obj[i]['index15'], index16=jg_obj[i]['index16'], index17=jg_obj[i]['index17'], index18=jg_obj[i]['index18'], index19=jg_obj[i]['index19'], index20=jg_obj[i]['index20'], index21=jg_obj[i]['index21'], index22=jg_obj[i]['index22'], index23=jg_obj[i]['index23'], Excellent=0, score=jg_obj[i]['score']) # 处理学院干部结果表 for i in range(0, len(xy_obj)): each_obj = Beiceping.objects.filter(IDcard=xy_obj[i]['beiceping_id']).\ values('name', 'department_id', 'beiKaoHe') # 姓名, 单位名 # 将学院干部结果表的内容添加到干部结果汇总表中 Gbresult.objects.create( name=each_obj[0]['name'], IDcard=xy_obj[i]['beiceping_id'], department=each_obj[0]['department_id'], category='学院', beiKaoHe=each_obj[0]['beiKaoHe'], year=year, index1=xy_obj[i]['index1'], index2=xy_obj[i]['index2'], index3=xy_obj[i]['index3'], index4=xy_obj[i]['index4'], index5=xy_obj[i]['index5'], index6=xy_obj[i]['index6'], index7=xy_obj[i]['index7'], index8=xy_obj[i]['index8'], index9=xy_obj[i]['index9'], index10=xy_obj[i]['index10'], index11=xy_obj[i]['index11'], index12=xy_obj[i]['index12'], index13=xy_obj[i]['index13'], index14=xy_obj[i]['index14'], index15=xy_obj[i]['index15'], index16=xy_obj[i]['index16'], index17=xy_obj[i]['index17'], index18=xy_obj[i]['index18'], index19=xy_obj[i]['index19'], index20=xy_obj[i]['index20'], index21=xy_obj[i]['index21'], index22=xy_obj[i]['index22'], Excellent=0, score=xy_obj[i]['score']) # 处理直属单位干部结果表 for i in range(0, len(zsdw_obj)): each_obj = Beiceping.objects.filter(IDcard=zsdw_obj[i]['beiceping_id']).\ values('name', 'department_id', 'beiKaoHe') # 姓名, 单位名 # 将直属单位干部结果表内容添加到干部结果汇总表中 Gbresult.objects.create( name=each_obj[0]['name'], IDcard=zsdw_obj[i]['beiceping_id'], department=each_obj[0]['department_id'], category='直属单位', beiKaoHe=each_obj[0]['beiKaoHe'], year=year, index1=zsdw_obj[i]['index1'], index2=zsdw_obj[i]['index2'], index3=zsdw_obj[i]['index3'], index4=zsdw_obj[i]['index4'], index5=zsdw_obj[i]['index5'], index6=zsdw_obj[i]['index6'], index7=zsdw_obj[i]['index7'], index8=zsdw_obj[i]['index8'], index9=zsdw_obj[i]['index9'], index10=zsdw_obj[i]['index10'], index11=zsdw_obj[i]['index11'], index12=zsdw_obj[i]['index12'], index13=zsdw_obj[i]['index13'], index14=zsdw_obj[i]['index14'], index15=zsdw_obj[i]['index15'], index16=zsdw_obj[i]['index16'], index17=zsdw_obj[i]['index17'], index18=zsdw_obj[i]['index18'], index19=zsdw_obj[i]['index19'], index20=zsdw_obj[i]['index20'], index21=zsdw_obj[i]['index21'], index22=zsdw_obj[i]['index22'], index23=zsdw_obj[i]['index23'], Excellent=0, score=zsdw_obj[i]['score']) # 综合排名 gb_obj = Gbresult.objects.values( 'IDcard', 'score').order_by('score').reverse() for i in range(0, len(gb_obj)): Gbresult.objects.filter(IDcard=gb_obj[i]['IDcard']).update( rankingOfAll=i + 1) # 学院排名 xy_obj = Gbresult.objects.filter(category='学院').values( 'IDcard', 'score').order_by('score').reverse() for i in range(0, len(xy_obj)): Gbresult.objects.filter(IDcard=xy_obj[i]['IDcard']).update( rankingofCategory=i + 1) # 机关排名 jg_obj = Gbresult.objects.filter(category='机关').values( 'IDcard', 'score').order_by('score').reverse() for i in range(0, len(jg_obj)): Gbresult.objects.filter(IDcard=jg_obj[i]['IDcard']).update( rankingofCategory=i + 1) # 直属单位排名 zsdw_obj = Gbresult.objects.filter(category='直属单位').values( 'IDcard', 'score').order_by('score').reverse() for i in range(0, len(zsdw_obj)): Gbresult.objects.filter( IDcard=zsdw_obj[i]['IDcard']).update( rankingofCategory=i + 1) # 单位排名 category = [] category_obj = Gbresult.objects.values_list( 'department').distinct() for i in category_obj: category.append(i[0]) for i in category: dep_obj = Gbresult.objects.filter(department=i).values( 'IDcard', 'score').order_by('score').reverse() for j in range(0, len(dep_obj)): Gbresult.objects.filter( IDcard=dep_obj[j]['IDcard']).update( rankingOfDep=j + 1) # 前百分之十,后百分之十 total = len(gb_obj) # 总人数 top_ten = int(total * 0.1) # 前百分之十的最后一位 last_ten = total - int(total * 0.1) + 1 # 后百分之十的第一位 top_ten_score = Gbresult.objects.filter( rankingOfAll=top_ten).values('score') # 前百分之十最后一名的分数 last_ten_score = Gbresult.objects.filter( rankingOfAll=last_ten).values('score') # 后百分之十第一名的分数 for i in range(0, len(gb_obj)): if gb_obj[i]['score'] >= top_ten_score[0]['score']: Gbresult.objects.filter( IDcard=gb_obj[i]['IDcard']).update(TopTen=True) if gb_obj[i]['score'] <= last_ten_score[0]['score']: Gbresult.objects.filter( IDcard=gb_obj[i]['IDcard']).update(LastTen=True) ret.code = 205 ret.data = '数据统计成功' except Exception as e: ret.code = 206 ret.error = '数据统计失败' return Response(ret.dict) return Response(ret.dict)
def get(self, request): """ 班子考核汇总表 将两张班子考核结果表的数据汇总在一起,并计算排名 """ ret = BaseResponse() year = datetime.datetime.now().year # 当前年份 xy_obj = xybzResult.objects.values() # 学院班子结果表 zsdw_obj = zsdwbzResult.objects.values() # 直属单位班子结果表 try: with transaction.atomic(): # 处理学院班子结果表 for i in range(0, len(xy_obj)): each_obj = BzTjInfo.objects.filter( number_id=xy_obj[i]['beiceping_id']).values( 'department_id') # 单位名 # 将学院班子结果表的内容添加到班子结果汇总表中 Bzresult.objects.create( beiceping=each_obj[0]['department_id'], year=year, index1=xy_obj[i]['index1'], index2=xy_obj[i]['index2'], index3=xy_obj[i]['index3'], index4=xy_obj[i]['index4'], index5=xy_obj[i]['index5'], index6=xy_obj[i]['index6'], index7=xy_obj[i]['index7'], index8=xy_obj[i]['index8'], index9=xy_obj[i]['index9'], index10=xy_obj[i]['index10'], index11=xy_obj[i]['index11'], index12=xy_obj[i]['index12'], score=xy_obj[i]['score']) # 处理直属单位班子结果表 for i in range(0, len(zsdw_obj)): each_obj = BzTjInfo.objects.filter( number_id=zsdw_obj[i]['beiceping_id']).values( 'department_id') # 单位名 # 将直属单位班子结果表内容添加到班子结果汇总表中 Bzresult.objects.create( beiceping=each_obj[0]['department_id'], year=year, index1=zsdw_obj[i]['index1'], index2=zsdw_obj[i]['index2'], index3=zsdw_obj[i]['index3'], index4=zsdw_obj[i]['index4'], index5=zsdw_obj[i]['index5'], index6=zsdw_obj[i]['index6'], index7=zsdw_obj[i]['index7'], index8=zsdw_obj[i]['index8'], index9=zsdw_obj[i]['index9'], index10=zsdw_obj[i]['index10'], index11=zsdw_obj[i]['index11'], index12=zsdw_obj[i]['index12'], index19=zsdw_obj[i]['index19'], index20=zsdw_obj[i]['index20'], index21=zsdw_obj[i]['index21'], score=zsdw_obj[i]['score']) # 综合排名 bz_obj = Bzresult.objects.values( 'beiceping', 'score').order_by('score').reverse() for i in range(0, len(bz_obj)): Bzresult.objects.filter( beiceping=bz_obj[i]['beiceping']).update( rankingOfAll=i + 1) # 前百分之十,后百分之十 total = len(bz_obj) # 总人数 top_ten = int(total * 0.1) # 前百分之十的最后一位 last_ten = total - int(total * 0.1) + 1 # 后百分之十的第一位 top_ten_score = Bzresult.objects.filter( rankingOfAll=top_ten).values('score') # 前百分之十最后一名的分数 last_ten_score = Bzresult.objects.filter( rankingOfAll=last_ten).values('score') # 后百分之十第一名的分数 for i in range(0, len(bz_obj)): if bz_obj[i]['score'] >= top_ten_score[0]['score']: Bzresult.objects.filter( beiceping=bz_obj[i]['beiceping']).update( TopTen=True) if bz_obj[i]['score'] <= last_ten_score[0]['score']: Bzresult.objects.filter( beiceping=bz_obj[i]['beiceping']).update( LastTen=True) except Exception as e: ret.code = 206 ret.error = '数据统计失败' return Response(ret.dict) ret.code = 205 ret.data = '数据统计成功' return Response(ret.dict)
def post(self, request): """ 结果表查询 干部条件搜索 :param request: :return: """ ret = BaseResponse() condition = {} # 存储filter条件 id_lst = [] # 存储人员ID data = request.data try: category = data['category'] # 分类 department = data['department'] # 单位 zhiwu = data['zhiwu'] # 职务 level = data['level'] # 职级 name = data['name'] # 姓名 index = data['index'] # 指标 if department: dep_num = Department.objects.filter( department=department).values_list('number', flat=True)[0] # 单位名 condition['depNum_id'] = dep_num # 单位名 if zhiwu: condition['zhiwu'] = zhiwu # 职务 if level: condition['level'] = level # 级别关系 if name: beiceping_id = Beiceping.objects.filter( name__contains=name).values_list('IDcard') for i in range(0, len(beiceping_id)): id_lst.append(beiceping_id[i][0]) condition['beiceping_id__in'] = id_lst # 人员列表 if category == 'xueyuan': lst = [ 'beiceping_id__name', 'level', 'depNum_id__department', 'zhiwu', 'count', 'score', 'index19', 'index20', 'index21', 'index22' ] for i in index: lst.append(i) result = xygbResult.objects.filter(**condition).values(*lst) if len(condition) == 0: lst = [ 'beiceping_id__name', 'level', 'depNum_id__department', 'zhiwu', 'count', 'score', 'index1', 'index2', 'index3', 'index4', 'index5', 'index6', 'index7', 'index8', 'index9', 'index10', 'index11', 'index12', 'index13', 'index14', 'index15', 'index16', 'index17', 'index18', 'index19', 'index20', 'index21', 'index22' ] result = xygbResult.objects.values(*lst) elif category == 'jiguan': lst = [ 'beiceping_id__name', 'level', 'depNum_id__department', 'zhiwu', 'count', 'score', 'index19', 'index20', 'index21', 'index22', 'index23' ] for i in index: lst.append(i) result = jggbResult.objects.filter(**condition).values(*lst) if len(condition) == 0: lst = [ 'beiceping_id__name', 'level', 'depNum_id__department', 'zhiwu', 'count', 'score', 'index1', 'index2', 'index3', 'index4', 'index5', 'index6', 'index7', 'index8', 'index9', 'index10', 'index11', 'index12', 'index13', 'index14', 'index15', 'index16', 'index17', 'index18', 'index19', 'index20', 'index21', 'index22', 'index23' ] result = jggbResult.objects.values(*lst) elif category == 'zhishudanwei': lst = [ 'beiceping_id__name', 'level', 'depNum_id__department', 'zhiwu', 'count', 'score', 'index19', 'index20', 'index21', 'index22', 'index23' ] for i in index: lst.append(i) result = zsdwgbResult.objects.filter(**condition).values(*lst) if len(condition) == 0: lst = [ 'beiceping_id__name', 'level', 'depNum_id__department', 'zhiwu', 'count', 'score', 'index1', 'index2', 'index3', 'index4', 'index5', 'index6', 'index7', 'index8', 'index9', 'index10', 'index11', 'index12', 'index13', 'index14', 'index15', 'index16', 'index17', 'index18', 'index19', 'index20', 'index21', 'index22', 'index23' ] result = zsdwgbResult.objects.values(*lst) ret.info = result # if len(condition) == 0: # ret.code = 207 # ret.data = '请输入查询条件' # return Response(ret.dict) ret.code = 201 ret.data = '数据加载成功' except Exception as e: ret.code = 202 ret.error = '数据加载失败' return Response(ret.dict) return Response(ret.dict)
def post(self, request): """ 结果表查询 班子条件搜索 :param request: :return: """ ret = BaseResponse() data = request.data try: category = data['category'] # 分类 department = data['department'] # 单位 index = data['index'] # 指标 # if not department: # ret.code = 207 # ret.data = '请输入查询条件' # return Response(ret.dict) if department: dep_num = Department.objects.filter( department=department).values_list('number', flat=True)[0] if category == 'xueyuan': if not department: lst = [ 'beiceping_id__department', 'count', 'score', 'index1', 'index2', 'index3', 'index4', 'index5', 'index6', 'index7', 'index8', 'index9', 'index10', 'index11', 'index12' ] result = xybzResult.objects.values(*lst) else: lst = ['beiceping_id__department', 'count', 'score'] for i in index: lst.append(i) result = xybzResult.objects.filter( beiceping_id=dep_num).values(*lst) elif category == 'zhishudanwei': if not department: lst = [ 'beiceping_id__department', 'count', 'score', 'index1', 'index2', 'index3', 'index4', 'index5', 'index6', 'index7', 'index8', 'index9', 'index10', 'index11', 'index12', 'index19', 'index20', 'index21' ] result = zsdwbzResult.objects.values(*lst) else: lst = [ 'beiceping_id__department', 'count', 'score', 'index19', 'index20', 'index21' ] for i in index: lst.append(i) result = zsdwbzResult.objects.filter( beiceping_id=dep_num).values(*lst) ret.info = result ret.code = 201 ret.data = '数据加载成功' except Exception as e: print(e) ret.code = 202 ret.error = '数据加载失败' return Response(ret.dict) return Response(ret.dict)
def post(self, request): res = BaseResponse() # 1 获取前端的数据以及user_id course_list = request.data.get("course_list", "") user_id = request.user.pk # 2 校验数据的合法性 for course_id in course_list: # 2.1 判断course_id 是否在购物车中 shopping_car_key = SHOPPINGCAR_KEY % (user_id, course_id) if not CONN.exists(shopping_car_key): res.code = 1050 res.error = "课程ID不合法" return Response(res.dict) # 3 构建数据结构 # 3.1 获取用户的所有合法优惠券 user_all_coupons = CouponRecord.objects.filter( account_id=user_id, status=0, coupon__valid_begin_date__lte=now(), coupon__valid_end_date__gte=now(), ).all() print(user_all_coupons) # 3.2 构建优惠券dict course_coupon_dict = {} global_coupon_dict = {} for coupon_record in user_all_coupons: coupon = coupon_record.coupon if coupon.object_id == course_id: course_coupon_dict[coupon.id] = { "id": coupon.id, "name": coupon.name, "coupon_type": coupon.get_coupon_type_display(), "object_id": coupon.object_id, "money_equivalent_value": coupon.money_equivalent_value, "off_percent": coupon.off_percent, "minimum_consume": coupon.minimum_consume } elif coupon.object_id == "": global_coupon_dict[coupon.id] = { "id": coupon.id, "name": coupon.name, "coupon_type": coupon.get_coupon_type_display(), "money_equivalent_value": coupon.money_equivalent_value, "off_percent": coupon.off_percent, "minimum_consume": coupon.minimum_consume } # 3.3 构建写入redis的数据结构 course_info = CONN.hgetall(shopping_car_key) price_policy_dict = json.loads(course_info["price_policy_dict"]) default_policy_id = course_info["default_price_policy_id"] valid_period = price_policy_dict[default_policy_id][ "valid_period_display"] price = price_policy_dict[default_policy_id]["price"] settlement_info = { "id": course_info["id"], "title": course_info["title"], "course_img": course_info["course_img"], "valid_period": valid_period, "price": price, "course_coupon_dict": json.dumps(course_coupon_dict, ensure_ascii=False) } # 4 写入redis settlement_key = SETTLEMENT_KEY % (user_id, course_id) global_coupon_key = GLOBAL_COUPON_KEY % user_id CONN.hmset(settlement_key, settlement_info) if global_coupon_dict: CONN.hmset(global_coupon_key, global_coupon_dict) # 5 删除购物车中的数据 CONN.delete(shopping_car_key) res.data = "加入结算中心成功" return Response(res.dict)
def get(self, request): """ 导出带有折线图报表excel :param request: :return: """ ret = BaseResponse() try: year = datetime.datetime.now().year # 当前年份 department = Gbresult.objects.filter( category='学院').values('department').distinct() # 学院单位 random_file = get_random_str() # 创建随机文件名 os.mkdir( get_desktop_path() + '\\{}年度反馈给各学院的结果报表{}'.format(year, random_file)) # 先创建一个文件夹 xy_index = list( xygbIndexWeight.objects.filter( code__isnull=False, weight__isnull=False).values_list('index')) score_index = [] for each in xy_index: score_index.append(each[0]) columnname = ['单位', '姓名', '最终分数'] value = ['department', 'name', 'score'] for i in range(0, len(xy_index)): columnname.insert(i + 2, xy_index[i][0]) for i in range(0, len(xy_index)): value.insert(i + 2, 'index{}'.format(i + 1)) # 需要转换成数值型的列 value1 = [] for each_value in value: if 'index' in each_value: value1.append(each_value) value1.append('score') for each_department in department: max_score = 0 # 最小值 min_score = 100 # 最大值 data = Gbresult.objects.filter(department=each_department['department']). \ values(*value).order_by('rankingOfDep') # 转换为数值型 for each_data in data: for each in value1: each_data[each] = float(each_data[each]) if each_data[each] > max_score: max_score = each_data[each] if each_data[each] < min_score: min_score = each_data[each] filename = '{}年{}单位中层干部民主测评结果.xlsx'.format( year, each_department['department']) # 处理导出路径 # path = get_desktop_path() + r'\反馈给各学院的结果报表{}\{}'.format(random_file, filename) path = get_desktop_path() + '\\{}年度反馈给各学院的结果报表{}\\{}'.format( year, random_file, filename) data = pandas.DataFrame(data) # data.to_excel(path, na_rep='', header=columnname, index=False) sheet_name = 'Sheet1' writer = pandas.ExcelWriter(path, engine='xlsxwriter') # data = list(data)[0] data.to_excel(writer, sheet_name=sheet_name, na_rep='', header=columnname, index=False) # 导出折线图 max_row = len(data) workbook = writer.book worksheet = writer.sheets[sheet_name] chart = workbook.add_chart({'type': 'line'}) # Configure the series of the chart from the dataframe data. for i in range(0, max_row): row = i + 1 chart.add_series({ 'name': ['Sheet1', row, 1], 'categories': ['Sheet1', 0, 2, 0, 14], 'values': ['Sheet1', row, 2, row, 14], # 'name': ['Sheet1', 0, col], # 'categories': ['Sheet1', 1, 0, max_row, 0], # 'values': ['Sheet1', 1, col, max_row, col], }) # Configure the chart axes. chart.set_x_axis({ 'name': '{}年{}单位中层干部民主测评结果'.format(year, each_department['department']) }) chart.set_y_axis({ 'name': '得分', 'major_gridlines': { 'visible': False }, 'min': min_score, 'max': max_score }) # Insert the chart into the worksheet. worksheet.insert_chart('G2', chart) # Close the Pandas Excel writer and output the Excel file. writer.save() except Exception as e: ret.code = 0 ret.error = '导出失败' return Response(ret.dict) ret.code = 1 ret.data = '导出成功' return Response(ret.dict)
def post(self, request): """ 导出报表excel :param request: :return: """ ret = BaseResponse() try: # 接收数据 data = request.data year = data['year'] flag = data['flag'] category = data['category'] # 判断条件是否为空 if not year: ret.code = 1 ret.data = '请选择年份' return Response(ret.dict) if not category: ret.code = 2 ret.data = '请选择单位类别' return Response(ret.dict) # 处理文件名 if flag: filename = '{}年度{}中层干部考核结果排名({}).xlsx'.format( year, category, flag) else: filename = '{}年度{}全体中层干部考核结果排名.xlsx'.format( year, category, flag) # 处理导出路径 path = get_desktop_path() + r'\{}'.format(filename) # 处理表头和数据 if category == '机关': jg_index = list( jggbIndexWeight.objects.filter( code__isnull=False, weight__isnull=False).values_list('index')) if flag: columnname = ['单位', '姓名', '最终分数', '全校综合排名', '机关分类排名'] value = ['department', 'name', 'score', 'rankingOfAll'] else: columnname = ['单位', '姓名', '最终分数', '全校综合排名', '机关综合排名'] value = [ 'department', 'name', 'score', 'rankingOfAll', 'rankingofCategory' ] # 最终表头 for i in range(0, len(jg_index)): columnname.insert(i + 2, jg_index[i][0]) # 查找的参数 for i in range(0, len(jg_index)): value.insert(i + 2, 'index{}'.format(i + 1)) elif category == '学院': xy_index = list( xygbIndexWeight.objects.filter( code__isnull=False, weight__isnull=False).values_list('index')) if flag: columnname = ['单位', '姓名', '最终分数', '学院综合排名', '学院分类排名'] value = [ 'department', 'name', 'score', 'rankingofCategory' ] else: columnname = ['单位', '姓名', '最终分数', '全校综合排名', '学院综合排名'] value = [ 'department', 'name', 'score', 'rankingOfAll', 'rankingofCategory' ] for i in range(0, len(xy_index)): columnname.insert(i + 2, xy_index[i][0]) for i in range(0, len(xy_index)): value.insert(i + 2, 'index{}'.format(i + 1)) elif category == '直属单位': zsdw_index = list( zsdwgbIndexWeight.objects.filter( code__isnull=False, weight__isnull=False).values_list('index')) if flag: columnname = ['单位', '姓名', '最终分数', '直属单位综合排名', '直属单位分类排名'] value = [ 'department', 'name', 'score', 'rankingofCategory' ] else: columnname = ['单位', '姓名', '最终分数', '全校综合排名', '直属单位综合排名'] value = [ 'department', 'name', 'score', 'rankingOfAll', 'rankingofCategory' ] for i in range(0, len(zsdw_index)): columnname.insert(i + 2, zsdw_index[i][0]) for i in range(0, len(zsdw_index)): value.insert(i + 2, 'index{}'.format(i + 1)) if flag: data = Gbresult.objects.filter(category=category, beiKaoHe__contains=flag). \ values(*value).order_by('rankingOfAll') for i in range(0, len(data)): data[i]['rank'] = i + 1 else: data = Gbresult.objects.filter(category=category). \ values(*value).order_by('rankingofCategory') data = pandas.DataFrame(data) data.to_excel(path, na_rep='', header=columnname, index=False) except Exception as e: ret.code = 0 ret.error = '导出失败' return Response(ret.dict) ret.code = 3 ret.data = '导出成功' return Response(ret.dict)
def post(self, request): res = BaseResponse() # 1 ,获取前端的数据以及user_id course_list = request.data.get('course_list', "") user_id = request.user.pk # 2,校验数据的合法性 for course_id in course_list: # 2.1 判断 course_id 是否在购物车里 shopping_car_key = SHOPPINGCAR_KEY % (user_id, course_id) if not CONN.exists(shopping_car_key): res.code = 1050 res.error = '课程ID不合法' return Response(res.dict) # 3,构建数据结构 # 3.1 获取用户的所有合法优惠券 user_all_coupons = CouponRecord.objects.filter( account_id=user_id, status=0, coupon__valid_begin_date__lte=now(), coupon__valid_end_date__gte=now(), ).all() # 3.2 构建优惠券 dict course_coupon_dict = {} global_coupon_dict = {} for coupon_record in user_all_coupons: coupon = coupon_record.coupon if coupon.object_id == course_id: course_coupon_dict[coupon.id] = { 'id': coupon.id, 'name': coupon.name, 'coupon_type': coupon.get_coupon_type_display()(), 'object_id': coupon.object_id, 'money_equivalent_value': coupon.money_equivalent_value, 'off_percent': coupon.off_percent, 'minunum_consume': coupon.minimum_consume } elif coupon.object_id == '': global_coupon_dict[coupon.id] = { 'id': coupon.id, 'name': coupon.name, 'coupon_type': coupon.get_coupon_type_display()(), 'money_equivalent_value': coupon.money_equivalent_value, 'off_percent': coupon.off_percent, 'minunum_consume': coupon.minimum_consume } # 3.3 构建写入redis的数据结构 course_info = CONN.hgetall(shopping_car_key) price_policy_dict = json.loads(course_info['price_policy_dict']) default_policy_id = course_info['default_price_policy_id'] valid_period = price_policy_dict[default_policy_id][ 'valid_period_display'] price = price_policy_dict[default_policy_id]['price'] settlement_info = { 'id': course_info['id'], 'title': course_info['title'], 'course_img': course_info['course_img'], 'valid_period': valid_period, 'price': price, 'course_coupon_dict': json.dumps(course_coupon_dict, ensure_ascii=False) } # 4,写入redis中 settlement_key = SETTLEMENT_KEY % (user_id, course_id) global_coupon_key = GLOBAL_COUPON_KEY % user_id CONN.hmset(settlement_key, settlement_info) if global_coupon_dict: CONN.hmset(global_coupon_key, global_coupon_dict) # 5,删除购物车中的数据 CONN.delete(shopping_car_key) res.data = '加入结算中心成功' return Response(res.dict)
def get(self,request): r = BaseResponse() r.data={"id":1,"title":"title"} return Response(r.dic)