Esempio n. 1
0
async def create_html_lessons(lang='en'):
    for a_dir in os.listdir("./lesson_source/"):
        if a_dir.startswith('.') or a_dir.startswith('_'):
            continue
        path = os.path.abspath('lesson_source/{}'.format(a_dir)) + '/'
        images = path + 'images'
        path += lang
        l_path = path + '.md'
        e_path = path + '.exercises'
        m_path = path + '.meta'
        with open(l_path) as file:
            html = markdown.markdown(file.read(), extensions=['markdown.extensions.codehilite'])
        with open('static/lessons/{}.html'.format(a_dir), 'w') as file:
            file.write(HEADER + html + FOOTER)
        with open(m_path) as file:
            meta = yaml.load(file.read())
        meta['author'] = 1
        lesson = models.Lesson(**meta)
        lid = await lesson.update_or_create(*meta.keys())
        with open(e_path) as file:
            exe = yaml.load(file.read())
        if exe:
            for _, val in exe.items():
                exercise = models.Exercise(lesson=lid, **val)
                await exercise.update_or_create(*val.keys())
        dest = os.path.abspath('static/lessons/images/')
        if os.path.exists(images):
            for file in os.listdir(images):
                src = os.path.join(images, file)
                if os.path.isfile(src):
                    dst = dest + '/' + file
                    shutil.copy(src, dst)
                    print(src + ' copied')
                else:
                    print(src + ' NOT copied')
def make_exercise(exercise_name, *prereqs):
    query = models.Exercise.all().filter('name =', exercise_name)
    if query.get():
        raise "Already exists!"

    prereqs = [p for p in prereqs]
    if prereqs and hasattr(prereqs[0], "name"):
        prereqs = [e.name for e in prereqs]

    exercise = models.Exercise(name=exercise_name,
                               prerequisites=list(prereqs),
                               covers=[],
                               author=None,
                               live=True)
    exercise.put()
    return exercise
Esempio n. 3
0
import json
import models

session = models.Session()

exercises = json.load(open('exercises.json', 'r'))

for category in exercises['exercise_categories']:
    session.add(models.ExerciseCategory(**category))

for exercise in exercises['exercises']:
    session.add(models.Exercise(**exercise))

session.commit()
Esempio n. 4
0
 async def process(a_dir, lang=lang):
     less_counter = LessonCounter()
     if a_dir.startswith('.') or a_dir.startswith('_'):
         return
     path = os.path.abspath('../lesson_source/{}'.format(a_dir)) + '/'
     images = path + 'images'
     path += lang
     l_path = path + '.md'
     e_path = path + '.exercises'
     m_path = path + '.meta'
     q_path = path + '.quiz'
     try:  # lesson generation will be deprecated in future
         with open(l_path) as file:
             html = markdown.markdown(file.read(),
                                      extensions=[
                                          'markdown.extensions.codehilite',
                                          'markdown.extensions.tables'
                                      ])
     except FileNotFoundError:
         return
     with open(m_path) as file:
         meta = yaml.load(file.read())
     meta['author'] = DEFAULT_USER
     meta['file'] = '{}.html'.format(a_dir)
     meta['lesson_no'] = int(a_dir)
     try:
         with open(q_path) as file:
             questions = yaml.load(file.read())
         less_counter.quiz_outcome = 'found'
     except Exception as err:
         questions = False
         less_counter.quiz_outcome = 'none'
     if questions:
         quiz = models.Quiz(title=meta['title'],
                            users=DEFAULT_USER,
                            description=meta['description'])
         quiz_id = await quiz.update_or_create('title')
         meta['quiz'] = quiz_id
         question_order = 1
         for _, val in questions.items():
             try:
                 question = models.Question(**val)
                 qid = await question.update_or_create(*val.keys())
                 qq = models.QuizQuestions(quiz=quiz_id,
                                           question=qid,
                                           question_order=question_order)
                 question_order += 1
                 await qq.update_or_create('question', 'quiz')
                 less_counter.quiz_details_done += 1
             except Exception as err:
                 print(err)
                 less_counter.quiz_details_error += 1
     try:
         lesson = models.Lesson(**meta)
         lid, updated = await lesson.update_or_create('lesson_no',
                                                      verbose=True)
         less_counter.lesson_outcome = 'found'
         if updated:
             less_counter.lesson_outcome = 'updated'
             counter.updated_lessons += 1
         else:
             less_counter.lesson_outcome = 'created'
             counter.added_lessons += 1
     except Exception as err:
         print(err)
         less_counter.lesson_outcome += 'error'
         counter.error_lessons += 1
     try:
         with open(e_path) as file:
             exe = yaml.load(file)
             less_counter.exercise_outcome = 'found'
     except Exception as err:
         exe = False
         less_counter.exercise_outcome = 'not found'
         print(err)
     if exe:
         try:
             for val in exe.values():
                 exercise = models.Exercise(lesson=lid, **val)
                 id, updated = await exercise.update_or_create('title',
                                                               verbose=True)
                 if updated:
                     less_counter.exercise_details_updated += 1
                 else:
                     less_counter.exercise_details_created += 1
         except Exception as err:
             print('error creating exercise')
             less_counter.exercise_details_error += 1
             print(exe)
             print(err)
     dest = os.path.abspath('static/images/')
     if os.path.exists(images):
         for file in os.listdir(images):
             src = os.path.join(images, file)
             if os.path.isfile(src):
                 dst = dest + '/' + file
                 shutil.copy(src, dst)
                 less_counter.lesson_imgs_done += 1
             else:
                 less_counter.lesson_imgs_errors += 1
     return less_counter
