Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Always set bundle + coerce query params #86

Merged
merged 2 commits into from
Dec 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions worf/views/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,10 +204,24 @@ def set_bundle(self, raw_bundle):
self.bundle[field] = raw_bundle[key]
self.keymap[field] = key

def set_bundle_from_request(self, request):
if request.method == "GET":
self.set_bundle_from_query_string(request)
return

self.set_bundle_from_request_body(request)

def set_bundle_from_query_string(self, request):
raw_bundle = self.flatten_bundle(parse_qs(request.META["QUERY_STRING"]))

self.set_bundle(raw_bundle)
strings = dict(true=True, false=False, null=None)

coerced_bundle = {
key: strings.get(str(value).lower(), value)
for key, value in raw_bundle.items()
}

self.set_bundle(coerced_bundle)

def set_bundle_from_request_body(self, request):
raw_bundle = {}
Expand All @@ -233,7 +247,7 @@ def dispatch(self, request, *args, **kwargs):

try:
self._check_permissions() # only returns 200 or HTTP_EXCEPTIONS
self.set_bundle_from_request_body(request)
self.set_bundle_from_request(request)
return handler(request, *args, **kwargs) # calls self.serialize()
except HTTP_EXCEPTIONS as e:
return self.render_to_response(dict(message=e.message), e.status)
Expand Down
2 changes: 0 additions & 2 deletions worf/views/list.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,6 @@ def set_search_lookup_kwargs(self):
if not self.filter_fields and not self.search_fields:
return

self.set_bundle_from_query_string(self.request)

# Whatever is not q or page as a querystring param will
# be used for key-value search.
query = self.bundle.pop("q", "").strip()
Expand Down