rakkapy/main/converters.py

23 lines
775 B
Python
Raw Normal View History

2022-02-27 23:23:26 +11:00
from .models import Page,Redirect
2022-01-27 20:59:32 +11:00
class AutoPageConverter:
regex='.*'
def to_python(self,value):
try: p=Page.objects.get(url=value)
except Page.DoesNotExist: raise ValueError(f'No such page {value}')
if not p: raise ValueError(f'No such page {value}')
return p
def to_url(self,value):
if not isinstance(value,Page): raise ValueError('Given value is not a Page')
2022-02-27 23:23:26 +11:00
return p.url
class RedirectConverter:
regex='.*'
def to_python(self,value):
try: r=Redirect.objects.get(match=value)
except Redirect.DoesNotExist: raise ValueError(f'No such redirect {value}')
2022-02-27 23:23:26 +11:00
if not r: raise ValueError(f'No such redirect {value}')
return r
def to_url(self,value):
if not isinstance(value,Redirect): raise ValueError('Given value is not a Redirect')
return p.match