def get_uploaded_file_name(schedule_id): for item in get_schedule(): if item["conf_key"] == schedule_id: break else: raise Exception("Bad schedule ID: %r" % (schedule_id, )) prefix = schedule_item_file_prefix(item) return "%s-%s" % (prefix, mk_random_id())
def get_uploaded_file_name(schedule_id): for item in get_schedule(): if item["conf_key"] == schedule_id: break else: raise Exception("Bad schedule ID: %r" %(schedule_id, )) prefix = schedule_item_file_prefix(item) return "%s-%s" %(prefix, mk_random_id())
def index(): if request.method == "POST": schd_id = int(request.form["schedule_id"]) fprefix = get_uploaded_file_name(schd_id) fobj = request.files["slides"] fname = save_uploaded_file(fprefix, fobj, schd_id) return flask.Response( "%s saved. Thanks!" % (fname or "File", ), content_type="text/plain", ) resp = flask.make_response( flask.render_template( "index.html", schedule=get_schedule(), )) if "client_id" not in request.cookies: resp.set_cookie("client_id", mk_random_id()) return resp
def index(): if request.method == "POST": schd_id = int(request.form["schedule_id"]) fprefix = get_uploaded_file_name(schd_id) fobj = request.files["slides"] fname = save_uploaded_file(fprefix, fobj, schd_id) return flask.Response( "%s saved. Thanks!" %(fname or "File", ), content_type="text/plain", ) resp = flask.make_response( flask.render_template( "index.html", schedule=get_schedule(), ) ) if "client_id" not in request.cookies: resp.set_cookie("client_id", mk_random_id()) return resp
def save_uploaded_file(fprefix, fobj, schd_id): release = request.form.getlist("release") environ_items = request.environ.items() fname_list = [] # weee this can fail and we'll have noooooo idea and this is terrible :D # TODO: fix that... # NB: need to do this in the background because Heroku likes its workers to # respond quickly, but saving to dropbox can be a bit slow. temp_fname = "/tmp/temp-upload-%s" %(mk_random_id(), ) fobj.save(temp_fname) temp_fobj = open(temp_fname) thread = Thread( target=save_uploaded_file_real, args=(fname_list, fprefix, temp_fobj, fobj.filename, schd_id, release, environ_items), ) os.unlink(temp_fname) thread.start() thread.join(10) if thread.is_alive(): return None return fname_list[0]