generated from uw-it-aca/django-vue
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #426 from uw-it-aca/develop
Develop
- Loading branch information
Showing
29 changed files
with
870 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
# Copyright 2024 UW-IT, University of Washington | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
from compass.dao.person import ( | ||
get_students_by_system_keys, get_students_by_student_numbers) | ||
from compass.utils import format_system_key, format_student_number | ||
from compass.exceptions import InvalidCSV | ||
from logging import getLogger | ||
import chardet | ||
import csv | ||
import os | ||
|
||
logger = getLogger(__name__) | ||
|
||
|
||
def normalize(s): | ||
return s.replace(' ', '').replace('_', '').lower() | ||
|
||
|
||
class InsensitiveDict(dict): | ||
""" | ||
Override get() to strip() and lower() the input key, and strip() the | ||
returned value. | ||
""" | ||
def get(self, *k, default=None): | ||
for i in k: | ||
if normalize(i) in self: | ||
try: | ||
return super().get(normalize(i)).strip() | ||
except AttributeError: | ||
break | ||
return default | ||
|
||
|
||
class InsensitiveDictReader(csv.DictReader): | ||
""" | ||
Override the csv.fieldnames property to strip() and lower() the fieldnames. | ||
""" | ||
@property | ||
def fieldnames(self): | ||
return [normalize(field) for field in super().fieldnames] | ||
|
||
def __next__(self): | ||
return InsensitiveDict(super().__next__()) | ||
|
||
|
||
class StudentCSV(): | ||
def __init__(self): | ||
self.encoding = None | ||
self.has_header = False | ||
self.dialect = None | ||
self.student_id_col = None | ||
self.system_key_cols = ['systemkey', 'syskey'] | ||
self.student_num_cols = ['studentid', 'studentno', 'studentnum', | ||
'studentnumber', 'studentidnumber'] | ||
|
||
def decode_file(self, csvfile): | ||
if not self.encoding: | ||
result = chardet.detect(csvfile) | ||
self.encoding = result['encoding'] | ||
return csvfile.decode(self.encoding) | ||
|
||
def validate(self, fileobj): | ||
# Read the first line of the file to validate the header | ||
try: | ||
decoded_file = self.decode_file(fileobj.readline()) | ||
self.has_header = csv.Sniffer().has_header(decoded_file) | ||
self.dialect = csv.Sniffer().sniff(decoded_file) | ||
except Exception as err: | ||
raise InvalidCSV(str(err)) | ||
|
||
reader = InsensitiveDictReader(decoded_file.splitlines(), | ||
dialect=self.dialect) | ||
|
||
self.student_id_col = next(( | ||
s for s in (self.system_key_cols + self.student_num_cols) if s in ( | ||
reader.fieldnames)), None) | ||
|
||
if self.student_id_col is None: | ||
raise InvalidCSV('Missing header row or student identifier') | ||
|
||
fileobj.seek(0, 0) | ||
|
||
def students_from_file(self, fileobj): | ||
""" | ||
Reads a CSV file object, and returns a list of person JSON objects | ||
Supported column names are contained in self.system_key_cols and | ||
self.student_num_cols, with priority given to system_keys. | ||
All other field names are ignored. | ||
""" | ||
self.validate(fileobj) | ||
decoded_file = self.decode_file(fileobj.read()).splitlines() | ||
|
||
student_ids = [] | ||
for row in InsensitiveDictReader(decoded_file, dialect=self.dialect): | ||
if self.student_id_col in self.system_key_cols: | ||
student_id = format_system_key(row.get(self.student_id_col)) | ||
else: | ||
student_id = format_student_number( | ||
row.get(self.student_id_col)) | ||
|
||
if student_id is not None: | ||
student_ids.append(student_id) | ||
|
||
students = [] | ||
if len(student_ids): | ||
if self.student_id_col in self.system_key_cols: | ||
identifier = 'system_key' | ||
students_dict = get_students_by_system_keys(student_ids) | ||
else: | ||
identifier = 'student_number' | ||
students_dict = get_students_by_student_numbers(student_ids) | ||
|
||
for sid in student_ids: | ||
if sid in students_dict: | ||
students.append(students_dict[sid]) | ||
else: | ||
students.append({ | ||
identifier: sid, | ||
'error': f'Student not found', | ||
}) | ||
|
||
return students |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
"field1"," field2","Field3",FIELD4,"field5","field 6","field7",Field_8 | ||
"ök1",øk2,ok3,ok4," ok5 "," ",,ok_8 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
800000,"javerage","Average","J","AB","2.0","GRADUATE","Biology","[email protected]" | ||
40000,"jjulius","Julius","J","AB","2.0","GRADUATE","Oceanography","[email protected]" | ||
1000000,"baverage","Average","Bill P","AC","2.0","GRADUATE","Oceanography","[email protected]" | ||
7000000,"student1","Student 1","","AA","2.0","UNDERGRADUATE","Health Services","[email protected]" | ||
9000000,"student2","Student","2","AA","2.0","NON_MATRIC","Non Matriculated","[email protected]" | ||
2000000,"student3","Student","3","AC","2.0","NON_MATRIC","Non Matriculated","[email protected]" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
UWNetID,LastName,FirstName,Section,Credits,Class,Major,Email | ||
"javerage","Average","J","AB","2.0","GRADUATE","Biology","[email protected]" | ||
"jjulius","Julius","J","AB","2.0","GRADUATE","Oceanography","[email protected]" | ||
"baverage","Average","Bill P","AC","2.0","GRADUATE","Oceanography","[email protected]" | ||
"student1","Student 1","","AA","2.0","UNDERGRADUATE","Health Services","[email protected]" | ||
"student2","Student","2","AA","2.0","NON_MATRIC","Non Matriculated","[email protected]" | ||
"student3","Student","3","AC","2.0","NON_MATRIC","Non Matriculated","[email protected]" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
Student_No,UWNetID,LastName,FirstName,System_Key,Section,Credits,Class,Major,Email | ||
"1033334","javerage","Average","J","532353230", "AB","2.0","GRADUATE","Biology","[email protected]" | ||
"1233338","lisa","Simpson","Lisa","888777333", "AB","2.0","GRADUATE","Oceanography","[email protected]" | ||
"1233334","jbothell","Bothell","J","820582050","AC","2.0","GRADUATE","Oceanography","[email protected]" | ||
"7000000","student1","Student 1","","700000001","AA","2.0","UNDERGRADUATE","Health Services","[email protected]" | ||
"","student3","Student","3","","AC","2.0","NON_MATRIC","Non Matriculated","[email protected]" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
Student_No,UWNetID,LastName,FirstName,Section,Credits,Class,Major,Email | ||
"1033334","javerage","Average","J","AB","2.0","GRADUATE","Biology","[email protected]" | ||
"1233338","lisa","Simpson","Lisa","AB","2.0","GRADUATE","Oceanography","[email protected]" | ||
"1233334","jbothell","Bothell","J","AC","2.0","GRADUATE","Oceanography","[email protected]" | ||
"7000000","student1","Student 1","","AA","2.0","UNDERGRADUATE","Health Services","[email protected]" | ||
"","student3","Student","3","AC","2.0","NON_MATRIC","Non Matriculated","[email protected]" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
SystemKey,UWNetID,LastName,FirstName,Section,Credits,Class,Major,Email | ||
"532353230","javerage","Average","J","AB","2.0","GRADUATE","Biology","[email protected]" | ||
"888777333","lisa","Simpson","Lisa","AB","2.0","GRADUATE","Oceanography","[email protected]" | ||
"820582050","jbothell","Bothell","J","AC","2.0","GRADUATE","Oceanography","[email protected]" | ||
"700000001","student1","Student 1","","AA","2.0","UNDERGRADUATE","Health Services","[email protected]" | ||
"","student3","Student","3","AC","2.0","NON_MATRIC","Non Matriculated","[email protected]" |
Oops, something went wrong.