Coverage for app/logic/fileHandler.py: 87%
68 statements
« prev ^ index » next coverage.py v7.2.7, created at 2024-08-27 21:57 +0000
« prev ^ index » next coverage.py v7.2.7, created at 2024-08-27 21:57 +0000
1import os
2from flask import redirect, url_for
3from app import app
4from app.models.attachmentUpload import AttachmentUpload
6class FileHandler:
7 def __init__(self, files=None, courseId=None, eventId=None):
8 self.files = files
9 self.path = app.config['files']['base_path']
10 self.courseId = courseId
11 self.eventId = eventId
12 if courseId:
13 self.path = os.path.join(self.path, app.config['files']['course_attachment_path'], str(courseId))
14 elif eventId:
15 self.path = os.path.join(self.path, app.config['files']['event_attachment_path'])
17 def makeDirectory(self):
18 try:
19 extraDir = str(self.eventId) if self.eventId else ""
20 os.makedirs(os.path.join(self.path, extraDir))
21 except OSError as e:
22 if e.errno != 17:
23 print(f'Fail to create directory: {e}')
24 raise e
26 def getFileFullPath(self, newfilename=''):
27 try:
28 filePath = (os.path.join(self.path, newfilename))
29 except AttributeError:
30 pass
31 except FileExistsError:
32 pass
33 return filePath
35 def saveFiles(self, saveOriginalFile=None):
36 try:
37 for file in self.files:
38 saveFileToFilesystem = None
39 if self.eventId:
40 attachmentName = str(saveOriginalFile.id) + "/" + file.filename
41 isFileInEvent = AttachmentUpload.select().where(AttachmentUpload.event_id == self.eventId,
42 AttachmentUpload.fileName == attachmentName).exists()
43 if not isFileInEvent:
44 AttachmentUpload.create(event=self.eventId, fileName=attachmentName)
45 if saveOriginalFile and saveOriginalFile.id == self.eventId:
46 saveFileToFilesystem = attachmentName
47 elif self.courseId:
48 isFileInCourse = AttachmentUpload.select().where(AttachmentUpload.course == self.courseId, AttachmentUpload.fileName == file.filename).exists()
49 if not isFileInCourse:
50 AttachmentUpload.create(course=self.courseId, fileName=file.filename)
51 saveFileToFilesystem = file.filename
52 else:
53 saveFileToFilesystem = file.filename
54 if saveFileToFilesystem:
55 self.makeDirectory()
56 file.save(self.getFileFullPath(newfilename=saveFileToFilesystem))
57 except AttributeError:
58 pass
60 def retrievePath(self, files):
61 pathDict = {}
62 for file in files:
63 pathDict[file.fileName] = ((self.path + "/" + file.fileName)[3:], file)
64 return pathDict
66 def deleteFile(self, fileId):
67 file = AttachmentUpload.get_by_id(fileId)
68 file.delete_instance()
69 if not AttachmentUpload.select().where(AttachmentUpload.fileName == file.fileName).exists():
70 path = os.path.join(self.path, file.fileName)
71 os.remove(path)
73 def changeDisplay(self, fileId, isDisplayed):
74 file = AttachmentUpload.get_by_id(fileId)
76 # Uncheck all other checkboxes for the same event
77 AttachmentUpload.update(isDisplayed=False).where(AttachmentUpload.event == file.event).execute()
79 # Check the selected checkbox
80 AttachmentUpload.update(isDisplayed=isDisplayed).where(AttachmentUpload.id == fileId).execute()
81 return ""