2018-09-16 16:52:23 +00:00
|
|
|
from django.db import models
|
|
|
|
|
2018-10-02 13:24:59 +00:00
|
|
|
# Represents an authenticated reviewer or reader of reviews.
|
|
|
|
class User(models.Model):
|
|
|
|
# A non-unique name for the user.
|
|
|
|
name = models.CharField(max_length=64)
|
|
|
|
# The user's birth date.
|
|
|
|
birth_date = models.DateField()
|
|
|
|
|
2018-10-02 14:27:29 +00:00
|
|
|
# Returns the name as the string representation of the user.
|
|
|
|
def __str__(self):
|
|
|
|
return self.name
|
|
|
|
|
2018-09-25 13:40:58 +00:00
|
|
|
# Represents any object for which reviews can be made. (Universities, Professors, etc.)
|
|
|
|
class RateableEntity(models.Model):
|
2018-10-02 13:24:59 +00:00
|
|
|
# Constants defined for types of rateable entities.
|
|
|
|
UNIVERSITY = 0
|
|
|
|
COURSE = 1
|
|
|
|
PROFESSOR = 2
|
|
|
|
TYPE_CHOICES = (
|
|
|
|
(UNIVERSITY, 'University'),
|
|
|
|
(COURSE, 'Course'),
|
|
|
|
(PROFESSOR, 'Professor')
|
|
|
|
)
|
|
|
|
|
2018-09-25 13:40:58 +00:00
|
|
|
# The human-readable name of this entity.
|
|
|
|
name = models.CharField(max_length=256)
|
2018-10-02 13:24:59 +00:00
|
|
|
# The date and time at which this entity was created.
|
|
|
|
created_date = models.DateTimeField(auto_now_add=True)
|
|
|
|
# The type of entity this is.
|
|
|
|
entity_type = models.SmallIntegerField(choices=TYPE_CHOICES)
|
2018-09-25 13:40:58 +00:00
|
|
|
|
2018-10-02 14:27:29 +00:00
|
|
|
# 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
|
2018-10-03 20:00:08 +00:00
|
|
|
if reviews.count() == 0:
|
|
|
|
return None
|
2018-10-02 14:27:29 +00:00
|
|
|
return rating_sum / reviews.count()
|
|
|
|
|
2018-10-18 09:06:55 +00:00
|
|
|
# Gets a 5-item list of the count of each rating, and the percentage of total votes.
|
2018-10-18 08:04:24 +00:00
|
|
|
def getRatingDistribution(self):
|
|
|
|
reviews = self.review_set.select_related()
|
2018-10-18 09:06:55 +00:00
|
|
|
distribution = [[0, 0], [0, 0], [0, 0], [0, 0], [0, 0]]
|
|
|
|
review_count = len(reviews)
|
|
|
|
|
2018-10-18 08:04:24 +00:00
|
|
|
for review in reviews:
|
2018-10-18 09:06:55 +00:00
|
|
|
distribution[review.rating-1][0] += 1
|
|
|
|
|
|
|
|
max_val = 0
|
|
|
|
for rating_dist in distribution:
|
|
|
|
rating_dist[1] = (rating_dist[0] / review_count) * 100
|
|
|
|
if (rating_dist[1] > max_val):
|
|
|
|
max_val = rating_dist[1]
|
|
|
|
|
|
|
|
print(max_val)
|
|
|
|
print(distribution)
|
|
|
|
for rating_dist in distribution:
|
|
|
|
rating_dist[1] = (rating_dist[1] / max_val) * 100
|
|
|
|
|
|
|
|
print(distribution)
|
2018-10-18 08:04:24 +00:00
|
|
|
return distribution
|
|
|
|
|
2018-10-02 14:27:29 +00:00
|
|
|
# Simply returns the name as the string representation.
|
|
|
|
def __str__(self):
|
|
|
|
return self.name
|
|
|
|
|
2018-10-11 12:10:19 +00:00
|
|
|
def getType(self):
|
|
|
|
for (t, t_name) in RateableEntity.TYPE_CHOICES:
|
|
|
|
if t == self.entity_type:
|
|
|
|
return t_name
|
|
|
|
return ''
|
|
|
|
|
2018-09-25 13:40:58 +00:00
|
|
|
# A review represents any single data entry to the database.
|
2018-09-25 12:13:25 +00:00
|
|
|
class Review(models.Model):
|
2018-09-25 13:40:58 +00:00
|
|
|
# An integer rating in the domain [1, 5]
|
2018-09-25 12:13:25 +00:00
|
|
|
rating = models.IntegerField(default=1)
|
2018-09-25 15:09:39 +00:00
|
|
|
# A title for the review (brief summary of sentiment)
|
|
|
|
title = models.CharField(max_length=128)
|
2018-09-25 13:40:58 +00:00
|
|
|
# The textual content of the review.
|
|
|
|
content = models.TextField()
|
|
|
|
# A foreign key referencing the entity for which this review was made.
|
2018-09-25 15:09:39 +00:00
|
|
|
rateable_entity = models.ForeignKey('postings.RateableEntity', on_delete=models.CASCADE)
|
|
|
|
# The date and time at which this review was published.
|
|
|
|
created_date = models.DateTimeField(auto_now_add=True)
|
|
|
|
# The date and time at which the last modification to this review was published.
|
|
|
|
last_updated_date = models.DateTimeField(auto_now=True)
|
2018-10-02 13:24:59 +00:00
|
|
|
# A reference to the person who created this review.
|
2018-10-02 14:27:29 +00:00
|
|
|
author = models.ForeignKey('postings.User', on_delete=models.PROTECT, null=True, blank=True)
|
2018-10-18 08:32:37 +00:00
|
|
|
# TEMPORARY: Name of person who gave review.
|
|
|
|
author_name = models.CharField(max_length=64, default='Anonymous')
|
2018-10-02 14:27:29 +00:00
|
|
|
|
|
|
|
# Gets the total number of votes which marked this review as 'helpful'.
|
2018-10-11 10:16:57 +00:00
|
|
|
@property
|
|
|
|
def helpful_vote_count(self):
|
|
|
|
return ReviewHelpfulVote.objects.filter(review=self.pk, helpful=True).count()
|
2018-10-02 14:27:29 +00:00
|
|
|
|
|
|
|
# Gets the total number of votes which marked this review as 'unhelpful'.
|
2018-10-11 10:16:57 +00:00
|
|
|
@property
|
|
|
|
def unhelpful_vote_count(self):
|
|
|
|
return ReviewHelpfulVote.objects.filter(review=self.pk, helpful=False).count()
|
2018-09-25 15:09:39 +00:00
|
|
|
|
|
|
|
# A vote for a review as either positive or negative.
|
|
|
|
class ReviewHelpfulVote(models.Model):
|
|
|
|
# A reference to the review that this vote is for.
|
|
|
|
review = models.ForeignKey('postings.Review', on_delete=models.CASCADE)
|
|
|
|
# Whether or not the referenced review was helpful.
|
|
|
|
helpful = models.BooleanField()
|
2018-09-25 13:40:58 +00:00
|
|
|
|
|
|
|
# A RateableEntity for universities.
|
|
|
|
class University(RateableEntity):
|
2018-10-02 13:24:59 +00:00
|
|
|
# A string referring to the URL of the university. Every single university should have one.
|
|
|
|
website_url = models.URLField()
|
|
|
|
# A string referring to the location of the university.
|
|
|
|
location = models.CharField(max_length=256)
|
2018-09-25 13:40:58 +00:00
|
|
|
|
|
|
|
# A RateableEntity for professors, who belong to one or more university.
|
|
|
|
class Professor(RateableEntity):
|
|
|
|
# The universities that this professor teaches or has taught at.
|
2018-09-25 20:52:38 +00:00
|
|
|
universities = models.ManyToManyField('postings.University')
|
2018-09-25 13:40:58 +00:00
|
|
|
|
|
|
|
# A RateableEntity for courses, which belong to a university, and have one or more professors.
|
|
|
|
class Course(RateableEntity):
|
|
|
|
# The university that this course belongs to.
|
2018-09-25 14:47:01 +00:00
|
|
|
taught_at_university = models.ForeignKey('postings.University', on_delete=models.CASCADE)
|
2018-09-25 13:40:58 +00:00
|
|
|
# A list of professors that teach this course.
|
2018-09-25 14:47:01 +00:00
|
|
|
professors = models.ManyToManyField('postings.Professor')
|
2018-09-25 12:13:25 +00:00
|
|
|
|
2018-09-16 16:52:23 +00:00
|
|
|
# Create your models here.
|
|
|
|
class UniversityReview(models.Model):
|
|
|
|
university_name = models.CharField(max_length=200)
|
|
|
|
username = models.CharField(max_length=200)
|
2018-09-26 11:14:02 +00:00
|
|
|
rating = models.IntegerField(default=1)
|
2018-09-16 16:52:23 +00:00
|
|
|
title = models.CharField(max_length=200)
|
2018-09-27 12:41:34 +00:00
|
|
|
date_published = models.DateTimeField(auto_now_add=True)
|
2018-09-16 16:52:23 +00:00
|
|
|
content = models.CharField(max_length=200)
|
|
|
|
|