Skip to content

Commit 11b0797

Browse files
Merge pull request #68 from peopledoc/Fix-unhashable-ModelChoiceIteratorValue-on-django3
Fix how choice value is retrieved on the get_filters() for django3
2 parents 8c36514 + 2ec943d commit 11b0797

File tree

5 files changed

+116
-77
lines changed

5 files changed

+116
-77
lines changed

demoproject/filter/forms.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class UserListForm(gf.FilteredForm):
1414

1515
def get_order_by_choices(self):
1616
return [
17-
("date_joined", _(u"date joined")),
18-
("last_login", _(u"last login")),
19-
("last_name", _(u"Name")),
17+
("date_joined", _("date joined")),
18+
("last_login", _("last login")),
19+
("last_name", _("Name")),
2020
]

django_genericfilters/tests/test_templatetags.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ def test_tag_update_query_string(self):
5757
}
5858
)
5959
),
60-
u"/fake?page=2",
60+
"/fake?page=2",
6161
)
6262

6363
def test_is_checkbox(self):

django_genericfilters/tests/test_views.py

+24
Original file line numberDiff line numberDiff line change
@@ -445,6 +445,30 @@ def test_filtered_list_view__modelchoice__none(self):
445445
str(view.form_valid(view.form).query),
446446
)
447447

448+
def test_filtered_list_view__modelchoice_unhashable(self):
449+
"""
450+
On Django3, the ModelChoiceIteratorValue is not hashable
451+
causing an error on get_filters() method
452+
"""
453+
peopleA = People.objects.create(name="fakeA")
454+
455+
view = views.FilteredListView(
456+
qs_filter_fields={"city": "city", "people": "parent"},
457+
filter_fields=["parent"],
458+
form_class=self.Form,
459+
model=Something,
460+
)
461+
data = {"parent": peopleA.pk}
462+
setup_view(view, RequestFactory().get("/fake", data))
463+
assert view.form.is_valid()
464+
filters = view.get_filters()
465+
# assert peopleA is selected on the filter choices
466+
assert peopleA.pk == next(
467+
getattr(f.value, "value", f.value)
468+
for f in filters[0].choices
469+
if f.is_selected
470+
)
471+
448472
def test_filtered_list_view__multiplemodelchoice(self):
449473
"""
450474
FIXED : filtered fields has HiddenWidget widgets that cannot handle

django_genericfilters/views.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,6 @@ def get_filters(self):
205205
template.
206206
"""
207207
filters = []
208-
209208
if hasattr(self, "filter_fields"):
210209
for field in self.filter_fields:
211210
new_filter = Munch()
@@ -228,7 +227,8 @@ def get_filters(self):
228227
"pk",
229228
self.form.cleaned_data[field],
230229
)
231-
if value == yesno.get(choice[0], choice[0]):
230+
choice_value = getattr(choice[0], "value", choice[0])
231+
if value == yesno.get(choice_value, choice_value):
232232
new_choice.is_selected = True
233233
selected = True
234234
else:

docs/conf.py

+86-71
Original file line numberDiff line numberDiff line change
@@ -25,40 +25,44 @@
2525

2626
doc_dir = os.path.dirname(os.path.abspath(__file__))
2727
project_dir = os.path.dirname(doc_dir)
28-
version_filename = os.path.join(project_dir, 'VERSION')
28+
version_filename = os.path.join(project_dir, "VERSION")
2929

3030

3131
# If extensions (or modules to document with autodoc) are in another directory,
3232
# add these directories to sys.path here. If the directory is relative to the
3333
# documentation root, use os.path.abspath to make it absolute, like shown here.
34-
#sys.path.insert(0, os.path.abspath('.'))
34+
# sys.path.insert(0, os.path.abspath('.'))
3535

3636
# -- General configuration -----------------------------------------------------
3737

3838
# If your documentation needs a minimal Sphinx version, state it here.
39-
#needs_sphinx = '1.0'
39+
# needs_sphinx = '1.0'
4040

4141
# Add any Sphinx extension module names here, as strings. They can be extensions
4242
# coming with Sphinx (named 'sphinx.ext.*') or your custom ones.
43-
extensions = ['sphinx.ext.autodoc', 'sphinx.ext.autosummary',
44-
'sphinx.ext.doctest', 'sphinx.ext.coverage',
45-
'sphinx.ext.viewcode']
43+
extensions = [
44+
"sphinx.ext.autodoc",
45+
"sphinx.ext.autosummary",
46+
"sphinx.ext.doctest",
47+
"sphinx.ext.coverage",
48+
"sphinx.ext.viewcode",
49+
]
4650

4751
# Add any paths that contain templates here, relative to this directory.
48-
templates_path = ['_templates']
52+
templates_path = ["_templates"]
4953

5054
# The suffix of source filenames.
51-
source_suffix = '.txt'
55+
source_suffix = ".txt"
5256

5357
# The encoding of source files.
54-
#source_encoding = 'utf-8-sig'
58+
# source_encoding = 'utf-8-sig'
5559

