import os from flask import Blueprint, render_template, request, redirect, url_for, flash, Response from mg import application as m, tasks from mg import pool from mgweb.decorators import webtask blueprint = Blueprint('dists', __name__) @blueprint.route('/') @blueprint.route('/index') def index(): return render_template('dists.html', dists=m.pool.list_local(pool.dists), dist_sources=m.pool.dists.sources) @blueprint.route('/delete/') @webtask def delete(dist): m.pool.remove(pool.dists, dist) flash('Distributive "%s" successfully removed' % dist) return redirect(url_for('dists.index')) @blueprint.route('/download/') @webtask def download(dist): f = m.pool.name_to_path(pool.dists, dist) filename = os.path.basename(f) return Response(open(f).read(), mimetype='application/x-tgz', headers={"Content-Disposition": "attachment;filename=%s" % filename}) @blueprint.route('/add', methods=["POST"]) def add(): m.pool.get(pool.dists, request.form['dist']) return redirect(url_for('dists.index')) @blueprint.route('/rename', methods=["POST"]) def rename(): m.pool.rename(pool.dists, request.form['pk'], request.form['value']) return redirect(url_for('dists.index')) @blueprint.route('/download_remote', methods=["POST"]) @webtask def download_remote(): m.pool.dists.get_source_by_id(request.form['dist_source']).download(request.form['dist'], m.pool.dists.name_to_path(request.form['dist'])) return redirect(url_for('dists.index')) @blueprint.route('/upload_remote', methods=["POST"]) @webtask def upload_remote(): m.pool.dists.get_source_by_id(request.form['dist_source']).upload_local(m.pool.dists.name_to_path(request.form['dist']), request.form['dist']) return redirect(url_for('dists.index'))