예제 #1
0
파일: views.py 프로젝트: NaomiGeng/iforj
def askquestion(request):
    """提问模板"""
    name =request.session.get('name')
    if request.method == "POST" and request.is_ajax():

        form = QuestionForm(request.POST)
        if form.is_valid():
            data = form.cleaned_data
            title = data['title']
            text = data['text']
            qtype = data['q_type'].lower()
            user = User.objects.get(name=name.split()[0])
            try:
                questiontype = QuestionType.objects.get(name=qtype)
            except QuestionType.DoesNotExist:
                questiontype = QuestionType(name=qtype)
                questiontype.save()

            question = Question(user=user, title=title, text=text, q_type=questiontype)
            question.save()


            # return HttpResponseRedirect(request.META.get('HTTP_REFERER', '/'))
            return JsonResponse({'status':'ok'})
        else:
            # return HttpResponse(request.POST.get('title'))
            return render(request, 'askquestion.html')
    if name:
        return render(request,'askquestion.html',{'QuestionForm':QuestionForm, 'name':name.split()})
    else:
        return HttpResponseRedirect("/")
예제 #2
0
파일: views.py 프로젝트: beaulian/iforj
def askquestion(request):
    """提问模板"""
    name = request.session.get('name')
    if request.method == "POST":
        form = QuestionForm(request.POST)
        if form.is_valid():
            data = form.cleaned_data
            title = data['title']
            text = data['text']
            qtype = data['q_type']
            user = User.objects.get(name=name.split()[0])
            questiontype = QuestionType(name=qtype)
            questiontype.save()
            question = Question(user=user, title=title, text=text, q_type=questiontype)
            question.save()
            # return HttpResponseRedirect(request.META.get('HTTP_REFERER', '/'))
            return JsonResponse({'status':'ok'})
        else:
            # return HttpResponse(request.POST.get('title'))
            return render(request, 'askquestion.html')
    if name:
        access_key = "wmN715-Lo5SC1jYIkuqObCLl1bhZoURTxewUGyq2"
        secret_key = "IXXeA4-Rzu9RB6nkf687UjQt9YCOp1JpWptm0C0y"
        bucket_name = "iforj"
        q = qiniu.Auth(access_key, secret_key)
        token = q.upload_token(bucket_name)
        return render(request, 'askquestion.html', {'QuestionForm': QuestionForm, 'uptoken': token, 'name':name.split()})
    else:
        return HttpResponseRedirect("/")
예제 #3
0
    def quiz(self, quiz_id: int, block: int = None, question: int = None):
        quiz = model.Quiz.query.get(quiz_id)
        if quiz is None:
            return abort(404)

        fill = model.FilledQuiz.query\
            .filter_by(user_id=current_user.id)\
            .filter_by(quiz_id=quiz_id).first()

        if fill is None:
            fill = model.FilledQuiz(
                user_id=current_user.id,
                quiz_id=quiz_id)
            model.db.session.add(fill)
            model.db.session.commit()

        if block is None:
            block = fill.current_block
        else:
            finish = request.args.get('finish', False)
            block = quiz.blocks.filter_by(order_number=block).first()
            if finish:
                fill.finished_blocks.append(block)
                model.db.session.commit()

        if block is None:
            block = quiz.blocks.order_by(model.Block.order_number).first()

        if block in fill.finished_blocks:
            answers = fill.answers\
                .join(model.Question)\
                .filter(model.Question.block_id == block.id)\
                .order_by(model.Question.order_number)

            block_points = model.int_or_float(
                sum([ans.points for ans in answers])
            )
            return self.render('answers.html', block=block,
                               answers=answers,
                               block_points=block_points,
                               total=fill.points)

        question = question or 1
        question = block.questions\
            .filter_by(order_number = question)\
            .first()

        if question not in fill.available_questions:
            return abort(403)

        if block == fill.current_block:
            form = QuestionForm.from_model(question, fill)
            return self.render('quiz.html', form=form)
        else:
            answer = fill.answers\
                .filter(model.Answer.question_id == question.id)\
                .first()
            return self. render('answer.html', answer=answer)
예제 #4
-1
파일: views.py 프로젝트: chengkeai/iforj
def askquestion(request):
    """提问模板"""
    name = request.session.get("name")
    if request.method == "POST":
        form = QuestionForm(request.POST)
        if form.is_valid():
            data = form.cleaned_data
            title = data["title"]
            text = data["text"]
            qtype = data["q_type"]
            user = User.objects.get(name=name.split()[0])
            questiontype = QuestionType(name=qtype)
            questiontype.save()
            question = Question(user=user, title=title, text=text, q_type=questiontype)
            question.save()
            # return HttpResponseRedirect(request.META.get('HTTP_REFERER', '/'))
            return JsonResponse({"status": "ok"})
        else:
            # return HttpResponse(request.POST.get('title'))
            return render(request, "askquestion.html")
    if name:
        access_key = "wmN715-Lo5SC1jYIkuqObCLl1bhZoURTxewUGyq2"
        secret_key = "IXXeA4-Rzu9RB6nkf687UjQt9YCOp1JpWptm0C0y"
        bucket_name = "iforj"
        q = qiniu.Auth(access_key, secret_key)
        token = q.upload_token(bucket_name)
        return render(
            request, "askquestion.html", {"QuestionForm": QuestionForm, "uptoken": token, "name": name.split()}
        )
    else:
        return HttpResponseRedirect("/")