Esempio n. 5
0
    def get(self):
        user = models.UserData.current().user

        exercise_name = self.request.get('name')
        if not exercise_name:
            self.response.out.write(
                "No exercise submitted, please resubmit if you just logged in."
            )
            return

        query = models.Exercise.all()
        query.filter('name =', exercise_name)
        exercise = query.get()
        if not exercise:
            exercise = models.Exercise(name=exercise_name)
            exercise.prerequisites = []
            exercise.covers = []
            exercise.author = user
            exercise.summative = self.request_bool("summative", default=False)
            path = os.path.join(os.path.dirname(__file__),
                                exercise_name + '.html')

        v_position = self.request.get('v_position')
        h_position = self.request.get('h_position')
        short_display_name = self.request.get('short_display_name')

        add_video = self.request.get('add_video')
        delete_video = self.request.get('delete_video')
        add_playlist = self.request.get('add_playlist')
        delete_playlist = self.request.get('delete_playlist')

        exercise.prerequisites = []
        for c_check_prereq in range(0, 1000):
            prereq_append = self.request_string("prereq-%s" % c_check_prereq,
                                                default="")
            if prereq_append and not prereq_append in exercise.prerequisites:
                exercise.prerequisites.append(prereq_append)

        exercise.covers = []
        for c_check_cover in range(0, 1000):
            cover_append = self.request_string("cover-%s" % c_check_cover,
                                               default="")
            if cover_append and not cover_append in exercise.covers:
                exercise.covers.append(cover_append)

        if v_position:
            exercise.v_position = int(v_position)

        if h_position:
            exercise.h_position = int(h_position)

        if short_display_name:
            exercise.short_display_name = short_display_name

        exercise.live = self.request_bool("live", default=False)

        if not exercise.is_saved():
            # Exercise needs to be saved before checking related videos.
            exercise.put()

        video_keys = []
        for c_check_video in range(0, 1000):
            video_name_append = self.request_string("video-%s-readable" %
                                                    c_check_video,
                                                    default="")
            if video_name_append:
                video = models.Video.get_for_readable_id(video_name_append)
                if not video.key() in video_keys:
                    video_keys.append(str(video.key()))

            video_append = self.request_string("video-%s" % c_check_video,
                                               default="")
            if video_append and not video_append in video_keys:
                video_keys.append(video_append)

        query = models.ExerciseVideo.all()
        query.filter('exercise =', exercise.key())
        existing_exercise_videos = query.fetch(1000)

        existing_video_keys = []
        for exercise_video in existing_exercise_videos:
            existing_video_keys.append(exercise_video.video.key())
            if not exercise_video.video.key() in video_keys:
                exercise_video.delete()

        for video_key in video_keys:
            if not video_key in existing_video_keys:
                exercise_video = models.ExerciseVideo()
                exercise_video.exercise = exercise
                exercise_video.video = db.Key(video_key)
                exercise_video.exercise_order = models.VideoPlaylist.all(
                ).filter('video =', exercise_video.video).get().video_position
                exercise_video.put()

        exercise.put()

        #Start ordering
        ExerciseVideos = models.ExerciseVideo.all().filter(
            'exercise =', exercise.key()).fetch(1000)
        playlists = []
        for exercise_video in ExerciseVideos:
            playlists.append(
                models.VideoPlaylist.get_cached_playlists_for_video(
                    exercise_video.video))

        if playlists:

            playlists = list(itertools.chain(*playlists))
            titles = map(lambda pl: pl.title, playlists)
            playlist_sorted = []
            for p in playlists:
                playlist_sorted.append([p, titles.count(p.title)])
            playlist_sorted.sort(key=lambda p: p[1])
            playlist_sorted.reverse()

            playlists = []
            for p in playlist_sorted:
                playlists.append(p[0])
            playlist_dict = {}
            exercise_list = []
            playlists = list(set(playlists))
            for p in playlists:
                playlist_dict[p.title] = []
                for exercise_video in ExerciseVideos:
                    if p.title in map(
                            lambda pl: pl.title,
                            models.VideoPlaylist.
                            get_cached_playlists_for_video(
                                exercise_video.video)):
                        playlist_dict[p.title].append(exercise_video)
                        # ExerciseVideos.remove(exercise_video)

                if playlist_dict[p.title]:
                    playlist_dict[p.title].sort(
                        key=lambda e: models.VideoPlaylist.all().filter(
                            'video =', e.video).filter('playlist =', p).get(
                            ).video_position)
                    exercise_list.append(playlist_dict[p.title])

            if exercise_list:
                exercise_list = list(itertools.chain(*exercise_list))
                for e in exercise_list:
                    e.exercise_order = exercise_list.index(e)
                    e.put()

        self.redirect('/editexercise?saved=1&name=' + exercise_name)
Esempio n. 6
0
 async def process(a_dir, lang=lang):
     if a_dir.startswith('.') or a_dir.startswith('_'):
         return
     path = os.path.abspath('lesson_source/{}'.format(a_dir)) + '/'
     images = path + 'images'
     path += lang
     l_path = path + '.md'
     e_path = path + '.exercises'
     m_path = path + '.meta'
     q_path = path + '.quiz'
     try:
         with open(l_path) as file:
             html = markdown.markdown(
                 file.read(), extensions=['markdown.extensions.codehilite'])
     except FileNotFoundError:
         return
     with open('static/lessons/{}.html'.format(a_dir), 'w') as file:
         file.write(HEADER + html + FOOTER)
     with open(m_path) as file:
         meta = yaml.load(file.read())
     meta['author'] = 1
     meta['file'] = '{}.html'.format(a_dir)
     try:
         with open(q_path) as file:
             questions = yaml.load(file.read())
         print('found quiz')
     except Exception as err:
         questions = False
         print(err)
     if questions:
         quiz = models.Quiz(title=meta['title'],
                            users=14,
                            description=meta['description'])
         quiz_id = await quiz.update_or_create('title')
         meta['quiz'] = quiz_id
         question_order = 1
         for _, val in questions.items():
             qustion = models.Question(**val)
             qid = await qustion.update_or_create(*val.keys())
             qq = models.QuizQuestions(quiz=quiz_id,
                                       question=qid,
                                       question_order=question_order)
             question_order += 1
             await qq.update_or_create('question', 'quiz')
             print('question created')
     lesson = models.Lesson(**meta)
     lid = await lesson.update_or_create(*meta.keys())
     try:
         with open(e_path) as file:
             exe = yaml.load(file.read())
     except:
         exe = False
     if exe:
         try:
             for _, val in exe.items():
                 exercise = models.Exercise(lesson=lid, **val)
                 await exercise.update_or_create(*val.keys())
         except Exception as err:
             print('error creating exercise')
             print(exe)
             print(val)
             print(err)
     dest = os.path.abspath('static/images/')
     if os.path.exists(images):
         for file in os.listdir(images):
             src = os.path.join(images, file)
             if os.path.isfile(src):
                 dst = dest + '/' + file
                 shutil.copy(src, dst)
                 print(src + ' copied')
             else:
                 print(src + ' NOT copied')
