summaryrefslogtreecommitdiff
path: root/grades/models.py
blob: fcea51020d3a2b5612a3bc8cc6bc5c17991f3ee9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
from django.db import models
from django.contrib.auth.models import User


class GradingSystem(models.Model):
    name = models.CharField(max_length=255, unique=True)
    description = models.TextField(default='About the grading system!')
    creator = models.ForeignKey(User, null=True, blank=True)

    def get_grade(self, marks):
        ranges = self.graderange_set.all()
        lower_limits = ranges.values_list('lower_limit', flat=True)
        upper_limits = ranges.values_list('upper_limit', flat=True)
        lower_limit = self._get_lower_limit(marks, lower_limits)
        upper_limit = self._get_upper_limit(marks, upper_limits)
        grade_range = ranges.filter(lower_limit=lower_limit,
                                    upper_limit=upper_limit).first()
        if grade_range:
            return grade_range.grade

    def _get_upper_limit(self, marks, upper_limits):
        greater_than = [upper_limit for upper_limit in upper_limits
                        if upper_limit > marks]
        if greater_than:
            return min(greater_than, key=lambda x: x-marks)

    def _get_lower_limit(self, marks, lower_limits):
        less_than = []
        for lower_limit in lower_limits:
            if lower_limit == marks:
                return lower_limit
            if lower_limit < marks:
                less_than.append(lower_limit)
        if less_than:
            return max(less_than, key=lambda x: x-marks)

    def __str__(self):
        return self.name.title()


class GradeRange(models.Model):
    system = models.ForeignKey(GradingSystem)
    lower_limit = models.FloatField()
    upper_limit = models.FloatField()
    grade = models.CharField(max_length=10)
    description = models.CharField(max_length=127, null=True, blank=True)