3
3
from django .views .generic import View
4
4
import traceback
5
5
from django .http import HttpResponse
6
+ from django .conf import settings
7
+ from django .contrib .auth import get_user_model
6
8
import csv
7
9
import json
8
10
from openpyxl import Workbook
@@ -1088,27 +1090,27 @@ def submitGradesProf(request):
1088
1090
@login_required (login_url = "/accounts/login" )
1089
1091
def download_template (request ):
1090
1092
des = request .session .get ("currentDesignationSelected" )
1091
- if des == "acadadmin" or str (des ) == "Associate Professor" or str (des ) == "Professor" or str (des ) == "Assistant Professor" or des == "Dean Academic" :
1092
- pass
1093
- else :
1094
- if request .is_ajax (): # For AJAX or JSON requests
1093
+ if des not in ["acadadmin" , "Associate Professor" , "Professor" , "Assistant Professor" , "Dean Academic" ]:
1094
+ if request .is_ajax ():
1095
1095
return JsonResponse ({"success" : False , "error" : "Access denied." }, status = 403 )
1096
- else : # For non-AJAX requests
1096
+ else :
1097
1097
return HttpResponseRedirect ('/dashboard/' )
1098
+
1098
1099
course = request .GET .get ('course' )
1099
1100
year = request .GET .get ('year' )
1100
1101
1101
1102
if not course or not year :
1102
1103
return JsonResponse ({'error' : 'Course and year are required' }, status = 400 )
1103
1104
1104
1105
try :
1105
-
1106
+ # Fetching the custom user model
1107
+ User = get_user_model ()
1108
+
1106
1109
course_info = course_registration .objects .filter (
1107
1110
course_id_id = course ,
1108
1111
working_year = year
1109
1112
)
1110
1113
1111
-
1112
1114
if not course_info .exists ():
1113
1115
return JsonResponse ({'error' : 'No registration data found for the provided course and year' }, status = 404 )
1114
1116
@@ -1121,17 +1123,18 @@ def download_template(request):
1121
1123
writer = csv .writer (response )
1122
1124
1123
1125
# Write header
1124
- writer .writerow (["roll_no" , "grade" , "remarks" ])
1126
+ writer .writerow (["roll_no" , "name" , " grade" , "remarks" ])
1125
1127
1126
- # Write student roll numbers
1128
+ # Write student roll numbers and names
1127
1129
for entry in course_info :
1128
1130
student_entry = entry .student_id
1129
- writer .writerow ([student_entry .id_id , "" , "" ])
1131
+ # Fetching the user instance dynamically
1132
+ student_user = User .objects .get (username = student_entry .id_id )
1133
+ writer .writerow ([student_entry .id_id , student_user .first_name + " " + student_user .last_name , "" , "" ])
1130
1134
1131
1135
return response
1132
1136
1133
1137
except Exception as e :
1134
- # Log the error (consider using Python's logging module)
1135
1138
print (f"Error in download_template: { str (e )} " )
1136
1139
return JsonResponse ({'error' : 'An unexpected error occurred' }, status = 500 )
1137
1140
0 commit comments