forked from Shyamnath-Sankar/Feedback-System
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutils.py
More file actions
280 lines (240 loc) · 11.5 KB
/
utils.py
File metadata and controls
280 lines (240 loc) · 11.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
import csv
import os
import hashlib
import base64
from config import (
RATING_FILE, STUDENT_FILE, ADMIN_MAPPING_FILE,
MAINRATING_FILE, REQUIRED_FILES
)
# Secret key for encryption (in a real application, this should be stored securely)
SECRET_KEY = "VSB_FEEDBACK_SYSTEM_SECRET_KEY"
def normalize_regno(regno):
"""Normalize a registration number by removing leading zeros."""
try:
return str(int(regno))
except (ValueError, TypeError):
return regno
def encrypt_regno(regno):
"""
Encrypt a registration number using a one-way hash function.
This ensures the registration number cannot be recovered from the stored value,
but the same registration number will always produce the same hash.
"""
if not regno:
print("[DEBUG] encrypt_regno: Empty registration number")
return ""
# Normalize the registration number
normalized_regno = normalize_regno(regno)
print(f"[DEBUG] encrypt_regno: Normalized {regno} to {normalized_regno}")
# Create a hash using the normalized number and secret key
input_str = normalized_regno + SECRET_KEY
print(f"[DEBUG] encrypt_regno: Input string length: {len(input_str)}")
hash_obj = hashlib.sha256(input_str.encode())
hash_str = base64.b64encode(hash_obj.digest()).decode('utf-8')
result = hash_str[:32]
print(f"[DEBUG] encrypt_regno: Input: {regno} -> Normalized: {normalized_regno} -> Output: {result}")
return result
def is_encrypted(value):
"""
Check if a value is already encrypted.
Encrypted values are base64 strings of a specific length.
"""
print(f"[DEBUG] is_encrypted: Checking value: {value}, type: {type(value)}")
if not value:
print("[DEBUG] is_encrypted: Empty value")
return False
# Check if the value looks like a base64 string of the right length
try:
if len(value) == 32:
print(f"[DEBUG] is_encrypted: Length check passed")
valid_chars = all(c in "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=" for c in value)
print(f"[DEBUG] is_encrypted: Character check result: {valid_chars}")
if valid_chars:
return True
except Exception as e:
print(f"[DEBUG] is_encrypted: Error during check: {str(e)}")
pass
print("[DEBUG] is_encrypted: Not encrypted")
return False
def read_csv_as_list(filename):
"""Return a list of values from the specified column in the CSV file."""
if not os.path.exists(filename):
return []
with open(filename, newline='', encoding='utf-8') as f:
reader = csv.DictReader(f)
header = REQUIRED_FILES[filename][0] # Get the expected header for this file
return [row[header].strip() for row in reader if row.get(header)]
def load_admin_mapping(department, semester):
"""Return a list of mapping dictionaries matching the given department and semester."""
mappings = []
dep_norm = department.strip()
sem_norm = semester.strip()
if sem_norm.lower().startswith("semester"):
sem_norm = sem_norm[len("semester"):].strip()
if os.path.exists(ADMIN_MAPPING_FILE):
with open(ADMIN_MAPPING_FILE, newline='', encoding='utf-8') as f:
reader = csv.DictReader(f)
for row in reader:
row_dep = row.get('department', '').strip()
row_sem = row.get('semester', '').strip()
if row_sem.lower().startswith("semester"):
row_sem = row_sem[len("semester"):].strip()
if row_dep == dep_norm and row_sem == sem_norm:
mappings.append(row)
return mappings
def update_admin_mappings(department, semester, new_mappings):
"""
Overwrite any existing mappings for the given department and semester
with new_mappings. Other mappings are preserved.
"""
dep_norm = department.strip()
sem_norm = semester.strip()
if sem_norm.lower().startswith("semester"):
sem_norm = sem_norm[len("semester"):].strip()
existing = []
if os.path.exists(ADMIN_MAPPING_FILE):
with open(ADMIN_MAPPING_FILE, newline='', encoding='utf-8') as f:
reader = csv.DictReader(f)
for row in reader:
row_dep = row.get('department', '').strip()
row_sem = row.get('semester', '').strip()
if row_sem.lower().startswith("semester"):
row_sem = row_sem[len("semester"):].strip()
if row_dep == dep_norm and row_sem == sem_norm:
continue
else:
existing.append(row)
combined = existing + new_mappings
with open(ADMIN_MAPPING_FILE, 'w', newline='', encoding='utf-8') as f:
fieldnames = ['department', 'semester', 'staff', 'subject']
writer = csv.DictWriter(f, fieldnames=fieldnames)
writer.writeheader()
for row in combined:
writer.writerow(row)
def append_ratings(rating_rows):
"""Append rating rows (list of dicts) to RATING_FILE."""
file_exists = os.path.exists(RATING_FILE)
with open(RATING_FILE, 'a', newline='', encoding='utf-8') as f:
fieldnames = ['registerno', 'department', 'semester', 'staff', 'subject',
'q1', 'q2', 'q3', 'q4', 'q5', 'q6', 'q7', 'q8', 'q9', 'q10', 'average']
writer = csv.DictWriter(f, fieldnames=fieldnames)
if not file_exists:
writer.writeheader()
for row in rating_rows:
# Store the registration number as is (no encryption)
writer.writerow(row)
def get_student_info(registerno):
"""Return student info (as a dict) from STUDENT_FILE by registration number."""
if not os.path.exists(STUDENT_FILE):
print("[DEBUG] Student file does not exist")
return None
print(f"[DEBUG] Looking for registration number: {registerno}")
# Normalize input number
reg_num = normalize_regno(registerno)
print(f"[DEBUG] Normalized to: {reg_num}")
if registerno != reg_num:
print(f"[DEBUG] Registration number was normalized from {registerno} to {reg_num}")
with open(STUDENT_FILE, newline='', encoding='utf-8') as f:
reader = csv.DictReader(f)
for row in reader:
stored_regno = row.get('registerno', '')
# Try to normalize the stored regno if it's not encrypted
if not is_encrypted(stored_regno):
stored_regno = normalize_regno(stored_regno)
print(f"[DEBUG] Comparing with stored number: {stored_regno}")
# Check if the stored regno matches either the normalized input or its encrypted form
if stored_regno == reg_num or stored_regno == encrypt_regno(reg_num):
print(f"[DEBUG] Found match!")
return row
print(f"[DEBUG] No match found for {reg_num}")
return None
def has_submitted_feedback(registerno):
"""Return True if the student has already submitted feedback."""
if not os.path.exists(RATING_FILE):
print("[DEBUG] Rating file does not exist")
return False
# Normalize input number
reg_num = normalize_regno(registerno)
print(f"[DEBUG] Checking feedback for registration number: {registerno} (normalized: {reg_num})")
if registerno != reg_num:
print(f"[DEBUG] Registration number was normalized from {registerno} to {reg_num}")
with open(RATING_FILE, newline='', encoding='utf-8') as f:
reader = csv.DictReader(f)
for row in reader:
stored_regno = row.get('registerno', '')
# Try to normalize the stored regno if it's not encrypted
if not is_encrypted(stored_regno):
stored_regno = normalize_regno(stored_regno)
print(f"[DEBUG] Comparing with stored number: {stored_regno}")
# Check if the stored regno matches either the normalized input or its encrypted form
if stored_regno == reg_num or stored_regno == encrypt_regno(reg_num):
print(f"[DEBUG] Found feedback submission!")
return True
print(f"[DEBUG] No feedback found for {reg_num}")
return False
def update_mainratings():
"""
Aggregate ratings from RATING_FILE grouped by department, semester, staff, and subject,
and write the aggregated (overall average) data to MAINRATING_FILE.
Also calculates per-question averages.
"""
aggregated = {}
if os.path.exists(RATING_FILE):
with open(RATING_FILE, newline='', encoding='utf-8') as f:
reader = csv.DictReader(f)
for row in reader:
dep = row.get('department', '').strip()
sem = row.get('semester', '').strip()
staff = row.get('staff', '').strip()
subject = row.get('subject', '').strip()
key = (dep, sem, staff, subject)
# Initialize if this is the first rating for this combination
if key not in aggregated:
aggregated[key] = {
'q_sums': [0.0] * 10, # Sum for each question
'count': 0, # Number of ratings
'total_avg': 0.0 # Running sum of averages
}
# Add individual question ratings
for i in range(1, 11):
try:
q_val = float(row.get(f'q{i}', 0))
aggregated[key]['q_sums'][i-1] += q_val
except (ValueError, TypeError):
continue
try:
avg = float(row.get('average', 0))
aggregated[key]['total_avg'] += avg
aggregated[key]['count'] += 1
except (ValueError, TypeError):
continue
with open(MAINRATING_FILE, 'w', newline='', encoding='utf-8') as f:
fieldnames = ['department', 'semester', 'staff', 'subject', 'q1_avg', 'q2_avg',
'q3_avg', 'q4_avg', 'q5_avg', 'q6_avg', 'q7_avg', 'q8_avg', 'q9_avg',
'q10_avg', 'overall_average']
writer = csv.DictWriter(f, fieldnames=fieldnames)
writer.writeheader()
for key, data in aggregated.items():
dep, sem, staff, subject = key
count = data['count']
if count > 0:
row_data = {
'department': dep,
'semester': sem,
'staff': staff,
'subject': subject,
}
# Calculate per-question averages
for i in range(10):
q_avg = data['q_sums'][i] / count
row_data[f'q{i+1}_avg'] = f"{q_avg:.2f}"
# Calculate overall average
overall_avg = data['total_avg'] / count
row_data['overall_average'] = f"{overall_avg:.2f}"
writer.writerow(row_data)
def normalize_semester(semester):
"""Normalize semester string by removing 'semester' prefix if present."""
semester = semester.strip()
if semester.lower().startswith("semester"):
semester = semester[len("semester"):].strip()
return semester