def edit_coach(key_id): coach = Coach.retrieve_by_id(key_id) if not coach: return redirect(url_for('coach.admin.index')) if request.method == 'POST' and 'delete_coach' in request.form: coach.key.delete() return redirect(url_for('coach.admin.index')) url = blobstore.create_upload_url( url_for('coach.admin.edit', key_id=key_id)) form = CoachForm(obj=coach) 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(coach) if file_: if coach.file: coach.file.delete() coach.file = file_.key coach.put() return redirect(url_for('coach.admin.index')) return render_template( 'coach/admin/edit.html', form=form, url=url, coach=coach )
def add_coach(): form = CoachForm() url = blobstore.create_upload_url( url_for('coach.admin.add')) 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(), title=form.name.data, description=form.description.data, is_public=form.is_public.data, filename=os.path.basename(blob_info.filename.replace('\\', '/')), size=blob_info.size, content_type=blob_info.content_type) file_.put() else: blob_info.delete() coach = Coach() form.populate_obj(coach) if file_: coach.file = file_.key coach.put() return redirect(url_for('coach.admin.index')) return render_template( 'coach/admin/add.html', form=form, url=url )