5660
# The master toctree document.
57-
master_doc = 'index'
61+
master_doc = "index"
5862

5963
# General information about the project.
60-
project = u'django-generic-filters'
61-
copyright = u'2013, Novapost'
64+
project = "django-generic-filters"
65+
copyright = "2013, Novapost"
6266

6367
# The version info for the project you're documenting, acts as replacement for
6468
# |version| and |release|, also used in various other places throughout the
@@ -71,176 +75,181 @@
7175

7276
# The language for content autogenerated by Sphinx. Refer to documentation
7377
# for a list of supported languages.
74-
language = 'en'
78+
language = "en"
7579

7680
# There are two options for replacing |today|: either, you set today to some
7781
# non-false value, then it is used:
78-
#today = ''
82+
# today = ''
7983
# Else, today_fmt is used as the format for a strftime call.
80-
#today_fmt = '%B %d, %Y'
84+
# today_fmt = '%B %d, %Y'
8185

8286
# List of patterns, relative to source directory, that match files and
8387
# directories to ignore when looking for source files.
84-
exclude_patterns = ['_build']
88+
exclude_patterns = ["_build"]
8589

8690
# The reST default role (used for this markup: `text`) to use for all documents.
87-
#default_role = None
91+
# default_role = None
8892

8993
# If true, '()' will be appended to :func: etc. cross-reference text.
90-
#add_function_parentheses = True
94+
# add_function_parentheses = True
9195

9296
# If true, the current module name will be prepended to all description
9397
# unit titles (such as .. function::).
94-
#add_module_names = True
98+
# add_module_names = True
9599

96100
# If true, sectionauthor and moduleauthor directives will be shown in the
97101
# output. They are ignored by default.
98-
#show_authors = False
102+
# show_authors = False
99103

100104
# The name of the Pygments (syntax highlighting) style to use.
101-
pygments_style = 'sphinx'
105+
pygments_style = "sphinx"
102106

103107
# A list of ignored prefixes for module index sorting.
104-
#modindex_common_prefix = []
108+
# modindex_common_prefix = []
105109

106110

107111
# -- Options for HTML output ---------------------------------------------------
108112

109113
# The theme to use for HTML and HTML Help pages. See the documentation for
110114
# a list of builtin themes.
111-
html_theme = 'default'
115+
html_theme = "default"
112116

113117
# Theme options are theme-specific and customize the look and feel of a theme
114118
# further. For a list of options available for each theme, see the
115119
# documentation.
116-
#html_theme_options = {}
120+
# html_theme_options = {}
117121

118122
# Add any paths that contain custom themes here, relative to this directory.
119-
#html_theme_path = []
123+
# html_theme_path = []
120124

121125
# The name for this set of Sphinx documents. If None, it defaults to
122126
# "<project> v<release> documentation".
123-
#html_title = None
127+
# html_title = None
124128

125129
# A shorter title for the navigation bar. Default is the same as html_title.
126-
#html_short_title = None
130+
# html_short_title = None
127131

128132
# The name of an image file (relative to this directory) to place at the top
129133
# of the sidebar.
130-
#html_logo = None
134+
# html_logo = None
131135

132136
# The name of an image file (within the static path) to use as favicon of the
133137
# docs. This file should be a Windows icon file (.ico) being 16x16 or 32x32
134138
# pixels large.
135-
#html_favicon = None
139+
# html_favicon = None
136140

137141
# Add any paths that contain custom static files (such as style sheets) here,
138142
# relative to this directory. They are copied after the builtin static files,
139143
# so a file named "default.css" will overwrite the builtin "default.css".
140-
html_static_path = ['_static']
144+
html_static_path = ["_static"]
141145

142146
# If not '', a 'Last updated on:' timestamp is inserted at every page bottom,
143147
# using the given strftime format.
144-
#html_last_updated_fmt = '%b %d, %Y'
148+
# html_last_updated_fmt = '%b %d, %Y'
145149

146150
# If true, SmartyPants will be used to convert quotes and dashes to
147151
# typographically correct entities.
148-
#html_use_smartypants = True
152+
# html_use_smartypants = True
149153

150154
# Custom sidebar templates, maps document names to template names.
151155
html_sidebars = {
152-
'**': ['globaltoc.html',
153-
'relations.html',
154-
'sourcelink.html',
155-
'searchbox.html'],
156+
"**": ["globaltoc.html", "relations.html", "sourcelink.html", "searchbox.html"],
156157
}
157158

158159
# Additional templates that should be rendered to pages, maps page names to
159160
# template names.
160-
#html_additional_pages = {}
161+
# html_additional_pages = {}
161162

162163
# If false, no module index is generated.
163-
#html_domain_indices = True
164+
# html_domain_indices = True
164165

165166
# If false, no index is generated.
166-
#html_use_index = True
167+
# html_use_index = True
167168

168169
# If true, the index is split into individual pages for each letter.
169-
#html_split_index = False
170+
# html_split_index = False
170171

