-
Notifications
You must be signed in to change notification settings - Fork 2
Bugfix/swi 63 handle empty policy area #1933
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
base: master
Are you sure you want to change the base?
Changes from all commits
013414c
cf3e613
0b1800b
f53e61d
10e322c
0e9525f
bfbf22f
ddc3461
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -71,6 +71,10 @@ def __init__( | |
| sort_by: str | None = None, | ||
| sort_order: str | None = None | ||
| ) -> None: | ||
| # ignore empty policy areas swi-63 | ||
| if policy_area and '' in policy_area: | ||
| policy_area = [i for i in policy_area if i] | ||
|
|
||
|
Comment on lines
+74
to
+77
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This check should go away if the converter is doing its job |
||
| super().__init__(page) | ||
| self.app = app | ||
| self.session = app.session() | ||
|
|
||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This seems a little bit too complex for a converter, I would choose two simple converter functions like we do for things like The other issue is that you're converting from
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm also not sure if this will actually work, since |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| from __future__ import annotations | ||
| import re | ||
| from typing import TYPE_CHECKING | ||
|
|
||
| import morepath | ||
|
|
||
|
|
||
| if TYPE_CHECKING: | ||
| PolicyAreaConverterBase = morepath.Converter[str] | ||
| else: | ||
| PolicyAreaConverterBase = morepath.Converter | ||
|
|
||
|
|
||
| class PolicyAreaListConverter(PolicyAreaConverterBase): | ||
|
|
||
| def verify_format(self, s: str) -> bool: | ||
| # verify is a number or in '1.13.136' format, | ||
| # no alphanumeric character allowed | ||
| return bool(re.fullmatch(r'\d+(\.\d+)*', s)) | ||
|
|
||
| def verify_components(self, s: str) -> bool: | ||
| # verify that componenet starts with previous component if | ||
| # split over `.` valid '1.12.123' but invalid '1.22.123' | ||
| components = s.split('.') | ||
| if len(components) == 1: | ||
| return True | ||
|
|
||
| for component in components[1:]: | ||
| if not component.startswith( | ||
| components[components.index(component) - 1]): | ||
| return False | ||
|
|
||
| return True | ||
|
|
||
| def validate(self, s: str) -> bool: | ||
| """ Basically drops invalid policy areas. """ | ||
| if not s: | ||
| return False | ||
|
|
||
| return self.verify_format(s) and self.verify_components(s) | ||
|
|
||
| def decode(self, s: str) -> list[str]: # type: ignore[override] | ||
| if not s: | ||
| return [] | ||
| return [item for item in s if self.validate(item)] | ||
|
|
||
| def encode(self, l: list[str]) -> list[str]: # type: ignore[override] | ||
| if not l: | ||
| return [] | ||
| return [item for item in l if item] |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| from onegov.swissvotes.converters import PolicyAreaListConverter | ||
|
|
||
|
|
||
| def test_policy_area_converter(): | ||
| converter = PolicyAreaListConverter(str) | ||
|
|
||
| assert converter.decode(['']) == [] | ||
| assert converter.decode([None]) == [] | ||
| assert converter.decode([]) == [] | ||
| assert converter.decode(['1']) == ['1'] | ||
| assert converter.decode(['1', '4']) == ['1', '4'] | ||
| assert converter.decode(['1', '4', '8', '10']) == ['1', '4', '8', '10'] | ||
| assert converter.decode(['1', '', '8', '22']) == ['1', '8', '22'] | ||
| assert converter.decode(['1', '', '', '22']) == ['1', '22'] | ||
|
|
||
| assert converter.encode(None) == [] | ||
| assert converter.encode('') == [] | ||
| assert converter.encode([]) == [] | ||
| assert converter.encode(['1']) == ['1'] | ||
| assert converter.encode(['21']) == ['21'] | ||
| assert converter.encode(['1', '4']) == ['1', '4'] | ||
| assert converter.encode(['1', '4', '8', '10']) == ['1', '4', '8', '10'] | ||
|
|
||
| assert converter.decode(['1.12']) == ['1.12'] | ||
| assert converter.decode(['1.12.121']) == ['1.12.121'] | ||
| assert converter.decode(['4.42.421']) == ['4.42.421'] | ||
| assert converter.decode(['10.102']) == ['10.102'] | ||
| assert converter.decode(['10.103.1035']) == ['10.103.1035'] | ||
| assert converter.decode(['12.125.1251']) == ['12.125.1251'] | ||
| assert converter.decode(['1.12.123.1231']) == ['1.12.123.1231'] | ||
|
|
||
| # invalid policy area(s) | ||
| assert converter.decode(['z']) == [] | ||
| assert converter.decode(['1,12,121']) == [] | ||
| assert converter.decode(['1.32.121']) == [] | ||
| assert converter.decode(['4.92.421']) == [] | ||
| assert converter.decode(['a.a2.a21']) == [] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you should merge master, this looks like changes that happened a while ago on master, so they should not be included in the diff.