<commit_before>from django.db import models
from django.db.models import Avg
from django.db.models.signals import post_save
from django.conf import settings
from datetime import date, datetime, timedelta
import json
from django.core.serializers.json import DjangoJSONEncoder
from django.contrib.auth.models import User
from django.template.defaultfilters import slugify

CAMPUSES = (
    (1, u'PO'), (2, u'SC'), (3, u'CMC'), (4, u'HM'), (5, u'PZ'), (6, u'CGU'), (7, u'CU'), (8, u'KS'), (-1, u'?'))
CAMPUSES_FULL_NAMES = {1: 'Pomona', 2: 'Scripps', 3: 'Claremont-McKenna', 4: '<NAME>', 5: 'Pitzer'}
CAMPUSES_LOOKUP = dict([(a[1], a[0]) for a in CAMPUSES])

# Some campuses are represented more than one way so we make aliases
CAMPUSES_LOOKUP['CM'] = CAMPUSES_LOOKUP['CMC']
CAMPUSES_LOOKUP['CUC'] = CAMPUSES_LOOKUP['CU']
CAMPUSES_LOOKUP['CG'] = CAMPUSES_LOOKUP['CGU']

SESSIONS = ((u'SP', u'Spring'), (u'FA', u'Fall'))
SUBSESSIONS = ((u'P1', u'1'), (u'P2', u'2'))

POSSIBLE_GRADES = (
    (1, u'A'), (2, u'A-'), (3, u'B+'), (4, u'B'), (5, u'B-'), (6, u'C+'), (7, u'C'),
    (8, u'C-'), (9, u'D+'), (10, u'D'), (11, u'D-'), (12, u'F'))

# TODO: Make this robust for different semesters
# (see the academic calendar at http://catalog.pomona.edu/content.php?catoid=14&navoid=2582)
START_DATE = date(2015, 9, 1)
END_DATE = date(2015, 12, 18)


class Term(models.Model):
    key = models.CharField(max_length=20, unique=True)
    year = models.PositiveSmallIntegerField()
    session = models.CharField(max_length=2, choices=SESSIONS)

    def is_current_term(self):
        current_term = Term.objects.all()[0]
        return self == current_term

    def __unicode__(self):
        return u'%s %s' % (self.session, self.year)

    class Meta:
        ordering = ['-year', 'session']


class Instructor(models.Model):
    name = models.CharField(max_length=100)
    rating = models.FloatField(blank = True, null = True)
    useful_rating = models.FloatField(blank=True, null=True)
    engagement_rating = models.FloatField(blank=True, null=True)
    difficulty_rating = models.FloatField(blank=True, null=True)
    competency_rating = models.FloatField(blank=True, null=True)
    lecturing_rating = models.FloatField(blank=True, null=True)
    enthusiasm_rating = models.FloatField(blank=True, null=True)
    approachable_rating = models.FloatField(blank=True, null=True)

    def __unicode__(self):
        return self.name

    def slug(self):
        return slugify(self.name)

    def get_miscellaneous_ratings(self):
        return [self.useful_rating, self.engagement_rating, self.difficulty_rating, self.competency_rating, self.lecturing_rating, self.enthusiasm_rating, self.approachable_rating]


class Department(models.Model):
    code = models.CharField(max_length=20, unique=True, db_index=True)
    name = models.CharField(max_length=100)

    def course_count(self):
        return len(self.primary_course_set.all())

    def non_breaking_name(self):
        return self.name.replace(' ', '&nbsp;')

    def __unicode__(self):
        return u'[%s] %s' % (self.code, self.name)


class RequirementArea(models.Model):
    code = models.CharField(max_length=20, unique=True, db_index=True)
    name = models.CharField(max_length=100)
    campus = models.SmallIntegerField(choices=CAMPUSES)

    def course_count(self):
        return len(self.course_set.all())

    def non_breaking_name(self):
        return self.name.replace(' ', '&nbsp;')

    def __unicode__(self):
        return u'[%s] %s' % (self.code, self.name)

    @models.permalink
    def get_absolute_url(self):
        return ('requirement_area_detail', (), {'slug': self.code, })


