def new_photo(slide_id): slide = Slide.query.filter_by(id=slide_id).first() if not slide: flash(BAD_KITTY, 'danger') return redirect(url_for('index')) journey = Journey.query.filter_by(id=slide.journey_id, user_id=current_user.id).first() if not journey: flash(BAD_KITTY, 'danger') return redirect(url_for('index')) form = NewPhotoForm(slide_id=slide_id) if form.validate_on_submit(): photo = Photo() tmp = tempfile.NamedTemporaryFile(suffix='.jpg') form.photo.data.save(tmp) tmp.flush() photo.create(form.data['title'], form.data['description'], slide_id, tmp.name) db.session.add(photo) db.session.commit() flash('Photo added to slide', 'success') return redirect(url_for('edit_slide', slide_id=slide_id)) flash_errors(form) return render_template('new-photo.html', form=form, title='New Photo')
def new_photo(slide_id): slide = Slide.query.filter_by(id=slide_id).first() if not slide: flash(BAD_KITTY, 'danger') return redirect(url_for('index')) journey = Journey.query.filter_by(id=slide.journey_id, user_id=current_user.id).first() if not journey: flash(BAD_KITTY, 'danger') return redirect(url_for('index')) form = NewPhotoForm(slide_id=slide_id) if form.validate_on_submit(): photo = Photo() tmp = tempfile.NamedTemporaryFile(suffix = '.jpg') form.photo.data.save(tmp) tmp.flush() photo.create(form.data['title'], form.data['description'], slide_id, tmp.name) db.session.add(photo) db.session.commit() flash('Photo added to slide', 'success') return redirect(url_for('edit_slide', slide_id=slide_id)) flash_errors(form) return render_template('new-photo.html', form=form, title='New Photo')
def upload(request): storage = Client(domain = settings.MOGILEFS_DOMAIN, trackers = settings.MOGILEFS_TRACKERS) photo_file = request.FILES.get('photo') photo_desc = request.POST.get('desc', '') if photo_file is None or not photo_file.name.endswith('.jpg') or \ photo_file.size > settings.MAX_PHOTO_SIZE: # Mamy błąd return HttpResponseRedirect('/') else: model = Photo.create(owner=request.user.id, desc=photo_desc) #with open(path.join(settings.UNPROCESSED_PHOTOS_DIR, '%s.jpg' % model.id), 'wb') as f: with storage.new_file(path.join(settings.UNPROCESSED_PHOTOS_DIR, '%s,%s.jpg' % (model.owner, model.nb))) as f: for chunk in photo_file.chunks(): f.write(chunk) f.close() prepare_photo_files.delay(model.owner, model.nb) return HttpResponseRedirect('/')
def gallery(): template = env.get_template('gallery_add.html') if request.method == 'GET': photos = Photo.select() return template.render(photos=photos) elif request.method == 'POST': photo_file = request.files.get('photo') file_ext = os.path.splitext(photo_file.filename)[1] gallery_folder = static_path('img/gallery/') f_name = generate_filename(prefix='photo', suffix=file_ext) file_path = os.path.join(gallery_folder, f_name) # photo_file.save('/img/gallery/') # new Bottle with open(file_path, 'wb') as open_file: open_file.write(photo_file.file.read()) photo = Photo.create(desc=post_get('desc'), photo=f_name) app.flash(u'Фото успішно додане') redirect('/gallery_add')
def upload(request): storage = Client(domain=settings.MOGILEFS_DOMAIN, trackers=settings.MOGILEFS_TRACKERS) photo_file = request.FILES.get('photo') photo_desc = request.POST.get('desc', '') if photo_file is None or not photo_file.name.endswith('.jpg') or \ photo_file.size > settings.MAX_PHOTO_SIZE: # Mamy błąd return HttpResponseRedirect('/') else: model = Photo.create(owner=request.user.id, desc=photo_desc) #with open(path.join(settings.UNPROCESSED_PHOTOS_DIR, '%s.jpg' % model.id), 'wb') as f: with storage.new_file( path.join(settings.UNPROCESSED_PHOTOS_DIR, '%s,%s.jpg' % (model.owner, model.nb))) as f: for chunk in photo_file.chunks(): f.write(chunk) f.close() prepare_photo_files.delay(model.owner, model.nb) return HttpResponseRedirect('/')