Skip to content

Commit

Permalink
fix(toolchain): queryset parsing for list values
Browse files Browse the repository at this point in the history
  • Loading branch information
Kl0ven committed Feb 3, 2023
1 parent bf9c3ae commit 32568b6
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 4 deletions.
12 changes: 12 additions & 0 deletions admin_action_tools/tests/unit/test_toolchain.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from django.http import QueryDict

from admin_action_tools.constants import CONFIRM_FORM
from admin_action_tools.tests.helpers import AdminConfirmTestCase
from admin_action_tools.toolchain import ToolChain
Expand Down Expand Up @@ -52,3 +54,13 @@ def test_toolchain_wrong_date_type(self):

# test data is save
self.assertEqual(request.session[name]["history"], [])

def test_toolchain_querydict(self):
data = QueryDict("a=1&a=2&a=3")
request = self.factory.request()
name = f"toolchain{request.path}"
toolchain = ToolChain(request)
res = toolchain._ToolChain__clean_data(data, {})

# test toolchain reset
self.assertEqual(res["data"], {"a": ["1", "2", "3"]})
19 changes: 15 additions & 4 deletions admin_action_tools/toolchain.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from datetime import datetime, timedelta
from typing import Dict, Optional, Tuple

from django.http import HttpRequest
from django.http import HttpRequest, QueryDict

from admin_action_tools.constants import BACK, CANCEL, FUNCTION_MARKER, ToolAction

Expand Down Expand Up @@ -130,10 +130,21 @@ def get_next_step(self, tool_name: str) -> ToolAction:
return ToolAction.INIT

def __clean_data(self, data, metadata):
data = data.dict()
data.pop("csrfmiddlewaretoken", None)
new_data = data.dict()
new_data.pop("csrfmiddlewaretoken", None)

if isinstance(data, QueryDict):
new_data = self.__process_query_dict(new_data, data)

metadata = metadata or {}
return {"data": data, "metadata": metadata}
return {"data": new_data, "metadata": metadata}

def __process_query_dict(self, new_data: dict, data: QueryDict):
for key in new_data:
old_field = data.getlist(key)
if len(old_field) > 1:
new_data[key] = old_field
return new_data

def get_history(self):
return self.data["history"]

0 comments on commit 32568b6

Please sign in to comment.