Implemented basic template functionality. #9

Merged
andrewlalis merged 6 commits from al/template_homepage into develop 2018-10-03 20:03:20 +00:00
7 changed files with 75 additions and 4 deletions
Showing only changes of commit 8a9c51e856 - Show all commits

Binary file not shown.

View File

@ -9,4 +9,5 @@ admin.site.register(RateableEntity)
admin.site.register(ReviewHelpfulVote) admin.site.register(ReviewHelpfulVote)
admin.site.register(University) admin.site.register(University)
admin.site.register(Professor) 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. # The user's birth date.
birth_date = models.DateField() 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.) # Represents any object for which reviews can be made. (Universities, Professors, etc.)
class RateableEntity(models.Model): class RateableEntity(models.Model):
# Constants defined for types of rateable entities. # Constants defined for types of rateable entities.
@ -26,6 +30,18 @@ class RateableEntity(models.Model):
# The type of entity this is. # The type of entity this is.
entity_type = models.SmallIntegerField(choices=TYPE_CHOICES) 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. # A review represents any single data entry to the database.
class Review(models.Model): class Review(models.Model):
# An integer rating in the domain [1, 5] # 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. # The date and time at which the last modification to this review was published.
last_updated_date = models.DateTimeField(auto_now=True) last_updated_date = models.DateTimeField(auto_now=True)
# A reference to the person who created this review. # 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. # A vote for a review as either positive or negative.
class ReviewHelpfulVote(models.Model): class ReviewHelpfulVote(models.Model):
@ -49,7 +73,8 @@ class ReviewHelpfulVote(models.Model):
review = models.ForeignKey('postings.Review', on_delete=models.CASCADE) review = models.ForeignKey('postings.Review', on_delete=models.CASCADE)
# Whether or not the referenced review was helpful. # Whether or not the referenced review was helpful.
helpful = models.BooleanField() 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. # A RateableEntity for universities.
class University(RateableEntity): class University(RateableEntity):

View File

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

View File

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