From 82c5fb7159cbb0902797d438b23fdf25aa60e13b Mon Sep 17 00:00:00 2001 From: Anish Singh Date: Sat, 21 Jul 2018 15:35:49 +0530 Subject: [PATCH 1/5] Rev remaned --- Lecture_10/zomato/templates/main/restaurant.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lecture_10/zomato/templates/main/restaurant.html b/Lecture_10/zomato/templates/main/restaurant.html index 718e1f9..fe29443 100644 --- a/Lecture_10/zomato/templates/main/restaurant.html +++ b/Lecture_10/zomato/templates/main/restaurant.html @@ -22,7 +22,7 @@

Reviews

{% if success %} -

Restaurant review Added

+

Review Added

{% endif %}

Add Review

From 39a49702b400d076bb3b04988067878832c70b14 Mon Sep 17 00:00:00 2001 From: Anish Singh Date: Sat, 21 Jul 2018 20:18:24 +0530 Subject: [PATCH 2/5] 3 Tasks Completed --- Lecture_10/zomato/db.sqlite3 | Bin 143360 -> 143360 bytes Lecture_10/zomato/main/models.py | 52 +++++++------- Lecture_10/zomato/main/urls.py | 4 +- Lecture_10/zomato/main/views.py | 58 +++++++++------ Lecture_10/zomato/templates/main/base.html | 66 ++++++++++++++---- Lecture_10/zomato/templates/main/index.html | 23 +++--- .../zomato/templates/main/restaurant.html | 17 ++--- .../zomato/templates/main/restaurants.html | 30 +++++--- Lecture_10/zomato/zomato/settings.py | 2 +- 9 files changed, 163 insertions(+), 89 deletions(-) diff --git a/Lecture_10/zomato/db.sqlite3 b/Lecture_10/zomato/db.sqlite3 index d38ab2b110a4438d4a10db94cff87239904d457e..9e693fcd03604de62d61e34bce8e6f08e6753e6d 100644 GIT binary patch delta 113 zcmZp8z|ru4V}dlJ)kGO*Rx1WQi4z-B9Q7GlnwRNsU#8C(cY%k6Ux9&Nfxm}8mjBvj zMFj!==?eTzD+~p-O=V@BGIMfD6#^2AHCWgf|Kw+4R{#J?Iv`O1 delta 85 zcmV-b0IL6h;0S==2#^~AIguPg1vvmN6ydRCK`#LVgQhRHrY`|r&WS*LTPMlbs$h-b97-KNOg3VIS&FPrVs)kGxZn3 diff --git a/Lecture_10/zomato/main/models.py b/Lecture_10/zomato/main/models.py index 999699b..1859391 100644 --- a/Lecture_10/zomato/main/models.py +++ b/Lecture_10/zomato/main/models.py @@ -1,33 +1,33 @@ from django.db import models -from django.db.models import Avg -from django.core.validators import MaxValueValidator, MinValueValidator +from django.core.validators import MaxValueValidator , MinValueValidator -# Create your models here. -class Restaurant(models.Model): - name = models.CharField(max_length = 100) - address = models.TextField(max_length = 512) - open_time = models.TimeField() - close_time = models.TimeField() - - def get_rating(self): - return self.review_set.aggregate(Avg('rating'))['rating__avg'] - def __str__(self): - return self.name +class Restaurant(models.Model): + name = models.CharField(max_length = 100) + address = models.TextField(max_length = 512) + open_time = models.TimeField() + close_time = models.TimeField() + def get_data(self): + return { + 'Name': self.name, + 'Address': self.address, + 'Open Time': self.open_time.strftime('%I:%M %p'), + 'Close Time': self.close_time.strftime('%I:%M %p'), + 'Average Rating': self.review_set.aggregate(models.Avg('rating'))['rating__avg'], + } + def __str__(self): + return self.name class Review(models.Model): - restaurant = models.ForeignKey(Restaurant, on_delete = models.CASCADE) - author = models.CharField(max_length = 100) - title = models.CharField(max_length = 100) - content = models.TextField(max_length=1024) - rating = models.IntegerField(validators=[ - MaxValueValidator(5), - MinValueValidator(0) - ]) - - def get_stars(self): - return "* "*self.rating + restaurant = models.ForeignKey(Restaurant, on_delete = models.CASCADE) + author = models.CharField(max_length = 100) + title = models.CharField(max_length = 100) + content = models.TextField(max_length=1024) + rating = models.IntegerField(validators=[ + MaxValueValidator(5), + MinValueValidator(0) + ]) + def __str__(self): + return self.title - def __str__(self): - return self.title \ No newline at end of file diff --git a/Lecture_10/zomato/main/urls.py b/Lecture_10/zomato/main/urls.py index 4e188ba..ec8b1ea 100644 --- a/Lecture_10/zomato/main/urls.py +++ b/Lecture_10/zomato/main/urls.py @@ -3,8 +3,8 @@ urlpatterns = [ path('', views.index, name = 'index'), - path('restaurants/', views.restaurants, name = 'restaurants'), - path('addRestaurants/', views.add_restraunt, name = 'addRestaurants'), + path('restaurants/', views.Restaurants, name = 'restaurants'), + path('addRestaurants/', views.add_rest, name = 'addRestaurants'), path('restaurant/', views.restaurant, name = 'restaurant'), path('review/', views.review, name = 'review') ] \ No newline at end of file diff --git a/Lecture_10/zomato/main/views.py b/Lecture_10/zomato/main/views.py index 879b96d..5e0a7ee 100644 --- a/Lecture_10/zomato/main/views.py +++ b/Lecture_10/zomato/main/views.py @@ -1,31 +1,41 @@ -from django.http import HttpResponse, Http404 -from django.shortcuts import render, get_object_or_404 -from django.db.models import Avg - +from django.shortcuts import render , get_object_or_404 +from django.http import HttpResponse from . import models +from django.db.models import Avg from . import forms -# Create your views here. + def index(request): - context = {} - return render(request, 'main/index.html', context) + context={} + return render(request,r'main\index.html',context) -def restaurants(request): - query_set = models.Restaurant.objects.all() +def Restaurants(request): + qs=models.Restaurant.objects.all() + + f=request.GET['filter'] + r=request.GET['reverse'] - query_set = query_set.annotate(average_rating = Avg('review__rating')).order_by('-average_rating') + if f=='a2z' and r=='0': + qs=qs.order_by('name') + elif f=='a2z' and r=='1': + qs=qs.order_by('-name') + elif f=='rating' and r=='0': + qs=qs.annotate(average_rating=Avg('review__rating')).order_by('average_rating') + elif f=='rating' and r=='1': + qs=qs.annotate(average_rating=Avg('review__rating')).order_by('-average_rating') - print(request.GET) - context = { - "query_set": query_set, + print(request.GET) + context={ + 'qs' :qs, + 'r':r, + 'f':f } - return render(request, 'main/restaurants.html', context) - -def add_restraunt(request): + return render(request,r'main\restaurants.html',context ) +def add_rest(request): if request.method == "GET": - form = forms.RestaurantForm() - else: # POST request + form = forms.RestaurantForm() + else: form = forms.RestaurantForm(request.POST) if form.is_valid(): @@ -35,12 +45,13 @@ def add_restraunt(request): context = { 'form': form } - return render(request, 'main/addRestaurant.html', context) + return render(request, r'main/addRestaurant.html', context) def restaurant(request, id): + rest = get_object_or_404(models.Restaurant, pk = id) success = False - + # Handling the form if request.method == "GET": form = forms.ReviewForm() @@ -58,7 +69,7 @@ def restaurant(request, id): 'form': form, 'success': success } - return render(request, 'main/restaurant.html', context) + return render(request, r'main\restaurant.html', context) def review(request, id): obj = get_object_or_404(models.Review, pk = id) @@ -66,4 +77,7 @@ def review(request, id): context = { 'review': obj } - return render(request, 'main/review.html', context) \ No newline at end of file + return render(request, r'main/review.html', context) + + + diff --git a/Lecture_10/zomato/templates/main/base.html b/Lecture_10/zomato/templates/main/base.html index 2ac247b..07cad07 100644 --- a/Lecture_10/zomato/templates/main/base.html +++ b/Lecture_10/zomato/templates/main/base.html @@ -1,17 +1,55 @@ - - - - Zomato Application - - - - {% block head %} {% endblock %} - - - - {% block body %} {% endblock %} - - + + + + + Zomato Application + + + + + + {% block body %} {% endblock %} + \ No newline at end of file diff --git a/Lecture_10/zomato/templates/main/index.html b/Lecture_10/zomato/templates/main/index.html index e152754..09fd03a 100644 --- a/Lecture_10/zomato/templates/main/index.html +++ b/Lecture_10/zomato/templates/main/index.html @@ -2,13 +2,20 @@ {% block body %} -

