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