summaryrefslogtreecommitdiff
path: root/yaksh
diff options
context:
space:
mode:
authoradityacp2021-01-25 10:54:25 +0530
committeradityacp2021-01-25 10:54:25 +0530
commitc474c68a2621e8c470ce2751103128fa793be62e (patch)
tree96ee60da7e4a27faa3772f234396e8c49a9fa7b9 /yaksh
parent419c66d29f20123d6f1e04072829973d84ff40c5 (diff)
downloadonline_test-c474c68a2621e8c470ce2751103128fa793be62e.tar.gz
online_test-c474c68a2621e8c470ce2751103128fa793be62e.tar.bz2
online_test-c474c68a2621e8c470ce2751103128fa793be62e.zip
Refactor code for download csv for course progress
Diffstat (limited to 'yaksh')
-rw-r--r--yaksh/models.py2
-rw-r--r--yaksh/views.py25
2 files changed, 15 insertions, 12 deletions
diff --git a/yaksh/models.py b/yaksh/models.py
index b0247ac..7609859 100644
--- a/yaksh/models.py
+++ b/yaksh/models.py
@@ -1159,7 +1159,7 @@ class Course(models.Model):
return grade
def get_current_unit(self, user):
- course_status = CourseStatus.objects.filter(course=self, user=user)
+ course_status = CourseStatus.objects.filter(course=self, user_id=user)
if course_status.exists():
return course_status.first().current_unit
diff --git a/yaksh/views.py b/yaksh/views.py
index 32d1e72..3c83ee9 100644
--- a/yaksh/views.py
+++ b/yaksh/views.py
@@ -3217,7 +3217,7 @@ def course_status(request, course_id):
stud_details = [(student, course.get_grade(student),
course.get_completion_percent(student),
- course.get_current_unit(student))
+ course.get_current_unit(student.id))
for student in students.object_list]
context = {
'course': course, 'objects': students, 'is_progress': True,
@@ -3400,19 +3400,22 @@ def download_course_progress(request, course_id):
course = get_object_or_404(Course, pk=course_id)
if not course.is_creator(user) and not course.is_teacher(user):
raise Http404('This course does not belong to you')
- students = course.students.order_by("-id")
- stud_details = [(student.get_full_name(), course.get_grade(student),
- course.get_completion_percent(student),
- course.get_current_unit(student))
- for student in students]
+ students = course.students.order_by("-id").values_list("id")
+ stud_details = list(CourseStatus.objects.filter(
+ course_id=course_id, user_id__in=students
+ ).values(
+ "user_id", "user__first_name", "user__last_name",
+ "grade", "percent_completed"
+ ))
+ for student in stud_details:
+ student["current_unit"] = course.get_current_unit(
+ student.pop("user_id")
+ )
+ df = pd.DataFrame(stud_details)
response = HttpResponse(content_type='text/csv')
response['Content-Disposition'] = 'attachment; filename="{0}.csv"'.format(
(course.name).lower().replace(' ', '_'))
- header = ['Name', 'Grade', 'Completion Percent', 'Current Unit']
- writer = csv.writer(response)
- writer.writerow(header)
- for student in stud_details:
- writer.writerow(student)
+ df.to_csv(response, index=False)
return response