Mercurial > farol > farol
view farol/cache.py @ 122:eb790c2848c3 0.2.2
Prepare the release of Farol 0.2.2
author | Benoît Allard <benoit.allard@greenbone.net> |
---|---|
date | Fri, 17 Oct 2014 16:11:05 +0200 |
parents | 8a9fdf02bf5b |
children |
line wrap: on
line source
# -*- encoding: utf-8 -*- # Description: # Web stuff related to the cache. # # Authors: # BenoƮt Allard <benoit.allard@greenbone.net> # # Copyright: # Copyright (C) 2014 Greenbone Networks GmbH # # This program is free software; you can redistribute it and/or # modify it under the terms of the GNU General Public License # as published by the Free Software Foundation; either version 2 # of the License, or (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. """\ The caching stuff ... """ import os from flask import (Blueprint, current_app, session, flash, redirect, url_for, render_template, request, abort) from werkzeug import secure_filename from farolluz.parsers.cvrf import parse from farolluz.renderer import render from .session import get_current, set_current, del_current, has_current, document_required mod = Blueprint('cache', __name__) def caching_type(): """\ Returns the type of caching we are using: None, 'session' or 'global' """ c_type = current_app.config.get('CACHING', 'global') return {'disabled': None}.get(c_type, c_type) def _caching_dir(): """\ Returns the current caching directory """ c_type = caching_type() if c_type is None: return None root_dir = current_app.config.get('CACHE_DIRECTORY', os.path.join(current_app.instance_path, '_cache')) if c_type == 'global': return root_dir uid = session.get('id') # No uid yet, so no cache ... if uid is None: return None return os.path.join(root_dir, uid.hex) def cache_content(): """\ The content of the cache """ dirname = _caching_dir() if dirname is None: return [] if not os.path.exists(dirname): os.makedirs(dirname) l = [] for path in os.listdir(dirname): name, ext = os.path.splitext(path) if ext == '.xml': l.append(name) return l def cleanup_cache(): """\ Remove old documents ... maybe ... from time to time ... """ pass @mod.route('/save', methods=['GET', 'POST']) @document_required def save(): if caching_type() is None: current_app.logger.warning('Tried to save although caching is disabled.') flash('This version does not allow you to save your document !', 'danger') abort(403) if request.method != 'POST': return render_template('cache/save.j2', id_=get_current().getDocId()) # Get some kind of filename, and save the cvrf on cache (disk) path = secure_filename(request.form['fname']) path, _ = os.path.splitext(path) dirname = _caching_dir() with open(os.path.join(dirname, path + '.xml'), 'wt') as f: f.write(render(get_current(), 'cvrf.j2').encode('utf-8')) flash('File saved as %s' % path) del_current() return redirect(url_for('new')) @mod.route('/load/<element>', methods=['POST']) def load(element): dirname = _caching_dir() element = secure_filename(element) if dirname is None: current_app.logger.warning('Tried to load something although no' ' caching dir present: %s' % element) flash('Not able to load %s' % element) abort(404) fpath = os.path.join(dirname, element+'.xml') if not os.path.exists(fpath): current_app.logger.warning('Tried to load an unexisting document: %s' % element) flash('Document %s does not exists !' % element) abort(404) with open(fpath, 'rt') as f: set_current(parse(f)) os.remove(fpath) flash('"%s" has been removed from cache' % element) # Get some kind of id, and load the file. return redirect(url_for('document.view'))