Skip to content

Commit

Permalink
Worked more fs into the views
Browse files Browse the repository at this point in the history
  • Loading branch information
palewire committed Dec 24, 2017
1 parent 7b1ead5 commit e16895d
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 40 deletions.
19 changes: 11 additions & 8 deletions bakery/views/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import gzip
import logging
import mimetypes
from fs import path
from django.apps import apps
from django.conf import settings
from bakery import DEFAULT_GZIP_CONTENT_TYPES
Expand All @@ -28,6 +29,8 @@ class BuildableMixin(object):
"""
Common methods we will use in buildable views.
"""
fs = apps.get_app_config("bakery").filesystem

def create_request(self, path):
"""
Returns a GET request object for use when building views.
Expand All @@ -47,15 +50,16 @@ def get_content(self):
"""
return self.get(self.request).render().content

def prep_directory(self, path):
def prep_directory(self, target_dir):
"""
Prepares a new directory to store the file at the provided path,
if needed.
Prepares a new directory to store the file at the provided path, if needed.
"""
dirname = os.path.dirname(path)
dirname = path.dirname(target_dir)
if dirname:
dirname = os.path.join(settings.BUILD_DIR, dirname)
os.path.exists(dirname) or os.makedirs(dirname)
dirname = path.join(settings.BUILD_DIR, dirname)
if not self.fs.exists(dirname):
logger.debug("Creating directory {}".format(dirname))
self.fs.makedirs(dirname)

def build_file(self, path, html):
if self.is_gzippable(path):
Expand All @@ -68,8 +72,7 @@ def write_file(self, path, html):
Writes out the provided HTML to the provided path.
"""
logger.debug("Building HTML file to %s" % path)
fs = apps.get_app_config("bakery").filesystem
with fs.open(six.u(path), 'wb') as outfile:
with self.fs.open(six.u(path), 'wb') as outfile:
outfile.write(six.binary_type(html))
outfile.close()

Expand Down
47 changes: 28 additions & 19 deletions bakery/views/dates.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import os
import shutil
import logging
from fs import path
from datetime import date
from django.conf import settings
from bakery.views import BuildableMixin
Expand Down Expand Up @@ -49,8 +50,8 @@ def build_queryset(self):
logger.debug("Building %s" % self.build_path)
self.request = self.create_request(self.build_path)
self.prep_directory(self.build_path)
path = os.path.join(settings.BUILD_DIR, self.build_path)
self.build_file(path, self.get_content())
target_path = path.join(settings.BUILD_DIR, self.build_path)
self.build_file(target_path, self.get_content())


class BuildableYearArchiveView(YearArchiveView, BuildableMixin):
Expand Down Expand Up @@ -94,9 +95,11 @@ def get_build_path(self):
would like your page at a different location. By default it
will be built at self.get_url() + "/index.html"
"""
path = os.path.join(settings.BUILD_DIR, self.get_url().lstrip('/'))
os.path.exists(path) or os.makedirs(path)
return os.path.join(path, 'index.html')
target_path = path.join(settings.BUILD_DIR, self.get_url().lstrip('/'))
if not self.fs.exists(target_path):
logger.debug("Creating {}".format(target_path))
self.fs.makedirs(target_path)
return path.join(target_path, 'index.html')

def build_year(self, dt):
"""
Expand All @@ -105,8 +108,8 @@ def build_year(self, dt):
self.year = str(dt.year)
logger.debug("Building %s" % self.year)
self.request = self.create_request(self.get_url())
path = self.get_build_path()
self.build_file(path, self.get_content())
target_path = self.get_build_path()
self.build_file(target_path, self.get_content())

def build_dated_queryset(self):
"""
Expand All @@ -122,9 +125,10 @@ def unbuild_year(self, dt):
"""
self.year = str(dt.year)
logger.debug("Unbuilding %s" % self.year)
path = os.path.split(self.get_build_path())[0]
if os.path.exists(path):
shutil.rmtree(path)
target_path = os.path.split(self.get_build_path())[0]
if self.fs.exists(target_path):
logger.debug("Removing {}".format(target_path))
self.fs.removetree(target_path)


class BuildableMonthArchiveView(MonthArchiveView, BuildableMixin):
Expand Down Expand Up @@ -177,9 +181,11 @@ def get_build_path(self):
would like your page at a different location. By default it
will be built at self.get_url() + "/index.html"
"""
path = os.path.join(settings.BUILD_DIR, self.get_url().lstrip('/'))
os.path.exists(path) or os.makedirs(path)
return os.path.join(path, 'index.html')
target_path = path.join(settings.BUILD_DIR, self.get_url().lstrip('/'))
if not self.fs.exists(target_path):
logger.debug("Creating {}".format(target_path))
self.fs.makedirs(target_path)
return path.join(target_path, 'index.html')

