Added average rating.

This commit is contained in:
Andrew Lalis 2018-10-02 16:27:29 +02:00
parent 8eb2d3053f
commit 8a9c51e856
7 changed files with 75 additions and 4 deletions

Binary file not shown.

View File

@ -9,4 +9,5 @@ admin.site.register(RateableEntity)
admin.site.register(ReviewHelpfulVote)
admin.site.register(University)
admin.site.register(Professor)
admin.site.register(Course)
admin.site.register(Course)
admin.site.register(User)

View File

@ -0,0 +1,19 @@
# Generated by Django 2.1.1 on 2018-10-02 13:38
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
('postings', '0001_initial'),
]
operations = [
migrations.AlterField(
model_name='review',
name='author',
field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.PROTECT, to='postings.User'),
),
]

View File

@ -0,0 +1,25 @@
# Generated by Django 2.1.1 on 2018-10-02 13:55
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
('postings', '0002_auto_20181002_1338'),
]
operations = [
migrations.AddField(
model_name='reviewhelpfulvote',
name='user',
field=models.ForeignKey(default=1, on_delete=django.db.models.deletion.CASCADE, to='postings.User'),
preserve_default=False,
),
migrations.AlterField(
model_name='review',
name='author',
field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.PROTECT, to='postings.User'),
),
]

View File

@ -7,6 +7,10 @@ class User(models.Model):
# The user's birth date.
birth_date = models.DateField()
# Returns the name as the string representation of the user.
def __str__(self):
return self.name
# Represents any object for which reviews can be made. (Universities, Professors, etc.)
class RateableEntity(models.Model):
# Constants defined for types of rateable entities.
@ -26,6 +30,18 @@ class RateableEntity(models.Model):
# The type of entity this is.
entity_type = models.SmallIntegerField(choices=TYPE_CHOICES)
# Gets the average of all the reviews.
def getAverageRating(self):
reviews = self.review_set.select_related()
rating_sum = 0
for review in reviews:
rating_sum += review.rating
return rating_sum / reviews.count()
# Simply returns the name as the string representation.
def __str__(self):
return self.name
# A review represents any single data entry to the database.
class Review(models.Model):
# An integer rating in the domain [1, 5]
@ -41,7 +57,15 @@ class Review(models.Model):
# The date and time at which the last modification to this review was published.
last_updated_date = models.DateTimeField(auto_now=True)
# A reference to the person who created this review.
author = models.ForeignKey('postings.User', on_delete=models.PROTECT)
author = models.ForeignKey('postings.User', on_delete=models.PROTECT, null=True, blank=True)
# Gets the total number of votes which marked this review as 'helpful'.
def getHelpfulVoteCount(self):
ReviewHelpfulVote.objects.filter(pk=self.pk, helpful=True).count()
# Gets the total number of votes which marked this review as 'unhelpful'.
def getUnhelpfulVoteCount(self):
ReviewHelpfulVote.objects.filter(pk=self.pk, helpful=False).count()
# A vote for a review as either positive or negative.
class ReviewHelpfulVote(models.Model):
@ -49,7 +73,8 @@ class ReviewHelpfulVote(models.Model):
review = models.ForeignKey('postings.Review', on_delete=models.CASCADE)
# Whether or not the referenced review was helpful.
helpful = models.BooleanField()
# TODO: Add a reference to the user who voted. The whole purpose of a separate vote object is to track who votes for what.
# The user who made this vote.
user = models.ForeignKey('postings.User', on_delete=models.CASCADE)
# A RateableEntity for universities.
class University(RateableEntity):

View File

@ -4,7 +4,7 @@
{% block content %}
<h2>Name: {{ entity.name }}</h2>
<h2>Name: {{ entity.name }}</h2> Average rating: {{ entity.average_rating }}
{% block entity_info %}
{% endblock %}

View File

@ -24,6 +24,7 @@ def universities(request):
def university_entity(request, university_id):
try:
university = University.objects.get(pk=university_id)
university.average_rating = university.getAverageRating()
except University.DoesNotExist:
raise Http404("University does not exist")
return render(request, 'postings/entity_pages/university.html', {'entity': university})