rakkapy/main/converters.py

12 lines
381 B
Python
Raw Normal View History

2022-01-27 20:59:32 +11:00
from .models import Page
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')
return p.url