summaryrefslogtreecommitdiff
path: root/app/builder
diff options
context:
space:
mode:
Diffstat (limited to 'app/builder')
-rw-r--r--app/builder/base.py118
1 files changed, 117 insertions, 1 deletions
diff --git a/app/builder/base.py b/app/builder/base.py
index 62cfad0..ded8bba 100644
--- a/app/builder/base.py
+++ b/app/builder/base.py
@@ -4,12 +4,128 @@ from decimal import Decimal
from django.test.client import Client
from django.template.loader import render_to_string
from django.template import Context
+from django.core.urlresolvers import reverse
from django.apps import apps
from django.conf import settings
+from jsmin import jsmin
+
+
+class _FileWriter(object):
+ """
+ Given a path and text object; write the page to disc
+ """
+ def __init__(self, path, text_object, ext='html', filename='index', base_path=settings.NFLATFILES_ROOT):
+ self.path = '%s%s' % (base_path, path)
+ if not os.path.isdir(self.path):
+ os.makedirs(self.path)
+ fpath = '%s%s.%s' % (self.path, filename, ext)
+ self.write(fpath, text_object)
+ if ext == 'js':
+ self.compress_js(filename, text_object)
+
+ def write(self, fpath, text_object):
+ f = open(fpath, 'wb')
+ f.write(text_object)
+ f.close()
+
+ def compress_js(self, filename, text_object):
+ path = '%s%s.min.js' % (self.path, filename)
+ compressed = jsmin(text_object.decode(encoding='UTF-8'))
+ self.write(path, compressed)
+
+
+class BuildNew():
+ def __init__(self, model, app):
+ self.model = apps.get_model(model, app)
+ self.get_model_queryset()
+ self.client = Client()
+
+ def build(self):
+ self.build_list_view()
+ self.build_detail_view()
+
+ def get_model_queryset(self):
+ return self.model.objects.filter(status__exact=1)
+
+ def write_file(self, path, text_object, ext='html', filename='index'):
+ self.writer = _FileWriter(path, text_object, ext=ext, filename=filename)
+
+ def get_pages(self, qs, paginate_by):
+ return int(ceil(Decimal(qs.count()) / Decimal(paginate_by)))
+
+ def build_list_view(self, base_path='', qs=None, paginate_by=10):
+ """
+ Archive Page builder that actually crawls the urls
+ because we need to be able to pass a request object to the template
+ """
+ if not qs:
+ qs = self.get_model_queryset()
+ pages = self.get_pages(qs, paginate_by)
+ for page in range(pages):
+ if int(pages) > 1:
+ path = '%s%s/' % (base_path, str(page + 1))
+ url = '%s%s/' % (base_path, str(page + 1))
+ else:
+ path = base_path
+ url = base_path
+ response = self.client.get(url, HTTP_HOST='127.0.0.1')
+ if page == 0:
+ self.write_file(base_path, response.content)
+ self.write_file(path, response.content)
+
+ def build_year_view(self, url, paginate_by=99999):
+ years = self.model.objects.dates('pub_date', 'year')
+ for year in years:
+ year = year.strftime('%Y')
+ qs = self.model.objects.filter(
+ status__exact=1,
+ pub_date__year=year
+ )
+ self.build_list_view(
+ base_path=reverse(url, kwargs={'year': year, }),
+ qs=qs,
+ paginate_by=paginate_by
+ )
+
+ def build_month_view(self, url, paginate_by=99999):
+ months = self.model.objects.dates('pub_date', 'month')
+ for m in months:
+ year = m.strftime('%Y')
+ month = m.strftime('%m')
+ qs = self.model.objects.filter(
+ status__exact=1,
+ pub_date__year=year,
+ pub_date__month=month
+ )
+ if qs.exists():
+ self.build_list_view(
+ base_path=reverse(url, kwargs={'year': year, 'month': month}),
+ qs=qs,
+ paginate_by=paginate_by
+ )
+
+ def build_detail_view(self):
+ '''
+ Grab all the blog posts, render them to a template
+ string and write that out to the filesystem
+ '''
+ for entry in self.get_model_queryset():
+ url = entry.get_absolute_url()
+ path, slug = os.path.split(entry.get_absolute_url())
+ path = '%s/' % path
+ # write html
+ response = self.client.get(url)
+ self.write_file(path, response.content, filename=slug)
+ # write txt
+ response = self.client.get('%s.txt' % url)
+ self.write_file(path, response.content, ext='txt', filename=slug)
+ # write AMP
+ response = self.client.get('%s.amp' % url)
+ self.write_file(path, response.content, ext='amp', filename=slug)
-#from pages.models import PageGenerator
class Build():
+
def write_file(self, path, text_object, ext='html', filename='index'):
"""
Given a path and object intended to be a webpage, write the page the