Exemple #1
0
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) + "/"
        })
Exemple #2
0
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
    })
Exemple #3
0
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) + "/"
        })