summaryrefslogtreecommitdiff
path: root/grades/models.py
diff options
context:
space:
mode:
authoradityacp2018-07-03 11:36:25 +0530
committeradityacp2018-07-03 11:36:25 +0530
commit637a75a5ea8fcae1e00c0200d52c471d50c8729a (patch)
tree9a2f13aaf80573fa44465031ccc6f895e7fdda0b /grades/models.py
parent02705e4c676750291b6a5c4ea14e2947f29cb6c7 (diff)
parente3ad85ace916354ab96b23c1359ee72a6c2a740b (diff)
downloadonline_test-637a75a5ea8fcae1e00c0200d52c471d50c8729a.tar.gz
online_test-637a75a5ea8fcae1e00c0200d52c471d50c8729a.tar.bz2
online_test-637a75a5ea8fcae1e00c0200d52c471d50c8729a.zip
Update to latest changes
Diffstat (limited to 'grades/models.py')
-rw-r--r--grades/models.py46
1 files changed, 46 insertions, 0 deletions
diff --git a/grades/models.py b/grades/models.py
new file mode 100644
index 0000000..fcea510
--- /dev/null
+++ b/grades/models.py
@@ -0,0 +1,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)