def _handle_files(section_name: str, module_name: str, module_modname: str, module_id: str, module_contents: []) -> [File]: """ Iterates over all files that are in a module or assignment and returns a list of all files @param module_contents: The list of content of the module or assignment. @params: All necessary parameters to create a file. @return: A list of files that exist in a module. """ files = [] for content in module_contents: content_type = content.get('type', '') content_filename = content.get('filename', '') content_filepath = content.get('filepath', '/') if content_filepath is None: content_filepath = '/' content_filesize = content.get('filesize', 0) content_fileurl = content.get('fileurl', '') content_timemodified = content.get('timemodified', 0) content_isexternalfile = content.get('isexternalfile', False) if content_fileurl == '' and module_modname == 'url': continue hash_description = None if content_type == 'description': content_description = content.get('description', '') hashable_description = ResultsHandler._filter_changing_attributes( content_description) m = hashlib.sha1() m.update(hashable_description.encode('utf-8')) hash_description = m.hexdigest() new_file = File( module_id=module_id, section_name=section_name, module_name=module_name, content_filepath=content_filepath, content_filename=content_filename, content_fileurl=content_fileurl, content_filesize=content_filesize, content_timemodified=content_timemodified, module_modname=module_modname, content_type=content_type, content_isexternalfile=content_isexternalfile, hash=hash_description, ) if content_type == 'description': new_file.text_content = content_description files.append(new_file) return files
def changes_to_notify(self) -> [Course]: changed_courses = [] conn = sqlite3.connect(self.db_file) conn.row_factory = sqlite3.Row cursor = conn.cursor() cursor.execute("""SELECT course_id, course_fullname FROM files WHERE notified = 0 GROUP BY course_id;""") curse_rows = cursor.fetchall() for course_row in curse_rows: course = Course(course_row['course_id'], course_row['course_fullname'], []) cursor.execute( """SELECT * FROM files WHERE notified = 0 AND course_id = ?;""", (course.id, ), ) file_rows = cursor.fetchall() course.files = [] for file_row in file_rows: notify_file = File.fromRow(file_row) if notify_file.modified or notify_file.moved: # add reference to new file cursor.execute( """SELECT * FROM files WHERE old_file_id = ?;""", (notify_file.file_id, ), ) file_row = cursor.fetchone() if file_row is not None: notify_file.new_file = File.fromRow(file_row) course.files.append(notify_file) changed_courses.append(course) conn.close() return changed_courses
def new_file(self, file: File, course_id: int, course_fullname: str): # saves a file to index conn = sqlite3.connect(self.db_file) cursor = conn.cursor() data = {'course_id': course_id, 'course_fullname': course_fullname} data.update(file.getMap()) cursor.execute( """INSERT INTO files (course_id, course_fullname, module_id, section_name, module_name, content_filepath, content_filename, content_fileurl, content_filesize, content_timemodified, module_modname, content_type, content_isexternalfile, saved_to, time_stamp, modified, deleted, notified, hash) VALUES (:course_id, :course_fullname, :module_id, :section_name, :module_name, :content_filepath, :content_filename, :content_fileurl, :content_filesize, :content_timemodified, :module_modname, :content_type, :content_isexternalfile, :saved_to, :time_stamp, :modified, :deleted, 0, :hash); """, data) conn.commit() conn.close()
def get_stored_files(self) -> [Course]: # get all stored files (that are not yet deleted) conn = sqlite3.connect(self.db_file) conn.row_factory = sqlite3.Row cursor = conn.cursor() stored_courses = [] cursor.execute("""SELECT course_id, course_fullname FROM files WHERE deleted = 0 GROUP BY course_id;""") curse_rows = cursor.fetchall() for course_row in curse_rows: course = Course(course_row['course_id'], course_row['course_fullname'], []) cursor.execute( """SELECT * FROM files WHERE deleted = 0 AND course_id = ?;""", (course.id, )) file_rows = cursor.fetchall() course.files = [] for file_row in file_rows: notify_file = File.fromRow(file_row) course.files.append(notify_file) stored_courses.append(course) conn.close() return stored_courses
def _handle_files(section_name: str, module_name: str, module_modname: str, module_id: str, module_contents: []) -> [File]: """ Iterates over all files that are in a module or assignment and returns a list of all files @param module_contents: The list of content of the module or assignment. @params: All necessary parameters to create a file. @return: A list of files that exist in a module. """ files = [] for content in module_contents: content_type = content.get("type", "") content_filename = content.get("filename", "") content_filepath = content.get("filepath", "/") if content_filepath is None: content_filepath = '/' content_filesize = content.get("filesize", 0) content_fileurl = content.get("fileurl", "") content_timemodified = content.get("timemodified", 0) content_isexternalfile = content.get("isexternalfile", False) files.append( File(module_id=module_id, section_name=section_name, module_name=module_name, content_filepath=content_filepath, content_filename=content_filename, content_fileurl=content_fileurl, content_filesize=content_filesize, content_timemodified=content_timemodified, module_modname=module_modname, content_type=content_type, content_isexternalfile=content_isexternalfile)) return files
def _handle_description(section_name: str, module_name: str, module_modname: str, module_id: str, module_description: str) -> [File]: """ Creates a description file @param module_description: The description of the module @params: All necessary parameters to create a file. @return: A list of files that exist in a module. """ files = [] content_type = 'description' content_filename = module_name content_filepath = '/' content_filesize = len(module_description) content_fileurl = '' content_timemodified = 0 content_isexternalfile = False m = hashlib.sha1() hashable_description = ResultsHandler._filter_changing_attributes( module_description) m.update(hashable_description.encode('utf-8')) hash_description = m.hexdigest() if module_modname == 'url': module_modname = 'url_description' description = File( module_id=module_id, section_name=section_name, module_name=module_name, content_filepath=content_filepath, content_filename=content_filename, content_fileurl=content_fileurl, content_filesize=content_filesize, content_timemodified=content_timemodified, module_modname=module_modname, content_type=content_type, content_isexternalfile=content_isexternalfile, hash=hash_description, ) description.text_content = module_description files.append(description) return files
def modifie_file(self, file: File, course_id: int, course_fullname: str): conn = sqlite3.connect(self.db_file) cursor = conn.cursor() data_new = {'course_id': course_id, 'course_fullname': course_fullname} data_new.update(file.getMap()) if file.old_file is not None: # insert a new file, # but it is already notified because the same file already exists # as modified data_new.update({ 'old_file_id': file.old_file.file_id, 'modified': 0, 'moved': 0, 'deleted': 0, 'notified': 1 }) cursor.execute(File.INSERT, data_new) data_old = { 'course_id': course_id, 'course_fullname': course_fullname } data_old.update(file.old_file.getMap()) cursor.execute( """UPDATE files SET notified = 0, modified = 1, saved_to = :saved_to WHERE file_id = :file_id; """, data_old, ) else: # this should never happen, but the old file is not saved in the # file descriptor, so we need to inform about the new file # notified = 0 data_new.update({ 'modified': 0, 'deleted': 0, 'moved': 0, 'notified': 0 }) cursor.execute(File.INSERT, data_new) conn.commit() conn.close()
def delete_file(self, file: File, course_id: int, course_fullname: str): conn = sqlite3.connect(self.db_file) cursor = conn.cursor() data = {'course_id': course_id, 'course_fullname': course_fullname} data.update(file.getMap()) cursor.execute( """UPDATE files SET notified = 0, deleted = 1, time_stamp = :time_stamp WHERE file_id = :file_id; """, data) conn.commit() conn.close()
def new_file(self, file: File, course_id: int, course_fullname: str): # saves a file to index conn = sqlite3.connect(self.db_file) cursor = conn.cursor() data = {'course_id': course_id, 'course_fullname': course_fullname} data.update(file.getMap()) data.update({'modified': 0, 'deleted': 0, 'moved': 0, 'notified': 0}) cursor.execute(File.INSERT, data) conn.commit() conn.close()
def delete_file(self, file: File, course_id: int, course_fullname: str): conn = sqlite3.connect(self.db_file) cursor = conn.cursor() data = {'course_id': course_id, 'course_fullname': course_fullname} data.update(file.getMap()) cursor.execute( """UPDATE files SET notified = 0, deleted = 1, time_stamp = :time_stamp WHERE module_id = :module_id AND course_id = :course_id AND course_fullname = :course_fullname AND section_name = :section_name AND content_filepath = :content_filepath AND content_filename = :content_filename AND content_fileurl = :content_fileurl AND content_filesize = :content_filesize AND content_timemodified = :content_timemodified AND deleted = 0; """, data) conn.commit() conn.close()