def testPicture(activeClient): # named tuples to mock form form = namedtuple('form', ['picture', 'deletePictures']) picture = namedtuple('picture', 'data') filename = "test1.jpg" p = os.path.join(current_app.config['MEDIA_FOLDER'], 'source', filename) f = open(p, 'rb') fs = FileStorage(stream=f, filename=filename, content_type='image/jpeg') testform = form(picture([fs]), None) pic = Picture.savePictures(testform)[0] pic.review_id = 1 db.session.add(pic) db.session.commit() path = Path( os.path.join( current_app.config['MEDIA_FOLDER'], str(current_user.id), f'{current_user.username}_1.jpg' ) ) yield path try: rmtree(path.parent) except FileNotFoundError as e: print(e) pass
def review(): if request.method == 'GET': if len(request.args) != 0: form = ReviewForm(request.args) else: flash("Invalid request. Please search for provider first and then" " add review.") return redirect(url_for('main.index')) elif request.method == 'POST': form = ReviewForm() provider = Provider.query.get(form.id.data) form.category.choices = [(c.id, c.name) for c in provider.categories] if form.validate_on_submit(): pictures = Picture.savePictures(form) Review.create(user_id=current_user.id, provider_id=form.id.data, category_id=form.category.data, rating=form.rating.data, cost=form.cost.data, price_paid=form.price_paid.data, description=form.description.data, service_date=form.service_date.data, comments=form.comments.data, pictures=pictures) flash("review added") return redirect(url_for('main.index')) elif request.method == "POST" and not form.validate(): return render_template("review.html", title="Review", form=form), 422 if not current_user.email_verified: disableForm(form) flash("Form disabled. Please verify email to unlock.") return render_template("review.html", title="Review", form=form)
def reviewEdit(id): review = Review.query.get(id) if request.method == 'GET': session['referrer'] = request.referrer form = ReviewEditForm(id=review.id, name=review.provider.name, category_id=review.category_id, rating=review.rating, cost=review.cost, price_paid=review.price_paid, description=review.description, service_date=review.service_date, comments=review.comments) form.populate_choices(review) return render_template("review_edit.html", title="Review - Edit", form=form, review=review) else: form = ReviewEditForm() form.populate_choices(review) if form.validate_on_submit(): if form.deletePictures.data is not None: try: Picture.deletePictures(form) except FileNotFoundError as e: flash(str(e)) if form.picture.data: new_pictures = Picture.savePictures(form, review.pictures) try: review.update(category_id=form.category.data, rating=form.rating.data, cost=form.cost.data, price_paid=form.price_paid.data, description=form.description.data, service_date=form.service_date.data, comments=form.comments.data, pictures=new_pictures) flash("Review updated.") except SQLAlchemyError as e: flash(str(e)) return render_template("review_edit.html", title="Review - Edit", form=form), 422 try: url = session['referrer'] session.pop('referrer') except KeyError: url = url_for('main.user', username=current_user.username) finally: return redirect(url) flash("Please correct form errors.") return render_template("review_edit.html", title="Review - Edit", form=form), 422
def test_savePictures(self, activeClient): filename = "test.jpg" path = os.path.join(current_app.config['MEDIA_FOLDER'], 'source', filename) f = open(path, 'rb') fs = FileStorage(stream=f, filename=filename, content_type='image/jpeg') testform = form(picture([fs]), None) try: Picture.savePictures(testform) path = Path( os.path.join(current_app.config['MEDIA_FOLDER'], str(current_user.id), filename)) check_path = Path( os.path.join(current_app.config['MEDIA_FOLDER'], str(current_user.id), f'{current_user.username}_1.jpg')) assert check_path.is_file() finally: path = os.path.join(current_app.config['MEDIA_FOLDER'], str(current_user.id)) rmtree(path)