Skip to content

Commit

Permalink
linting
Browse files Browse the repository at this point in the history
  • Loading branch information
tschaume committed Apr 10, 2024
1 parent 71d34b2 commit 2d7e65f
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 26 deletions.
28 changes: 16 additions & 12 deletions mpcontribs-api/mpcontribs/api/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,7 @@ def get_filter_params(name, filters):


def get_specs(klass, method, collection):
method_name = (
method.__name__ if hasattr(method, "__name__") else method
)
method_name = method.__name__ if hasattr(method, "__name__") else method
default_response = {
"description": "Error",
"schema": {"type": "object", "properties": {"error": {"type": "string"}}},
Expand Down Expand Up @@ -155,12 +153,14 @@ def get_specs(klass, method, collection):
o.pattern if isinstance(o, Pattern) else o
for o in klass.resource.allowed_ordering
]
order_params = [{
"name": "_sort",
"in": "query",
"type": "string",
"description": f"sort {collection} via {allowed_ordering}. Prepend +/- for asc/desc.",
}]
order_params = [
{
"name": "_sort",
"in": "query",
"type": "string",
"description": f"sort {collection} via {allowed_ordering}. Prepend +/- for asc/desc.",
}
]

spec = None
if method_name == "Fetch":
Expand Down Expand Up @@ -471,8 +471,9 @@ def is_anonymous(self, request):
return is_anonymous

def is_external(self, request):
return request.headers.get("X-Forwarded-Host") is not None and \
not request.headers.get("Origin")
return request.headers.get(
"X-Forwarded-Host"
) is not None and not request.headers.get("Origin")

def is_admin(self, groups):
admin_group = os.environ.get("ADMIN_GROUP", "admin")
Expand Down Expand Up @@ -583,12 +584,15 @@ def has_read_permission(self, request, qs):
if q and "project" in q and "$in" in q["project"]:
names = q.pop("project").pop("$in")

qfilter = self.get_projects_filter(username, groups, filter_names=names)
qfilter = self.get_projects_filter(
username, groups, filter_names=names
)
return qs.filter(qfilter)
else:
# get component Object IDs for queryset
pk = request.view_args.get("pk")
from mpcontribs.api.contributions.document import get_resource

resource = get_resource(component)
qfilter = lambda qs: qs.clone()

Expand Down
39 changes: 25 additions & 14 deletions mpcontribs-portal/mpcontribs/portal/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,11 @@
BUCKET = os.environ.get("S3_DOWNLOADS_BUCKET", "mpcontribs-downloads")
COMPONENTS = {"structures", "tables", "attachments"}
j2h = Json2Html()
s3_client = boto3.client('s3')
lambda_client = boto3.client('lambda')
redis_store = Redis.from_url("redis://" + os.environ["REDIS_ADDRESS"], decode_responses=True)
s3_client = boto3.client("s3")
lambda_client = boto3.client("lambda")
redis_store = Redis.from_url(
"redis://" + os.environ["REDIS_ADDRESS"], decode_responses=True
)


def visit(path, key, value):
Expand All @@ -46,8 +48,10 @@ def visit(path, key, value):

def get_consumer(request):
names = [
"X-Authenticated-Groups", "X-Consumer-Groups",
"X-Consumer-Username", "X-Anonymous-Consumer"
"X-Authenticated-Groups",
"X-Consumer-Groups",
"X-Consumer-Username",
"X-Anonymous-Consumer",
]
headers = {}
for name in names:
Expand Down Expand Up @@ -80,7 +84,7 @@ def get_context(request):

new_parts = ["localhost"] if is_localhost else []
new_parts.append(api_subdomain)
new_parts += parts[subdomain_index+1:]
new_parts += parts[subdomain_index + 1 :]
ctx["API_CNAME"] = ".".join(new_parts)

scheme = "http" if is_localhost else "https"
Expand Down Expand Up @@ -236,7 +240,9 @@ def show_component(request, oid):
try:
resp = client.get_attachment(oid)
except HTTPNotFound:
return HttpResponse(f"Component with ObjectId {oid} not found.", status=404)
return HttpResponse(
f"Component with ObjectId {oid} not found.", status=404
)

if resp is not None:
return HttpResponse(resp.info().display())
Expand All @@ -250,7 +256,9 @@ def download_component(request, oid):
client = Client(**ckwargs)

try:
resp = client.structures.getStructureById(pk=oid, _fields=["name", "cif"]).result()
resp = client.structures.getStructureById(
pk=oid, _fields=["name", "cif"]
).result()
name = resp["name"]
content = gzip.compress(bytes(resp["cif"], "utf-8"))
content_type = "application/gzip"
Expand All @@ -271,7 +279,9 @@ def download_component(request, oid):
content_type = resp["mime"]
filename = f"{oid}_{name}"
except HTTPNotFound:
return HttpResponse(f"Component with ObjectId {oid} not found.", status=404)
return HttpResponse(
f"Component with ObjectId {oid} not found.", status=404
)

if content:
response = HttpResponse(content, content_type=content_type)
Expand Down Expand Up @@ -327,7 +337,7 @@ def _get_filename(query, include):
def _get_download(key, content_type="application/zip"):
try:
retr = s3_client.get_object(Bucket=BUCKET, Key=key)
resp = retr['Body'].read()
resp = retr["Body"].read()
except ClientError:
return HttpResponse(f"Download {key} not available", status=404)

Expand Down Expand Up @@ -429,7 +439,8 @@ def make_download(headers, query, include=None):
return JsonResponse({"error": "No results for query."})

kwargs = {
k: v for k, v in query.items()
k: v
for k, v in query.items()
if k not in {"format", "_sort", "_fields", "_limit", "per_page"}
}
last_modified = client.contributions.queryContributions(
Expand Down Expand Up @@ -459,13 +470,13 @@ def make_download(headers, query, include=None):
"host": os.environ["MPCONTRIBS_CLIENT_HOST"],
"headers": headers,
"query": query,
"include": include
"include": include,
}
try:
response = lambda_client.invoke(
FunctionName="mpcontribs-make-download",
InvocationType='Event',
Payload=json.dumps(payload)
InvocationType="Event",
Payload=json.dumps(payload),
)
if response["StatusCode"] == 202:
status = "SUBMITTED"
Expand Down

0 comments on commit 2d7e65f

Please sign in to comment.