Skip to content

Commit

Permalink
Add Redirects view (#21)
Browse files Browse the repository at this point in the history
  • Loading branch information
tomdyson authored Sep 29, 2020
1 parent 37a98d7 commit fe6de2b
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 23 deletions.
27 changes: 16 additions & 11 deletions wagtailnetlify/management/commands/netlify.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,30 @@
from wagtail.wagtailredirects.models import Redirect


def build_redirects():
out = "# Redirects from what the browser requests to what we serve\n"
count = 0
for redirect in Redirect.objects.all():
status_code = "302"
if redirect.is_permanent:
status_code = "301"
out += "%s\t%s\t%s\n" % (redirect.old_path, redirect.link, status_code)
count += 1
return out, count


class Command(BaseCommand):

help = "Deploys your baked Wagtail site to Netlify"

def build_redirects(self):
def write_redirects(self):
# Redirects are configured in a file called '_redirects' at the root of the build directory
if not hasattr(settings, "BUILD_DIR"):
raise CommandError("BUILD_DIR is not defined in settings")
redirect_file = os.path.join(settings.BUILD_DIR, "_redirects")
redirects_str, count = build_redirects()
fo = open(redirect_file, "w")
fo.write("# Redirects from what the browser requests to what we serve\n")
# for each redirect, write old path, new url, status code
count = 0
for redirect in Redirect.objects.all():
status_code = "302"
if redirect.is_permanent:
status_code = "301"
fo.write("%s\t%s\t%s\n" % (redirect.old_path, redirect.link, status_code))
count += 1
fo.write(redirects_str)
fo.close()
self.stdout.write("Written %s redirect(s) to %s" % (count, redirect_file))

Expand Down Expand Up @@ -67,6 +72,6 @@ def add_arguments(self, parser):

def handle(self, *args, **kwargs):
no_deploy = kwargs["no_deploy"]
self.build_redirects()
self.write_redirects()
if not no_deploy:
self.deploy()
7 changes: 4 additions & 3 deletions wagtailnetlify/urls.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from django.conf.urls import url
from wagtailnetlify.views import success_hook
from wagtailnetlify.views import success_hook, redirects

urlpatterns = [
url(r'^success$', success_hook, name='success_hook'),
]
url(r"^success$", success_hook, name="success_hook"),
url(r"^redirects$", redirects, name="redirect_builder"),
]
23 changes: 14 additions & 9 deletions wagtailnetlify/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,26 @@
from django.views.decorators.csrf import csrf_exempt
from django.utils import timezone
from wagtailnetlify.models import Deployment
from wagtailnetlify.management.commands.netlify import build_redirects


@csrf_exempt
def success_hook(request):
""" Handle incoming webhook from Netlify, to record deployment completion """
body_unicode = request.body.decode('utf-8')
body_unicode = request.body.decode("utf-8")
payload = json.loads(body_unicode)
# get the first deployment without a Netlify ID
deployment = Deployment.objects \
.filter(netlify_id__isnull=True) \
.order_by('id') \
.first()
deployment.netlify_id = payload['id']
deployment.url = payload['url']
deployment.deployment_url = payload['deploy_ssl_url']
deployment = (
Deployment.objects.filter(netlify_id__isnull=True).order_by("id").first()
)
deployment.netlify_id = payload["id"]
deployment.url = payload["url"]
deployment.deployment_url = payload["deploy_ssl_url"]
deployment.datetime_finished = timezone.now()
deployment.save()
return HttpResponse("Thanks\n")
return HttpResponse("Thanks\n")


def redirects(request):
redirects_str, count = build_redirects()
return HttpResponse(redirects_str)

0 comments on commit fe6de2b

Please sign in to comment.