Ejemplo n.º 1
0
def render_code_editor_with_tabs(request, level_defaults, lang, max_level, level_number, menu, translations, version, loaded_program, adventures, adventure_name):

  if not level_defaults:
    return utils.page_404 (translations, menu, current_user(request) ['username'], lang, translations.get_translations (lang, 'ui').get ('no_such_level'))


  arguments_dict = {}

  # Meta stuff
  arguments_dict['level_nr'] = str(level_number)
  arguments_dict['lang'] = lang
  arguments_dict['level'] = level_number
  arguments_dict['prev_level'] = int(level_number) - 1 if int(level_number) > 1 else None
  arguments_dict['next_level'] = int(level_number) + 1 if int(level_number) < max_level else None
  arguments_dict['menu'] = menu
  arguments_dict['latest'] = version
  arguments_dict['selected_page'] = 'code'
  arguments_dict['page_title'] = f'Level {level_number} – Hedy'
  arguments_dict['auth'] = translations.get_translations (lang, 'Auth')
  arguments_dict['username'] = current_user(request) ['username']
  arguments_dict['is_teacher'] = is_teacher(request)
  arguments_dict['loaded_program'] = loaded_program
  arguments_dict['adventures'] = adventures
  arguments_dict['adventure_name'] = adventure_name

  # Translations
  arguments_dict.update(**translations.get_translations(lang, 'ui'))

  # Merge level defaults into adventures so it is rendered as the first tab
  arguments_dict.update(**attr.asdict(level_defaults))

  return render_template("code-page.html", **arguments_dict)
Ejemplo n.º 2
0
    def get_class_info(user, class_id):
        if not is_teacher(user):
            return utils.page_403(ui_message='retrieve_class')
        Class = DATABASE.get_class(class_id)
        if not Class or Class['teacher'] != user['username']:
            return utils.page_404(ui_message='no_such_class')

        if hedy_content.Adventures(g.lang).has_adventures():
            adventures = hedy_content.Adventures(
                g.lang).get_adventure_keyname_name_levels()
        else:
            adventures = hedy_content.Adventures(
                "en").get_adventure_keyname_name_levels()
        levels = hedy_content.LevelDefaults(g.lang).levels
        preferences = DATABASE.get_customizations_class(class_id)

        return render_template(
            'customize-class.html',
            page_title=hedyweb.get_page_title('customize class'),
            class_info={
                'name': Class['name'],
                'id': Class['id']
            },
            levels=levels,
            adventures=adventures,
            preferences=preferences,
            current_page='for-teachers')
Ejemplo n.º 3
0
    def prejoin_class(class_id, link):
        Class = DATABASE.get_class(class_id)
        if not Class or Class['link'] != link:
            return utils.page_404(ui_message='invalid_class_link')
        user = {}
        if request.cookies.get(cookie_name):
            token = DATABASE.get_token(request.cookies.get(cookie_name))
            if token:
                if token['username'] in Class.get('students', []):
                    return render_template(
                        'class-already-joined.html',
                        page_title=hedyweb.get_page_title('join class'),
                        current_page='my-profile',
                        class_info={'name': Class['name']})
                user = DATABASE.user_by_username(token['username'])

        return render_template('class-prejoin.html',
                               page_title=hedyweb.get_page_title('join class'),
                               current_page='my-profile',
                               class_info={
                                   'link':
                                   '/class/' + Class['id'] + '/join/' +
                                   Class['link'] + '?lang=' + g.lang,
                                   'name':
                                   Class['name'],
                               })
