changeset 927:175d80cf75d7

serve: Admin dashboard improvements. - Remove total source counts to make it faster to display. - Fix WIP display.
author Ludovic Chabant <ludovic@chabant.com>
date Sun, 01 Oct 2017 20:38:19 -0700
parents 86b684cc0551
children 41db689d36b6
files piecrust/admin/views/dashboard.py
diffstat 1 files changed, 29 insertions(+), 40 deletions(-) [+]
line wrap: on
line diff
--- a/piecrust/admin/views/dashboard.py	Sun Oct 01 20:37:11 2017 -0700
+++ b/piecrust/admin/views/dashboard.py	Sun Oct 01 20:38:19 2017 -0700
@@ -5,11 +5,11 @@
     current_app, g, request,
     render_template, url_for, redirect)
 from flask.ext.login import login_user, logout_user, login_required
-from piecrust.configuration import parse_config_header
+from piecrust.sources.fs import FSContentSourceBase
 from piecrust.sources.interfaces import IInteractiveSource
 from piecrust.uriutil import split_uri
 from ..textutil import text_preview
-from ..blueprint import foodtruck_bp, load_user, after_this_request
+from ..blueprint import foodtruck_bp, load_user
 from ..views import with_menu_context
 
 
@@ -32,30 +32,29 @@
         if not isinstance(source, IInteractiveSource):
             continue
 
-        items = source.getAllContents()
         src_data = {
             'name': source.name,
-            'list_url': url_for('.list_source', source_name=source.name),
-            'page_count': len(items)}
+            'list_url': url_for('.list_source', source_name=source.name)}
         data['sources'].append(src_data)
 
-        fe = getattr(source, 'fs_endpoint', None)
-        if fe:
-            fs_endpoints[fe] = source
+        if isinstance(source, FSContentSourceBase):
+            fs_endpoints[source.fs_endpoint] = source
 
     data['new_pages'] = []
     data['edited_pages'] = []
     data['misc_files'] = []
     if site.scm:
         st = site.scm.getStatus()
+        auto_formats = site.piecrust_app.config.get('site/auto_formats',
+                                                    ['html'])
         for p in st.new_files:
-            pd = _getWipData(p, site, fs_endpoints)
+            pd = _getWipData(p, fs_endpoints, auto_formats, site.piecrust_app)
             if pd:
                 data['new_pages'].append(pd)
             else:
                 data['misc_files'].append(p)
         for p in st.edited_files:
-            pd = _getWipData(p, site, fs_endpoints)
+            pd = _getWipData(p, fs_endpoints, auto_formats, site.piecrust_app)
             if pd:
                 data['edited_pages'].append(pd)
             else:
@@ -65,14 +64,19 @@
     data['url_publish'] = url_for('.publish')
     data['url_preview'] = url_for('.preview_root_page')
 
+    pub_tgts = pcapp.config.get('publish', {})
+    data['publish'] = {'targets': list(pub_tgts.keys())}
+
+    micropub = pcapp.config.get('micropub', {})
+    data['publish'] = micropub.get('publish_target')
+
     with_menu_context(data)
     return render_template('dashboard.html', **data)
 
 
-def _getWipData(path, site, fs_endpoints):
-    auto_formats = site.piecrust_app.config.get('site/auto_formats', ['html'])
+def _getWipData(path, fs_endpoints, auto_formats, pcapp):
     pathname, pathext = os.path.splitext(path)
-    if pathext not in auto_formats:
+    if pathext.lstrip('.') not in auto_formats:
         return None
 
     source = None
@@ -83,42 +87,27 @@
     if source is None:
         return None
 
-    fac = source.buildPageFactory(os.path.join(site.root_dir, path))
-    route = site.piecrust_app.getSourceRoute(source.name, fac.metadata)
-    if not route:
+    content_item = source.findContentFromPath(path)
+    if content_item is None:
         return None
 
-    qp = QualifiedPage(fac.buildPage(), route, fac.metadata)
-    uri = qp.getUri()
-    _, slug = split_uri(site.piecrust_app, uri)
-
-    with open(fac.path, 'r', encoding='utf8') as fp:
-        raw_text = fp.read()
+    page = pcapp.getPage(source, content_item)
+    uri = page.getUri()
+    _, slug = split_uri(pcapp, uri)
 
-    header, offset = parse_config_header(raw_text)
-    extract = text_preview(raw_text, offset=offset)
+    seg = page.getSegment()
+    if not seg:
+        return None
+
+    extract = text_preview(seg.content)
     return {
-        'title': qp.config.get('title'),
+        'title': page.config.get('title'),
         'slug': slug,
-        'url': url_for('.edit_page', slug=slug),
+        'url': url_for('.edit_page', uri=slug),
         'text': extract
     }
 
 
-@login_required
-@foodtruck_bp.route('/switch_site', methods=['POST'])
-def switch_site():
-    site_name = request.form.get('site_name')
-    if not site_name:
-        return redirect(url_for('.index'))
-
-    @after_this_request
-    def _save_site(resp):
-        resp.set_cookie('foodtruck_site_name', site_name)
-
-    return redirect(url_for('.index'))
-
-
 @foodtruck_bp.route('/login', methods=['GET', 'POST'])
 def login():
     data = {}