routing

New in version 1.1.0.

URL-to-function routing.

class smisk.mvc.routing.Destination(leaf)

A callable destination.

action
canonical_leaf
path

Canonical exposed path.

Return type:list
template_path

Template path.

Return type:list
uri

Canonical exposed URI.

Return type:string
class smisk.mvc.routing.Filter
match(method, url)

Test this filter against method and url.

Returns:(list args, dict params) or None if no match
Return type:tuple
class smisk.mvc.routing.RegExpFilter(pattern, destination_path, regexp_flags=2, match_on_full_url=False, methods=None, params={})
match(method, url)

Test this filter against method and url.

Returns:(list args, dict params) or None if no match
Return type:tuple
class smisk.mvc.routing.Router

Default router handling both RegExp mappings and class tree mappings.

Consider the following tree of controllers:

class root(Controller):
  def __call__(self, *args, **params):
    return 'Welcome!'

class employees(root):
  def __call__(self, *args, **params):
    return {'employees': Employee.query.all()}

  def show(self, name, *args, **params):
    return {'employee': Employee.get_by(name=name)}

  class edit(employees):
    def save(self, employee_id, *args, **params):
      Employee.get_by(id=employee_id).save_or_update(**params)

Now, this list shows what URIs would map to what begin called:

/                         => root().__call__()
/employees                => employees().__call__()
/employees/               => employees().__call__()
/employees/show           => employees().show()
/employees/show?name=foo  => employees().show(name='foo')
/employees/show/123       => None
/employees/edit/save      => employees.edit().save()

See source of smisk.test.routing for more examples.

configure(config_key='smisk.mvc.routes')
filter(pattern, destination_path, regexp_flags=2, match_on_full_url=False, params={}, methods=None)

Explicitly map an leaf to paths or urls matching regular expression pattern.

Parameters:
  • pattern (string or re.Regex) – Pattern
  • destination_path (string) – Path to leaf, expressed in internal canonical form. i.e. “/controller/leaf”.
  • regexp_flags (int) – Defaults to re.I (case-insensitive)
  • match_on_full_url (bool) – Where there or not to perform matches on complete URL (i.e. “https://foo.tld/bar?question=2“). Defauts to False (i.e.matches on path only. “/bar”)
  • params (dict) – Parameters are saved and later included in every call to leafs taking this route.
Return type:

RegExpFilter