Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
D
django-rest-framework
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
edx
django-rest-framework
Commits
4d69286e
Commit
4d69286e
authored
Aug 06, 2015
by
Tom Christie
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Use same structure for both .choices and .grouped_choices
parent
a6fefe78
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
23 additions
and
26 deletions
+23
-26
rest_framework/fields.py
+23
-26
No files found.
rest_framework/fields.py
View file @
4d69286e
...
@@ -108,53 +108,50 @@ def set_value(dictionary, keys, value):
...
@@ -108,53 +108,50 @@ def set_value(dictionary, keys, value):
dictionary
[
keys
[
-
1
]]
=
value
dictionary
[
keys
[
-
1
]]
=
value
def
pairwise_choices
(
choices
):
def
to_choices_dict
(
choices
):
"""
"""
Convert
any single choices into key/value pairs instead
.
Convert
choices into key/value dicts
.
pairwise_choices([1]) ->
[(1, 1)]
pairwise_choices([1]) ->
{1: 1}
pairwise_choices([(1, '1st')
]) -> [(1, '1st')]
pairwise_choices([(1, '1st')
, (2, '2nd')]) -> {1: '1st', 2: '2nd'}
pairwise_choices([('Group', ((1, '1st'), 2))]) ->
[(1, '1st'), (2, 2)]
pairwise_choices([('Group', ((1, '1st'), 2))]) ->
{'Group': {1: '1st', 2: '2nd'}}
"""
"""
# Allow single, paired or grouped choices style:
# Allow single, paired or grouped choices style:
# choices = [1, 2, 3]
# choices = [1, 2, 3]
# choices = [(1, 'First'), (2, 'Second'), (3, 'Third')]
# choices = [(1, 'First'), (2, 'Second'), (3, 'Third')]
# choices = [('Category', ((1, 'First'), (2, 'Second'))), (3, 'Third')]
# choices = [('Category', ((1, 'First'), (2, 'Second'))), (3, 'Third')]
ret
=
[]
ret
=
OrderedDict
()
for
choice
in
choices
:
for
choice
in
choices
:
if
(
not
isinstance
(
choice
,
(
list
,
tuple
))):
if
(
not
isinstance
(
choice
,
(
list
,
tuple
))):
# single choice
# single choice
item
=
(
choice
,
choice
)
ret
[
choice
]
=
choice
ret
.
append
(
item
)
else
:
else
:
key
,
value
=
choice
key
,
value
=
choice
if
isinstance
(
value
,
(
list
,
tuple
)):
if
isinstance
(
value
,
(
list
,
tuple
)):
# grouped choices (category, sub choices)
# grouped choices (category, sub choices)
item
=
(
key
,
pairwise_choices
(
value
))
ret
[
key
]
=
to_choices_dict
(
value
)
ret
.
append
(
item
)
else
:
else
:
# paired choice (key, display value)
# paired choice (key, display value)
item
=
(
key
,
value
)
ret
[
key
]
=
value
ret
.
append
(
item
)
return
ret
return
ret
def
flatten_choices
(
choices
):
def
flatten_choices
_dict
(
choices
):
"""
"""
Convert a group choices
into a flat lis
t of choices.
Convert a group choices
dict into a flat dic
t of choices.
flatten_choices(
[(1, '1st')]) -> [(1, '1st')]
flatten_choices(
{1: '1st', 2: '2nd'}) -> {1: '1st', 2: '2nd'}
flatten_choices(
[('Grp', ((1, '1st'), (2, '2nd')))]) -> [(1, '1st'), (2, '2nd')]
flatten_choices(
{'Group': {1: '1st', 2: '2nd'}}) -> {1: '1st', 2: '2nd'}
"""
"""
ret
=
[]
ret
=
OrderedDict
()
for
key
,
value
in
choices
:
for
key
,
value
in
choices
.
items
()
:
if
isinstance
(
value
,
(
list
,
tuple
)
):
if
isinstance
(
value
,
dict
):
# grouped choices (category, sub choices)
# grouped choices (category, sub choices)
ret
.
extend
(
flatten_choices
(
value
))
for
sub_key
,
sub_value
in
value
.
items
():
ret
[
sub_key
]
=
sub_value
else
:
else
:
# choice (key, display value)
# choice (key, display value)
item
=
(
key
,
value
)
ret
[
key
]
=
value
ret
.
append
(
item
)
return
ret
return
ret
...
@@ -1161,8 +1158,8 @@ class ChoiceField(Field):
...
@@ -1161,8 +1158,8 @@ class ChoiceField(Field):
}
}
def
__init__
(
self
,
choices
,
**
kwargs
):
def
__init__
(
self
,
choices
,
**
kwargs
):
self
.
grouped_choices
=
pairwise_choices
(
choices
)
self
.
grouped_choices
=
to_choices_dict
(
choices
)
self
.
choices
=
OrderedDict
(
flatten_choices
(
self
.
grouped_choices
)
)
self
.
choices
=
flatten_choices_dict
(
self
.
grouped_choices
)
# Map the string representation of choices to the underlying value.
# Map the string representation of choices to the underlying value.
# Allows us to deal with eg. integer choices while supporting either
# Allows us to deal with eg. integer choices while supporting either
...
@@ -1207,10 +1204,10 @@ class ChoiceField(Field):
...
@@ -1207,10 +1204,10 @@ class ChoiceField(Field):
self
.
value
=
value
self
.
value
=
value
self
.
display_text
=
display_text
self
.
display_text
=
display_text
for
key
,
value
in
self
.
grouped_choices
:
for
key
,
value
in
self
.
grouped_choices
.
items
()
:
if
isinstance
(
value
,
(
list
,
tuple
)):
if
isinstance
(
value
,
(
list
,
tuple
)):
yield
StartOptionGroup
(
label
=
key
)
yield
StartOptionGroup
(
label
=
key
)
for
sub_key
,
sub_value
in
value
:
for
sub_key
,
sub_value
in
value
.
items
()
:
yield
Option
(
value
=
sub_key
,
display_text
=
sub_value
)
yield
Option
(
value
=
sub_key
,
display_text
=
sub_value
)
yield
EndOptionGroup
()
yield
EndOptionGroup
()
else
:
else
:
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment