Skip to content

Commit

Permalink
Merge pull request #106 from jeniaSakirko/add_avg_rating
Browse files Browse the repository at this point in the history
Add average rating to the daycare profile
  • Loading branch information
jeniaSakirko authored May 10, 2022
2 parents 467add9 + 82de0b2 commit c1051cb
Show file tree
Hide file tree
Showing 5 changed files with 157 additions and 119 deletions.
237 changes: 121 additions & 116 deletions daycare/templates/daycare/daycare-homepage.html
Original file line number Diff line number Diff line change
@@ -1,132 +1,137 @@
{% extends "main/base_template.html" %}

{% block stylesheets %}
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.7.0/css/all.css"
integrity="sha384-lZN37f5QGtY3VHgisS14W3ExzMWZxybE1SJSEsQp9S+oqd12jhcu+A56Ebc1zFSJ" crossorigin="anonymous">
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/jquery.slim.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/umd/popper.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"></script>
<style>
/* Make the image fully responsive */
.img {
float: left;
width: 100px;
height: 100px;
object-fit:cover;
}
</style>
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.7.0/css/all.css"
integrity="sha384-lZN37f5QGtY3VHgisS14W3ExzMWZxybE1SJSEsQp9S+oqd12jhcu+A56Ebc1zFSJ" crossorigin="anonymous">
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/jquery.slim.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/umd/popper.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"></script>
<style>
/* Make the image fully responsive */
.img {
float: left;
width: 100px;
height: 100px;
object-fit: cover;
}
</style>
{% endblock %}

{% block content %}
<div class="card mb-2">
<div class="row row-cols-md-2">
<div class="col-md-7">
<hr>
<div class="mt-5 mb-5">
<h5> <i class="fas fa-user"></i> Hello {{ daycare.name }}</h5>
<h6>This is where you can manage your daycare.
<br> You will be able:</h6>
<p class="text-muted"># Manage your booking schedule</p>
<p class="text-muted"># Set filters of approved dogs and services you provide</p>
<p class="text-muted"># Read / write reviews of dog owners and their dogs</p>
<p class="text-muted"># Chat with dog owners</p>
</div>
<hr>
<div class="mt-5">
<h5>Have a look at your reviews</h5>
<table class="table table-hover table-bordered table-responsive">
<thead class="table-dark">
<tr>
<th scope="col">#</th>
<th scope="col">Name</th>
<th scope="col">Date</th>
<th scope="col">Review</th>
<th scope="col">Rating</th>
</tr>
</thead>
<tbody>
{% for review in reviews %}
<tr>
<th scope="row">{{ forloop.counter }}</th>
<td>{{ review.dogowner_id }}</td>
<td>{{ review.creation_date }}</td>
<td>{{ review.review }}</td>
<td>{{ review.rating }}</td>
</tr>
{% endfor %}
</tbody>
</table>
<div class="card mb-2">
<div class="row row-cols-md-2">
<div class="col-md-7">
<hr>
<div class="mt-5 mb-5">
<h5><i class="fas fa-user"></i> Hello {{ daycare.name }}</h5>
<h6>This is where you can manage your daycare.
<br> You will be able:</h6>
<p class="text-muted"># Manage your booking schedule</p>
<p class="text-muted"># Set filters of approved dogs and services you provide</p>
<p class="text-muted"># Read / write reviews of dog owners and their dogs</p>
<p class="text-muted"># Chat with dog owners</p>
</div>
<hr>
<div class="mt-5">
<h5>Have a look at your reviews</h5>
<table class="table table-hover table-bordered table-responsive">
<thead class="table-dark">
<tr>
<th scope="col">#</th>
<th scope="col">Name</th>
<th scope="col">Date</th>
<th scope="col">Review</th>
<th scope="col">Rating</th>
</tr>
</thead>
<tbody>
{% for review in reviews %}
<tr>
<th scope="row">{{ forloop.counter }}</th>
<td>{{ review.dogowner_id }}</td>
<td>{{ review.creation_date }}</td>
<td>{{ review.review }}</td>
<td>{{ review.rating }}</td>
</tr>
{% endfor %}
</tbody>
</table>

