示例#1
0
def show_component(request, oid):
    ckwargs = client_kwargs(request)
    headers = ckwargs.get("headers", {})
    resp = None

    if headers.get("X-Anonymous-Consumer", False):
        ctx = get_context(request)
        msg = f"""
        Please <a href=\"{ctx['OAUTH_URL']}\">log in</a> to show contribution component.
        """.strip()
        return HttpResponse(msg, status=403)

    client = Client(**ckwargs)
    try:
        resp = client.get_structure(oid)
    except HTTPNotFound:
        try:
            resp = client.get_table(oid)
        except HTTPNotFound:
            try:
                resp = client.get_attachment(oid)
            except HTTPNotFound:
                return HttpResponse(
                    f"Component with ObjectId {oid} not found.", status=404)

    if resp is not None:
        return HttpResponse(resp.info().display())

    return HttpResponse(status=404)
示例#2
0
def download_component(request, oid):
    ckwargs = client_kwargs(request)
    headers = ckwargs.get("headers", {})
    content = None

    if headers.get("X-Anonymous-Consumer", False):
        ctx = get_context(request)
        msg = f"""
        Please <a href=\"{ctx['OAUTH_URL']}\">log in</a> to download contribution component.
        """.strip()
        return HttpResponse(msg, status=403)

    client = Client(**ckwargs)
    try:
        resp = client.structures.get_entry(pk=oid, _fields=["name",
                                                            "cif"]).result()
        name = resp["name"]
        content = gzip.compress(bytes(resp["cif"], "utf-8"))
        content_type = "application/gzip"
        filename = f"{oid}_{name}.cif.gz"
    except HTTPNotFound:
        try:
            resp = client.get_table(oid)
            content = gzip.compress(bytes(resp.to_csv(), "utf-8"))
            resp = client.tables.get_entry(pk=oid, _fields=["name"]).result()
            name = resp["name"]
            content_type = "application/gzip"
            filename = f"{oid}_{name}.csv.gz"
        except HTTPNotFound:
            try:
                resp = client.get_attachment(oid)
                name = resp["name"]
                content = resp.decode()
                content_type = resp["mime"]
                filename = f"{oid}_{name}"
            except HTTPNotFound:
                return HttpResponse(
                    f"Component with ObjectId {oid} not found.", status=404)

    if content:
        response = HttpResponse(content, content_type=content_type)
        response["Content-Disposition"] = f"attachment; filename={filename}"
        return response

    return HttpResponse(status=404)