def review(movieId): if current_user.is_anonymous: return redirect(url_for('login')) form = ReviewForm() if form.validate_on_submit(): if len(form.comment.data) == 0: rating = Reviews(user_id=current_user.id, movie_id=movieId, rating=form.rating.data) db.session.add(rating) db.session.commit() else: rating_comment = Reviews(user_id=current_user.id, movie_id=movieId, rating=form.rating.data, comment=form.comment.data) db.session.add(rating_comment) db.session.commit() movie = Movies.query.filter_by(movieId=movieId).first() comments = Reviews.query.filter_by(movie_id=movieId).join( Movies, Reviews.movie_id == Movies.movieId).join( User, Reviews.user_id == User.id).add_columns(Movies.title, Movies.movieId, Reviews.comment, User.username).all() return render_template('review.html', movie=movie, title='Review', form=form, comments=comments)
def set_review(request, CustomerUsername, BusinessUsername, review, stars): cid = User.objects.get(username=CustomerUsername).id bid = User.objects.get(username=BusinessUsername).id _review = Reviews(reviewer_id=cid, business_id=bid, review=review, stars=stars) _review.save() return True
def reviews(business_id): current_user = get_jwt_identity() if request.method == 'POST': review_data = request.get_json() title = review_data.get('title') description = review_data.get('description') new_review = Reviews(title, description) new_review.add_reviews() response = {'message': 'Review Posted', 'Review by': current_user} return make_response(jsonify(response)), 201 reviews = Reviews.get_all_reviews() return make_response(jsonify(reviews)), 200
def item_init(dic): i = 0 catz = [] for key in dic['name']: seller = Seller.query.filter_by( username=dic['merchant_id'][key]).first() cat = dic['amazon_category_and_sub_category'][key] if pd.isna(cat) is True: cat = 'Other' else: cats = cat.split(' > ') cat = cats[0] if cat not in catz: toAdd = Category(name=cat) db.session.add(toAdd) catz.append(cat) if not seller: toAdd = Seller(username=str(dic['merchant_id'][key]), email=str(dic['merchant_id'][key]) + str(i) + "@NULL") i += 1 toAdd.set_password('123') db.session.add(toAdd) item = Item(id=key, name=dic['name'][key], price=dic['price'][key], quantity=dic['quantity'][key], description=dic['description'][key], seller=Seller.query.filter_by( username=dic['merchant_id'][key]).first(), category=cat) date = '' + str(datetime.now().month) + '/' + str( datetime.now().day) + '/' + str(datetime.now().year) location = ['USA', 'Canada', 'United Kingdom', 'China', 'Japan'] stars = [1, 2, 3, 4, 5] user_id = [] for user in User.query.all(): user_id.append(user.id) for i in range(5): review = Reviews(user_id=random.choice(user_id), item_id=item.id, date_time=date, location=random.choice(location), stars=random.choice(stars), content=get_random_string(10)) db.session.add(review) avg_stars = db.session.query(func.avg( Reviews.stars)).filter(Reviews.item_id == item.id).first() item.avg_user_rating = avg_stars[0] db.session.add(item) #for name in catz: #_search_params = { #'q': name, #'num': 1, #"print_urls":True #} #print(name) #gis.search(search_params=_search_params, path_to_dir='Images', width=500, height=500, custom_image_name=str(name)) db.session.commit()
def add_review(id, name, date, location, stars, content): review = Reviews(user_id=current_user.id, item_id=id, date_time=date, location=location, stars=stars, content=content) db.session.add(review) db.session.commit() flash('Successfully added review for item {}'.format(name)) return redirect(url_for('item', id=id))
def reviews(): Cdata=csvRead() for i in range(1, len(Cdata)): user = Reviews(id=int(Cdata[i][0]), userProductid=int(Cdata[i][1]),productRating=int(Cdata[i][2]),reviewTitle=Cdata[i][3],reviewDetails=Cdata[i][4]) db.session.add(user) db.session.commit() return "Transefer to db"
def addToWatchlist(movieId, from_page): if current_user.is_anonymous: return redirect(url_for('login')) watch = Reviews(user_id=current_user.id, movie_id=movieId, watchlist=1) db.session.add(watch) db.session.commit() return redirect(url_for('search'))
def write_review(): new_review = ReviewCreation() if new_review.validate_on_submit(): review = Reviews(review=new_review.review.data, location=1, user='******') db.session.add(review) db.session.commit() return redirect('/') return render_template('writereview.html', form=new_review)
def vote_down(request,area_name,d_id): s = Ad.objects.get(id= d_id) print s r = Reviews.objects.filter(rev = s) for i in r : print i.voteup, i.votedown if not r: new_r = Reviews(rev=s , voteup = 0, votedown = 1) new_r.save() else : print r[0].votedown r[0].votedown = r[0].votedown+1 r[0].save() print r[0].votedown return HttpResponseRedirect(reverse('app.views.showdata', kwargs={"area_name":area_name}))
class ReivewsTestcase(unittest.TestCase): def setUp(self): Reviews.reviews = {} self.post = Reviews( "Very Tasy", "I really liked the place, its clean, nice ambience, great music") def test_add_reviews(self): #Before posting a review self.assertEqual(len(self.post.reviews), 0) self.assertEqual(self.post.review_id, 0) #After posting a review self.post.add_reviews() self.assertIsInstance(self.post, Reviews) self.assertEqual(len(self.post.reviews), 1) self.assertEqual(self.post.review_id, 1)
def get_reviews(business_id): business = Businesses.get_one(business_id) if business: allreviews = Reviews.get_reviews(business_id) obj = [allreview.serialize() for allreview in allreviews] if len(obj) > 0: return make_response(jsonify(obj)), 200 else: return jsonify( {'message': 'No reviews yet.Please review business'}), 404 else: return jsonify({'message': 'Resource(Business) Not Found'}), 404
def reviews(request): if request.method == "POST": if request.user.is_authenticated: content = request.POST.get('content') rating = int(request.POST.get('rating')) r = '' for i in range(1, rating + 1): r += str(i) user = request.user Reviews(user=user, content=content, rating=r).save() else: messages.info(request, 'Please Login First') return redirect('reviews') reviews = Reviews.objects.all() return render(request, 'reviews.html', {'reviews': reviews})
def reviews(business_id): current_user = get_jwt_identity() business = Businesses.get_one(business_id) if business: if current_user != business.owner_id: review_data = request.get_json() title = review_data.get('title') description = review_data.get('description') data = {"title": title, "description": description} if validate.inputs(data): return jsonify(validate.inputs(data)), 406 user_id = business.owner_id business_id = business.id new_review = Reviews(title, description, user_id, business_id) new_review.add_review() response = { 'message': 'Review Posted', } return make_response(jsonify(response)), 201 else: return jsonify({'message': 'Cannot Review your own Business'}), 401 else: return jsonify( {'message': 'Cannot Review. Resource(Business) Not Found'}), 404
def rate_mechanic(): form = ReviewMechanic() ratingScore = None if form.validate_on_submit(): ratings = Reviews.query.filter_by(mechanic=form.mechanic.data) for x in ratings: ratingScore += x.rating average = ratingScore / len(ratings) review = Reviews(mechanic=form.mechanic.data, comments=form.comments.data, rating=average) db.session.add(review) db.session.commit() return render_template('MechanicRating.html', title='Mechanic Dashboard', form=form)
def reviews(): form = ReviewsForm() t_reviews = Reviews.query.all() if request.method == 'POST': file = form.r_img.data filename = secure_filename(file.filename) file.save(os.path.join(app.config['UPLOAD_PATH'], filename)) reviews = Reviews(r_img=filename, r_name=form.r_name.data, r_company=form.r_company.data, r_icon=form.r_icon.data, r_subject=form.r_subject.data) db.session.add(reviews) db.session.commit() return redirect('/admin/reviews/') return render_template('admin/reviews.html', form=form, t_reviews=t_reviews)
def rate_mechanic(): form = ReviewMechanic() all_reviews = Reviews.query.order_by(Reviews.mechanic) all_averages = Mechanic_Ratings.query.all() if form.validate_on_submit(): total = 0 total_reviews = [] review_made = Reviews(mechanic=form.mechanic.data, comment=form.comments.data, rating=form.rating.data, user=current_user.user) db.session.add(review_made) for x in all_reviews: if x.mechanic == form.mechanic.data: total_reviews += [x.rating] for x in total_reviews: total += x avg = total / len(total_reviews) print(avg) if Mechanic_Ratings.query.first() is None: mechanic_reviews = Mechanic_Ratings(mechanic=form.mechanic.data, average=round(avg, 2)) db.session.add(mechanic_reviews) else: for j in all_averages: if Mechanic_Ratings.query.filter_by( mechanic=form.mechanic.data) is None: if j.mechanic == form.mechanic.data: print("2nd if") j.average = round(avg, 2) else: print("else") mechanic_reviews = Mechanic_Ratings( mechanic=form.mechanic.data, average=round(avg, 2)) db.session.add(mechanic_reviews) db.session.commit() return redirect(url_for('view_rating')) return render_template('MechanicRating.html', title='Review Mechanic', form=form)
def put_endpoint_specific_json(num): product_id = num product_obj_instance = Products.query.get_or_404(product_id) asin = product_obj_instance.asin title = request.json['title'] review = request.json['review'] product_ids = product_id new_review = Reviews(asin=asin, title=title, review=review, product_ids=product_ids) db.session.add(new_review) db.session.commit() new_review_obj_instance = Reviews.query.order_by(-Reviews.id).filter_by( product_ids=product_ids).first().as_dict() return jsonify(new_review_obj_instance)
def _insertReviewsRow(self, row): review_id = row.get("review_id") user_id = row.get("user_id") business_id = row.get("business_id") stars = row.get("stars") date = row.get("date") text = row.get("text") useful = row.get("useful") funny = row.get("funny") cool = row.get("cool") obj = Reviews(review_id=review_id, user_id=user_id, business_id=business_id, stars=stars, date=date, text=text, useful=useful, funny=funny, cool=cool) db.session.add(obj)
def admin_new(): form = NewForm() if form.validate_on_submit(): g_address = form.address.data.replace(" ", "+") g_city = form.city.data.replace(" ", "+") url = f"https://maps.googleapis.com/maps/api/geocode/json?address={g_address},+{g_city},+{form.state.data}&key={os.environ.get('API')}" r = requests.get(url) j = json.loads(r.text) latitude = j['results'][0]['geometry']['location']['lat'] longitude = j['results'][0]['geometry']['location']['lng'] overall_score = form.tortilla_score.data * 2.0 + form.potato_score.data * 2 + form.texture_score.data * 2 + form.flavor_score.data * 2 + form.general_score.data * 2 overall_score = int(overall_score) burrito = Reviews(overview=form.overview.data, user_id=current_user.id, overall_score=overall_score, lat=latitude, lng=longitude, date=form.date.data, restaurant_name=form.restaurant_name.data, address=form.address.data, city=form.city.data, state=form.state.data, zip_code=form.zip_code.data, tortilla_desc=form.tortilla_desc.data, tortilla_score=form.tortilla_score.data, potato_desc=form.potato_desc.data, potato_score=form.potato_score.data, texture_desc=form.texture_desc.data, texture_score=form.texture_score.data, flavor_desc=form.flavor_desc.data, flavor_score=form.flavor_score.data, general_desc=form.general_desc.data, general_score=form.general_score.data, smother=form.smother.data, smother_score=form.smother_score.data, published=form.published.data) db.session.add(burrito) db.session.commit() flash(f'Your post is now live!') return redirect(url_for('index')) return render_template('new_burrito.html', title='New Burrito', form=form)
def add_review_to_db(product_public_key, review_string, rating): """ Function to add the review to the database input : product public key, review and rating """ with app.app_context(): prod = Products.query.filter( Products.public_key == product_public_key).first() review = Reviews(product=prod, review_string=review_string, rating=rating) db.session.add(review) db.session.commit() app.logger.info( f"Review added to Database for product {prod.public_key}, review ID {review.id} ..." ) new_score_evaluated = check_threshold() message = 'Review added. ' + ('New rating score evaluated.' if new_score_evaluated else 'Rating score not evaluated.') app.logger.info(message) return message, new_score_evaluated
def add_review(session_obj, restaurant_id, restaurant_name, yelp_restaurant_id, food_type, food_type_disp, date, rating, username, review, profile_picture_url, review_url, zipcode): """ Adds a review with the provided variables to the provided database. """ # I realized that having zipcode and restaurant id is wasteful. new_review = Reviews(restaurant_id=restaurant_id, restaurant_name=restaurant_name, yelp_restaurant_id=yelp_restaurant_id, food_type=food_type, food_type_disp=food_type_disp, date=date, rating=rating, username=username, review=review, profile_picture_url=profile_picture_url, review_url=review_url, zipcode=zipcode) session_obj.add(new_review) session_obj.commit()
def setUp(self): Reviews.reviews = {} self.post = Reviews( "Very Tasy", "I really liked the place, its clean, nice ambience, great music")
def post(self, business_id): """Add a business review. --- tags: - business reviews parameters: - in: path name: business id required: true description: a unique business id schema: type: integer - in: header name: authorization description: JSON Web Token type: string required: true x-authentication: Bearer - in: body name: review required: true schema: required: - review properties: review: type: string description: Business review responses: 201: description: Review added schema: properties: response_message: type: string status_code: type: integer 404: description: Business is not registered schema: properties: response_message: type: string 500: description: Internal server error schema: properties: response_message: type: string """ request_data = request.get_json(force=True) business_review = request_data.get('review') created_by = get_jwt_identity() if not business_id: response = jsonify({ 'response_message': 'Business not registered!', 'status_code': 404 }) return response if not business_review: response = jsonify({ 'response_message': 'Review field is required!', 'status_code': 406 }) return response business = Business.query.filter_by(id=business_id).first() if business: try: reviewer_name = User.query.filter_by(id=created_by).first() review = Reviews(business_review, business_id, created_by) review.save() response = jsonify({ 'reviewed_by': reviewer_name.username, 'review': business_review }) return response except Exception as error: response = jsonify({ 'response_message': str(error), 'status_code': 500 }) return response else: response = jsonify({ 'response_message': 'Business not registered!', 'status_code': 404 }) return response
with app.app_context(): # Inital setup. Create DB and Add all products. db.create_all() for prod in all_products: p = Products(name=prod['name'], image_url=prod['image'], public_key=uuid.uuid4().hex) db.session.add(p) db.session.commit() print("All products are added.") # Creating all sample reviews. prods = Products.query.all() for p in prods: for i in range(random.randint(1, 9)): rat_rev = random.choice(sample_reviews) review = Reviews(product=p, review_string=rat_rev[0], rating=rat_rev[1], status="PROCESSED") db.session.add(review) db.session.commit() print("All reviews are added") print("Initial update of the rating score for each product") prod_ids = [p.id for p in prods] prod_overall_rating = review_evaluation(prod_ids) update_rating_score(prod_overall_rating) print("Rating score initalized")