Welcome to our zomato app

- -

Navigation

- - +

Welcome to our zomato app

+

+Navigation +

+ {% endblock %} \ No newline at end of file diff --git a/Lecture_10/zomato/templates/main/restaurant.html b/Lecture_10/zomato/templates/main/restaurant.html index fe29443..89cbf74 100644 --- a/Lecture_10/zomato/templates/main/restaurant.html +++ b/Lecture_10/zomato/templates/main/restaurant.html @@ -2,12 +2,9 @@ {% block body %} - Name: {{ restaurant.name }}
- Address: {{ restaurant.address }}
- Open Time: {{ restaurant.open_time }}
- Close Time: {{ restaurant.close_time }}
- - Average Rating: {{ restaurant.get_rating }} + {% for f,v in restaurant.get_data.items %} + {{ f }}: {{v}}
+ {% endfor %}

Reviews

@@ -15,14 +12,18 @@

Reviews

{% for review in restaurant.review_set.all %} -
  • {{ review.title }} - {{ review.get_stars }}
  • +
  • {{ review.title }} - {{ review.get_stars }}
  • {% endfor %} {% if success %} -

    Review Added

    +

    Restaurant review Added

    {% endif %}

    Add Review

    diff --git a/Lecture_10/zomato/templates/main/restaurants.html b/Lecture_10/zomato/templates/main/restaurants.html index 191f8c2..73ae7a0 100644 --- a/Lecture_10/zomato/templates/main/restaurants.html +++ b/Lecture_10/zomato/templates/main/restaurants.html @@ -2,18 +2,32 @@ {% block body %} -

    Restaurants

    +

    Restaurants

    + +