23 lines
771 B
Python
23 lines
771 B
Python
from .models import Page,Redirect
|
|
|
|
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
|
|
|
|
class RedirectConverter:
|
|
regex='.*'
|
|
def to_python(self,value):
|
|
try: r=Redirect.objects.get(match=value)
|
|
except Page.DoesNotExist: raise ValueError(f'No such redirect {value}')
|
|
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 |