Skip to content

Instantly share code, notes, and snippets.

@kidlj
Forked from alej0varas/wagtailredirectpage.py
Last active August 29, 2015 14:14
Show Gist options
  • Save kidlj/febd5475648873e9fe81 to your computer and use it in GitHub Desktop.
Save kidlj/febd5475648873e9fe81 to your computer and use it in GitHub Desktop.
# Thanks to Ethan Jucovy
# http://www.coactivate.org/projects/ejucovy/blog/2014/05/10/wagtail-notes-managing-redirects-as-pages/
# This model comes from wagtaildemo
class LinkFields(models.Model):
link_external = models.URLField("External link", blank=True)
link_page = models.ForeignKey(
'wagtailcore.Page',
null=True,
blank=True,
related_name='+'
)
link_document = models.ForeignKey(
'wagtaildocs.Document',
null=True,
blank=True,
related_name='+'
)
@property
def link(self):
if self.link_page:
return self.link_page.url
elif self.link_document:
return self.link_document.url
else:
return self.link_external
panels = [
FieldPanel('link_external'),
PageChooserPanel('link_page'),
DocumentChooserPanel('link_document'),
]
class Meta:
abstract = True
# This is the model you are looking for
# from wagtail.wagtailcore.models import Page
class PageAlias(Page, LinkFields):
def serve(self, request):
return redirect(self.link, permanent=False)
PageAlias.content_panels = [FieldPanel('title')] + LinkFields.panels
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment