changeset 960:8101692fdc11

admin: Add a "rebake preview assets" button to the dashboard. Baking preview assets is needed for showing the site preview. Also, add flashed messages.
author Ludovic Chabant <ludovic@chabant.com>
date Sat, 07 Oct 2017 12:13:57 -0700
parents 4c69935ca415
children b1a00c2c0c7f
files piecrust/admin/siteinfo.py piecrust/admin/templates/dashboard.html piecrust/admin/views/dashboard.py
diffstat 3 files changed, 61 insertions(+), 11 deletions(-) [+]
line wrap: on
line diff
--- a/piecrust/admin/siteinfo.py	Sat Oct 07 12:12:28 2017 -0700
+++ b/piecrust/admin/siteinfo.py	Sat Oct 07 12:13:57 2017 -0700
@@ -6,6 +6,7 @@
 import threading
 import subprocess
 from flask import request, flash
+from piecrust import CACHE_DIR
 from piecrust.app import PieCrustFactory
 
 
@@ -80,17 +81,35 @@
     def publish_log_file(self):
         return os.path.join(self.piecrust_app.cache_dir, 'publish.log')
 
+    def rebakeAssets(self):
+        out_dir = os.path.join(
+            self.root_dir,
+            CACHE_DIR,
+            self.piecrust_factory.cache_key,
+            'server')
+        args = [
+            '--no-color',
+            'bake',
+            '-o', out_dir,
+            '--assets-only']
+        proc = self._runChef(args)
+        try:
+            proc.wait(timeout=2)
+            if proc.returncode == 0:
+                flash("Assets baked successfully!")
+            else:
+                flash("Asset baking process returned '%s'... check the log." %
+                      proc.returncode)
+        except subprocess.TimeoutExpired:
+            flash("Asset baking process is still running... check the log later.")
+
     def getPublishTargetLogFile(self, target):
         target = target.replace(' ', '_').lower()
         return os.path.join(self.piecrust_app.cache_dir,
                             'publish.%s.log' % target)
 
     def publish(self, target):
-        chef_path = os.path.realpath(os.path.join(
-            os.path.dirname(__file__),
-            '../../chef.py'))
         args = [
-            sys.executable, chef_path,
             '--no-color',
             '--pid-file', self.publish_pid_file,
             '--log', self.publish_log_file,
@@ -98,13 +117,7 @@
             '--log-publisher', self.getPublishTargetLogFile(target),
             '--log-debug-info',
             target]
-        env = {}
-        for k, v in os.environ.items():
-            env[k] = v
-        env['PYTHONHOME'] = sys.prefix
-        logger.info("Running publishing command: %s" % args)
-        proc = subprocess.Popen(args, cwd=self.root_dir, env=env)
-        logger.info("Publishing process ID: %s" % proc.pid)
+        proc = self._runChef(args)
         try:
             proc.wait(timeout=2)
             if proc.returncode == 0:
@@ -115,3 +128,18 @@
         except subprocess.TimeoutExpired:
             flash("Publish process is still running... check the log later.")
 
+    def _runChef(self, args):
+        chef_path = os.path.realpath(os.path.join(
+            os.path.dirname(__file__),
+            '../../chef.py'))
+        args = [sys.executable, chef_path] + args
+
+        env = {}
+        for k, v in os.environ.items():
+            env[k] = v
+        env['PYTHONHOME'] = sys.prefix
+
+        logger.info("Running chef command: %s" % args)
+        proc = subprocess.Popen(args, cwd=self.root_dir, env=env)
+        logger.info("Chef process ID: %s" % proc.pid)
+        return proc
--- a/piecrust/admin/templates/dashboard.html	Sat Oct 07 12:12:28 2017 -0700
+++ b/piecrust/admin/templates/dashboard.html	Sat Oct 07 12:13:57 2017 -0700
@@ -6,6 +6,15 @@
         <h1>{{site_title}} <a href="{{url_preview}}"><span class="icon ion-arrow-right-c"></span></a></h1>
     </div>
 </div>
+{% with messages = get_flashed_messages() %}
+{% if messages %}
+<div class="row">
+{% for message in messages %}
+<p class="bg-info">{{message}}</p>
+{% endfor %}
+</div>
+{% endif %}
+{% endwith %}
 <div class="row">
     <div class="col-md-6">
         <h2><span class="icon ion-stats-bars"></span> Site Summary</h2>
@@ -14,6 +23,11 @@
             <a href="{{s.list_url}}">{{s.page_count}} {{s.name}}</a>
         </div>
         {% endfor %}
+
+        <h2><span class="icon ion-wrench"></span> Site Management</h2>
+        <form action="{{url_bake_assets}}" method="POST">
+            <button type="submit" class="btn"><span class="icon ion-loop"></span> Rebake Preview Assets</button>
+        </form>
     </div>
     <div class="col-md-6">
         <h2><span class="icon ion-erlenmeyer-flask"></span> Work in Progress</h2>
--- a/piecrust/admin/views/dashboard.py	Sat Oct 07 12:12:28 2017 -0700
+++ b/piecrust/admin/views/dashboard.py	Sat Oct 07 12:13:57 2017 -0700
@@ -63,6 +63,7 @@
     data['site_title'] = pcapp.config.get('site/title', "Unnamed Website")
     data['url_publish'] = url_for('.publish')
     data['url_preview'] = url_for('.preview_root_page')
+    data['url_bake_assets'] = url_for('.rebake_assets')
 
     pub_tgts = pcapp.config.get('publish', {})
     data['publish'] = {'targets': list(pub_tgts.keys())}
@@ -109,6 +110,13 @@
     }
 
 
+@foodtruck_bp.route('/rebake_assets', methods=['POST'])
+@login_required
+def rebake_assets():
+    g.site.rebakeAssets()
+    return redirect(url_for('.index'))
+
+
 @foodtruck_bp.route('/login', methods=['GET', 'POST'])
 def login():
     data = {}