Edgewall Software

Opened 8 years ago

Closed 8 years ago

Last modified 7 years ago

#12426 closed enhancement (fixed)

Add as_int and as_bool methods to the Request class — at Version 3

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:
  • Added as_bool to _RequestArgs, which converts the value to a boolean or returns a default if an exception is raised.
  • Added as_int to _RequestArgs, which converts the value to an int or returns a default if an exception is raised.
  • Added getbool to _RequestArgs, which gets the value as a boolean or raises an HTTPBadRequest exception on invalid input.
  • Added getint to _RequestArgs, which gets the value as an integer or raises an HTTPBadRequest exception on invalid input.
  • Added require to _RequestArgs, which raises an HTTPBadRequest exception if the parameter is not in the dictionary.
API Changes:
Internal Changes:

Description (last modified by Ryan J Ollos)

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 if name not in dict
    • Return default if exception raised converting self[name] to an int
  • getint(self, name, default=None, min=None, max=None)
    • Return default if name not in dict
    • Raise HTTPBadRequest if exception is raised converting self[name] to an int

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.

Change History (3)

comment:1 by Ryan J Ollos, 8 years ago

Description: modified (diff)

comment:2 by Ryan J Ollos, 8 years ago

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 and getbool, thus always raising HTTPBadRequest 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 to getint or getbool seems like an API inconsistency. Therefore I added a require method to _RequestArgs instead: [ab37fe54/rjollos.git].

Last edited 8 years ago by Ryan J Ollos (previous) (diff)

comment:3 by Ryan J Ollos, 8 years ago

Release Notes: modified (diff)
Resolution: fixed
Status: assignedclosed

Fixed in [14693].

Note: See TracTickets for help on using tickets.