def home(request, hack=None): JS_FILES = ( "dist/js/bower-libs.min.js", "dist/js/unminifiable-libs.js", "dist/js/anatomy.min.js", ) CSS_FILES = ( "dist/css/bower-libs.css", "dist/css/app.css", ) if not hasattr(request.user, "userprofile") or request.user.userprofile is None: environment = get_environment() user = json.dumps({ 'user': {}, 'number_of_answers': environment.number_of_answers(user=request.user.id) if request.user.id is not None else 0, 'number_of_correct_answers': environment.number_of_correct_answers(user=request.user.id) if request.user.id is not None else 0, }) email = '' else: if hack is None: return redirect('/overview/') user = json.dumps(request.user.userprofile.to_json(stats=True)) email = request.user.email if not request.user.userprofile.public: request.user.userprofile.public = True request.user.userprofile.save() hour_ago = datetime.now() - timedelta(hours=1) stats = { 'number_of_answers': FlashcardAnswer.objects.count(), 'answers_per_second': FlashcardAnswer.objects.filter( time__gt=hour_ago).count() / 3600.0, 'number_of_flashcards': Flashcard.objects.filter( active=True, lang=get_language()).count(), } if hack == 'home': hack = None c = { 'title': _('Anatom.cz') + ' - ' + _('procvičování anatomie člověka v obrázcích'), 'headline': get_headline_from_url(hack), 'is_production': settings.ON_PRODUCTION, 'css_files': CSS_FILES, 'js_files': JS_FILES, 'screenshot_files': get_screenshot_files(request, hack), 'user_json': user, 'email': email, 'LANGUAGE_CODE': get_language(), 'LANGUAGES': settings.LANGUAGES, 'LANGUAGE_DOMAINS': settings.LANGUAGE_DOMAINS, 'is_homepage': hack is None, 'hack': hack or '', 'config_json': json.dumps(get_global_config()), 'DOMAIN': request.build_absolute_uri('/')[:-1], 'stats_json': json.dumps(stats), 'canonical_url': 'https://' + request.META['HTTP_HOST'] + request.get_full_path().split('?')[0].replace('//', '/'), 'base': '//' + request.META['HTTP_HOST'], 'canonical_path': request.get_full_path().split('?')[0][1:].replace('//', '/'), } return render_to_response('home.html', c)
def home(request, hack=None): JS_FILES = ( "dist/js/bower-libs.min.js", "dist/js/proso-apps-all.js", "dist/js/geography.min.js", "dist/js/geography.html.js", ) CSS_FILES = ( "dist/css/bower-libs.css", "dist/css/app.css", "dist/css/map.css" ) if not hasattr(request.user, "userprofile") or request.user.userprofile is None: environment = get_environment() user = json.dumps({ 'user': {}, 'number_of_answers': environment.number_of_answers(user=request.user.id) if request.user.id is not None else 0, 'number_of_correct_answers': environment.number_of_correct_answers(user=request.user.id) if request.user.id is not None else 0, }) email = '' else: if get_config('proso_user', 'google.openid.migration', default=True) and not is_user_id_overridden(request): migrated_user = migrate_google_openid_user(request.user) if migrated_user is not None: auth.logout(request) migrated_user.backend = 'social_auth.backends.google.GoogleOAuth2Backend' auth.login(request, migrated_user) user = json.dumps(request.user.userprofile.to_json(stats=True)) email = request.user.email c = { 'title': _(u'Slepé mapy') + ' - ' + _(u'inteligentní aplikace na procvičování zeměpisu'), 'map': get_map_from_url(hack), 'is_production': settings.ON_PRODUCTION, 'css_files': CSS_FILES, 'js_files': JS_FILES, 'continents': Category.objects.filter( lang=get_language(), type='continent').order_by('name'), 'states': Category.objects.filter( lang=get_language(), type='state').order_by('name'), 'user_json': user, 'email': email, 'LANGUAGE_CODE': get_language(), 'LANGUAGES': settings.LANGUAGES, 'is_homepage': hack is None, 'config_json': json.dumps(get_global_config()), } return render_to_response('home.html', c)
def home(request, hack=None): if not hasattr(request.user, "userprofile") or request.user.userprofile is None: environment = get_environment() user = json.dumps({ 'user': {}, 'number_of_answers': environment.number_of_answers(user=request.user.id) if request.user.id is not None else 0, 'number_of_correct_answers': environment.number_of_correct_answers(user=request.user.id) if request.user.id is not None else 0, }) email = '' else: if hack is None: return redirect('/overview/') if get_config('proso_user', 'google.openid.migration', default=True) and not is_user_id_overridden(request): migrated_user = migrate_google_openid_user(request.user) if migrated_user is not None: auth.logout(request) migrated_user.backend = 'social_auth.backends.google.GoogleOAuth2Backend' auth.login(request, migrated_user) user = json.dumps(request.user.userprofile.to_json(stats=True)) email = request.user.email c = { 'title': _(u'Slepé mapy') + ' - ' + _(u'inteligentní aplikace na procvičování zeměpisu'), 'map': get_map_from_url(hack), 'is_production': settings.ON_PRODUCTION, 'css_files': CSS_FILES, 'map_files': get_map_files(), 'js_files': JS_FILES, 'continents': Category.objects.filter( lang=get_language(), type='continent').order_by('name'), 'states': Category.objects.filter( lang=get_language(), type='state').order_by('name'), 'regions': Category.objects.filter( lang=get_language(), type='region').order_by('name'), 'user_json': user, 'email': email, 'LANGUAGE_CODE': get_language(), 'LANGUAGES': settings.LANGUAGES, 'LANGUAGE_DOMAINS': settings.LANGUAGE_DOMAINS if hasattr( settings, 'LANGUAGE_DOMAINS') else {}, 'is_homepage': hack is None, 'hack': hack or '', 'config_json': json.dumps(get_global_config()), 'DOMAIN': request.build_absolute_uri('/')[:-1], 'screenshot_files': get_screenshot_files(request, hack), } return render_to_response('home.html', c)
def config_processor(request): config = get_global_config() return {'config_json': json.dumps(config), 'config': config}
def migrate_answers(self): post_save.disconnect(update_predictive_model) Session.objects.all().delete() TaskAnswer.objects.all().delete() old_new_id_map = json.load( open('matmat/management/commands/old_new_id_map.json')) old_new_id_map = {k: v for k, v in old_new_id_map} task_instances = { t.identifier: t for t in TaskInstance.objects.all().select_related('task') } config = Config.objects.from_content(get_global_config()).id with connections['old'].cursor() as cursor: cursor.execute('SELECT ' ' a.timestamp, ' ' a.user_id, ' ' a.correctly_solved, ' ' a.solving_time, ' ' a.answer,' ' a.device, ' ' q.identifier,' ' a.log' ' FROM questions_answer a ' ' LEFT JOIN questions_question q ' ' ON q.id = a.question_id ' ' WHERE q.active IS TRUE ' ' ORDER BY a.id') sessions = defaultdict(lambda: (None, None)) answers = [] for answer in progress.bar(dict_fetch_all(cursor), every=max( 1, min(100, cursor.rowcount // 1000)), expected_size=cursor.rowcount): user = answer['user_id'] if answer['identifier'] not in old_new_id_map: print('Question ID not found: ', answer['identifier']) task_instance = task_instances[old_new_id_map[ answer['identifier']]] session, last_update = sessions[user] if session is None or (last_update is not None and (answer['timestamp'] - last_update) > timedelta(minutes=30)): session = Session.objects.create(user_id=user) sessions[user] = session, answer['timestamp'] device = answer['device'] try: meta = AnswerMeta.objects.from_content({ 'device': device, 'client_meta': json.loads(answer['log']) }) except ValueError: print('Skipping bad log for answer:', answer) meta = AnswerMeta.objects.from_content({ 'device': device, }) a = TaskAnswer( user_id=user, item_id=task_instance.item_id, item_asked_id=task_instance.item_id, item_answered_id=task_instance.item_id if answer['correctly_solved'] else None, response_time=answer['solving_time'] * 1000, lang='cs', question=task_instance.task.identifier, answer=None if answer['answer'] is None else answer['answer'][:255], session_id=session.pk, config_id=config, metainfo_id=meta.pk, time=answer['timestamp'], ) answers.append(a) a.save()
def config_processor(request): config = get_global_config() return { 'config_json': json.dumps(config), 'config': config }
def config(request): return render_json(request, get_global_config(), template='common_json.html')
def init_config(sender, instance, **kwargs): if not issubclass(sender, Answer): return if instance.config_id is None: instance.config_id = Config.objects.from_content(get_global_config()).id
def migrate_answers(self): post_save.disconnect(update_predictive_model) Session.objects.all().delete() TaskAnswer.objects.all().delete() old_new_id_map = json.load(open('matmat/management/commands/old_new_id_map.json')) old_new_id_map = {k: v for k, v in old_new_id_map} task_instances = {t.identifier: t for t in TaskInstance.objects.all().select_related('task')} config = Config.objects.from_content(get_global_config()).id with connections['old'].cursor() as cursor: cursor.execute('SELECT ' ' a.timestamp, ' ' a.user_id, ' ' a.correctly_solved, ' ' a.solving_time, ' ' a.answer,' ' a.device, ' ' q.identifier,' ' a.log' ' FROM questions_answer a ' ' LEFT JOIN questions_question q ' ' ON q.id = a.question_id ' ' WHERE q.active IS TRUE ' ' ORDER BY a.id' ) sessions = defaultdict(lambda: (None, None)) answers = [] for answer in progress.bar(dict_fetch_all(cursor), every=max(1, min(100, cursor.rowcount // 1000)), expected_size=cursor.rowcount): user = answer['user_id'] if answer['identifier'] not in old_new_id_map: print('Question ID not found: ', answer['identifier']) task_instance = task_instances[old_new_id_map[answer['identifier']]] session, last_update = sessions[user] if session is None or (last_update is not None and (answer['timestamp'] - last_update) > timedelta(minutes=30)): session = Session.objects.create(user_id=user) sessions[user] = session, answer['timestamp'] device = answer['device'] try: meta = AnswerMeta.objects.from_content({ 'device': device, 'client_meta': json.loads(answer['log']) }) except ValueError: print('Skipping bad log for answer:', answer) meta = AnswerMeta.objects.from_content({'device': device,}) a = TaskAnswer( user_id=user, item_id=task_instance.item_id, item_asked_id=task_instance.item_id, item_answered_id=task_instance.item_id if answer['correctly_solved'] else None, response_time=answer['solving_time'] * 1000, lang='cs', question=task_instance.task.identifier, answer=None if answer['answer'] is None else answer['answer'][:255], session_id=session.pk, config_id=config, metainfo_id=meta.pk, time=answer['timestamp'], ) answers.append(a) a.save()