-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
split the tasks and views into seperate modules
- Loading branch information
Showing
4 changed files
with
56 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,54 +1,11 @@ | ||
from flask import Flask, request, render_template, url_for, \ | ||
send_file, jsonify, make_response, send_from_directory | ||
from flask import Flask | ||
from celery import Celery | ||
|
||
from utils import zimit | ||
import os | ||
|
||
app = Flask(__name__) | ||
app.config.from_object('zimbalaka.default_settings') | ||
# app.config.from_envvar('ZIMBALAKA_SETTINGS') # used for loading production config | ||
|
||
celery = Celery(app.name, broker=app.config['CELERY_BROKER_URL']) | ||
celery.conf.update(app.config) | ||
|
||
@celery.task | ||
def prepare_zim(title, articles): | ||
'''task that prepares the zim file''' | ||
zimfile = zimit(title, articles) | ||
return zimfile | ||
|
||
@celery.task(ignore_result=True) | ||
def delete_zim(zimfile): | ||
'''task to delete the folder''' | ||
os.remove(zimfile) | ||
|
||
@app.route("/", methods=['POST', 'GET']) | ||
def index(): | ||
if request.method == 'GET': | ||
return render_template('index.html') | ||
if request.method == 'POST': | ||
task = prepare_zim.delay(request.form['title'], request.form['list']) | ||
return make_response( jsonify(status="started", task=task.id), 202 ) | ||
|
||
@app.route("/status/<task_id>") | ||
def status(task_id): | ||
task = prepare_zim.AsyncResult(task_id) | ||
if task.state == 'SUCCESS': | ||
return jsonify({ | ||
"status" : "success" | ||
}) | ||
return jsonify({ "status" : "pending" }) | ||
|
||
@app.route("/download/<task_id>/<filename>") | ||
def download(task_id,filename): | ||
task = prepare_zim.AsyncResult(task_id) | ||
if task.state != 'SUCCESS': | ||
return "Unavailable! Task ID: "+task_id | ||
res = task.result | ||
delete_zim.apply_async([res], countdown=3540) | ||
try: | ||
return send_file(res) | ||
except IOError: | ||
return 'The file you have requested has been deleted from the server. Zim files are stored only for 59 minutes.' | ||
|
||
import zimbalaka.views |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import os | ||
|
||
from zimbalaka import celery | ||
from zimbalaka.utils import zimit | ||
|
||
@celery.task | ||
def prepare_zim(title, articles): | ||
'''task that prepares the zim file''' | ||
zimfile = zimit(title, articles) | ||
return zimfile | ||
|
||
@celery.task(ignore_result=True) | ||
def delete_zim(zimfile): | ||
'''task to delete the folder''' | ||
os.remove(zimfile) | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
from zimbalaka import app | ||
from zimbalaka.tasks import prepare_zim, delete_zim | ||
|
||
from flask import request, render_template, url_for, \ | ||
send_file, jsonify, make_response, send_from_directory | ||
|
||
@app.route("/", methods=['POST', 'GET']) | ||
def index(): | ||
if request.method == 'GET': | ||
logger.error('Home page requested') | ||
return render_template('index.html') | ||
if request.method == 'POST': | ||
task = prepare_zim.delay(request.form['title'], request.form['list']) | ||
return make_response( jsonify(status="started", task=task.id), 202 ) | ||
|
||
@app.route("/status/<task_id>") | ||
def status(task_id): | ||
task = prepare_zim.AsyncResult(task_id) | ||
if task.state == 'SUCCESS': | ||
return jsonify({ | ||
"status" : "success" | ||
}) | ||
return jsonify({ "status" : "pending" }) | ||
|
||
@app.route("/download/<task_id>/<filename>") | ||
def download(task_id,filename): | ||
task = prepare_zim.AsyncResult(task_id) | ||
if task.state != 'SUCCESS': | ||
return "Unavailable! Task ID: "+task_id | ||
res = task.result | ||
delete_zim.apply_async([res], countdown=3540) | ||
try: | ||
return send_file(res) | ||
except IOError: | ||
return 'The file you have requested has been deleted from the server. Zim files are stored only for 59 minutes.' | ||
|