#12684 closed enhancement (fixed)
_RequestArgs.get should alias getfirst
Reported by: | Ryan J Ollos | Owned by: | Ryan J Ollos |
---|---|---|---|
Priority: | normal | Milestone: | 1.3.2 |
Component: | general | Version: | |
Severity: | normal | Keywords: | |
Cc: | Branch: | ||
Release Notes: | |||
API Changes: |
|
||
Internal Changes: |
Fixed exceptions due to |
Description (last modified by )
Discussed in #12682, to make the API more pythonic and to fix issues like the one seen in #12682, req.args.get
should return a string rather than a string or list. If a list is expected req.args.getlist
should be used.
_RequestArgs.getbinary
will be added, as a shorthand for: req.args.getint('key', default, min=0, max=1)
.
The as_
methods of _RequestArgs
should be modified/extended to be consistent with the changes.
Code in query.py
can also be refactored to use _RequestArgs
: trunk/trac/ticket/query.py@15573:949-971#L948.
Attachments (0)
Change History (8)
comment:1 by , 8 years ago
Description: | modified (diff) |
---|
comment:2 by , 8 years ago
comment:3 by , 8 years ago
I think we could use arg_list_to_args
rather than adding new arg_dict_to_args
:
-
trac/ticket/query.py
diff --git a/trac/ticket/query.py b/trac/ticket/query.py index 1b94a1f71..24d7e8cc0 100644
a b class QueryModule(Component): 929 929 constraints = self._get_constraints(arg_list=arg_list) 930 930 else: 931 931 query = Query.from_string(self.env, qstring) 932 arg _dict = {'order': query.order, 'group': query.group,933 'col': query.cols, 'max': query.max}932 args = [('order', query.order), ('group', query.group), 933 ('col', query.cols), ('max', query.max)] 934 934 if query.desc: 935 arg _dict['desc'] = '1'935 args.append(('desc', '1')) 936 936 if query.groupdesc: 937 arg _dict['groupdesc'] = '1'938 args = arg_ dict_to_args(arg_dict)937 args.append(('groupdesc', '1')) 938 args = arg_list_to_args(args) 939 939 constraints = query.constraints 940 940 941 941 # Substitute $USER, or ensure no field constraints that depend
Also, we could remove one of isinstance(vals, (list, tuple))
pattern in query.py:
-
trac/ticket/query.py
diff --git a/trac/ticket/query.py b/trac/ticket/query.py index 1b94a1f71..24d7e8cc0 100644
a b class QueryModule(Component): 1019 1019 # requested for clients without JavaScript 1020 1020 add_num = None 1021 1021 constraints = {} 1022 for k, vals in req.args.iteritems(): 1022 for k in req.args: 1023 vals = req.args.getlist(k) 1023 1024 match = self.add_re.match(k) 1024 1025 if match: 1025 1026 add_num = match.group(1) … … class QueryModule(Component): 1031 1032 clause_num = int(match.group('clause')) 1032 1033 if field not in fields: 1033 1034 continue 1034 if not isinstance(vals, (list, tuple)):1035 vals = [vals]1036 1035 if vals: 1037 1036 mode = req.args.get(k + '_mode') 1038 1037 if mode:
comment:4 by , 8 years ago
Another variant: values in req.args are used in trac/ticket/web_ui.py without checking type of the value. It would raise AttributeError: 'list' object has no attribute 'strip'
if custom field which is time type added to query string of /newticket
twice.
-
trac/ticket/web_ui.py
diff --git a/trac/ticket/web_ui.py b/trac/ticket/web_ui.py index 5068c00c4..2676dc5fa 100644
a b class TicketModule(Component): 823 823 824 824 def _populate(self, req, ticket, plain_fields=False): 825 825 if not plain_fields: 826 fields = {k[6:]: v for k, v in req.args.iteritems()826 fields = {k[6:]: req.args.get(k) for k in req.args 827 827 if k.startswith('field_') and 828 828 'revert_' + k[6:] not in req.args} 829 829 # Handle revert of checkboxes (in particular, revert to 1) … … class TicketModule(Component): 833 833 if 'revert_' + k in req.args: 834 834 fields[k] = ticket[k] 835 835 else: 836 fields = req.args.copy()836 fields = {k: req.args.get(k) for k in req.args} 837 837 # Prevent direct changes to protected fields (status and resolution are 838 838 # set in the workflow, in get_ticket_changes()) 839 839 for each in Ticket.protected_fields:
comment:5 by , 8 years ago
Those changes look very good. Thanks!
Changes added to log:rjollos.git:t12684_alias_getfirst.
comment:6 by , 8 years ago
API Changes: | modified (diff) |
---|
comment:7 by , 8 years ago
Resolution: | → fixed |
---|---|
Status: | assigned → closed |
Committed to trunk in [15611:15613].
Proposed changes in log:rjollos.git:t12684_alias_getfirst.