Ejemplo n.º 1
0
def edit_investment(key_id):
    investment = Investment.retrieve_by_id(key_id)
    if not investment:
        return redirect(url_for('investment.admin.index'))
    if request.method == 'POST' and 'delete_investment' in request.form:
        investment.key.delete()
        return redirect(url_for('investment.admin.index'))
    url = blobstore.create_upload_url(
            url_for('investment.admin.edit', key_id=key_id))
    form = InvestmentForm(obj=investment)
    if request.method == 'POST':
        upload_files = get_uploads(request, 'attach_file')
        file_ = None
        if len(upload_files):
            blob_info = upload_files[0]
        else:
            blob_info = None

        if form.validate_on_submit():
            if blob_info:
                blob_info = blobstore.BlobInfo.get(blob_info.key())
                if blob_info.size:
                    file_ = File.create(
                        blob_key=blob_info.key(),
                        filename=os.path.basename(blob_info.filename.replace('\\', '/')),
                        size=blob_info.size,
                        content_type=blob_info.content_type)
                    file_.put()
                else:
                    blob_info.delete()

            form.populate_obj(investment)
            if file_:
                if investment.file:
                    investment.file.delete()
                investment.file = file_.key
            investment.put()
            return redirect(url_for('investment.admin.index'))
    return render_template(
        'investment/admin/edit.html',
        form=form,
        url=url,
        investment=investment
    )