def get_interpreter_and_planning_from(uuid): planning = _get_planning(uuid, g.user_id) moodle_archive_path = planning.mbz_fullpath try: calendar = CalendarReader(planning.ics_fullpath) calendar_meetings = calendar.get_all_meetings() except CAPException as e: raise e except Exception as e: raise CAPException({'type': 'danger', 'msg': str(e)}, 400) if not moodle_archive_path: return Interpreter(calendar_meetings, None), planning # Make tmp directory for MBZ extraction with tempfile.TemporaryDirectory() as tmp_path: # Extract Moodle course to tmp folder try: with tarfile.open(moodle_archive_path) as tar_file: tar_file.extractall(tmp_path) course = MoodleCourse(tmp_path) except Exception: _bad_mbz() return Interpreter(calendar_meetings, course), planning
def download_planning(uuid): try: planning = _get_planning(uuid, g.user_id) except CAPException as e: return e.res moodle_archive_path = planning.mbz_fullpath planning_txt = planning.planning_txt if not planning_txt: return _bad_request() # Make tmp directory for MBZ extraction and ics download with tempfile.TemporaryDirectory() as tmp_path: # Download calendar to tmp folder calendar = CalendarReader(planning.ics_fullpath) calendar_meetings = calendar.get_all_meetings() # Extract Moodle course to tmp folder with tarfile.open(moodle_archive_path) as tar_file: tar_file.extractall(tmp_path) course = MoodleCourse(tmp_path) interpreter = Interpreter(calendar_meetings, course) for line in planning_txt.split('\n'): event = interpreter.get_new_event_from_string(line) course.replace_event(event) folder = os.path.join(app.config['UPLOAD_FOLDER'], uuid) latest_mbz_path = os.path.join(folder, 'latest.mbz') course.write(latest_mbz_path) return send_from_directory(folder, 'latest.mbz', as_attachment=True)
def setUp(self): # Setup calendar calendar = CalendarReader(self.calendar_path) self.calendar_meetings = calendar.get_all_meetings() # Setup Moodle course self.tmp_path = tempfile.mkdtemp() with tarfile.open(self.moodle_archive_path) as tar_file: tar_file.extractall(self.tmp_path) self.course = MoodleCourse(self.tmp_path) self.interpreter = Interpreter(self.calendar_meetings, self.course)
def preview_planning(uuid): try: planning = _get_planning(uuid, g.user_id) except CAPException as e: return e.res moodle_archive_path = planning.mbz_fullpath planning_txt = planning.planning_txt # Make tmp directory for MBZ extraction with tempfile.TemporaryDirectory() as tmp_path: try: calendar = CalendarReader(planning.ics_fullpath) calendar_meetings = calendar.get_all_meetings() except Exception as e: _bad_cal() # Extract Moodle course to tmp folder course = None if moodle_archive_path: try: with tarfile.open(moodle_archive_path) as tar_file: tar_file.extractall(tmp_path) course = MoodleCourse(tmp_path) except Exception as e: return jsonify(alerts=[{ 'type': 'danger', 'msg': 'MBZ file could not be read.' }]), 400 alerts = [] preview = None inventory = None try: interpreter = Interpreter(calendar_meetings, course) inventory = _build_inventory(interpreter, planning_txt) preview = _build_preview(interpreter, planning_txt) alerts = _build_alerts_for_preview(interpreter) except InvalidSyntaxException as e: alerts.append({'type': 'danger', 'msg': e.message}) return jsonify({ 'preview': preview, 'inventory': inventory, 'alerts': alerts }), 200
def setUp(self): self.calendar = CalendarReader(self.calendar_path)