diff --git a/wagtailnetlify/management/commands/netlify.py b/wagtailnetlify/management/commands/netlify.py index 7d60b68..50abb55 100644 --- a/wagtailnetlify/management/commands/netlify.py +++ b/wagtailnetlify/management/commands/netlify.py @@ -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)) @@ -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() diff --git a/wagtailnetlify/urls.py b/wagtailnetlify/urls.py index 353b3e6..e9e3363 100644 --- a/wagtailnetlify/urls.py +++ b/wagtailnetlify/urls.py @@ -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'), -] \ No newline at end of file + url(r"^success$", success_hook, name="success_hook"), + url(r"^redirects$", redirects, name="redirect_builder"), +] diff --git a/wagtailnetlify/views.py b/wagtailnetlify/views.py index 6be2e7c..17a388f 100644 --- a/wagtailnetlify/views.py +++ b/wagtailnetlify/views.py @@ -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") \ No newline at end of file + return HttpResponse("Thanks\n") + + +def redirects(request): + redirects_str, count = build_redirects() + return HttpResponse(redirects_str)