class Course(models.Model):
    code = models.CharField(max_length=20, unique=True, db_index=True)
    code_slug = models.CharField(max_length=20, unique=True, db_index=True)

    number = models.IntegerField(default=0)
    name = models.CharField(max_length=256)
    rating = models.FloatField(blank=True, null=True)
    useful_rating = models.FloatField(blank=True, null=True)
    engagement_rating = models.FloatField(blank=True, null=True)
    difficulty_rating = models.FloatField(blank=True, null=True)
    competency_rating = models.FloatField(blank=True, null=True)
    lecturing_rating = models.FloatField(blank=True, null=True)
    enthusiasm_rating = models.FloatField(blank=True, null=True)
    approachable_rating = models.FloatField(blank=True, null=True)

    primary_department = models.ForeignKey(Department, related_name='primary_course_set', null=True)
    departments = models.ManyToManyField(Department, related_name='course_set')
    requirement_areas = models.ManyToManyField(RequirementArea, related_name='course_set')

    def __unicode__(self):
        return u'[%s] %s' % (self.code, self.name)

    class Meta:
        ordering = ('code',)

    @models.permalink
    def get_absolute_url(self):
        return ('course_detail', (),
                {'course_code': self.code_slug})

    def get_miscellaneous_ratings(self):
        return [self.useful_rating, self.engagement_rating, self.difficulty_rating, self.competency_rating, self.lecturing_rating, self.enthusiasm_rating, self.approachable_rating]

    # TODO: Merge instructors who taught this class previously
    def get_instructors_from_all_sections(self):
        instructors = []
        for section in self.sections.all():
            instructors += section.instructors.all()
        return instructors

    def get_average_rating(self):
        reviews = CourseReview.objects.filter(course=self)
        return reviews.aggregate(Avg("overall_rating"))["overall_rating__avg"]

    def get_most_recent_section(self):
        return self.sections.all().order_by('term')[0]

class Section(models.Model):
    term = models.ForeignKey(Term, related_name='sections')
    course = models.ForeignKey(Course, related_name='sections')
<commit_msg>Displays 0 as category rating if none exists.<commit_after>from django.db import models
from django.db.models import Avg
from django.db.models.signals import post_save
from django.conf import settings
from datetime import date, datetime, timedelta
import json
from django.core.serializers.json import DjangoJSONEncoder
from django.contrib.auth.models import User
from django.template.defaultfilters import slugify

CAMPUSES = (
    (1, u'PO'), (2, u'SC'), (3, u'CMC'), (4, u'HM'), (5, u'PZ'), (6, u'CGU'), (7, u'CU'), (8, u'KS'), (-1, u'?'))
CAMPUSES_FULL_NAMES = {1: 'Pomona', 2: 'Scripps', 3: 'Claremont-McKenna', 4: '<NAME>', 5: 'Pitzer'}
CAMPUSES_LOOKUP = dict([(a[1], a[0]) for a in CAMPUSES])

# Some campuses are represented more than one way so we make aliases
CAMPUSES_LOOKUP['CM'] = CAMPUSES_LOOKUP['CMC']
CAMPUSES_LOOKUP['CUC'] = CAMPUSES_LOOKUP['CU']
CAMPUSES_LOOKUP['CG'] = CAMPUSES_LOOKUP['CGU']

SESSIONS = ((u'SP', u'Spring'), (u'FA', u'Fall'))
SUBSESSIONS = ((u'P1', u'1'), (u'P2', u'2'))

POSSIBLE_GRADES = (
    (1, u'A'), (2, u'A-'), (3, u'B+'), (4, u'B'), (5, u'B-'), (6, u'C+'), (7, u'C'),
    (8, u'C-'), (9, u'D+'), (10, u'D'), (11, u'D-'), (12, u'F'))

# TODO: Make this robust for different semesters
# (see the academic calendar at http://catalog.pomona.edu/content.php?catoid=14&navoid=2582)
START_DATE = date(2015, 9, 1)
END_DATE = date(2015, 12, 18)


class Term(models.Model):
    key = models.CharField(max_length=20, unique=True)
    year = models.PositiveSmallIntegerField()
    session = models.CharField(max_length=2, choices=SESSIONS)

    def is_current_term(self):
        current_term = Term.objects.all()[0]
        return self == current_term

    def __unicode__(self):
        return u'%s %s' % (self.session, self.year)

    class Meta:
        ordering = ['-year', 'session']