Ejemplo n.º 4
0
def render_code_editor_with_tabs(level_defaults, max_level, level_number, version, loaded_program, adventures, restrictions, adventure_name):
  user = current_user()

  if not level_defaults:
    return utils.page_404 (ui_message='no_such_level')


  arguments_dict = {}

  # Meta stuff
  arguments_dict['level_nr'] = str(level_number)
  arguments_dict['level'] = level_number
  arguments_dict['prev_level'] = int(level_number) - 1 if int(level_number) > 1 else None
  arguments_dict['next_level'] = int(level_number) + 1 if int(level_number) < max_level else None
  arguments_dict['example_programs'] = restrictions['example_programs']
  arguments_dict['hide_prev_level'] = restrictions['hide_prev_level']
  arguments_dict['hide_next_level'] = restrictions['hide_next_level']
  arguments_dict['menu'] = True
  arguments_dict['latest'] = version
  arguments_dict['selected_page'] = 'code'
  arguments_dict['page_title'] = f'Level {level_number} – Hedy'
  arguments_dict['username'] = user['username']
  arguments_dict['is_teacher'] = is_teacher(user)
  arguments_dict['loaded_program'] = loaded_program
  arguments_dict['adventures'] = adventures
  arguments_dict['adventure_name'] = adventure_name

  # Merge level defaults into adventures so it is rendered as the first tab
  arguments_dict.update(**attr.asdict(level_defaults))

  return render_template("code-page.html", **arguments_dict)
Ejemplo n.º 5
0
 def resolve_class_link(link_id):
     Class = DATABASE.resolve_class_link(link_id)
     if not Class:
         return utils.page_404(ui_message='invalid_class_link')
     return redirect(request.url.replace(
         '/hedy/l/' + link_id,
         '/class/' + Class['id'] + '/prejoin/' + link_id),
                     code=302)
Ejemplo n.º 6
0
    def join_class(user, class_id, link):
        Class = DATABASE.get_class(class_id)
        if not Class or Class['link'] != link:
            return utils.page_404(ui_message='invalid_class_link')

        DATABASE.add_student_to_class(Class['id'], user['username'])

        return redirect(request.url.replace(
            '/class/' + class_id + '/join/' + link, '/my-profile'),
                        code=302)
Ejemplo n.º 7
0
    def get_class(user, class_id):
        app.logger.info('This is info output')
        if not is_teacher(user):
            return utils.page_403(ui_message='retrieve_class')
        Class = DATABASE.get_class(class_id)
        if not Class or Class['teacher'] != user['username']:
            return utils.page_404(ui_message='no_such_class')
        students = []
        for student_username in Class.get('students', []):
            student = DATABASE.user_by_username(student_username)
            programs = DATABASE.programs_for_user(student_username)
            highest_level = max(
                program['level']
                for program in programs) if len(programs) else 0
            sorted_public_programs = list(
                sorted(
                    [program for program in programs if program.get('public')],
                    key=lambda p: p['date']))
            if sorted_public_programs:
                latest_shared = sorted_public_programs[-1]
                latest_shared['link'] = f"/hedy/{latest_shared['id']}/view"
            else:
                latest_shared = None
            students.append({
                'username':
                student_username,
                'last_login':
                utils.datetotimeordate(
                    utils.mstoisostring(student['last_login'])),
                'programs':
                len(programs),
                'highest_level':
                highest_level,
                'latest_shared':
                latest_shared
            })

        if utils.is_testing_request(request):
            return jsonify({
                'students': students,
                'link': Class['link'],
                'name': Class['name'],
                'id': Class['id']
            })
        return render_template(
            'class-overview.html',
            current_page='for-teachers',
            page_title=hedyweb.get_page_title('class overview'),
            class_info={
                'students': students,
                'link': '/hedy/l/' + Class['link'],
                'name': Class['name'],
                'id': Class['id']
            })
Ejemplo n.º 8
0
 def resolve_class_link(link_id):
     Class = DATABASE.resolve_class_link(link_id)
     if not Class:
         return utils.page_404(
             TRANSLATIONS, render_main_menu('my-profile'),
             current_user(request)['username'], requested_lang(),
             TRANSLATIONS.get_translations(requested_lang(),
                                           'ui').get('invalid_class_link'))
     return redirect(request.url.replace(
         '/hedy/l/' + link_id,
         '/class/' + Class['id'] + '/prejoin/' + link_id),
                     code=302)
