Exemplo n.º 1
0
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 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):
    planning = _get_planning(uuid)
    if not planning:
        return jsonify({"message": 'Planning with uuid "%s" not found' % uuid}), 404

    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_path = _dl_and_save_ics_file(planning.ics_url, tmp_path)
        calendar = CalendarReader(calendar_path)
        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 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 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
class TestCalendarParsing(unittest.TestCase):

    calendar_path = '../data/multi-fr.ics'

    def setUp(self):
        self.calendar = CalendarReader(self.calendar_path)

    def test_event_count(self):
        meetings = self.calendar.get_all_meetings()
        self.assertEqual(13, len(meetings[Seminar]))
        self.assertEqual(13, len(meetings[Practica]))

        seminars = self.calendar.get_meetings_by_type(Seminar)
        practicas = self.calendar.get_meetings_by_type(Practica)
        self.assertEqual(13, len(seminars))
        self.assertEqual(13, len(practicas))

    def test_event_getters(self):
        seminars = self.calendar.get_meetings_by_type(Seminar)
        self.assertEqual(1389009600, seminars[0].calendar_event.begin.timestamp)

        expected = arrow.get(
            2014, 4, 7, 8, 0, 0, tzinfo=tz.gettz('America/Montreal')).datetime

        meeting = GenericMeeting(seminars[12].calendar_event)
        actual = meeting.get_end_datetime()
        self.assertEqual(expected, actual)

        for i, s in enumerate(seminars):
            self.assertEqual('log210 Cours magistral %d' % (i + 1),
                             s.calendar_event.name)
Exemplo n.º 7
0
class TestCalendarParsing(unittest.TestCase):

    calendar_path = '../data/multi-fr.ics'

    def setUp(self):
        self.calendar = CalendarReader(self.calendar_path)

    def test_event_count(self):
        meetings = self.calendar.get_all_meetings()
        self.assertEqual(13, len(meetings[Seminar]))
        self.assertEqual(13, len(meetings[Practicum]))
        self.assertEqual(13, len(meetings[Laboratory]))

        seminars = self.calendar.get_meetings_by_type(Seminar)
        practica = self.calendar.get_meetings_by_type(Practicum)
        laboratories = self.calendar.get_meetings_by_type(Laboratory)
        self.assertEqual(13, len(seminars))
        self.assertEqual(13, len(practica))
        self.assertEqual(13, len(laboratories))

    def test_event_getters(self):
        seminars = self.calendar.get_meetings_by_type(Seminar)
        self.assertEqual(1389009600, seminars[0].calendar_event.begin.timestamp)

        expected = arrow.get(
            2014, 4, 7, 8, 0, 0, tzinfo=tz.gettz('America/Montreal')).datetime

        meeting = GenericMeeting(seminars[12].calendar_event)
        actual = meeting.get_end_datetime()
        self.assertEqual(expected, actual)

        for i, s in enumerate(seminars):
            self.assertEqual('log210 Cours magistral %d' % (i + 1),
                             s.calendar_event.name)
    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):
    planning = _get_planning(uuid)
    if not planning:
        return jsonify({"message": 'Planning with uuid "%s" not found' % uuid}), 404

    moodle_archive_path = planning.mbz_fullpath
    planning_txt = planning.planning_txt

    # Make tmp directory for MBZ extraction and ics download
    with tempfile.TemporaryDirectory() as tmp_path:
        # Download calendar to tmp folder
        calendar_path = _dl_and_save_ics_file(planning.ics_url, tmp_path)
        calendar = CalendarReader(calendar_path)
        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)

    # Build preview
    preview = []

    if planning_txt:
        for line in planning_txt.split("\n"):
            event = interpreter.get_new_event_from_string(line)
            pretty_event_name = event.get_pretty_name()

            preview.append(
                {"title": "%s %d opens" % (pretty_event_name, event.rel_id), "timestamp": event.get_start_timestamp()}
            )
            preview.append(
                {"title": "%s %d closes" % (pretty_event_name, event.rel_id), "timestamp": event.get_end_timestamp()}
            )

    for meeting_type in calendar_meetings:
        clazz = meeting_type.__name__

        for i, meeting in enumerate(calendar_meetings[meeting_type]):
            rel_id = i + 1
            preview.append({"title": "%s %d opens" % (clazz, rel_id), "timestamp": meeting.get_start_timestamp()})
            preview.append({"title": "%s %d closes" % (clazz, rel_id), "timestamp": meeting.get_end_timestamp()})

    # Return preview sorted by timestamp
    return jsonify({"preview": sorted(preview, key=lambda p: p["timestamp"])}), 200
Exemplo n.º 10
0
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 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
Exemplo n.º 12
0
 def setUp(self):
     self.calendar = CalendarReader(self.calendar_path)
 def setUp(self):
     self.calendar = CalendarReader(self.calendar_path)