def build_month(self, dt):
"""
Expand Down Expand Up @@ -281,9 +287,11 @@ def get_build_path(self):
would like your page at a different location. By default it
will be built at self.get_url() + "/index.html"
"""
path = os.path.join(settings.BUILD_DIR, self.get_url().lstrip('/'))
os.path.exists(path) or os.makedirs(path)
return os.path.join(path, 'index.html')
target_path = path.join(settings.BUILD_DIR, self.get_url().lstrip('/'))
if not self.fs.exists(target_path):
logger.debug("Creating {}".format(target_path))
self.fs.makedirs(target_path)
return os.path.join(target_path, 'index.html')

def build_day(self, dt):
"""
Expand Down Expand Up @@ -313,6 +321,7 @@ def unbuild_day(self, dt):
self.month = str(dt.month)
self.day = str(dt.day)
logger.debug("Building %s-%s-%s" % (self.year, self.month, self.day))
path = os.path.split(self.get_build_path())[0]
if os.path.exists(path):
shutil.rmtree(path)
target_path = os.path.split(self.get_build_path())[0]
if self.fs.exists(target_path):
logger.debug("Removing {}".format(target_path))
self.fs.removetree(target_path)
21 changes: 12 additions & 9 deletions bakery/views/detail.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
for building flat files.
"""
import os
import shutil
import logging
from fs import path
from .base import BuildableMixin
from django.conf import settings
from django.views.generic import DetailView
Expand Down Expand Up @@ -45,9 +45,11 @@ def get_build_path(self, obj):
would like your detail page at a different location. By default it
will be built at get_url() + "index.html"
"""
path = os.path.join(settings.BUILD_DIR, self.get_url(obj).lstrip('/'))
os.path.exists(path) or os.makedirs(path)
return os.path.join(path, 'index.html')
target_path = path.join(settings.BUILD_DIR, self.get_url(obj).lstrip('/'))
if not self.fs.exists(target_path):
logger.debug("Creating {}".format(target_path))
self.fs.makedirs(target_path)
return path.join(target_path, 'index.html')

def set_kwargs(self, obj):
slug_field = self.get_slug_field()
Expand All @@ -63,8 +65,8 @@ def build_object(self, obj):
logger.debug("Building %s" % obj)
self.request = self.create_request(self.get_url(obj))
self.set_kwargs(obj)
path = self.get_build_path(obj)
self.build_file(path, self.get_content())
target_path = self.get_build_path(obj)
self.build_file(target_path, self.get_content())

def build_queryset(self):
[self.build_object(o) for o in self.get_queryset().all()]
Expand All @@ -74,6 +76,7 @@ def unbuild_object(self, obj):
Deletes the directory at self.get_build_path.
"""
logger.debug("Unbuilding %s" % obj)
path = os.path.split(self.get_build_path(obj))[0]
if os.path.exists(path):
shutil.rmtree(path)
target_path = os.path.split(self.get_build_path(obj))[0]
if self.fs.exists(target_path):
logger.debug("Removing {}".format(target_path))
self.fs.removetree(target_path)
6 changes: 3 additions & 3 deletions bakery/views/list.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
Views that inherit from Django's class-based generic views and add methods
for building flat files.
"""
import os
import logging
from fs import path
from .base import BuildableMixin
from django.conf import settings
from django.views.generic import ListView
Expand Down Expand Up @@ -40,5 +40,5 @@ def build_queryset(self):
logger.debug("Building %s" % self.build_path)
self.request = self.create_request(self.build_path)
self.prep_directory(self.build_path)
path = os.path.join(settings.BUILD_DIR, self.build_path)
self.build_file(path, self.get_content())
target_path = path.join(settings.BUILD_DIR, self.build_path)
self.build_file(target_path, self.get_content())
2 changes: 1 addition & 1 deletion example/project/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,6 @@


try:
from settings_local import *
from .settings_local import *
except ImportError:
pass

0 comments on commit e16895d

Please sign in to comment.