diff --git a/backend/db.sqlite3 b/backend/db.sqlite3 index facb503..ae677ce 100644 Binary files a/backend/db.sqlite3 and b/backend/db.sqlite3 differ diff --git a/backend/postings/admin.py b/backend/postings/admin.py index 5961a97..f33fcd3 100644 --- a/backend/postings/admin.py +++ b/backend/postings/admin.py @@ -9,4 +9,5 @@ admin.site.register(RateableEntity) admin.site.register(ReviewHelpfulVote) admin.site.register(University) admin.site.register(Professor) -admin.site.register(Course) \ No newline at end of file +admin.site.register(Course) +admin.site.register(User) \ No newline at end of file diff --git a/backend/postings/migrations/0002_auto_20181002_1338.py b/backend/postings/migrations/0002_auto_20181002_1338.py new file mode 100644 index 0000000..04aacfd --- /dev/null +++ b/backend/postings/migrations/0002_auto_20181002_1338.py @@ -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'), + ), + ] diff --git a/backend/postings/migrations/0003_auto_20181002_1355.py b/backend/postings/migrations/0003_auto_20181002_1355.py new file mode 100644 index 0000000..dc0361f --- /dev/null +++ b/backend/postings/migrations/0003_auto_20181002_1355.py @@ -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'), + ), + ] diff --git a/backend/postings/models.py b/backend/postings/models.py index 70049a5..ce3fb64 100644 --- a/backend/postings/models.py +++ b/backend/postings/models.py @@ -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): diff --git a/backend/postings/templates/postings/entity_pages/entity.html b/backend/postings/templates/postings/entity_pages/entity.html index b066368..24d220b 100644 --- a/backend/postings/templates/postings/entity_pages/entity.html +++ b/backend/postings/templates/postings/entity_pages/entity.html @@ -4,7 +4,7 @@ {% block content %} -

Name: {{ entity.name }}

+

Name: {{ entity.name }}

Average rating: {{ entity.average_rating }} {% block entity_info %} {% endblock %} diff --git a/backend/postings/views.py b/backend/postings/views.py index 61c25b9..d8ae6df 100644 --- a/backend/postings/views.py +++ b/backend/postings/views.py @@ -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})