Esempio n. 7
0
    def get(self):
        if not users.is_current_user_admin():
            self.redirect(users.create_login_url(self.request.uri))
            return

        user = util.get_current_user()

        exercise_name = self.request.get('name')
        if not exercise_name:
            self.response.out.write(
                "No exercise submitted, please resubmit if you just logged in."
            )
            return

        query = models.Exercise.all()
        query.filter('name =', exercise_name)
        exercise = query.get()
        if not exercise:
            exercise = models.Exercise(name=exercise_name)
            exercise.prerequisites = []
            exercise.covers = []
            exercise.author = user
            exercise.summative = self.request_bool("summative", default=False)
            path = os.path.join(os.path.dirname(__file__),
                                exercise_name + '.html')
            raw_html = self.request.get('raw_html')
            if not os.path.exists(
                    path) and not exercise.summative and raw_html:
                exercise.raw_html = db.Text(raw_html)
                exercise.last_modified = datetime.datetime.now()
                exercise.ensure_sanitized()

        v_position = self.request.get('v_position')
        h_position = self.request.get('h_position')
        short_display_name = self.request.get('short_display_name')

        add_video = self.request.get('add_video')
        delete_video = self.request.get('delete_video')
        add_playlist = self.request.get('add_playlist')
        delete_playlist = self.request.get('delete_playlist')

        exercise.prerequisites = []
        for c_check_prereq in range(0, 1000):
            prereq_append = self.request_string("prereq-%s" % c_check_prereq,
                                                default="")
            if prereq_append and not prereq_append in exercise.prerequisites:
                exercise.prerequisites.append(prereq_append)

        exercise.covers = []
        for c_check_cover in range(0, 1000):
            cover_append = self.request_string("cover-%s" % c_check_cover,
                                               default="")
            if cover_append and not cover_append in exercise.covers:
                exercise.covers.append(cover_append)

        if v_position:
            exercise.v_position = int(v_position)

        if h_position:
            exercise.h_position = int(h_position)

        if short_display_name:
            exercise.short_display_name = short_display_name

        exercise.live = self.request_bool("live", default=False)

        if not exercise.is_saved():
            # Exercise needs to be saved before checking related videos.
            exercise.put()

        video_keys = []
        for c_check_video in range(0, 1000):
            video_append = self.request_string("video-%s" % c_check_video,
                                               default="")
            if video_append and not video_append in video_keys:
                video_keys.append(video_append)

        query = models.ExerciseVideo.all()
        query.filter('exercise =', exercise.key())
        existing_exercise_videos = query.fetch(1000)

        existing_video_keys = []
        for exercise_video in existing_exercise_videos:
            existing_video_keys.append(exercise_video.video.key())
            if not exercise_video.video.key() in video_keys:
                exercise_video.delete()

        for video_key in video_keys:
            if not video_key in existing_video_keys:
                exercise_video = models.ExerciseVideo()
                exercise_video.exercise = exercise
                exercise_video.video = db.Key(video_key)
                exercise_video.put()

        exercise.put()

        self.redirect('/editexercise?saved=1&name=' + exercise_name)