Skip to content
This repository has been archived by the owner on Dec 3, 2019. It is now read-only.

Deal with StreamingHttpResponse in disk renderer #20

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
11 changes: 7 additions & 4 deletions django_medusa/management/commands/staticsitegen.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from django.core.management.base import BaseCommand
from django_medusa.renderers import StaticSiteRenderer
from django_medusa.utils import get_static_renderers


Expand All @@ -10,10 +9,14 @@ class Command(BaseCommand):
'a class for processing one or more URL paths into static files.'

def handle(self, *args, **options):
StaticSiteRenderer.initialize_output()
renderers = get_static_renderers()

for Renderer in get_static_renderers():
for Renderer in renderers:
Renderer.initialize_output()

for Renderer in renderers:
r = Renderer()
r.generate()

StaticSiteRenderer.finalize_output()
for Renderer in renderers:
Renderer.finalize_output()
9 changes: 7 additions & 2 deletions django_medusa/renderers/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
from django.conf import settings
from django.utils import importlib
try:
# Django versions < 1.9
from django.utils.importlib import import_module
except ImportError:
# Django versions >= 1.9
from django.utils.module_loading import import_module
from .base import BaseStaticSiteRenderer
from .disk import DiskStaticSiteRenderer
from .appengine import GAEStaticSiteRenderer
Expand All @@ -12,7 +17,7 @@

def get_cls(renderer_name):
mod_path, cls_name = renderer_name.rsplit('.', 1)
mod = importlib.import_module(mod_path)
mod = import_module(mod_path)
return getattr(mod, cls_name)


Expand Down
9 changes: 8 additions & 1 deletion django_medusa/renderers/disk.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ def _disk_render_path(args):
outpath = os.path.join(DEPLOY_DIR, realpath)

resp = client.get(path)
if resp.status_code == 301 or resp.status_code == 302:
print('%s - redirect - skipping...' % path)
return None
if resp.status_code != 200:
raise Exception
if needs_ext:
Expand All @@ -53,7 +56,11 @@ def _disk_render_path(args):
outpath += "index.html"
print(outpath)
with open(outpath, 'wb') as f:
f.write(resp.content)
if resp.streaming:
for chunk in resp.streaming_content:
f.write(chunk)
else:
f.write(resp.content)


class DiskStaticSiteRenderer(BaseStaticSiteRenderer):
Expand Down