def add_comment(trail_id): user = flask_login.current_user profile = Hiker.objects.get({'_id': user.id}) current_trail = Trails.objects.get({'_id': ObjectId(trail_id)}) form = CommentsForm() if form.validate_on_submit(): # sightings = request.form.getlist('sightings') # this line makes test_comments.py work sightings = [objects['tag'] for objects in form.sightings.data] comment = Comment( author=profile, date_comment=datetime.datetime.now(), body=form.body.data, sightings=sightings, date_started=form.date_started.data, ratings=form.ratings.data, hours_taken=form.hours_taken.data, minutes_taken=form.minutes_taken.data ) current_trail.comments.append(comment) comment_date = comment.date_comment.strftime("%b, %d %Y, %H:%M") try: current_trail.save() flash(f"New comments by '{comment}', on '{comment_date}'" f'have been added.', 'teal') return redirect(url_for('get_trail', trail_id=trail_id)) except ValidationError as ve: current_trail.comments.pop() print(ve) print(ve.message) flash(ve, 'deep-orange darken-3') return render_template('trails/new_comments.template.html', form=form, current_trail=current_trail)
def edit_comment(trail_id, n): user = flask_login.current_user current_trail = Trails.objects.get({'_id': ObjectId(trail_id)}) comment_to_edit = current_trail.comments[n] trail_name = current_trail.trail_name if comment_to_edit.author._id != user.id: error_msg = "You're not authorized to edit this comment" flash(error_msg, 'deep-orange darken-3') return redirect(url_for('get_trail', trail_id=trail_id)) form = CommentsForm(obj=comment_to_edit) if form.validate_on_submit(): sightings = [objects['tag'] for objects in form.sightings.data] try: Trails.objects.raw({'_id': ObjectId(trail_id)}).update( {"$set": { "comments."+str(n)+".date_comment": datetime.datetime.now(), "comments."+str(n)+".body": form.body.data, "comments."+str(n)+".sightings": sightings, "comments."+str(n)+".date_started": datetime.datetime.combine(form.date_started.data, datetime.time()), "comments."+str(n)+".ratings": form.ratings.data, "comments."+str(n)+".hours_taken": form.hours_taken.data, "comments."+str(n)+".minutes_taken": form.minutes_taken.data } }) comment_date = datetime.datetime.now().strftime("%b, %d %Y, %H:%M") flash("Comments have been editted by" f"'{comment_to_edit.author}', on '{comment_date}'.", 'teal') except ValidationError as ve: current_trail.comments.pop() comment_errors = ve.message['comments'][-1] flash(comment_errors, 'deep-orange darken-3') return redirect(url_for('get_trail', trail_id=trail_id)) return render_template('trails/edit_comments.template.html', form=form, comment=comment_to_edit, n=n, trail_id=trail_id, trailName=trail_name)
def get_recipe_database(id): """Return recipe Information--ingredients,nutritional data, directions etc""" recipe = Recipe.query.get(id) comment_form = CommentsForm() if not recipe: flash(f"Recipe Not Found...") return redirect('/') if comment_form.validate_on_submit() and CURR_USER_KEY in session: comment = Comment(comment=comment_form.comment.data, user_id=g.user.id, recipe_id=id) db.session.add(comment) db.session.commit() return redirect(f'/api/get-recipe-database/{id}') comments = recipe.comments users, times = u_util.get_users(recipe) avg_rate = r_util.calculate_average_rating(id, recipe.average_rate) user_rating = rec_util.is_empty_query(id) percentages = r_util.calculate_percentages_stars(id) nutrients, vitamins = rec_util.get_nutrients_recipe( recipe, recipe.servings) nutrients = n_util.sort_nutrients(nutrients) fats, carbs, no_daily = n_util.get_fats_carbs() calories = math.ceil(recipe.calories / recipe.servings) return render_template("/recipes/recipe-check.html", recipe=recipe, nutrients=nutrients, vitamins=vitamins, fats=fats, carbs=carbs, calories=calories, no_daily=no_daily, user_rating=user_rating, percentages=percentages, comment_form=comment_form, comments=comments, users=users, times=times)