refactor: added custom routing for profiles

- installed a regex conveter to werkzeug to support regular expresions in routes
- added a website route rule to match all profiles
This commit is contained in:
Anand Chitipothu
2021-04-06 17:52:23 +05:30
parent 175bd19a51
commit 06f7698e8f
4 changed files with 91 additions and 2 deletions

25
community/routing.py Normal file
View File

@@ -0,0 +1,25 @@
"""Utilities for making custom routing.
"""
from werkzeug.routing import BaseConverter, Map
from werkzeug.datastructures import ImmutableDict
class RegexConverter(BaseConverter):
"""werkzeug converter that supports custom regular expression.
The `install_regex_converter` function must be called before using
regex converter in rules.
"""
def __init__(self, map, regex):
super().__init__(map)
self.regex = regex
def install_regex_converter():
"""Installs the RegexConvetor to the default converters supported by werkzeug.
This allows specifing rules using regex. For example:
/profiles/<regex("[a-z0-9]{5,}"):username>
"""
default_converters = dict(Map.default_converters, regex=RegexConverter)
Map.default_converters = ImmutableDict(default_converters)