Guest User

Untitled

a guest
Feb 13th, 2015
303
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.41 KB | None | 0 0
  1. import os, shutil
  2.  
  3. from django.conf import settings
  4. from django.core.management import call_command
  5. from django.core.management.base import BaseCommand, CommandError
  6. from django.core.urlresolvers import reverse
  7. from django.test.client import Client
  8.  
  9. def get_pages():
  10.     for name in os.listdir(settings.SITE_PAGES_DIR):
  11.         if name.endswith('.html'):
  12.             yield name[:-5]
  13.  
  14. class Command(BaseCommand):
  15.        
  16.     def handle(self, *args, **options):
  17.         """Request pages and build output."""
  18.  
  19.         if args:
  20.             pages = args
  21.             available = list(get_pages())
  22.             invalid  = []
  23.  
  24.             for page in pages:
  25.                 if page not in available:
  26.                     invalid.append(page)
  27.             if invalid:
  28.                 msg = "Invalid Pages: {}".format(', '.join(invalid))
  29.                 raise CommandError(msg)
  30.         else:
  31.             pages = get_pages()
  32.             if os.path.exists(settings.SITE_OUTPUT_DIR):
  33.                 shutil.rmtree(settings.SITE_OUTPUT_DIR)
  34.             os.mkdir(settings.SITE_OUTPUT_DIR)
  35.             os.makedirs(settings.STATIC_ROOT)
  36.         call_command('collectstatic', interactive=False, clear=True, verbosity=0)
  37.         client = Client()
  38.        
  39.         for page in get_pages():
  40.             url = reverse('page', kwargs={'slug' : page})
  41.             response = client.get(url)
  42.             if page == 'index':
  43.                 output_dir = settings.SITE_OUTPUT_DIR
  44.             else:
  45.                 output_dir = os.path.join(settings.SITE_OUTPUT_DIR, page)
  46.                 os.makedirs(output_dir)                
  47.             with open(os.path.join(output_dir, 'index.html'), 'wb') as f:
  48.                 f.write(response.content)
Advertisement
Add Comment
Please, Sign In to add comment