Ejemplo n.º 9
0
    def join_class(user, class_id, link):
        Class = DATABASE.get_class(class_id)
        if not Class or Class['link'] != link:
            return utils.page_404(
                TRANSLATIONS, render_main_menu('my-profile'),
                current_user(request)['username'], requested_lang(),
                TRANSLATIONS.get_translations(requested_lang(),
                                              'ui').get('invalid_class_link'))

        DATABASE.add_student_to_class(Class['id'], user['username'])

        return redirect(request.url.replace(
            '/class/' + class_id + '/join/' + link, '/my-profile'),
                        code=302)
Ejemplo n.º 10
0
    def prejoin_class(class_id, link):
        Class = DATABASE.get_class(class_id)
        if not Class or Class['link'] != link:
            return utils.page_404(
                TRANSLATIONS, render_main_menu('my-profile'),
                current_user(request)['username'], requested_lang(),
                TRANSLATIONS.get_translations(requested_lang(),
                                              'ui').get('invalid_class_link'))
        user = {}
        if request.cookies.get(cookie_name):
            token = DATABASE.get_token(request.cookies.get(cookie_name))
            if token:
                if token['username'] in Class.get('students', []):
                    return render_template(
                        'class-already-joined.html',
                        lang=requested_lang(),
                        auth=TRANSLATIONS.get_translations(
                            requested_lang(), 'Auth'),
                        menu=render_main_menu('my-profile'),
                        username=current_user(request)['username'],
                        current_page='my-profile',
                        class_info={'name': Class['name']})
                user = DATABASE.user_by_username(token['username'])

        return render_template(
            'class-prejoin.html',
            lang=requested_lang(),
            auth=TRANSLATIONS.get_translations(requested_lang(), 'Auth'),
            menu=render_main_menu('my-profile'),
            username=current_user(request)['username'],
            is_teacher=is_teacher(request),
            current_page='my-profile',
            class_info={
                'link':
                os.getenv('BASE_URL') + '/class/' + Class['id'] + '/join/' +
                Class['link'] + '?lang=' + requested_lang(),
                'name':
                Class['name']
            })
Ejemplo n.º 11
0
    def get_class(user, class_id):
        if not is_teacher(request):
            return 'Only teachers can retrieve classes', 403
        Class = DATABASE.get_class(class_id)
        if not Class or Class['teacher'] != user['username']:
            return utils.page_404(
                TRANSLATIONS, render_main_menu('my-profile'),
                current_user(request)['username'], requested_lang(),
                TRANSLATIONS.get_translations(requested_lang(),
                                              'ui').get('no_such_class'))
        students = []
        for student_username in Class.get('students', []):
            student = DATABASE.user_by_username(student_username)
            programs = DATABASE.programs_for_user(student_username)
            highest_level = max(
                program['level']
                for program in programs) if len(programs) else 0
            sorted_public_programs = list(
                sorted(
                    [program for program in programs if program.get('public')],
                    key=lambda p: p['date']))
            if sorted_public_programs:
                latest_shared = sorted_public_programs[-1]
                latest_shared['link'] = os.getenv(
                    'BASE_URL') + f"/hedy/{latest_shared['id']}/view"
            else:
                latest_shared = None
            students.append({
                'username':
                student_username,
                'last_login':
                utils.mstoisostring(student['last_login']),
                'programs':
                len(programs),
                'highest_level':
                highest_level,
                'latest_shared':
                latest_shared
            })

        if utils.is_testing_request(request):
            return jsonify({
                'students': students,
                'link': Class['link'],
                'name': Class['name'],
                'id': Class['id']
            })
        return render_template(
            'class-overview.html',
            lang=requested_lang(),
            auth=TRANSLATIONS.get_translations(requested_lang(), 'Auth'),
            menu=render_main_menu('my-profile'),
            username=current_user(request)['username'],
            is_teacher=is_teacher(request),
            current_page='my-profile',
            class_info={
                'students': students,
                'link': os.getenv('BASE_URL') + '/hedy/l/' + Class['link'],
                'name': Class['name'],
                'id': Class['id']
            })