def polls_list(request): template_name = 'polls_list.html' courses = get_subscribed_courses(request.user) polls = Poll.objects.filter(course__in=courses) context = get_default_context(request, Poll, None, polls) return render(request, template_name, context)
def poll_results(request, pk): template_name = 'poll_results.html' courses = get_subscribed_courses(request.user) poll = get_object_or_404(Poll, pk=pk, course__in=courses) context = get_default_context(request, Poll, poll) if hasattr(request.user, "student") and not SubmittedResult.objects.filter(poll=poll, student=request.user.student): return redirect('poll_mode', pk) return render(request, template_name, context)
def poll_mode(request, pk): template_name = 'poll_mode.html' courses = get_subscribed_courses(request.user) poll = get_object_or_404(Poll, pk=pk, course__in=courses) context = get_default_context(request, Poll, poll) if hasattr(request.user, "trainer") or SubmittedResult.objects.filter(poll=poll, student=request.user.student): print("Super bad situation!") return redirect('poll_results', pk) if request.method == 'POST': selected_options = [] user_options = {} for k,v in request.POST._iterlists(): try: question_pk = int(k) if "usr" in v: # User-defined option found del v[v.index("usr")] user_options[question_pk] = request.POST.get("usr_%s" % question_pk, "") option_ids = list(map(int, v)) except (IndexError, TypeError, ValueError): continue q = Question.objects.filter(pk=question_pk).first() if q.poll == poll: for option in Option.objects.filter(pk__in=option_ids): selected_options.append(option) option.votes += 1 option.save() else: return redirect('/') result = SubmittedResult.objects.update_or_create( poll=poll, student=request.user.student)[0] if result.options.all(): result.options.clear() if result.user_options.all(): result.user_options.clear() for option in selected_options: result.options.add(option) for k,v in user_options.items(): user_option = UserOption.objects.update_or_create( question=Question.objects.filter(pk=k).first(), student=request.user.student, content=v)[0] result.user_options.add(user_option) result.save() return redirect('poll_results', pk) return render(request, template_name, context)
def quiz_result(request, submittedresult_id): model = SubmittedResult context = get_default_context(request, model, None) submitted_result = get_one_or_404(SubmittedResult, pk=submittedresult_id) quiz = submitted_result.quiz user = request.user answers = submitted_result.answers.all() courses = get_subscribed_courses(user) offline_courses = get_subscribed_offline_courses(user) questions = [] quiz_list = set() for course in courses: for q in course.quiz_set.all(): quiz_list.add(q) # Add quizes from offline courses for off_course in offline_courses: for session in get_allowed_offline_sessions(off_course, user): quiz_list.add(session.quiz) if quiz not in quiz_list: raise NotFound else: recalculate_if_needed(submitted_result) is_trainer = False try: is_trainer = bool(user.trainer) except ObjectDoesNotExist: pass for answer in answers: questions.append(answer.question) questions = set(questions) context.update( submitted_result=submitted_result, questions=questions, is_trainer=is_trainer, ) return render(request, 'quiz/submitted_result.html', context)
def list_quizes(request): model = Quiz user = request.user courses = get_subscribed_courses(user) offline_courses = get_subscribed_offline_courses(user) context = get_default_context(request, model, None) quizes = set() for course in courses: for quiz in course.quiz_set.all(): quizes.add(quiz) # Add quizes from offline courses for off_course in offline_courses: for session in get_allowed_offline_sessions(off_course, user): quizes.add(session.quiz) context.update(quizes=quizes, ) return render(request, 'quiz/list_quizes.html', context)
def list_threads(request): discussion_threads = DiscussionThread.objects.all() # GLO API Key: e80d6b8cbea3754bab60a51a6e72b35329df377b # payload= {'login':'******', 'password':'******'} # with requests.Session() as s: # r = s.get('https://glo.globallogic.com/apps/glo/login') # r = s.post('https://glo.globallogic.com/apps/glo/authenticate', data=payload) # print(r.text) # credentials = b"r.rzhenetskyy:R1986ttyl" # en_credentials = base64.b64encode(credentials) # s = requests.session() # r = s.post('http://gloapis.globallogic.com/e80d6b8cbea3754bab60a51a6e72b35329df377b/gloapis/login', headers={'Authorization': en_credentials}) # print(r.text) # token = ET.fromstring(r.content) # lst = [] # for node in token.iter(): # lst.append(node.text) # print(lst[1]) # p = s.get('http://gloapis.globallogic.com/e80d6b8cbea3754bab60a51a6e72b35329df377b/%s/gloapis/users/r.rzhenetskyy/profile_picture/profile' % token) # print(p) if request.method == "POST" and not request.POST['thread_subject'] == '': discussion_thread = DiscussionThread.objects.get_or_create( author=request.user, subject=request.POST["thread_subject"], content=request.POST["thread_content"], ) discussion_thread = discussion_thread[0] discussion_thread.save() return redirect('discussions:thread_detail', discussionthread_id=discussion_thread.id) context = get_default_context(request, DiscussionThread, None, discussion_threads) return render(request, 'discussions/list_threads.html', context)
def quiz_mode(request, quiz_id): model = Quiz quiz = get_one_or_404(Quiz, pk=quiz_id) question_list = quiz.question_set.all().order_by('?')[:quiz.test_length] user = request.user # gets the currently logged user context = get_default_context(request, model, None) courses = get_subscribed_courses( user) # lists all courses the user is subscribed for offline_courses = get_subscribed_offline_courses(user) quiz_list = set() is_trainer = False try: is_trainer = bool(user.trainer) except ObjectDoesNotExist: pass for course in courses: for q in course.quiz_set.all(): quiz_list.add(q) # Add quizes from offline courses for off_course in offline_courses: for session in get_allowed_offline_sessions(off_course, user): quiz_list.add(session.quiz) if is_trainer: context = { 'quiz': quiz, 'courses': courses, } return render(request, 'quiz/quiz_detail.html', context) elif quiz not in quiz_list: raise NotFound else: if SubmittedResult.objects.filter( quiz=quiz, student=user.student ).exists( ): # checks if there is submitted_result object for this quiz/user submitted_result = get_one_or_404(SubmittedResult, quiz=quiz, student=user.student) recalculate_if_needed(submitted_result) return redirect('quiz:submitted_result', submittedresult_id=submitted_result.id) else: if request.method == "POST": if quiz in quiz_list: # if user is subscribed to the course this quiz is associated with submitted_result = SubmittedResult.objects.get_or_create( student=user.student, quiz=quiz) if submitted_result[1] == False: submitted_result = submitted_result[0] return redirect('quiz:submitted_result', submittedresult_id=submitted_result.id) else: submitted_result = submitted_result[0] for question, answer in request.POST._iterlists(): try: correct = [ str(answer.id) for answer in Question.objects.get( id=int(question)).answer_set.all() if answer.correct ] # returns list of only correct answers for the question for a in answer: submitted_result.answers.add( Answer.objects.get(id=int(a)) ) # adds chosen answers to the submitted_result object if ( set(answer) == set(correct) ): # checks if the question was answered correctly submitted_result.score += Question.objects.get( id=int(question)).score submitted_result.questions_passed += 1 except: continue if submitted_result.quiz.passed_by_score: # checks if quiz is being passed by score, else by number of correct questions if submitted_result.score >= submitted_result.quiz.passing_score: submitted_result.passed = True else: if submitted_result.questions_passed >= submitted_result.quiz.passing_questions: submitted_result.passed = True submitted_result.save() return redirect('quiz:submitted_result', submittedresult_id=submitted_result.id) number_of_tries = QuizTries.objects.get_or_create(student=user.student, quiz=quiz) number_of_tries = number_of_tries[0] number_of_tries.tries += 1 number_of_tries.save() context.update( question_list=question_list, quiz_name=quiz.name, ) return render(request, 'quiz/quiz_mode.html', context)
def thread_detail(request, discussionthread_id): discussion_thread = get_object_or_404(DiscussionThread, pk=discussionthread_id) discussion_messages = discussion_thread.discussionmessage_set.all() paginator = Paginator(discussion_messages, 5) page = request.GET.get('page') pages = [x for x in range(1, paginator.num_pages + 1)] if len(pages) > 5: if page == None or int(page) == 1 or int(page) <= 3: # if page is 1-3 pages = pages[:5] + ['...'] elif int(page) == pages[-1]: # if page is last pages = ['...'] + pages[int(page) - 5:] elif int(page) > 3 and not int( page) == pages[-2]: # if page is 4 and further if int(page) == pages[-3]: pages = ['...'] + pages[int(page) - 3:int(page) + 2] else: pages = ['...'] + pages[int(page) - 3:int(page) + 2] + ['...'] else: # if page is pre-last pages = ['...'] + pages[int(page) - 4:] try: messages = paginator.page(page) except PageNotAnInteger: messages = paginator.page(1) except EmptyPage: messages = paginator.page(paginator.num_pages) if request.method == "POST" and 'message' in request.POST and request.POST[ 'message'] != '': if 'messageID' in request.POST: message = get_object_or_404(DiscussionMessage, pk=request.POST['messageID']) if message.author == request.user: message.content = request.POST['message'] message.save() else: message = DiscussionMessage.objects.create( author=request.user, content=request.POST['message'], thread=DiscussionThread.objects.get(id=discussion_thread.id), ) if 'target_messageid' in request.POST: message.quote = request.POST['quotedmessage'] message.save() last_page = paginator.num_pages if discussion_messages.count() % 5 == 1: last_page += 1 return HttpResponseRedirect("%s?page=%s" % (reverse( 'discussions:thread_detail', kwargs={'discussionthread_id': discussion_thread.id}), last_page)) elif request.method == "POST" and 'threadContent' in request.POST: discussion_thread = get_object_or_404(DiscussionThread, pk=request.POST['threadID']) if discussion_thread.author == request.user: discussion_thread.content = request.POST['threadContent'] discussion_thread.save() last_page = paginator.num_pages if discussion_messages.count() % 5 == 1: last_page += 1 return HttpResponseRedirect("%s?page=%s" % (reverse( 'discussions:thread_detail', kwargs={'discussionthread_id': discussion_thread.id}), last_page)) context = get_default_context(request, DiscussionThread, discussion_thread) context.update(messages=messages, pages=pages, paginator=paginator) return render(request, 'discussions/thread_detail.html', context)