def movie(request, pk): if request.method == 'POST': movie = Movie.objects.get(pk=pk) form_obj = ReviewForm(request.POST) if form_obj.is_valid(): db_obj = form_obj.save(commit=False) db_obj.movie = movie if request.user.is_authenticated(): db_obj.added_by = request.user db_obj.save() pk_value = db_obj.pk return HttpResponseRedirect(reverse('movie_detail', args=(pk, ))) else: reviews = paginate_reviews(request, movie) return render(request, 'movie.html', { 'movie': movie, 'form': form, 'reviews': reviews }) elif request.method == 'GET': movie = Movie.objects.get(pk=pk) form = ReviewForm() reviews = paginate_reviews(request, movie) return render(request, 'movie.html', { 'movie': movie, 'form': form, 'reviews': reviews }) else: raise Http404("Request method type not defined")
def post(self, request, pk=None) : reviewer = get_object_or_404(Reviewer, reviewer=request.user) review = get_object_or_404(Review, review_id=pk, reviewer=reviewer) review_rate = float(review.stars) form = ReviewForm(request.POST, request.FILES or None, instance=review) if not form.is_valid() : ctx = {'form' : form} return render(request, self.template, ctx) review = form.save(commit=False) review.save() f = get_object_or_404(App, app_id=review.app.app_id) length = len(f.review_set.all()) f.rating = str((float(f.rating)*length-review_rate+float(review.stars))/length) f.save() return redirect(self.success_url)
def post(self, request, pk) : form = ReviewForm(request.POST, request.FILES or None) # if not form.is_valid() : # ctx = {'form' : form} # return render(request, self.template, ctx) # Add developer to the model before saving review = form.save(commit=False) f = get_object_or_404(App, app_id=pk) reviewer = get_object_or_404(Reviewer, reviewer=request.user) review.reviewer = reviewer review.app = f review.save() length = len(f.review_set.all()) f.rating = str((float(f.rating)*(length-1)+float(review.stars))/length) f.save() return redirect(reverse('app:app_detail', args=[pk]))
def review(request): review_form = ReviewForm() if request.method == "POST": review_form = ReviewForm(request.POST) if review_form.is_valid(): new_review = review_form.save(commit=False) new_review.user_name = request.user.username new_review.save() return redirect(review) SaitReviews = Review.objects.all() assert isinstance(request, HttpRequest) return render( request, 'app/review.html', { 'title': 'Отзыв', 'review_form': review_form, 'SaitReviews': SaitReviews, 'message': 'Если вам понравился сайт, вы можете оставить свой отзыв о проделанной мною работе', 'year': datetime.now().year, })
def hotel_detail(request, id, slug): template = 'app/listing-single.html' hotel = get_object_or_404(Hotel, id=id, slug=slug) # cities = get_object_or_404(Hotel, city=city) hotels = Hotel.published.all() # room = Room.objects.all() # rooms = room.filter(hotel=id) cart_hotel_form = CartAddHotelForm() posts = Post.published.order_by('-publish')[:3] reviews = hotel.reviews.filter(active=True) new_review = None total = Reviews.objects.all().filter(hotel=hotel) try: avg = { "over_all": 0, 'cleanliness': 0, 'comfort': 0, 'staff': 0, 'facilities': 0 } m = 0 for i in total: m += 1 avg['cleanliness'] += i.cleanliness avg['comfort'] += i.comfort avg['staff'] += i.staff avg['facilities'] += i.facilities avg['over_all'] += i.over_all avg = { "over_all": avg['over_all'] / m, 'cleanliness': avg['cleanliness'] / m, 'comfort': avg['comfort'] / m, 'staff': avg['staff'] / m, 'facilities': avg['facilities'] / m } avg_bar = { 'cleanliness': avg['cleanliness'] * 20, 'comfort': avg['comfort'] * 20, 'staff': avg['staff'] * 20, 'facilities': avg['facilities'] * 20 } except ZeroDivisionError as ze: avg = { "over_all": 0, 'cleanliness': 0, 'comfort': 0, 'staff': 0, 'facilities': 0 } avg_bar = { "over_all": 0, 'cleanliness': 0, 'comfort': 0, 'staff': 0, 'facilities': 0 } if request.method == 'POST': # A comment was posted review_form = ReviewForm(data=request.POST) if review_form.is_valid(): # Create Comment object but don't save to database yet new_review = review_form.save(commit=False) # Assign the current post to the comment new_review.hotel = hotel # Save the comment to the database new_review.save() else: review_form = ReviewForm() r = Recommender() recommended_hotels = r.suggest_hotels_for([hotel], 4) context = { 'hotel': hotel, "city_list": hotels, 'cart_hotel_form': cart_hotel_form, 'reviews': reviews, 'new_review': new_review, 'review_form': review_form, 'avg': avg, 'avg_bar': avg_bar, 'recommended_hotels': recommended_hotels, "posts": posts } return render(request, template, context)
def get_object(self, *args, **kwargs): request = self.request pk = self.kwargs.get('pk') slug = self.kwargs.get('slug') instance = Hotel.published.get_by_id(pk, slug) instance['reviews'] = instance.reviews.filter(active=True) instance['new_review'] = None total = Reviews.objects.all().filter(pk) try: avg = { "over_all": 0, 'cleanliness': 0, 'comfort': 0, 'staff': 0, 'facilities': 0 } m = 0 for i in total: m += 1 avg['cleanliness'] += i.cleanliness avg['comfort'] += i.comfort avg['staff'] += i.staff avg['facilities'] += i.facilities avg['over_all'] += i.over_all avg = { "over_all": avg['over_all'] / m, 'cleanliness': avg['cleanliness'] / m, 'comfort': avg['comfort'] / m, 'staff': avg['staff'] / m, 'facilities': avg['facilities'] / m } instance['avg_bar'] = { 'cleanliness': avg['cleanliness'] * 20, 'comfort': avg['comfort'] * 20, 'staff': avg['staff'] * 20, 'facilities': avg['facilities'] * 20 } except ZeroDivisionError as ze: instance['avg'] = { "over_all": 0, 'cleanliness': 0, 'comfort': 0, 'staff': 0, 'facilities': 0 } instance['avg_bar'] = { "over_all": 0, 'cleanliness': 0, 'comfort': 0, 'staff': 0, 'facilities': 0 } if request.method == 'POST': # A comment was posted review_form = ReviewForm(data=request.POST) if review_form.is_valid(): # Create Comment object but don't save to database yet new_review = review_form.save(commit=False) # Assign the current post to the comment new_review.hotel = instance # Save the comment to the database new_review.save() else: instance['review_form'] = ReviewForm() if instance is None: raise Http404("Hotel doesn't exist") return instance