Coverage for app/logic/downloadFile.py: 85%

27 statements  

« prev     ^ index     » next       coverage.py v7.2.7, created at 2024-10-24 20:53 +0000

1from app import app 

2from app.models.courseInstructor import CourseInstructor 

3from app.models.courseStatus import CourseStatus 

4 

5import csv 

6 

7class fileMaker: 

8 ''' 

9 Create the file for the download buttons 

10 

11 requestedInfo: Query object to be formatted and input to the file as text. 

12 fileType: Specifies what type of file is going to be made. Currently implemented: (CSV) 

13 fileFormat (optional): The format of the file, primarily for CSV headers. Type: (dictionary of lists) 

14 ''' 

15 def __init__(self, designator, requestedInfo, fileType, fileFormat = None): 

16 self.relativePath = app.config['files']['base_path'] 

17 self.designator = designator 

18 self.requestedInfo = requestedInfo 

19 self.fileType = fileType 

20 self.fileFormat = fileFormat 

21 self.makeFile(fileType) 

22 

23 

24 def makeFile(self, fileType): 

25 ''' 

26 Creates the file 

27 ''' 

28 try: 

29 if self.designator == "downloadApprovedCourses": 

30 if fileType == "CSV": 

31 with open(self.relativePath + "/ApprovedCourses.csv", 'w', encoding='utf-8', errors="backslashreplace") as csvfile: 

32 self.filewriter = csv.writer(csvfile, delimiter = ',') 

33 headers = self.fileFormat.get("headers") 

34 self.filewriter.writerow(headers) 

35 csvWriteList = [] 

36 for approvedCourse in self.requestedInfo: 

37 csvWriteList = [approvedCourse.courseName, approvedCourse.courseAbbreviation, approvedCourse.instructors, approvedCourse.term.description] 

38 self.filewriter.writerow(csvWriteList) 

39 return "File Downloaded Created Successfully" 

40 

41 except Exception as e: 

42 return e