class Instructor(models.Model):
    name = models.CharField(max_length=100)
    rating = models.FloatField(blank = True, null = True)
    useful_rating = models.FloatField(blank=True, null=True)
    engagement_rating = models.FloatField(blank=True, null=True)
    difficulty_rating = models.FloatField(blank=True, null=True)
    competency_rating = models.FloatField(blank=True, null=True)
    lecturing_rating = models.FloatField(blank=True, null=True)
    enthusiasm_rating = models.FloatField(blank=True, null=True)
    approachable_rating = models.FloatField(blank=True, null=True)

    def __unicode__(self):
        return self.name

    def slug(self):
        return slugify(self.name)

    def get_miscellaneous_ratings(self):
        return [self.useful_rating or 0, self.engagement_rating or 0, self.difficulty_rating or 0, self.competency_rating or 0, self.lecturing_rating or 0, self.enthusiasm_rating or 0, self.approachable_rating or 0]


class Department(models.Model):
    code = models.CharField(max_length=20, unique=True, db_index=True)
    name = models.CharField(max_length=100)

    def course_count(self):
        return len(self.primary_course_set.all())

    def non_breaking_name(self):
        return self.name.replace(' ', '&nbsp;')

    def __unicode__(self):
        return u'[%s] %s' % (self.code, self.name)


class RequirementArea(models.Model):
    code = models.CharField(max_length=20, unique=True, db_index=True)
    name = models.CharField(max_length=100)
    campus = models.SmallIntegerField(choices=CAMPUSES)

    def course_count(self):
        return len(self.course_set.all())

    def non_breaking_name(self):
        return self.name.replace(' ', '&nbsp;')

    def __unicode__(self):
        return u'[%s] %s' % (self.code, self.name)

    @models.permalink
    def get_absolute_url(self):
        return ('requirement_area_detail', (), {'slug': self.code, })


class Course(models.Model):
    code = models.CharField(max_length=20, unique=True, db_index=True)
    code_slug = models.CharField(max_length=20, unique=True, db_index=True)

    number = models.IntegerField(default=0)
    name = models.CharField(max_length=256)
    rating = models.FloatField(blank=True, null=True)
    useful_rating = models.FloatField(blank=True, null=True)
    engagement_rating = models.FloatField(blank=True, null=True)
    difficulty_rating = models.FloatField(blank=True, null=True)
    competency_rating = models.FloatField(blank=True, null=True)
    lecturing_rating = models.FloatField(blank=True, null=True)
    enthusiasm_rating = models.FloatField(blank=True, null=True)
    approachable_rating = models.FloatField(blank=True, null=True)

    primary_department = models.ForeignKey(Department, related_name='primary_course_set', null=True)
    departments = models.ManyToManyField(Department, related_name='course_set')
    requirement_areas = models.ManyToManyField(RequirementArea, related_name='course_set')

    def __unicode__(self):
        return u'[%s] %s' % (self.code, self.name)

    class Meta:
        ordering = ('code',)

    @models.permalink
    def get_absolute_url(self):
        return ('course_detail', (),
                {'course_code': self.code_slug})

    def get_miscellaneous_ratings(self):
        return [self.useful_rating or 0, self.engagement_rating or 0, self.difficulty_rating or 0, self.competency_rating or 0, self.lecturing_rating or 0, self.enthusiasm_rating or 0, self.approachable_rating or 0]

    # TODO: Merge instructors who taught this class previously
    def get_instructors_from_all_sections(self):
        instructors = []
        for section in self.sections.all():
            instructors += section.instructors.all()
        return instructors

    def get_average_rating(self):
        reviews = CourseReview.objects.filter(course=self)
        return reviews.aggregate(Avg("overall_rating"))["overall_rating__avg"]

    def get_most_recent_section(self):
        return self.sections.all().order_by('term')[0]

class Section(models.Model):
    term = models.ForeignKey(Term, related_name='sections')
    course = models.ForeignKey(Course, related_name='sections')

{'fraction_non_alphanumeric': 0.13810246039770813, 'fraction_numerical': 0.014914054600606673, 'mean_word_length': 4.412220702234382, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2320890', 'n_tokens_mistral': 4147, 'n_tokens_neox': 3988, 'n_words': 823}