Beispiel #1
0
def combreport(request, comb_id):
    user = request.user

    # check permission
    comb_report_list = user.get_all_report_combination_permissions()
    if comb_id not in [str(item.id) for item in comb_report_list]:
        return HttpResponseRedirect(reverse("SGRS_index"))

    comb_report_obj = ReportPermissionCombination.objects.get(id=comb_id)
    report_perm_list = comb_report_obj.report_permissions.all().order_by("id")
    group_filter_conf = {}
    for item in report_perm_list:
        tmp_filter_conf = json.loads(item.filter_conf)
        if len(tmp_filter_conf) > len(group_filter_conf):
            group_filter_conf = tmp_filter_conf

    form = parse_dynamic_form(group_filter_conf, data=request.POST.copy() if request.method == "POST" else None)

    comb_report_result = []
    if form.is_valid():
        for report_perm_obj in report_perm_list:
            filter_conf = json.loads(report_perm_obj.filter_conf)
            db_conf = report_perm_obj.db_conf
            sql_conf = report_perm_obj.SQL_conf

            sql = ""
            query_data = []
            header_data = []
            error_info = ""

            try:
                query_data, header_data, sql = parse_sql(user, filter_conf, db_conf, sql_conf, form=form, preview=False)
            except:
                error_info = u"Error, please contact the administrator."

            edit_permission_url = (
                request.build_absolute_uri("/admin/GeneralReport/reportpermission/%s" % report_perm_obj.id)
                if user.is_staff
                else ""
            )

            tmp_perm_result = ObjectDict(
                per_name=report_perm_obj.name,
                per_desc=report_perm_obj.description,
                sql=sql,
                query_data=query_data,
                header_data=header_data,
                error_info=error_info,
                edit_permission_url=edit_permission_url,
            )
            comb_report_result.append(tmp_perm_result)

    data = {"combreport_name": comb_report_obj.name, "filter_form": form, "comb_report_result": comb_report_result}
    return render(request, "GeneralReport/combreport.html", data)
Beispiel #2
0
def report(request, report_key):
    # check permission
    user = request.user
    if not user.has_report_perm(report_key):
        return HttpResponseRedirect(reverse("SGRS_index"))

    # load permission config
    report_perm = ReportPermission.objects.get(name=report_key)
    filter_conf = json.loads(report_perm.filter_conf)
    db_conf = report_perm.db_conf
    sql_conf = report_perm.SQL_conf

    sql = ""
    query_data = []
    query_data_total = 0
    header_data = []
    download_file_url = ""
    form = parse_dynamic_form(filter_conf)
    page_num = request.GET.get("page_num", None)
    page_size = request.GET.get("page_size", None)
    paging_urls = []

    user_role_list = user.get_all_roles()
    perm_role_list = report_perm.sgrsrole_set.all()
    can_download = any([i.can_download for i in user_role_list if i in perm_role_list])

    if request.method == "POST":
        query_dict = request.POST.copy()
        if "csrfmiddlewaretoken" in query_dict:
            del query_dict["csrfmiddlewaretoken"]
        if "use_paging" in query_dict:
            del query_dict["use_paging"]
        if "preview" in query_dict:
            del query_dict["preview"]
        form = parse_dynamic_form(filter_conf, data=request.POST.copy())

    elif page_num is not None and page_size is not None:
        query_dict = request.GET.copy()
        form = parse_dynamic_form(filter_conf, data=request.GET.copy())

    if form.is_valid():
        # query from cache, cache_key be made up of from field
        # (except use_paging,page_size and perview)
        cache_key_dict = form.cleaned_data.copy()
        if "use_paging" in cache_key_dict:
            del cache_key_dict["use_paging"]
        if "page_size" in cache_key_dict:
            del cache_key_dict["page_size"]
        if "preview" in cache_key_dict:
            del cache_key_dict["preview"]
        cache_key = "%s:%s:%s" % (
            user.username,
            report_key,
            "-".join(["%s=%s" % (k, v) for k, v in cache_key_dict.items()]),
        )
        cache_result = cache.get(cache_key)
        result = json.loads(cache_result) if cache_result else {}

        preview = form.cleaned_data["preview"]
        if result:
            sql = result.get("sql", "")
            if not preview:
                query_data, header_data = result["q_data"], result["h_data"]
        else:
            # if there is not cache, set a placeholder
            if not cache_result:
                cache.set(cache_key, json.dumps({}), 60 * 10)

            # query from database, and write into cache
            query_data, header_data, sql = parse_sql(user, filter_conf, db_conf, sql_conf, form=form, preview=preview)

            if not preview:
                result = {"q_data": query_data, "h_data": header_data, "sql": sql}
                if not cache_result:
                    cache.set(cache_key, json.dumps(result), 60 * 10)

        # create download file
        if can_download:
            download_file_name = create_download_file(user, report_key, header_data, query_data)

        query_data_total = len(query_data)

        # paging
        use_paging = form.cleaned_data["use_paging"]
        browser_line_limit = 50000
        page_size = int(form.cleaned_data["page_size"])
        if query_data_total >= browser_line_limit:
            use_paging = True
            page_size = 1000 if page_size < 1000 else page_size
        if use_paging or page_num and page_size:
            page_num = int(page_num or 1)
            max_page_num = len(query_data) / page_size
            if max_page_num * page_size < len(query_data):
                max_page_num += 1
            # create paging url
            for tmp_page_num in range(1, max_page_num + 1):
                query_dict["page_num"] = tmp_page_num
                page_url = request.build_absolute_uri("/sgrs/report/%s?%s" % (report_key, query_dict.urlencode()))
                page_info = ObjectDict(
                    url=page_url,
                    page_num=tmp_page_num,
                    link=0 if tmp_page_num == page_num else 1,  # current url can't click
                )
                paging_urls.append(page_info)

            # split query_data
            query_data = query_data[(page_num - 1) * page_size : page_num * page_size]

    if query_data and can_download:
        download_file_url = request.build_absolute_uri("/sgrs/download/%s" % (download_file_name))

    # edit entry for admin
    edit_permission_url = ""
    if user.is_staff:
        edit_permission_url = request.build_absolute_uri("/admin/GeneralReport/reportpermission/%s" % report_perm.id)

    data = {
        "filter_conf": filter_conf,
        "report_key": report_key,
        "perm_desc": report_perm.description,
        "sql": sql,
        "query_data_total": query_data_total,
        "query_data": query_data,
        "header_data": header_data,
        "filter_form": form,
        "download_file_url": download_file_url,
        "paging_urls": paging_urls,
        "edit_permission_url": edit_permission_url,
        "can_download": can_download,
    }
    return render(request, "GeneralReport/report.html", data)