Skip to content

Commit

Permalink
split the tasks and views into seperate modules
Browse files Browse the repository at this point in the history
  • Loading branch information
tecoholic committed May 11, 2015
1 parent 19ea286 commit d5b165c
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 45 deletions.
47 changes: 2 additions & 45 deletions zimbalaka/__init__.py
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
1 change: 1 addition & 0 deletions zimbalaka/default_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
# NOTE Change this if you want to use a different broker
CELERY_BROKER_URL = 'redis://localhost:6379/0'
CELERY_RESULT_BACKEND = 'redis://localhost:6379/0'
CELERY_TASK_SERIALIZER = 'json'

import os
# Utility Script locations
Expand Down
17 changes: 17 additions & 0 deletions zimbalaka/tasks.py
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)


36 changes: 36 additions & 0 deletions zimbalaka/views.py
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.'

0 comments on commit d5b165c

Please sign in to comment.