def get_ranking(request, ranking_id): """ :return: """ ranking = Table.get_table_by_id(int(ranking_id)) if request.method == "POST": print(request.POST) try: fields = request.POST.getlist("field_name_cn") for _field in fields: if not Field.objects.filter(table=ranking, name_cn=_field).count(): Field.objects.create(table=ranking, name_cn=_field) else: messages.info(request, "存在重复字段,已忽略") messages.success(request, "添加字段成功") except Exception as e: logger.error(str(e)) messages.error(request, "添加字段失败") fields = Table.get_fields_by_table_id(int(ranking_id)).order_by("id") urls = copy.deepcopy(SIDEBAR_URL) urls[0]["active"] = True return render(request, "backend/ranking/ranking.html", { "self": request.user, "ranking": ranking, "fields": fields, "urls": urls })
def delete_ranking(request): """ 删除一所 :param request: :return: """ return_dict = {} try: Table.delete_table(int(request.POST["_id"])) return_dict["success"] = "删除成功" except Exception as e: logger.error(str(e)) logger.error(traceback.format_exc()) return_dict["error"] = "删除失败" return JsonResponse(return_dict)
def rankings_content_index(request, batch_id): """ :return: """ batch = BatchOfTable.objects.get(id=int(batch_id)) ranking_fields = Table.get_fields_by_table_id(int( batch.table.id)).order_by("id") model_fields = [] for field in ranking_fields: model_fields.append(field.name_cn) urls = copy.deepcopy(SIDEBAR_URL) urls[1]["active"] = True return render( request, "backend/ranking/ranking_content.html", { "self": request.user, "urls": urls, "fields": model_fields, "title": str(batch.table.name_cn) + "(批次:" + str(batch.batch.text) + ")", "get_all_ranking_url": "/backend/rankings/content/retrieve/" + str(batch_id) + "/" })
def retrieve_rankings(request, ranking_id=""): """ 获取所有信息 :return: json """ if ranking_id == "": batches = BatchOfTable.objects.filter(table_type=1).order_by("id") elif "search_by_college" in request.GET: # 此时的ranking_id为college id relations = RankingAndCollegeRelation.objects.filter( college_id=int(ranking_id)) batches = [] no_repeat = {} for r in relations: if r.ranking.batch.id not in no_repeat: no_repeat[r.ranking.batch.id] = 1 batches.append(r.ranking.batch) else: _ranking = Table.get_table_by_id(int(ranking_id)) batches = BatchOfTable.objects.filter(table=_ranking).order_by("id") page = request.GET.get("page", 1) size = request.GET.get("size", 200) content, num_pages = common.with_paginator(batches, int(page), int(size)) return_dict = format_rankings(content, int(page), int(size)) # 格式化院校信息 return_dict["num_pages"] = num_pages return JsonResponse(return_dict)
def add_ranking(request): """ 添加榜单 :param request: :return: """ if request.method == "POST": try: _table = { "name_cn": request.POST["table_name_cn"], "type": 1 } # 榜单 _fields = request.POST.getlist("field_name_cn") table_id = Table.create_table(_table, _fields) if table_id: return HttpResponseRedirect("/backend/ranking/" + str(table_id) + "/") else: raise Exception("Add Ranking Failed") except Exception as e: logger.error(str(e)) messages.error(request, "添加榜单失败") urls = copy.deepcopy(SIDEBAR_URL) urls[3]["active"] = True return render(request, "backend/ranking/add.html", { "self": request.user, "urls": urls })
def batch_delete_ranking(request): """ 批量删除 :param request: :return: """ return_dict = {} try: delete_list = request.POST.getlist("_ids[]") for i in delete_list: Table.delete_table(int(i)) return_dict["success"] = "删除成功" except Exception as e: logger.error(str(e)) logger.error(traceback.format_exc()) return_dict["error"] = "删除失败" return JsonResponse(return_dict)
def retrieve_ranking(request): """ 获取所有信息 :return: json """ _ranking = Table.get_tables_by_type_id(1) # 1为榜单 page = request.GET.get("page", 1) size = request.GET.get("size", 200) content, num_pages = common.with_paginator(_ranking.order_by("id"), int(page), int(size)) return_dict = format_ranking(content, int(page), int(size)) # 格式化院校信息 return_dict["num_pages"] = num_pages return JsonResponse(return_dict)
def test2(request): return_list = [] try: tables = Table.get_fields_by_table_name(1) for t in tables: temp_dict = { "field_id": t.id, "field_name": t.field_name, "field_name_cn": t.field_name_cn, "field_type": str(t.field_type) } return_list.append(temp_dict) except Exception as e: print(e) finally: return HttpResponse(json.dumps(return_list))
def test1(request): return_list = [] try: tables = Table.get_all_tables() for t in tables: temp_dict = { "table_id": t.id, "table_name": t.table_name, "table_name_cn": t.table_name_cn, "create_time": str(t.create_time), } return_list.append(temp_dict) except Exception as e: print(e) finally: return HttpResponse(json.dumps(return_list))
def test3(request): result = [] tables = {"table_name": "test", "table_name_cn": "啊", "table_type": 1} fields = [{"field_name": "test", "field_name_cn": "啊啊", "field_type": 1}] result = Table.create_table(tables, fields) return HttpResponse(json.dumps(result))
def import_ranking(request, ranking_id): """ :return: """ if request.method == "POST": return_dict = {} try: _file = request.FILES.get("file_upload") # 上传的文件 wb = excel_loader.load_excel(_file) # 读取excel head = excel_loader.get_excel_head(wb) body = excel_loader.get_excel_body_generator(wb) batch = request.POST["extra"] _table = Table.get_table_by_id(int(ranking_id)) # 获取表 _batch = matching_batch(batch, _table) # 匹配批次,如果已存在批次,抛出错误 matching_field(_table, head) # 如果excel中出现未定义的字段,抛出错误 # 看字段中是否存在“学校名称”或者“学校标识码” if "学校标识码" in head: cur = head.index("学校标识码") # 优先记录学校标识码位置 elif "学校名称" in head: cur = head.index("学校名称") # 记录学校名称位置 else: cur = -1 # 分别处理每行数据 length = len(head) for record in body: data = {} for i in range(length): # 生成json数据,“字段”:数据 data[head[i]] = empty_is_empty_str(record[i]) ranking = Ranking.objects.create(batch=_batch, data=data) if cur != -1: # 字段中存在“学校名称”或者“学校标识码”,则建立与对应学校之间的关系 param = record[cur] # “学校名称”或者“学校标识码”对应字段 try: college = College.objects.get( Q(name_cn=param) | Q(id_code=param)) # 找到该学校 RankingAndCollegeRelation.objects.create( college=college, ranking=ranking) # 建立该字段与学校之间的关系 except ObjectDoesNotExist: logger.warning(param + "不存在") return_dict["success"] = "success" except IndexError as e: logger.error(str(e)) return_dict["error"] = "请选择批次" except IntegrityError as e: # 仅处理重复添加错误 logger.error(str(e)) return_dict["error"] = "批次重复" except Exception as e: logger.error(str(e)) logger.error(traceback.format_exc()) return_dict["error"] = "出现错误,请检查文件及内容格式" finally: return JsonResponse(return_dict) year = YearSeasonMonth.objects.filter(type=1) _ranking = Table.get_table_by_id(int(ranking_id)) _fields = Table.get_fields_by_table_id(int(ranking_id)) fields = [str(f.name_cn) for f in _fields] urls = copy.deepcopy(SIDEBAR_URL) # 侧边栏网址 urls[0]["active"] = True return render( request, "backend/ranking/import.html", { "self": request.user, "fields": fields, "ranking": _ranking, "year": year, "urls": urls, "file_upload_url": "/backend/ranking/import/" + str(ranking_id) + "/" })