#12426 closed enhancement (fixed)
Add as_int and as_bool methods to the Request class
Reported by: | Ryan J Ollos | Owned by: | Ryan J Ollos |
---|---|---|---|
Priority: | normal | Milestone: | 1.2 |
Component: | web frontend | Version: | |
Severity: | normal | Keywords: | |
Cc: | Branch: | ||
Release Notes: | |||
API Changes: |
|
||
Internal Changes: |
Description (last modified by )
comment:5:ticket:12325 suggested adding as_int
and as_bool
methods to the Request
class. This was discussed further a bit later in the ticket.
as_int
and as_bool
in trac.util
do not raise exceptions. Perhaps we should be somewhat consistent with the interface of those functions. We could add two pairs of methods with the following behaviors:
as_int(self, name, default=None, min=None, max=None)
- Return
default
ifname
not in dict - Return
default
if exception raised convertingself[name]
to an int
- Return
getint(self, name, default=None, min=None, max=None)
- Return
default
ifname
not in dict - Raise
HTTPBadRequest
if exception is raised convertingself[name]
to an int
- Return
get_int
would be a better name, but getint
is more consistent with getfirst
and getlist
. Also it seems reasonable to expect that a get
method might raise an exception.
Attachments (0)
Change History (10)
comment:1 by , 9 years ago
Description: | modified (diff) |
---|
comment:3 by , 9 years ago
Release Notes: | modified (diff) |
---|---|
Resolution: | → fixed |
Status: | assigned → closed |
Fixed in [14693].
comment:4 by , 9 years ago
comment:5 by , 9 years ago
[14693] broke sorting on the report page. The fix is to restore the previous logic:
-
trac/ticket/report.py
diff --git a/trac/ticket/report.py b/trac/ticket/report.py index 702328e..61b6026 100644
a b class ReportModule(Component): 405 405 if max: 406 406 params['max'] = max 407 407 params.update(kwargs) 408 params['asc'] = 1 if ascelse 0408 params['asc'] = 1 if params.get('asc', asc) else 0 409 409 return req.href.report(id, params) 410 410 411 411 data = {'action': 'view',
I don't remember why I thought to change that. I'll push the fix shortly.
comment:7 by , 8 years ago
comment:9 by , 8 years ago
Proposed changes in log:rjollos.git:t12426_bool_asc_regression. I used bool(asc)
in one or two places for clarify when a boolean argument was expected, even if not strictly necessary and an integer would technically suffice.
Proposed changes in log:rjollos.git:t12426_request_methods. I'm just posting for initial feedback on the methods added to the
_RequestArgs
class. There is still work to be done in utilizing the new methods throughout the codebase.I tended towards using
getint
andgetbool
, thus always raisingHTTPBadRequest
on bad input. However, we may want to handle differently the two common scenarios: values input by the user and values not directly input by the user.An example of values input by the user would be going back x days on the Timeline page. For the case that the input value is not an integer I initially chose an
HTTPBadRequest
, but reconsidered: [b5f40dee/rjollos.git].In cases for which the user does not directly enter a value, such as the page number on search or query results, I'm more convinced we should raise an
HTTPBadRequest
on bad input: [13a4e04a/rjollos.git#file0]. In these cases the bad input would only occur due to bots or the user editing the URL.Also at issue is how to require that a request argument is provided. Raising
HTTPBadRequest
when default is not provided togetint
orgetbool
seems like an API inconsistency. Therefore I added arequire
method to_RequestArgs
instead: [ab37fe54/rjollos.git].