def review(product_id): item = db.session.query(models.Items) \ .filter(models.Items.product_id == product_id).first() reviews = db.session.query(models.Reviews) \ .filter(models.Reviews.product_id == product_id) \ .group_by(models.Reviews.review_id).all() form = forms.ReviewFormFactory.form() if form.validate_on_submit(): num_reviews = len( [r for r in reviews if r.buyer_username == current_user.username]) num_purchases = len( db.session.query(models.Orders, models.inorder).filter( models.Orders.buyer_username == current_user.username).filter( models.Orders.order_id == models.inorder.order_id).filter( models.inorder.product_id == product_id).all()) if num_reviews >= num_purchases: flash('You can only review an item once for each purchase') return redirect(url_for('review', product_id=product_id)) try: new_review = models.Reviews() new_review.item_rating = form.item_rating.data new_review.comments = form.comments.data new_review.product_id = product_id new_review.seller_username = item.seller_username new_review.buyer_username = current_user.username print(new_review) db.session.add(new_review) db.session.commit() db.session.close() return redirect(url_for('review', product_id=product_id)) except BaseException as e: form.errors['database'] = str(e) avg_rating = 0 if len(reviews): for review in reviews: avg_rating += review.item_rating avg_rating /= len(reviews) avg_rating = round(avg_rating, 2) return render_template('reviews.html', item=item, reviews=reviews, avg_rating=avg_rating, form=form)
def review(resource_id): resource = db.session.query(models.Resources) \ .filter(models.Resources.resource_id == resource_id).first() reviews = db.session.query(models.Reviews) \ .filter(models.Reviews.resource_id == resource_id) \ .group_by(models.Reviews.review_id).all() form = forms.ReviewFormFactory.form() if form.validate_on_submit(): num_reviews = len([r for r in reviews if r.teacher_id == current_user.username]) if num_reviews > 1: flash('You can only review a resource once') return redirect(url_for('review', resource_id=resource_id)) if current_user.username == resource.teacher_id: flash('You cannot review your own item!') return redirect(url_for('review', resource_id=resource_id)) try: new_review = models.Reviews() new_review.item_rating = form.item_rating.data new_review.comments = form.comments.data new_review.resource_id = resource_id new_review.teacher_id = resource.teacher_id new_review.reviewer_username = current_user.username print(new_review) db.session.add(new_review) db.session.commit() db.session.close() return redirect(url_for('review', resource_id=resource_id)) except BaseException as e: form.errors['database'] = str(e) avg_rating = 0 if len(reviews): for review in reviews: avg_rating += review.item_rating avg_rating /= len(reviews) avg_rating = round(avg_rating, 2) return render_template('reviews.html', resource=resource, reviews=reviews, avg_rating=avg_rating, form=form)
def post(self): now = datetime.now() date = now.strftime("%d/%m/%Y %H:%M:%S") name = request.form.get('name') if name == '': name = 'Anonymous' reviews = request.form.get('reviews') if len(reviews) <= 3: review_obj = mds.Reviews.query.all() reviews_list = [] for review in review_obj: temp = [] temp.append(review.name) temp.append(review.date) temp.append(review.reviews) reviews_list.append(temp) msg = 'Review should have more than 3 letters.' return make_response( render_template("reviews.html", msg=msg, reviews_list=reviews_list), 200) # add food to database review_query = mds.Reviews(name=name, date=date, reviews=reviews) db.session.add(review_query) db.session.commit() db.session.close() msg = 'Thank you for your feedback.' review_obj = mds.Reviews.query.all() reviews_list = [] for review in review_obj: temp = [] temp.append(review.name) temp.append(review.date) temp.append(review.reviews) reviews_list.append(temp) return make_response( render_template("reviews.html", msg=msg, reviews_list=reviews_list), 200)
def parseUserReviews(g, html, user): soup = BeautifulSoup(html, 'html.parser') reviews = [] books = [] authors = [] table = soup.find('table', attrs={'id': 'books'}) table_body = table.find('tbody') rows = table_body.find_all('tr') for row in rows: review_id = int(re.search(r'\d+', row.get('id')).group()) div_title = row.find('td', attrs={ 'class': 'title' }).find('div', attrs={'class': 'value'}) title = div_title.getText().replace(" ", "") book_id = int( re.search(r'\d+', div_title.find('a').get('href')).group()) author_name_forname = row.find('td', attrs={ 'class': 'field author' }).find('a').text.replace(' ', '').split(',') if (len(author_name_forname) > 1): author_name = author_name_forname[1] + " " + author_name_forname[0] else: author_name = author_name_forname[ 0] #TODO give two colum for name and forname author_id = int( re.search( r'\d+', row.find('td', attrs={ 'class': 'field author' }).find('a').get('href')).group()) isbn = row.find('td', attrs={ 'class': 'field isbn' }).find('div', attrs={ 'class': 'value' }).text.replace(" ", "") isbn13 = row.find('td', attrs={ 'class': 'field isbn13' }).find('div', attrs={ 'class': 'value' }).text.replace(" ", "") avg_rating = float( row.find('td', attrs={ 'class': 'field avg_rating' }).find('div', attrs={ 'class': 'value' }).text.replace(" ", "")) num_ratings = int( row.find('td', attrs={ 'class': 'field num_ratings' }).find('div', attrs={ 'class': 'value' }).text.replace(",", "")) date_pub_string = row.find('td', attrs={ 'class': 'field date_added' }).find('div', attrs={ 'class': 'value' }).text.replace(" ", "") date_pub = stringToDatetime(date_pub_string) field_rating = row.find('td', attrs={'class': 'field rating'}) a_staticStars = field_rating.find('a', attrs={'class': 'staticStars'}) rating = None if (a_staticStars is None): stars = field_rating.find('div', attrs={'class': 'stars'}) if (stars is None): p10 = field_rating.findAll('span', attrs={'class': 'p10'}) rating = len(p10) else: rating = int(stars.get('data-rating')) else: #rating=int(a_staticStars['class'][2][-1]) rating = len(a_staticStars.find('span', attrs={'class': 'p10'})) if (rating < 3): continue spans = row.find('td', attrs={'class': 'review'}).findAll('span') review = '' if (len(spans) > 1): review = ascii(spans[1].getText()) elif (len(spans) > 0): review = ascii(spans[0].getText()) else: review = None #Author authorTable = models.Authors(id=uuid.uuid4(), gid=author_id, name=ascii(author_name)) authors.append(authorTable) #Book bookTable = models.Books(id=uuid.uuid4(), gid=book_id, title=ascii(title), isbn=ascii(isbn), isbn13=ascii(isbn13), publication_date=date_pub, average_rating=avg_rating, ratings_count=num_ratings) books.append(bookTable) #Review textblob = None if (review is not None): blob = TextBlob(ascii(review)) # tags=[] # for tag in blob.tags: # tags.append(models.tb_tag(word=re.sub(r'[^\x00-\x7F]+',' ', tag[0]), type=re.sub(r'[^\x00-\x7F]+',' ', tag[0]))) #noun_phrases=[] #for noun_phrase in blob.noun_phrases: # noun_phrases.append(re.sub(r'[^\x00-\x7F]+',' ', noun_phrase)) sentences = [] for sentence in blob.sentences: if (sentence.string is None): continue sentences.append( models.tb_sentence( sentence_text=ascii(sentence.string), sentence_polarity=sentence.polarity, sentence_subjectivity=sentence.subjectivity)) textblob = models.textblob(instantiated=True, sentences=sentences) else: textblob = models.textblob(instantiated=False) reviewTable = models.Reviews(id=uuid.uuid4(), actor=user, gid=review_id, rating=rating, text=review, textblob=textblob) reviews.append(reviewTable) return (reviews, books, authors)