def programs_page (request): username = current_user(request) ['username'] if not username: return "unauthorized", 403 from_user = request.args.get('user') or None if from_user and not is_admin (request): return "unauthorized", 403 texts=TRANSLATIONS.data [requested_lang ()] ['Programs'] ui=TRANSLATIONS.data [requested_lang ()] ['ui'] adventures = load_adventure_for_language(requested_lang ())['adventures'] result = db_get_many ('programs', {'username': from_user or username}, True) programs = [] now = timems () for item in result: measure = texts ['minutes'] date = round ((now - item ['date']) / 60000) if date > 90: measure = texts ['hours'] date = round (date / 60) if date > 36: measure = texts ['days'] date = round (date / 24) programs.append ({'id': item ['id'], 'code': item ['code'], 'date': texts ['ago-1'] + ' ' + str (date) + ' ' + measure + ' ' + texts ['ago-2'], 'level': item ['level'], 'name': item ['name'], 'adventure_name': item.get ('adventure_name'), 'public': item.get ('public')}) return render_template('programs.html', lang=requested_lang(), menu=render_main_menu('programs'), texts=texts, ui=ui, auth=TRANSLATIONS.data [requested_lang ()] ['Auth'], programs=programs, username=username, current_page='programs', from_user=from_user, adventures=adventures)
def index(level, step): # Sublevel requested if re.match ('\d+-\d+', level): pass # If level has a dash, we keep it as a string # Normal level requested elif re.match ('\d', level): try: g.level = level = int(level) except: return 'No such Hedy level!', 404 else: return 'No such Hedy level!', 404 g.lang = requested_lang() g.prefix = '/hedy' initialize_gfi_session(g.lang) loaded_program = '' loaded_program_name = '' adventure_name = '' # If step is a string that has more than two characters, it must be an id of a program if step and type_check (step, 'str') and len (step) > 2: result = db_get ('programs', {'id': step}) if not result: return 'No such program', 404 # If the program is not public, allow only the owner of the program, the admin user and the teacher users to access the program user = current_user (request) public_program = 'public' in result and result ['public'] if not public_program and user ['username'] != result ['username'] and not is_admin (request) and not is_teacher (request): return 'No such program!', 404 loaded_program = result ['code'] loaded_program_name = result ['name'] if 'adventure_name' in result: adventure_name = result ['adventure_name'] # We default to step 1 to provide a meaningful default assignment step = 1 adventure_assignments = load_adventure_assignments_per_level(g.lang, level) return hedyweb.render_assignment_editor( request=request, course=HEDY_COURSE[g.lang], level_number=level, assignment_number=step, menu=render_main_menu('hedy'), translations=TRANSLATIONS, version=version(), adventure_assignments=adventure_assignments, loaded_program=loaded_program, loaded_program_name=loaded_program_name, adventure_name=adventure_name)
def index(level, step): # Sublevel requested if re.match('\d+-\d+', level): pass # If level has a dash, we keep it as a string # Normal level requested elif re.match('\d', level): try: g.level = level = int(level) except: return 'No such Hedy level!', 404 else: return 'No such Hedy level!', 404 g.lang = requested_lang() g.prefix = '/hedy' loaded_program = '' adventure_name = '' # If step is a string that has more than two characters, it must be an id of a program if step and isinstance(step, str) and len(step) > 2: result = DATABASE.program_by_id(step) if not result: return 'No such program', 404 # If the program is not public, allow only the owner of the program, the admin user and the teacher users to access the program user = current_user(request) public_program = 'public' in result and result['public'] if not public_program and user['username'] != result[ 'username'] and not is_admin(request) and not is_teacher( request): return 'No such program!', 404 loaded_program = { 'code': result['code'], 'name': result['name'], 'adventure_name': result.get('adventure_name') } if 'adventure_name' in result: adventure_name = result['adventure_name'] adventures = load_adventures_per_level(g.lang, level) return hedyweb.render_code_editor_with_tabs(request=request, course=HEDY_COURSE[g.lang], level_number=level, menu=render_main_menu('hedy'), translations=TRANSLATIONS, version=version(), adventures=adventures, loaded_program=loaded_program, adventure_name=adventure_name)
def programs_page(request): username = current_user(request)['username'] if not username: # redirect users to /login if they are not logged in url = request.url.replace('/programs', '/login') return redirect(url, code=302) from_user = request.args.get('user') or None if from_user and not is_admin(request): if not is_teacher(request): return "unauthorized", 403 students = DATABASE.get_teacher_students(username) if from_user not in students: return "unauthorized", 403 texts = TRANSLATIONS.get_translations(requested_lang(), 'Programs') ui = TRANSLATIONS.get_translations(requested_lang(), 'ui') adventures = load_adventure_for_language(requested_lang())['adventures'] result = DATABASE.programs_for_user(from_user or username) programs = [] now = timems() for item in result: program_age = now - item['date'] if program_age < 1000 * 60 * 60: measure = texts['minutes'] date = round(program_age / (1000 * 60)) elif program_age < 1000 * 60 * 60 * 24: measure = texts['hours'] date = round(program_age / (1000 * 60 * 60)) else: measure = texts['days'] date = round(program_age / (1000 * 60 * 60 * 24)) programs.append({ 'id': item['id'], 'code': item['code'], 'date': texts['ago-1'] + ' ' + str(date) + ' ' + measure + ' ' + texts['ago-2'], 'level': item['level'], 'name': item['name'], 'adventure_name': item.get('adventure_name'), 'public': item.get('public') }) return render_template('programs.html', lang=requested_lang(), menu=render_main_menu('programs'), texts=texts, ui=ui, auth=TRANSLATIONS.get_translations( requested_lang(), 'Auth'), programs=programs, username=username, is_teacher=is_teacher(request), current_page='programs', from_user=from_user, adventures=adventures)