Changeset View
Changeset View
Standalone View
Standalone View
swh/deposit/api/private/deposit_list.py
Show All 17 Lines | class APIList(ListAPIView, APIPrivateView): | ||||
HTTP verbs supported: GET | HTTP verbs supported: GET | ||||
""" | """ | ||||
serializer_class = DepositSerializer | serializer_class = DepositSerializer | ||||
pagination_class = DefaultPagination | pagination_class = DefaultPagination | ||||
def get_queryset(self): | def get_queryset(self): | ||||
"""Retrieve iterable of deposits (with some optional filtering).""" | |||||
params = self.request.query_params | params = self.request.query_params | ||||
exclude_like = params.get("exclude") | exclude_like = params.get("exclude") | ||||
username = params.get("username") | username = params.get("username") | ||||
if username: | if username: | ||||
deposits = Deposit.objects.select_related("client").filter( | deposits_qs = Deposit.objects.select_related("client").filter( | ||||
client__username=username | client__username=username | ||||
) | ) | ||||
else: | else: | ||||
deposits = Deposit.objects.all() | deposits_qs = Deposit.objects.all() | ||||
if exclude_like: | if exclude_like: | ||||
# sql injection: A priori, nothing to worry about, django does it for | # sql injection: A priori, nothing to worry about, django does it for | ||||
# queryset | # queryset | ||||
# https://docs.djangoproject.com/en/3.0/topics/security/#sql-injection-protection # noqa | # https://docs.djangoproject.com/en/3.0/topics/security/#sql-injection-protection # noqa | ||||
deposits = deposits.exclude(external_id__startswith=exclude_like) | deposits_qs = deposits_qs.exclude(external_id__startswith=exclude_like) | ||||
return deposits.order_by("id") | |||||
deposits = [] | |||||
for deposit in deposits_qs.order_by("id"): | |||||
deposit_requests = deposit.depositrequest_set.filter( | |||||
type="metadata" | |||||
).order_by("-id") | |||||
# enrich deposit with raw metadata when we have some | |||||
if deposit_requests and len(deposit_requests) > 0: | |||||
anlambert: Can we have more than one metadata associated to a deposit ? If yes, should we not return them… | |||||
Done Inline Actionsgood question, a recent change is to actually take the latest one D7211, so it's fine as is. ardumont: good question, a recent change is to actually take the latest one D7211, so it's fine as is. | |||||
raw_meta = deposit_requests[0].raw_metadata | |||||
if raw_meta: | |||||
deposit.set_raw_metadata(raw_meta) | |||||
deposits.append(deposit) | |||||
return deposits |
Can we have more than one metadata associated to a deposit ? If yes, should we not return them all ?