</div>
</div>
<div class="col-md-5">
<div class="card h-100">
<ul class="nav nav-pills" id="myTab" role="tablist">
<li class="nav-item">
<a class="nav-link active" id="apt-tab-{{daycare.id}}" data-toggle="tab"
href="#apt-{{daycare.id}}" role="tab" aria-controls="apt-{{daycare.id}}"
aria-selected="true">Daycare Profile Details</a>
</li>
</ul>
</div>
</div>
<div class="col-md-5">
<div class="card h-100">
<ul class="nav nav-pills" id="myTab" role="tablist">
<li class="nav-item">
<a class="nav-link active" id="apt-tab-{{ daycare.id }}" data-toggle="tab"
href="#apt-{{ daycare.id }}" role="tab" aria-controls="apt-{{ daycare.id }}"
aria-selected="true">Daycare Profile Details</a>
</li>
</ul>

<div class="tab-content" id="card-tab-content">
<div class="tab-pane fade show active" id="apt-{{daycare.id}}" role="tabpanel"
aria-labelledby="apt-tab-{{daycare.id}}">
<div class="tab-content" id="card-tab-content">
<div class="tab-pane fade show active" id="apt-{{ daycare.id }}" role="tabpanel"
aria-labelledby="apt-tab-{{ daycare.id }}">

<div id="imageSlides" class="carousel slide" data-ride="carousel">
<!-- Indicators -->
<ul class="carousel-indicators">
{% for image in images %}
{% if forloop.counter == 1 %}
<li data-target="#imageSlides" data-slide-to={{image.id}} class="active"></li>
{% else %}
<li data-target="#imageSlides" data-slide-to={{image.id}}></li>
{% endif %}
{% endfor%}
</ul>
<div id="imageSlides" class="carousel slide" data-ride="carousel">
<!-- Indicators -->
<ul class="carousel-indicators">
{% for image in images %}
{% if forloop.counter == 1 %}
<li data-target="#imageSlides"
data-slide-to={{ image.id }} class="active"></li>
{% else %}
<li data-target="#imageSlides" data-slide-to={{ image.id }}></li>
{% endif %}
{% endfor %}
</ul>


<!-- The slideshow -->
<div class="carousel-inner">
{% for image in images %}
{% if forloop.counter == 1 %}
<div class="carousel-item active">
<img src={{image.url}} class="img-fluid" width="900" height="400">
</div>
{% else %}
<div class="carousel-item">
<img src={{image.url}} class="img-fluid" width="900" height="400">
</div>
{% endif %}
{% endfor %}
</div>
<!-- The slideshow -->
<div class="carousel-inner">
{% for image in images %}
{% if forloop.counter == 1 %}
<div class="carousel-item active">
<img src={{ image.url }} class="img-fluid" width="900" height="400">
</div>
{% else %}
<div class="carousel-item">
<img src={{ image.url }} class="img-fluid" width="900" height="400">
</div>
{% endif %}
{% endfor %}
</div>

<!-- Left and right controls -->
<a class="carousel-control-prev" href="#imageSlides" data-slide="prev">
<span class="carousel-control-prev-icon"></span>
</a>
<a class="carousel-control-next" href="#imageSlides" data-slide="next">
<span class="carousel-control-next-icon"></span>
</a>
</div>
<div class="card-body">
<h5 class="card-title">{{daycare.address}}, {{daycare.city}}</h5>
<br>
<h6><i class="fas fa-dog"></i> Capacity: {{daycare.capacity}}
<br>
<br>
<i class="fas fa-coins"></i> Price: {{daycare.price_per_day}}
</h6>
<br>
<h6><strong>About</strong></h6>
<p class="card-text">{{daycare.description}}</p>
</div>
<!-- Left and right controls -->
<a class="carousel-control-prev" href="#imageSlides" data-slide="prev">
<span class="carousel-control-prev-icon"></span>
</a>
<a class="carousel-control-next" href="#imageSlides" data-slide="next">
<span class="carousel-control-next-icon"></span>
</a>
</div>
<div class="card-body">
<h5 class="card-title">{{ daycare.address }}, {{ daycare.city }}</h5>
<br>
<h6>
<i class="fas fa-dog"></i> Capacity: {{ daycare.capacity }}
<br>
<br>
<i class="fas fa-coins"></i> Price: {{ daycare.price_per_day }}
<br>
<br>
<i class="fas fa-star"></i> Rating: {{ rating }}
</h6>
<br>
<h6><strong>About</strong></h6>
<p class="card-text">{{ daycare.description }}</p>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>