171172
# If true, links to the reST sources are added to the pages.
172-
#html_show_sourcelink = True
173+
# html_show_sourcelink = True
173174

174175
# If true, "Created using Sphinx" is shown in the HTML footer. Default is True.
175-
#html_show_sphinx = True
176+
# html_show_sphinx = True
176177

177178
# If true, "(C) Copyright ..." is shown in the HTML footer. Default is True.
178-
#html_show_copyright = True
179+
# html_show_copyright = True
179180

180181
# If true, an OpenSearch description file will be output, and all pages will
181182
# contain a <link> tag referring to it. The value of this option must be the
182183
# base URL from which the finished HTML is served.
183-
#html_use_opensearch = ''
184+
# html_use_opensearch = ''
184185

185186
# This is the file name suffix for HTML files (e.g. ".xhtml").
186-
#html_file_suffix = None
187+
# html_file_suffix = None
187188

188189
# Output file base name for HTML help builder.
189-
htmlhelp_basename = 'django-generic-filtersdoc'
190+
htmlhelp_basename = "django-generic-filtersdoc"
190191

191192

192193
# -- Options for LaTeX output --------------------------------------------------
193194

194195
latex_elements = {
195-
# The paper size ('letterpaper' or 'a4paper').
196-
#'papersize': 'letterpaper',
197-
198-
# The font size ('10pt', '11pt' or '12pt').
199-
#'pointsize': '10pt',
200-
201-
# Additional stuff for the LaTeX preamble.
202-
#'preamble': '',
196+
# The paper size ('letterpaper' or 'a4paper').
197+
#'papersize': 'letterpaper',
198+
# The font size ('10pt', '11pt' or '12pt').
199+
#'pointsize': '10pt',
200+
# Additional stuff for the LaTeX preamble.
201+
#'preamble': '',
203202
}
204203

205204
# Grouping the document tree into LaTeX files. List of tuples
206205
# (source start file, target name, title, author, documentclass [howto/manual]).
207206
latex_documents = [
208-
('index', 'django-generic-filters.tex', u'django-generic-filters Documentation',
209-
u'Novapost', 'manual'),
207+
(
208+
"index",
209+
"django-generic-filters.tex",
210+
"django-generic-filters Documentation",
211+
"Novapost",
212+
"manual",
213+
),
210214
]
211215

212216
# The name of an image file (relative to this directory) to place at the top of
213217
# the title page.
214-
#latex_logo = None
218+
# latex_logo = None
215219

216220
# For "manual" documents, if this is true, then toplevel headings are parts,
217221
# not chapters.
218-
#latex_use_parts = False
222+
# latex_use_parts = False
219223

220224
# If true, show page references after internal links.
221-
#latex_show_pagerefs = False
225+
# latex_show_pagerefs = False
222226

223227
# If true, show URL addresses after external links.
224-
#latex_show_urls = False
228+
# latex_show_urls = False
225229

226230
# Documents to append as an appendix to all manuals.
227-
#latex_appendices = []
231+
# latex_appendices = []
228232

229233
# If false, no module index is generated.
230-
#latex_domain_indices = True
234+
# latex_domain_indices = True
231235

232236

233237
# -- Options for manual page output --------------------------------------------
234238

235239
# One entry per manual page. List of tuples
236240
# (source start file, name, description, authors, manual section).
237241
man_pages = [
238-
('index', 'django-generic-filters', u'django-generic-filters Documentation',
239-
[u'Novapost'], 1)
242+
(
243+
"index",
244+
"django-generic-filters",
245+
"django-generic-filters Documentation",
246+
["Novapost"],
247+
1,
248+
)
240249
]
241250

242251
# If true, show URL addresses after external links.
243-
#man_show_urls = False
252+
# man_show_urls = False
244253

245254

246255
# -- Options for Texinfo output ------------------------------------------------
@@ -249,16 +258,22 @@
249258
# (source start file, target name, title, author,
250259
# dir menu entry, description, category)
251260
texinfo_documents = [
252-
('index', 'django-generic-filters', u'django-generic-filters Documentation',
253-
u'Novapost', 'django-generic-filters', 'One line description of project.',
254-
'Miscellaneous'),
261+
(
262+
"index",
263+
"django-generic-filters",
264+
"django-generic-filters Documentation",
265+
"Novapost",
266+
"django-generic-filters",
267+
"One line description of project.",
268+
"Miscellaneous",
269+
),
255270
]
256271

257272
# Documents to append as an appendix to all manuals.
258-
#texinfo_appendices = []
273+
# texinfo_appendices = []
259274

260275
# If false, no module index is generated.
261-
#texinfo_domain_indices = True
276+
# texinfo_domain_indices = True
262277

263278
# How to display URL addresses: 'footnote', 'no', or 'inline'.
264-
#texinfo_show_urls = 'footnote'
279+
# texinfo_show_urls = 'footnote'

0 commit comments

Comments
 (0)