def __call__(self, *args): exercise_number = args[0] moodle_data = self._storage.moodle_data self.printer.inform('Connecting to Moodle and collecting data.') self.printer.inform('This may take a few seconds.') with self._moodle: submissions = self._moodle.find_submissions( moodle_data.course_id, moodle_data.exercise_prefix, exercise_number, self.printer ) self.printer.inform(f"Found a total of {len(submissions)} for '{moodle_data.exercise_prefix}{exercise_number}'") all_students = {student.moodle_student_id: student for student in self._storage.all_students} submissions = [submission for submission in submissions if submission.moodle_student_id in all_students] self.printer.inform(f"Found {len(submissions)} submissions") folder = os.path.join( self._storage.storage_config.root, self._storage.storage_config.submission_root, f'{self._storage.storage_config.exercise_template}{exercise_number}', self._storage.storage_config.raw_folder ) ensure_folder_exists(folder) for submission in submissions: target_filename = os.path.join(folder, submission.file_name) if os.path.isfile(target_filename): self.printer.warning(f"Target path {submission.file_name} exists!") if self.printer.ask("Continue? ([y]/n)") == "n": break with open(target_filename, 'wb') as fp: try: self.printer.inform(f"Downloading submission of {all_students[submission.moodle_student_id]} ... ", end='') self._moodle.download(submission.url, fp) self.printer.confirm('[Ok]') except Exception as e: self.printer.error('[Err]') self.printer.error(str(e)) with open(os.path.join(folder, "meta.json"), 'w') as fp: try: self.printer.inform(f'Write meta data ... ', end='') dump([s.__dict__ for s in submissions], fp, indent=4) self.printer.confirm('[Ok]') except Exception as e: self.printer.error('[Err]') self.printer.error(str(e))
def save_meta_to_folder(self, directory): ensure_folder_exists(directory) meta_data = dict() meta_data["credits_per_task"] = self._credits_per_task meta_data["names"] = [ student.muesli_name for student in self._students ] meta_data["muesli_ids"] = [ student.muesli_student_id for student in self._students ] meta_data["muesli_mails"] = [ student.muesli_mail for student in self._students ] with open(os.path.join(directory, "meta.json"), 'w', encoding="utf-8") as fp: j_dump(meta_data, fp, indent=4) feedback_path = os.path.join(directory, self._file_name) with open(feedback_path, 'w', encoding="utf-8") as fp: for line in self._feedback: print(line, file=fp)
def __call__(self, exercise_number): moodle_data = self._storage.moodle_data self.printer.inform('Connecting to Moodle and collecting data.') self.printer.inform('This may take a few seconds.') with self._moodle: submissions = self._moodle.find_submissions( moodle_data.course_id, moodle_data.exercise_prefix, exercise_number, self.printer) self.printer.inform( f"Found a total of {len(submissions)} for '{moodle_data.exercise_prefix}{exercise_number}'" ) all_students = { student.moodle_student_id: student for student in self._storage.all_students } submissions = [ submission for submission in submissions if submission.moodle_student_id in all_students ] self.printer.inform(f"Found {len(submissions)} submissions") # Check for duplicates existing_submissions = {} for submission in submissions: if submission.file_name.lower() in existing_submissions: original_student = self._storage.get_student_by_moodle_id( existing_submissions[ submission.file_name.lower()].moodle_student_id) replace_student = self._storage.get_student_by_moodle_id( submission.moodle_student_id) self.printer.warning( f"Duplicate file {submission.file_name}. Original was uploaded by {original_student.moodle_name}, this was uploaded by {replace_student.moodle_name}" ) if self.printer.yes_no( "Do you want to replace the original file?", "n"): existing_submissions[ submission.file_name.lower()] = submission else: existing_submissions[ submission.file_name.lower()] = submission submissions = list(existing_submissions.values()) folder = os.path.join( self._storage.storage_config.root, self._storage.storage_config.submission_root, f'{self._storage.storage_config.exercise_template}{exercise_number}', self._storage.storage_config.raw_folder) ensure_folder_exists(folder) for submission in submissions: target_filename = os.path.join(folder, submission.file_name) if os.path.isfile(target_filename): self.printer.warning( f"Target path {submission.file_name} exists!") if self.printer.ask("Continue? ([y]/n)") == "n": break with open(target_filename, 'wb') as fp: try: self.printer.inform( f"Downloading submission of {all_students[submission.moodle_student_id]} ... ", end='') self._moodle.download(submission.url, fp) self.printer.confirm('[Ok]') except Exception as e: self.printer.error('[Err]') self.printer.error(str(e)) with open(os.path.join(folder, "meta.json"), 'w') as fp: try: self.printer.inform(f'Write meta data ... ', end='') dump([s.__dict__ for s in submissions], fp, indent=4) self.printer.confirm('[Ok]') except Exception as e: self.printer.error('[Err]') self.printer.error(str(e))