Created
July 7, 2015 15:46
-
-
Save Kniyl/44acedbb7de112214132 to your computer and use it in GitHub Desktop.
Proxy model
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Proxy(Page): | |
""" | |
A general content type for creating links to existing pages | |
in the page menu. Act as a placeholder for the first child | |
page if no page is explicitely being proxied. | |
""" | |
target = models.ForeignKey("Page", related_name="proxies", blank=True, null=True) | |
class Meta: | |
verbose_name = _("Proxy page") | |
verbose_name_plural = _("Proxies pages") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@processor_for(Proxy) | |
def proxy_processor(request, page): | |
""" | |
Redirect to the proxied page. If none exist, try | |
to redirect to the first child page. | |
""" | |
target = page.proxy.target | |
if target: | |
return redirect(target) | |
target = list(page.children.published().order_by('_order')[:1]) | |
if target: | |
return redirect(target[0]) | |
raise Http404 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment