Mercurial > farol > farol
diff farol/main.py @ 12:4219d6fb4c38
Implement three kind of caches
author | Benoît Allard <benoit.allard@greenbone.net> |
---|---|
date | Tue, 30 Sep 2014 12:18:52 +0200 |
parents | a32f9b86edb4 |
children | d5265a0da13a |
line wrap: on
line diff
--- a/farol/main.py Mon Sep 29 14:19:07 2014 +0200 +++ b/farol/main.py Tue Sep 30 12:18:52 2014 +0200 @@ -33,8 +33,9 @@ from flask import Flask, request, render_template, redirect, url_for, flash from werkzeug import secure_filename +from . import cache from .document import document -from .session import get_current, set_current, has_current, del_current, document_required +from .session import get_current, set_current, has_current, document_required from .vulnerability import vulnerability from .producttree import producttree @@ -42,6 +43,7 @@ app.config.from_object('farol.config.Config') app.config.from_pyfile('farol.cfg', silent=True) +app.register_blueprint(cache.mod, url_prefix='/cache') app.register_blueprint(document, url_prefix='/document') app.register_blueprint(vulnerability, url_prefix='/vulnerability') app.register_blueprint(producttree, url_prefix='/producttree') @@ -56,16 +58,8 @@ @app.context_processor def cache_content(): """ List the documents in cache """ - dirname = app.config.get('CACHE_DIRECTORY', - os.path.join(app.instance_path, '_cache')) - 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 dict(cache=l) + return dict(caching=cache.caching_type(), + cache=cache.cache_content()) @app.context_processor def doc_properties(): @@ -98,7 +92,7 @@ def new(): if request.method != 'POST': return render_template('new.j2', has_document=has_current(), now=utcnow()) - url = None + if 'rhsa' in request.form: year, index = request.form['id'].split(':') parse_url("https://www.redhat.com/security/data/cvrf/%(year)s/cvrf-rhsa-%(year)s-%(index)s.xml" % {'year': year, 'index': index}) @@ -136,39 +130,6 @@ doc = render_cvrf(cvrf, format_ + '.j2') return render_template('render.j2', format_=format_, title=cvrf._title, type_=cvrf._type, doc=doc ) -@app.route('/save', methods=['GET', 'POST']) -@document_required -def save(): - if request.method != 'POST': - return render_template('save.j2', id_=get_current()._tracking._identification._id) - # Get some kind of filename, and save the cvrf on cache (disk) - path = secure_filename(request.form['fname']) - path, _ = os.path.splitext(path) - dirname = app.config.get('CACHE_DIRECTORY', - os.path.join(app.instance_path, '_cache')) - with open(os.path.join(dirname, path + '.xml'), 'wt') as f: - f.write(render_cvrf(get_current(), 'cvrf.j2').encode('utf-8')) - flash('File saved as %s' % path) - del_current() - return redirect(url_for('new')) - -@app.route('/load/<element>', methods=['GET', 'POST']) -def load(element): - if request.method != 'POST': - if has_current(): - # Suggest to save first - return render_template('load.j2', element=element) - - dirname = app.config.get('CACHE_DIRECTORY', - os.path.join(app.instance_path, '_cache')) - fpath = os.path.join(dirname, element+'.xml') - 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')) - @app.route('/about') def about(): return render_template('about.j2', instance_dir=app.instance_path)