Follow pylint rule for result app
This commit is contained in:
parent
cf81b3e8a3
commit
27d24a9b28
308
result/models.py
308
result/models.py
@ -1,3 +1,5 @@
|
|||||||
|
from decimal import Decimal
|
||||||
|
|
||||||
from django.db import models
|
from django.db import models
|
||||||
from django.urls import reverse
|
from django.urls import reverse
|
||||||
|
|
||||||
@ -5,22 +7,21 @@ from accounts.models import Student
|
|||||||
from core.models import Semester
|
from core.models import Semester
|
||||||
from course.models import Course
|
from course.models import Course
|
||||||
|
|
||||||
|
# Constants
|
||||||
YEARS = (
|
YEARS = (
|
||||||
(1, "1"),
|
(1, "1"),
|
||||||
(2, "2"),
|
(2, "2"),
|
||||||
(3, "3"),
|
(3, "3"),
|
||||||
(4, "4"),
|
(4, "4"),
|
||||||
(4, "5"),
|
(5, "5"),
|
||||||
(4, "6"),
|
(6, "6"),
|
||||||
)
|
)
|
||||||
|
|
||||||
# LEVEL_COURSE = "Level course"
|
BACHELOR_DEGREE = "Bachelor"
|
||||||
BACHLOAR_DEGREE = "Bachloar"
|
|
||||||
MASTER_DEGREE = "Master"
|
MASTER_DEGREE = "Master"
|
||||||
|
|
||||||
LEVEL = (
|
LEVEL_CHOICES = (
|
||||||
# (LEVEL_COURSE, "Level course"),
|
(BACHELOR_DEGREE, "Bachelor Degree"),
|
||||||
(BACHLOAR_DEGREE, "Bachloar Degree"),
|
|
||||||
(MASTER_DEGREE, "Master Degree"),
|
(MASTER_DEGREE, "Master Degree"),
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -28,7 +29,7 @@ FIRST = "First"
|
|||||||
SECOND = "Second"
|
SECOND = "Second"
|
||||||
THIRD = "Third"
|
THIRD = "Third"
|
||||||
|
|
||||||
SEMESTER = (
|
SEMESTER_CHOICES = (
|
||||||
(FIRST, "First"),
|
(FIRST, "First"),
|
||||||
(SECOND, "Second"),
|
(SECOND, "Second"),
|
||||||
(THIRD, "Third"),
|
(THIRD, "Third"),
|
||||||
@ -47,7 +48,7 @@ D = "D"
|
|||||||
F = "F"
|
F = "F"
|
||||||
NG = "NG"
|
NG = "NG"
|
||||||
|
|
||||||
GRADE = (
|
GRADE_CHOICES = (
|
||||||
(A_PLUS, "A+"),
|
(A_PLUS, "A+"),
|
||||||
(A, "A"),
|
(A, "A"),
|
||||||
(A_MINUS, "A-"),
|
(A_MINUS, "A-"),
|
||||||
@ -65,19 +66,39 @@ GRADE = (
|
|||||||
PASS = "PASS"
|
PASS = "PASS"
|
||||||
FAIL = "FAIL"
|
FAIL = "FAIL"
|
||||||
|
|
||||||
COMMENT = (
|
COMMENT_CHOICES = (
|
||||||
(PASS, "PASS"),
|
(PASS, "PASS"),
|
||||||
(FAIL, "FAIL"),
|
(FAIL, "FAIL"),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
GRADE_BOUNDARIES = [
|
||||||
|
(90, A_PLUS),
|
||||||
|
(85, A),
|
||||||
|
(80, A_MINUS),
|
||||||
|
(75, B_PLUS),
|
||||||
|
(70, B),
|
||||||
|
(65, B_MINUS),
|
||||||
|
(60, C_PLUS),
|
||||||
|
(55, C),
|
||||||
|
(50, C_MINUS),
|
||||||
|
(45, D),
|
||||||
|
(0, F),
|
||||||
|
]
|
||||||
|
|
||||||
class TakenCourseManager(models.Manager):
|
GRADE_POINT_MAPPING = {
|
||||||
def new(self, user=None):
|
A_PLUS: 4.0,
|
||||||
user_obj = None
|
A: 4.0,
|
||||||
if user is not None:
|
A_MINUS: 3.75,
|
||||||
if user.is_authenticated():
|
B_PLUS: 3.5,
|
||||||
user_obj = user
|
B: 3.0,
|
||||||
return self.model.objects.create(user=user_obj)
|
B_MINUS: 2.75,
|
||||||
|
C_PLUS: 2.5,
|
||||||
|
C: 2.0,
|
||||||
|
C_MINUS: 1.75,
|
||||||
|
D: 1.0,
|
||||||
|
F: 0.0,
|
||||||
|
NG: 0.0,
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
class TakenCourse(models.Model):
|
class TakenCourse(models.Model):
|
||||||
@ -85,202 +106,111 @@ class TakenCourse(models.Model):
|
|||||||
course = models.ForeignKey(
|
course = models.ForeignKey(
|
||||||
Course, on_delete=models.CASCADE, related_name="taken_courses"
|
Course, on_delete=models.CASCADE, related_name="taken_courses"
|
||||||
)
|
)
|
||||||
assignment = models.DecimalField(max_digits=5, decimal_places=2, default=0.0)
|
assignment = models.DecimalField(
|
||||||
mid_exam = models.DecimalField(max_digits=5, decimal_places=2, default=0.0)
|
max_digits=5, decimal_places=2, default=Decimal("0.00")
|
||||||
quiz = models.DecimalField(max_digits=5, decimal_places=2, default=0.0)
|
)
|
||||||
attendance = models.DecimalField(max_digits=5, decimal_places=2, default=0.0)
|
mid_exam = models.DecimalField(
|
||||||
final_exam = models.DecimalField(max_digits=5, decimal_places=2, default=0.0)
|
max_digits=5, decimal_places=2, default=Decimal("0.00")
|
||||||
total = models.DecimalField(max_digits=5, decimal_places=2, default=0.0)
|
)
|
||||||
grade = models.CharField(choices=GRADE, max_length=2, blank=True)
|
quiz = models.DecimalField(max_digits=5, decimal_places=2, default=Decimal("0.00"))
|
||||||
point = models.DecimalField(max_digits=5, decimal_places=2, default=0.0)
|
attendance = models.DecimalField(
|
||||||
comment = models.CharField(choices=COMMENT, max_length=200, blank=True)
|
max_digits=5, decimal_places=2, default=Decimal("0.00")
|
||||||
|
)
|
||||||
|
final_exam = models.DecimalField(
|
||||||
|
max_digits=5, decimal_places=2, default=Decimal("0.00")
|
||||||
|
)
|
||||||
|
total = models.DecimalField(
|
||||||
|
max_digits=5, decimal_places=2, default=Decimal("0.00"), editable=False
|
||||||
|
)
|
||||||
|
grade = models.CharField(
|
||||||
|
choices=GRADE_CHOICES, max_length=2, blank=True, editable=False
|
||||||
|
)
|
||||||
|
point = models.DecimalField(
|
||||||
|
max_digits=5, decimal_places=2, default=Decimal("0.00"), editable=False
|
||||||
|
)
|
||||||
|
comment = models.CharField(
|
||||||
|
choices=COMMENT_CHOICES, max_length=200, blank=True, editable=False
|
||||||
|
)
|
||||||
|
|
||||||
def get_absolute_url(self):
|
def get_absolute_url(self):
|
||||||
return reverse("course_detail", kwargs={"slug": self.course.slug})
|
return reverse("course_detail", kwargs={"slug": self.course.slug})
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return "{0} ({1})".format(self.course.title, self.course.code)
|
return f"{self.course.title} ({self.course.code})"
|
||||||
|
|
||||||
# @staticmethod
|
def get_total(self):
|
||||||
def get_total(self, assignment, mid_exam, quiz, attendance, final_exam):
|
return sum(
|
||||||
return (
|
[
|
||||||
float(assignment)
|
self.assignment,
|
||||||
+ float(mid_exam)
|
self.mid_exam,
|
||||||
+ float(quiz)
|
self.quiz,
|
||||||
+ float(attendance)
|
self.attendance,
|
||||||
+ float(final_exam)
|
self.final_exam,
|
||||||
|
]
|
||||||
)
|
)
|
||||||
|
|
||||||
# @staticmethod
|
def get_grade(self):
|
||||||
def get_grade(self, total):
|
total = self.total
|
||||||
# total = float(assignment) + float(mid_exam) + float(quiz) + float(attendance) + float(final_exam)
|
for boundary, grade in GRADE_BOUNDARIES:
|
||||||
# total = self.get_total(assignment=assignment, mid_exam=mid_exam, quiz=quiz, attendance=attendance, final_exam=final_exam)
|
if total >= boundary:
|
||||||
# total = total
|
|
||||||
if total >= 90:
|
|
||||||
grade = A_PLUS
|
|
||||||
elif total >= 85:
|
|
||||||
grade = A
|
|
||||||
elif total >= 80:
|
|
||||||
grade = A_MINUS
|
|
||||||
elif total >= 75:
|
|
||||||
grade = B_PLUS
|
|
||||||
elif total >= 70:
|
|
||||||
grade = B
|
|
||||||
elif total >= 65:
|
|
||||||
grade = B_MINUS
|
|
||||||
elif total >= 60:
|
|
||||||
grade = C_PLUS
|
|
||||||
elif total >= 55:
|
|
||||||
grade = C
|
|
||||||
elif total >= 50:
|
|
||||||
grade = C_MINUS
|
|
||||||
elif total >= 45:
|
|
||||||
grade = D
|
|
||||||
elif total < 45:
|
|
||||||
grade = F
|
|
||||||
else:
|
|
||||||
grade = NG
|
|
||||||
return grade
|
return grade
|
||||||
|
return NG
|
||||||
|
|
||||||
# @staticmethod
|
def get_comment(self):
|
||||||
def get_comment(self, grade):
|
if self.grade in [F, NG]:
|
||||||
if grade == F or grade == NG:
|
return FAIL
|
||||||
comment = FAIL
|
return PASS
|
||||||
# elif grade == NG:
|
|
||||||
# comment = FAIL
|
|
||||||
else:
|
|
||||||
comment = PASS
|
|
||||||
return comment
|
|
||||||
|
|
||||||
def get_point(self, grade):
|
def get_point(self):
|
||||||
p = 0
|
|
||||||
# point = 0
|
|
||||||
# for i in student:
|
|
||||||
credit = self.course.credit
|
credit = self.course.credit
|
||||||
if self.grade == A_PLUS:
|
grade_point = GRADE_POINT_MAPPING.get(self.grade, 0.0)
|
||||||
point = 4
|
return Decimal(credit) * Decimal(grade_point)
|
||||||
elif self.grade == A:
|
|
||||||
point = 4
|
|
||||||
elif self.grade == A_MINUS:
|
|
||||||
point = 3.75
|
|
||||||
elif self.grade == B_PLUS:
|
|
||||||
point = 3.5
|
|
||||||
elif self.grade == B:
|
|
||||||
point = 3
|
|
||||||
elif self.grade == B_MINUS:
|
|
||||||
point = 2.75
|
|
||||||
elif self.grade == C_PLUS:
|
|
||||||
point = 2.5
|
|
||||||
elif self.grade == C:
|
|
||||||
point = 2
|
|
||||||
elif self.grade == C_MINUS:
|
|
||||||
point = 1.75
|
|
||||||
elif self.grade == D:
|
|
||||||
point = 1
|
|
||||||
else:
|
|
||||||
point = 0
|
|
||||||
p += int(credit) * point
|
|
||||||
return p
|
|
||||||
|
|
||||||
def calculate_gpa(self, total_credit_in_semester):
|
def save(self, *args, **kwargs):
|
||||||
current_semester = Semester.objects.get(is_current_semester=True)
|
self.total = self.get_total()
|
||||||
student = TakenCourse.objects.filter(
|
self.grade = self.get_grade()
|
||||||
student=self.student,
|
self.point = self.get_point()
|
||||||
course__level=self.student.level,
|
self.comment = self.get_comment()
|
||||||
course__semester=current_semester,
|
super().save(*args, **kwargs)
|
||||||
)
|
|
||||||
p = 0
|
|
||||||
point = 0
|
|
||||||
for i in student:
|
|
||||||
credit = i.course.credit
|
|
||||||
if i.grade == A_PLUS:
|
|
||||||
point = 4
|
|
||||||
elif i.grade == A:
|
|
||||||
point = 4
|
|
||||||
elif i.grade == A_MINUS:
|
|
||||||
point = 3.75
|
|
||||||
elif i.grade == B_PLUS:
|
|
||||||
point = 3.5
|
|
||||||
elif i.grade == B:
|
|
||||||
point = 3
|
|
||||||
elif i.grade == B_MINUS:
|
|
||||||
point = 2.75
|
|
||||||
elif i.grade == C_PLUS:
|
|
||||||
point = 2.5
|
|
||||||
elif i.grade == C:
|
|
||||||
point = 2
|
|
||||||
elif i.grade == C_MINUS:
|
|
||||||
point = 1.75
|
|
||||||
elif i.grade == D:
|
|
||||||
point = 1
|
|
||||||
else:
|
|
||||||
point = 0
|
|
||||||
p += int(credit) * point
|
|
||||||
try:
|
|
||||||
gpa = p / total_credit_in_semester
|
|
||||||
return round(gpa, 2)
|
|
||||||
except ZeroDivisionError:
|
|
||||||
return 0
|
|
||||||
|
|
||||||
def calculate_cgpa(self):
|
def calculate_gpa(self):
|
||||||
current_semester = Semester.objects.get(is_current_semester=True)
|
current_semester = Semester.objects.filter(is_current_semester=True).first()
|
||||||
previousResult = Result.objects.filter(
|
if not current_semester:
|
||||||
student__id=self.student.id, level__lt=self.student.level
|
return Decimal("0.00")
|
||||||
)
|
|
||||||
previous_cgpa = 0
|
|
||||||
for i in previousResult:
|
|
||||||
if i.cgpa is not None:
|
|
||||||
previous_cgpa += i.cgpa
|
|
||||||
cgpa = 0
|
|
||||||
if str(current_semester) == SECOND:
|
|
||||||
first_sem_gpa = 0.0
|
|
||||||
sec_sem_gpa = 0.0
|
|
||||||
try:
|
|
||||||
first_sem_result = Result.objects.get(
|
|
||||||
student=self.student.id, semester=FIRST, level=self.student.level
|
|
||||||
)
|
|
||||||
first_sem_gpa += first_sem_result.gpa
|
|
||||||
except:
|
|
||||||
first_sem_gpa = 0
|
|
||||||
|
|
||||||
try:
|
|
||||||
sec_sem_result = Result.objects.get(
|
|
||||||
student=self.student.id, semester=SECOND, level=self.student.level
|
|
||||||
)
|
|
||||||
sec_sem_gpa += sec_sem_result.gpa
|
|
||||||
except:
|
|
||||||
sec_sem_gpa = 0
|
|
||||||
|
|
||||||
taken_courses = TakenCourse.objects.filter(
|
taken_courses = TakenCourse.objects.filter(
|
||||||
student=self.student, student__level=self.student.level
|
student=self.student,
|
||||||
|
course__level=self.student.level,
|
||||||
|
course__semester=current_semester.semester,
|
||||||
)
|
)
|
||||||
taken_course_credits = 0
|
|
||||||
taken_course_points = 0
|
|
||||||
for i in taken_courses:
|
|
||||||
taken_course_points += float(i.point)
|
|
||||||
for i in taken_courses:
|
|
||||||
taken_course_credits += int(i.course.credit)
|
|
||||||
# cgpa = (first_sem_gpa + sec_sem_gpa) / 2
|
|
||||||
|
|
||||||
print("taken_course_points = ", taken_course_points)
|
total_points = sum(tc.point for tc in taken_courses)
|
||||||
print("taken_course_credits = ", taken_course_credits)
|
total_credits = sum(tc.course.credit for tc in taken_courses)
|
||||||
print("first_sem_gpa = ", first_sem_gpa)
|
|
||||||
print("sec_sem_gpa = ", sec_sem_gpa)
|
|
||||||
print("cgpa = ", round(taken_course_points / taken_course_credits, 2))
|
|
||||||
|
|
||||||
try:
|
if total_credits > 0:
|
||||||
cgpa = taken_course_points / taken_course_credits
|
gpa = total_points / Decimal(total_credits)
|
||||||
|
return round(gpa, 2)
|
||||||
|
return Decimal("0.00")
|
||||||
|
|
||||||
|
def calculate_cgpa(self):
|
||||||
|
taken_courses = TakenCourse.objects.filter(student=self.student)
|
||||||
|
|
||||||
|
total_points = sum(tc.point for tc in taken_courses)
|
||||||
|
total_credits = sum(tc.course.credit for tc in taken_courses)
|
||||||
|
|
||||||
|
if total_credits > 0:
|
||||||
|
cgpa = total_points / Decimal(total_credits)
|
||||||
return round(cgpa, 2)
|
return round(cgpa, 2)
|
||||||
except ZeroDivisionError:
|
return Decimal("0.00")
|
||||||
return 0
|
|
||||||
|
|
||||||
# return round(cgpa, 2)
|
|
||||||
|
|
||||||
|
|
||||||
class Result(models.Model):
|
class Result(models.Model):
|
||||||
student = models.ForeignKey(Student, on_delete=models.CASCADE)
|
student = models.ForeignKey(Student, on_delete=models.CASCADE)
|
||||||
gpa = models.FloatField(null=True)
|
gpa = models.FloatField(null=True)
|
||||||
cgpa = models.FloatField(null=True)
|
cgpa = models.FloatField(null=True)
|
||||||
semester = models.CharField(max_length=100, choices=SEMESTER)
|
semester = models.CharField(max_length=100, choices=SEMESTER_CHOICES)
|
||||||
session = models.CharField(max_length=100, blank=True, null=True)
|
session = models.CharField(max_length=100, blank=True, null=True)
|
||||||
level = models.CharField(max_length=25, choices=LEVEL, null=True)
|
level = models.CharField(max_length=25, choices=LEVEL_CHOICES, null=True)
|
||||||
|
|
||||||
|
def __str__(self):
|
||||||
|
return f"Result for {self.student} - Semester: {self.semester}, Level: {self.level}"
|
||||||
|
|||||||
@ -456,7 +456,6 @@ def result_sheet_pdf_view(request, id):
|
|||||||
@login_required
|
@login_required
|
||||||
@student_required
|
@student_required
|
||||||
def course_registration_form(request):
|
def course_registration_form(request):
|
||||||
current_semester = Semester.objects.get(is_current_semester=True)
|
|
||||||
current_session = Session.objects.get(is_current_session=True)
|
current_session = Session.objects.get(is_current_session=True)
|
||||||
courses = TakenCourse.objects.filter(student__student__id=request.user.id)
|
courses = TakenCourse.objects.filter(student__student__id=request.user.id)
|
||||||
fname = request.user.username + ".pdf"
|
fname = request.user.username + ".pdf"
|
||||||
@ -514,7 +513,6 @@ def course_registration_form(request):
|
|||||||
Story.append(title)
|
Story.append(title)
|
||||||
student = Student.objects.get(student__pk=request.user.id)
|
student = Student.objects.get(student__pk=request.user.id)
|
||||||
|
|
||||||
style_right = ParagraphStyle(name="right", parent=styles["Normal"])
|
|
||||||
tbl_data = [
|
tbl_data = [
|
||||||
[
|
[
|
||||||
Paragraph(
|
Paragraph(
|
||||||
@ -550,8 +548,6 @@ def course_registration_form(request):
|
|||||||
semester_title = Paragraph(semester_title, semester)
|
semester_title = Paragraph(semester_title, semester)
|
||||||
Story.append(semester_title)
|
Story.append(semester_title)
|
||||||
|
|
||||||
elements = []
|
|
||||||
|
|
||||||
# FIRST SEMESTER
|
# FIRST SEMESTER
|
||||||
count = 0
|
count = 0
|
||||||
header = [
|
header = [
|
||||||
@ -598,7 +594,6 @@ def course_registration_form(request):
|
|||||||
"",
|
"",
|
||||||
)
|
)
|
||||||
]
|
]
|
||||||
color = colors.black
|
|
||||||
count += 1
|
count += 1
|
||||||
table_body = Table(data, 1 * [1.4 * inch], 1 * [0.3 * inch])
|
table_body = Table(data, 1 * [1.4 * inch], 1 * [0.3 * inch])
|
||||||
table_body.setStyle(
|
table_body.setStyle(
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user