</div>
</div>
</div>

{% endblock %}
{% endblock %}
6 changes: 5 additions & 1 deletion daycare/test_daycare.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ def test_daycare_redirected_homepage_after_login(self, client, create_daycare_us
assert response.status_code == 302
assert response['Location'] == '/homepage/'

def test_relevant_daycare_homepage_with_static_data(self, client, create_daycare_user):
def test_relevant_daycare_homepage_with_static_data(self, client):
dayCare = random.choice([daycare for daycare in DayCare.objects.all() if 'static' in daycare.user.username])
client.force_login(user=dayCare.user)

Expand All @@ -87,3 +87,7 @@ def test_relevant_daycare_homepage_with_static_data(self, client, create_daycare
reviews_shown_in_homepage = set(response.context['reviews'])
daycare_reviews = set(Review.get_review_by_daycare_id(daycare_id=dayCare.id))
assert reviews_shown_in_homepage == daycare_reviews

average_rating_in_homepage = response.context['rating']
average_rating = Review.get_average_rating_by_daycare_id(daycare_id=dayCare.id)
assert average_rating_in_homepage == average_rating
4 changes: 2 additions & 2 deletions daycare/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def daycare_home(request):
context = {
'daycare': request.user.daycare,
'reviews': Review.get_review_by_daycare_id(request.user.daycare.id),
'images': Image.get_images_by_daycare_id(request.user.daycare.id)
'images': Image.get_images_by_daycare_id(request.user.daycare.id),
'rating': Review.get_average_rating_by_daycare_id(request.user.daycare.id)
}

return render(request, 'daycare/daycare-homepage.html', context)
7 changes: 7 additions & 0 deletions review/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,10 @@ def get_review_by_daycare_id(daycare_id):
return Review.objects.filter(daycare_id=daycare_id)
except ObjectDoesNotExist:
return None

@staticmethod
def get_average_rating_by_daycare_id(daycare_id):
reviews = Review.get_review_by_daycare_id(daycare_id)
if reviews.exists():
return float("{:.2f}".format(reviews.aggregate(models.Avg('rating'))['rating__avg']))
return 0
22 changes: 22 additions & 0 deletions review/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,25 @@ def test_get_review_by_daycare_id(self, number_of_reviews, create_dog_owner_user
assert len(reviews_by_id_list) == number_of_reviews
for position, review in enumerate(reviews_by_id_list):
assert review == review_list[position]

@pytest.mark.parametrize('daycare_id, expected_rating', [(1, 2), (2, 4.67)])
def test_get_average_rating_for_test_data(self, daycare_id, expected_rating):
average_rating = Review.get_average_rating_by_daycare_id(daycare_id)

assert expected_rating == average_rating

def test_get_average_rating_for_invalid_daycare(self):
average_rating = Review.get_average_rating_by_daycare_id(-1)

assert 0 == average_rating

@pytest.mark.parametrize('number_of_reviews, rating', [(100, 1), (50, 2), (500, 3), (7, 4), (10, 5)])
def test_get_average_rating_by_daycare_id(self, number_of_reviews, rating, create_dog_owner_user,
create_daycare_user):
[Review.create(review='review', rating=rating, daycare_id=create_daycare_user.id,
dogowner_id=create_dog_owner_user.id)
for _ in range(number_of_reviews)]

average_rating = Review.get_average_rating_by_daycare_id(create_daycare_user.id)

assert rating == average_rating

0 comments on commit c1051cb

Please sign in to comment.