"""Public page paths supported by Studio's background editor.""" import re from django.core.exceptions import ValidationError from django.urls import Resolver404, resolve PAGE_CHOICES = ( ('/', 'Homepage (including its countdown layout)'), ('/coming-soon/', 'Coming soon'), ('/chapters/', 'Chapter archive'), ('/upcoming/', 'Upcoming releases'), ('/blog/', 'Blog'), ('/reviews/', 'Series reviews'), ('/cast/', 'Cast'), ('/world/', 'World archive'), ('/live/', 'SLX Live'), ('/books/', 'Books'), ('/community/', 'Community'), ('/search/', 'Search'), ('/newsletter/', 'Newsletter'), ('/fan/login/', 'Reader sign-in'), ('/fan/signup/', 'Reader sign-up'), ('/fan/library/', 'Reader library'), ('/fan/notifications/', 'Fan notifications'), ) SAFE_ROUTES = { 'home', 'coming-soon', 'chapters', 'chapter', 'chapter-discussion', 'upcoming', 'blog', 'blog-post', 'entries', 'entry', 'stream', 'books', 'slx-community', 'search', 'newsletter-signup', 'fan-login', 'fan-signup', 'library', 'fan-notices', 'slx-reviews', 'slx-review', 'slx-review-write', 'slx-series-reviews', 'slx-series-review', 'slx-series-review-write', 'slx-profile', } def validate_background_path(path): if not re.fullmatch(r'/(?:[a-zA-Z0-9_-]+/)*', path): raise ValidationError('Enter a site path such as /blog/ or /chapters/9/, including the final slash.') try: match = resolve(path) except Resolver404: raise ValidationError('Choose an existing SLX page path.') from None if match.namespace or match.url_name not in SAFE_ROUTES: raise ValidationError('Choose an SLX reader-facing page, such as /blog/ or /chapters/9/.') if match.url_name in {'entries', 'entry'} and match.kwargs.get('kind') not in {'news', 'cast', 'world'}: raise ValidationError('Choose an existing SLX page path.')