| 1 |
import base64 |
| 2 |
|
| 3 |
|
| 4 |
class Base64Converter: |
| 5 |
regex = r"[a-zA-Z0-9+/]*={0,2}" |
| 6 |
|
| 7 |
def to_python(self, value): |
| 8 |
return base64.b64decode(value) |
| 9 |
|
| 10 |
def to_url(self, value): |
| 11 |
return base64.b64encode(value).decode("ascii") |
| 12 |
|
| 13 |
|
| 14 |
class DynamicConverter: |
| 15 |
_dynamic_to_python = None |
| 16 |
_dynamic_to_url = None |
| 17 |
|
| 18 |
@property |
| 19 |
def regex(self): |
| 20 |
return r"[0-9a-zA-Z]+" |
| 21 |
|
| 22 |
@regex.setter |
| 23 |
def regex(self): |
| 24 |
raise Exception("You can't modify the regular expression.") |
| 25 |
|
| 26 |
def to_python(self, value): |
| 27 |
return type(self)._dynamic_to_python(value) |
| 28 |
|
| 29 |
def to_url(self, value): |
| 30 |
return type(self)._dynamic_to_url(value) |
| 31 |
|
| 32 |
@classmethod |
| 33 |
def register_to_python(cls, value): |
| 34 |
cls._dynamic_to_python = value |
| 35 |
|
| 36 |
@classmethod |
| 37 |
def register_to_url(cls, value): |
| 38 |
cls._dynamic_to_url = value |