Exemple #1
0
def restaurant_details(request, res_id):
    form = ReviewForm(request.POST or None)
    if form.is_valid():
        star_count = form.cleaned_data.get('star_count')
        description = form.cleaned_data.get('description')
        reveiw, created = Review.objects.update_or_create(user=request.user,
                                                          res_id=res_id,
                                                          defaults={
                                                              'star_count':
                                                              star_count,
                                                              'description':
                                                              description,
                                                          })
        if created:
            messages.success(request, 'Review Published Successfully')
        else:
            messages.success(request, 'Review Updated')
        return redirect('/restaurant/{0}'.format(str(res_id)))
    url = 'https://developers.zomato.com/api/v2.1/restaurant?res_id={0}'.format(
        str(res_id))
    header = {
        "User-agent": "curl/7.43.0",
        "Accept": "application/json",
        "user_key": "cd9ca69cad612fbd6b4cb9fe9d503906"
    }
    response = requests.get(url, headers=header)
    res_obj = response.json()
    context = {'res_data': res_obj, 'form': form, "url": config.url}
    get_review(context, res_id)
    return render(request, 'restaurant.html', context)
class CreateReviewView(TemplateView):

    """Page for creating a new restaurant review"""

    template_name = 'create_review.html'

    def get(self, request, pk):
        self.restaurant = get_object_or_404(Restaurant, pk=pk)
        self.form = ReviewForm()
        return super(CreateReviewView, self).get(request, pk)

    def post(self, request, pk):
        self.restaurant = get_object_or_404(Restaurant, pk=pk)
        self.form = ReviewForm(request.POST)
        if self.form.is_valid():
            review = self.form.save(commit=False)
            review.restaurant = self.restaurant
            review.save()
            messages.info(request, "Thank you for your review.")
            return redirect(self.restaurant)
        return self.render_to_response(self.get_context_data(request, pk))

    def get_context_data(self, *args, **kwargs):
        return {
            'restaurant': self.restaurant,
            'form': self.form
        }
 def post(self, request, pk):
     self.restaurant = get_object_or_404(Restaurant, pk=pk)
     self.form = ReviewForm(request.POST)
     if self.form.is_valid():
         review = self.form.save(commit=False)
         review.restaurant = self.restaurant
         review.save()
         messages.info(request, "Thank you for your review.")
         return redirect(self.restaurant)
     return self.render_to_response(self.get_context_data(request, pk))
 def get(self, request, pk):
     self.restaurant = get_object_or_404(Restaurant, pk=pk)
     self.form = ReviewForm()
     return super(CreateReviewView, self).get(request, pk)