def user_home_main_view(request): """ User Home page. Lands on the dashboard page If not authenticated redirects back to main page :param request: :return: Renders user_home page (dashboard) """ request, alert_type, alert_message = init_alerts(request=request) user_adapter = UserAdapter() user_profile_tools = UserProfileTools() if USER_PROFILE_MODEL not in request.session: user_model = user_adapter.get_user_instance_from_django_user(request.user) if not user_model: return HttpResponseRedirect(reverse(SERVICE_USER_SETUP)) request.session[USER_PROFILE_MODEL] = user_profile_tools.get_profile(user_model) context = { REQUEST: request, USER: request.user, ALERT_MESSAGE: alert_message, ALERT_TYPE: alert_type, USER_PROFILE_MODEL: request.session.get(USER_PROFILE_MODEL) } return render(request, USER_HOME_PAGE, context)
def user_quizarena_solve_view(request, quiz_id): """ Attempt the quiz here. Setup for the player to attempt. Disallow repeated attempts. :param request: :param quiz_id: :return: """ request, alert_type, alert_message = init_alerts(request=request) quiz_adapter = QuizAdapter() quiz_tools = QuizTools() quiz = quiz_adapter.exists(quiz_id) if not quiz: set_alert_session(session=request.session, message="No such quiz present", alert_type=DANGER) return HttpResponseRedirect(reverse(SERVICE_USER_QUIZARENA_HOME)) content = quiz_tools.download_quiz_content(quiz_model=quiz) if not content: set_alert_session(session=request.session, message="This quiz is unavailable", alert_type=DANGER) return HttpResponseRedirect(reverse(SERVICE_USER_QUIZARENA_HOME)) context = { REQUEST: request, USER: request.user, ALERT_MESSAGE: alert_message, ALERT_TYPE: alert_type, 'quiz_form': content.quiz_form } request.session['quiz'] = content.quiz_model return render(request, USER_QUIZARENA_SOLVE_PAGE, context)
def register_view(request): """ Register Returns page when user clicks the register button or redirected from invalid registration :param request: User request :return: Renders page """ request, alert_type, alert_message = init_alerts(request=request) return render(request, REGISTER_PAGE, {ALERT_MESSAGE: alert_message, ALERT_TYPE: alert_type})
def register_view(request): """ Register Returns page when user clicks the register button or redirected from invalid registration :param request: User request :return: Renders page """ request, alert_type, alert_message = init_alerts(request=request) return render(request, REGISTER_PAGE, { ALERT_MESSAGE: alert_message, ALERT_TYPE: alert_type })
def user_quizarena_result_view(request): """ Result of the quiz attempt :param request: :return: """ request, alert_type, alert_message = init_alerts(request=request) quiz = request.session.get('quiz') user_adapter = UserAdapter() quiz_tools = QuizTools() # Get the user model from the request. user = user_adapter.get_user_instance_from_django_user(request.user) if not quiz or not user: set_alert_session(session=request.session, message="Invalid quiz attempt.", alert_type=DANGER) return HttpResponseRedirect(reverse(SERVICE_USER_QUIZARENA_HOME)) content = quiz_tools.download_quiz_content(quiz_model=quiz) if not content: set_alert_session(session=request.session, message="This quiz is unavailable", alert_type=DANGER) return HttpResponseRedirect(reverse(SERVICE_USER_QUIZARENA_HOME)) answer_key = content.answer_key user_answer_key = dict(request.POST) quiz_result_model = quiz_tools.get_quiz_result(quiz_model=quiz, answer_key=answer_key, user_answer_key=user_answer_key) # Save result in model UserProfileTools.save_attempted_quiz_profile(session=request.session, quiz_result_model=quiz_result_model) # Save the profile user_profile_tools = UserProfileTools() future = user_profile_tools.save_profile(request.session) context = { REQUEST: request, USER: request.user, ALERT_MESSAGE: alert_message, ALERT_TYPE: alert_type, 'result': quiz_result_model } # Clearing the session del request.session['quiz'] # Wait for asynchronous callback future.result() return render(request, USER_QUIZARENA_RESULT_PAGE, context)
def user_story_home_view(request): """ User write a story home page. :param request: :return: """ request, alert_type, alert_message = init_alerts(request=request) context = { REQUEST: request, USER: request.user, ALERT_MESSAGE: alert_message, ALERT_TYPE: alert_type, } return render(request, USER_STORY_HOME_PAGE, context)
def user_quiz_init_view(request): """ Landing quiz page and verifier and name checker :param request: User request :return: renders quiz name form """ request, alert_type, alert_message = init_alerts(request=request) context = { REQUEST: request, USER: request.user, ALERT_MESSAGE: alert_message, ALERT_TYPE: alert_type, # "quiz_tag_names": json.dumps(quiz_tag_names, ensure_ascii=False) } return render(request, USER_QUIZ_INIT_PAGE, context)
def user_quizarena_home_view(request): """ Home page for the quiz arena :param request: :return: """ request, alert_type, alert_message = init_alerts(request=request) quiz_adapter = QuizAdapter() quiz_list = quiz_adapter.get_all_models() context = { REQUEST: request, USER: request.user, ALERT_MESSAGE: alert_message, ALERT_TYPE: alert_type, 'quiz_list': quiz_list } return render(request, USER_QUIZARENA_HOME_PAGE, context)
def main_home_view(request): """ Main home page Sends alerts if registration successful or login failure :param request: user request :return: renders a page with context """ # Clearing and displaying any alerts passed request, alert_type, alert_message = init_alerts(request=request) # Creating context context = { REQUEST: request, USER: request.user, ALERT_MESSAGE: alert_message, ALERT_TYPE: alert_type } return render(request, MAIN_HOME_PAGE, context)
def user_quiz_verifier_view(request): """ Verifies the form for errors Asks the user to prepare the answer key :param request: User request :return: Message depending on success or failure of quiz creation. """ request, alert_type, alert_message = init_alerts(request=request) quiz_tools = QuizTools() # Quiz form is the HTML form which can be displayed and submitted by a user quiz_form = request.POST.get('quiz_form') # Quiz data is the template created by the form builder. This can be editted quiz_data = request.POST.get('quiz_data') try: # Do not allow empty forms to be submitted if not quiz_data or not quiz_form: raise ValueError("Quiz data was not provided") # Parse the form to remove irrelevant data. # It will be better and cleaner to change the javascript so this will not be required quiz_form = quiz_tools.parse_form(quiz_form=quiz_form) except ValueError as err: set_alert_session(session=request.session, message=str(err), alert_type=DANGER) return HttpResponseRedirect(reverse(SERVICE_USER_QUIZ_INIT)) # Add this form so that it can be displayed in the next page context = { 'quiz_form': quiz_form, } # Save to session to pass to the next view quiz_complete_model = request.session.get('quiz_complete_model') quiz_complete_model.quiz_form = quiz_form quiz_complete_model.quiz_data = quiz_data session_insert_keys(request.session, quiz_complete_model=quiz_complete_model) return render(request, USER_QUIZ_VERIFIER_PAGE, context)
def controller_wrapper(*args, **kwargs): """ Wrapper function :param kwargs: :return: """ if kwargs: raise NotImplementedException("Keywords arguments not allowed. Only args.") request = args[0] request, alert_type, alert_message = init_alerts(request=request) result = controller_func(*args) if result.redirect: return HttpResponseRedirect(reverse(result.view)) result.context[REQUEST] = request result.context[USER] = request.user result.context[ALERT_MESSAGE] = alert_message result.context[ALERT_TYPE] = alert_type return render(request, result.view, result.context)
def user_myquiz_home_view(request): """ My quizzes home page :param request: User request :return: Renders page """ request, alert_type, alert_message = init_alerts(request=request) user_adapter = UserAdapter() quiz_adapter = QuizAdapter() user = user_adapter.get_user_instance_from_django_user(request.user) quiz_list = quiz_adapter.get_models_for_owner(user_model=user) context = { REQUEST: request, USER: request.user, ALERT_MESSAGE: alert_message, ALERT_TYPE: alert_type, 'quiz_list': quiz_list } return render(request, USER_MYQUIZ_HOME_PAGE, context)
def user_quizarena_solve_view(request, quiz_id): """ Attempt the quiz here. Setup for the player to attempt. Disallow repeated attempts. :param request: :param quiz_id: :return: """ request, alert_type, alert_message = init_alerts(request=request) quiz_adapter = QuizAdapter() quiz_tools = QuizTools() quiz = quiz_adapter.exists(quiz_id) try: if not quiz: raise ValueError("No such quiz present") quiz_complete_model = quiz_tools.download_quiz_content(quiz_model=quiz) if not quiz_complete_model: raise ValueError("This quiz is unavailable") quiz_tools.check_attempt_eligibility((request.session.get(USER_PROFILE_MODEL)), quiz_complete_model, quiz_id) except ValueError as err: set_alert_session(session=request.session, message=str(err), alert_type=DANGER) return HttpResponseRedirect(reverse(SERVICE_USER_QUIZARENA_HOME)) context = { REQUEST: request, USER: request.user, ALERT_MESSAGE: alert_message, ALERT_TYPE: alert_type, 'quiz_form': quiz_complete_model.quiz_form } request.session['quiz'] = quiz_complete_model.quiz_model return render(request, USER_QUIZARENA_SOLVE_PAGE, context)
def user_quiz_maker_view(request): """ Quiz creation page for user :param request: User request :return: Renders quiz page """ # Get the post variables from the quiz init view. quiz_name = request.POST.get('quiz_name') quiz_description = request.POST.get('quiz_description') start_date_time = request.POST.get('start_date_time') end_date_time = request.POST.get('end_date_time') attempts = request.POST.get('attempts') pass_percentage = request.POST.get('pass_percentage') tag_names = request.POST.get('quiz_tags') local = timezone.get_current_timezone() event_model = None # TODO: Remove this from here and put it in a function. event_start_date_time = None event_end_date_time = None if start_date_time and end_date_time: event_start_date_time = (local.localize(parse(start_date_time), is_dst=None)).astimezone(timezone.utc) event_end_date_time = (local.localize(parse(end_date_time), is_dst=None)).astimezone(timezone.utc) event_model = EventModel(start_time=event_start_date_time, end_time=event_end_date_time) request, alert_type, alert_message = init_alerts(request=request) # Initialize the adapters. user_adapter = UserAdapter() quiz_adapter = QuizAdapter() quiz_tag_adapter = QuizTagAdapter() context = { REQUEST: request, USER: request.user, ALERT_MESSAGE: alert_message, ALERT_TYPE: alert_type } try: # Get the user model from the request. user = user_adapter.get_user_instance_from_django_user(request.user) if not user: raise ValueError("User not recognized") if not quiz_name.rstrip(): raise ValueError("Quiz Name cannot be blank") if not re.match("^[A-Za-z0-9_ -]*$", quiz_name): raise ValueError("The quiz Name can contain ony alphanumeric characters, spaces, '-', '?' and '_'") # quiz_name could be None if there were errors in creating the model # Check if quiz_id is not set after creating the id. (This will mostly be true as we already have this check in # the javascript) quiz = quiz_adapter.get_quiz_for_owner(user, quiz_name) if quiz: raise ValueError("Quiz already present") # Create the quiz model quiz = quiz_adapter.create_model(quiz_name=quiz_name, quiz_description=quiz_description, quiz_owner=user, start_time=event_start_date_time, end_time=event_end_date_time) quiz_complete_model = QuizCompleteModel(quiz_model=quiz, attempts=attempts, pass_percentage=pass_percentage, event_model=event_model, tags=list()) # Create the tag names and put them into the quiz_complete_model tag_names_list = quiz_tag_adapter.split_and_verify_tag_names(tag_names) if tag_names_list: for tag_name in tag_names_list: quiz_complete_model.tags.append(tag_name) except ValueError as err: set_alert_session(session=request.session, message=str(err), alert_type=DANGER) return HttpResponseRedirect(reverse(SERVICE_USER_QUIZ_INIT)) session_insert_keys(session=request.session, quiz_complete_model=quiz_complete_model) return render(request, USER_QUIZ_MAKER_PAGE, context)
def user_quiz_maker_view(request): """ Quiz creation page for user :param request: User request :return: Renders quiz page """ # Get the post variables from the quiz init view. quiz_name = request.POST.get('quiz_name') quiz_description = request.POST.get('quiz_description') start_date_time = request.POST.get('start_date_time') end_date_time = request.POST.get('end_date_time') attempts = request.POST.get('attempts') pass_percentage = request.POST.get('pass_percentage') tag_names = request.POST.get('quiz_tags') local = timezone.get_current_timezone() event_model = None # TODO: Remove this from here and put it in a function. if start_date_time and end_date_time: event_start_date_time = (local.localize(parse(start_date_time), is_dst=None)).astimezone( timezone.utc) event_end_date_time = (local.localize(parse(end_date_time), is_dst=None)).astimezone( timezone.utc) event_model = EventModel(start_time=event_start_date_time, end_time=event_end_date_time) request, alert_type, alert_message = init_alerts(request=request) # Initialize the adapters. user_adapter = UserAdapter() quiz_adapter = QuizAdapter() quiz_tag_adapter = QuizTagAdapter() quiz_tools = QuizTools() context = { REQUEST: request, USER: request.user, ALERT_MESSAGE: alert_message, ALERT_TYPE: alert_type } try: # Get the user model from the request. user = user_adapter.get_user_instance_from_django_user(request.user) if not user: raise ValueError("User not recognized") # quiz_name could be None if there were errors in creating the model # Check if quiz_id is not set after creating the id. (This will mostly be true as we already have this check in # the javascript) quiz_id = quiz_tools.get_quiz_id(username=user.username, quiz_name=quiz_name) # Quiz ID is duplicate if quiz_adapter.exists(quiz_id): raise ValueError("Quiz ID already present") # Create the quiz model quiz = quiz_adapter.create_model(quiz_id=quiz_id, quiz_name=quiz_name, quiz_description=quiz_description, quiz_owner=user) quiz_complete_model = QuizCompleteModel( quiz_model=quiz, attempts=attempts, pass_percentage=pass_percentage, event_model=event_model, tags=list()) # Create the tag names and put them into the quiz_complete_model tag_names_list = quiz_tag_adapter.split_and_verify_tag_names(tag_names) if tag_names_list: for tag_name in tag_names_list: quiz_complete_model.tags.append(tag_name) except ValueError as err: set_alert_session(session=request.session, message=str(err), alert_type=DANGER) return HttpResponseRedirect(reverse(SERVICE_USER_QUIZ_INIT)) session_insert_keys(session=request.session, quiz_complete_model=quiz_complete_model) return render(request, USER_QUIZ_MAKER_PAGE, context)