コード例 #1
0
ファイル: manage.py プロジェクト: fallaha/quiza
def add_participant(uuid,
                    participants: AddParticipantsRequest,
                    current_user: User = Depends(get_current_user)):
    added_user = []
    try:
        quiz = current_user.created_quizzes.where(Quiz.uuid == uuid).get()
    except (peewee.DoesNotExist, peewee.DataError):
        return {'code': -2, 'msg': 'invalid quiz'}

    if quiz.public:
        return {'code': -12, 'msg': 'you cant add person to private quiz'}

    for email in participants.participants:
        try:
            user = User.select().where(User.email == email).get()
        except peewee.DoesNotExist:
            pass
        else:
            try:
                with database.atomic():
                    QuizUsers.create(quiz=quiz, user=user)
            except peewee.IntegrityError:
                pass
            else:
                added_user.append(user.email)
    return {'data': added_user}
コード例 #2
0
ファイル: manage.py プロジェクト: fallaha/quiza
def add_to_options(uuid,
                   number: int,
                   option: OptionRequest,
                   current_user: User = Depends(get_current_user)):
    try:
        quiz = current_user.created_quizzes.select().where(
            Quiz.uuid == uuid).get()
    except (peewee.DataError, peewee.DoesNotExist):
        return {'code': -1, 'msg': 'invalid quiz'}

    try:
        question = Question.select().where((Question.quiz == quiz) &
                                           (Question.number == number)).get()
    except peewee.DoesNotExist:
        return {'code': -5, 'msg': "this question number doesn't exist"}

    if option.number <= 0:
        return {'code': -11, 'msg': "the number should be greater than 0"}

    try:
        with database.atomic():
            option_response = MultipleOption.create(question=question,
                                                    content=option.content,
                                                    number=option.number)
    except peewee.IntegrityError:
        return {'code': -10, 'msg': "this number for option used previously."}

    return {'data': model_to_dict(option_response, recurse=False)}
コード例 #3
0
ファイル: manage.py プロジェクト: fallaha/quiza
def create_quiz_question(uuid,
                         q: QuestionRequest,
                         current_user: User = Depends(get_current_user)):
    try:
        quiz = current_user.created_quizzes.where(Quiz.uuid == uuid).get()
    except (peewee.DoesNotExist, peewee.DataError):
        return {'code': -2, 'msg': 'invalid quiz'}

    if not (q.qtype == QuestionType.MultiOption or q.qtype
            == QuestionType.Textual or q.qtype == QuestionType.FileUpload):
        return {'code': -3, 'msg': 'incorrect qtype'}

    with database.atomic():
        try:
            new_question = Question.create(qtype=q.qtype,
                                           text=q.text,
                                           quantum=q.quantum,
                                           number=q.number,
                                           photo_url=q.photo_url,
                                           quiz=quiz)
        except peewee.IntegrityError:
            return {'code': -4, 'msg': 'number is already exists'}
    if new_question:
        return {'data': model_to_dict(new_question)}
    else:
        return error_response
コード例 #4
0
ファイル: manage.py プロジェクト: fallaha/quiza
def delete_quiz(uuid, current_user: User = Depends(get_current_user)):
    with database.atomic():
        try:
            qz = current_user.created_quizzes.where(Quiz.uuid == uuid).get()
            res = qz.delete_instance()
        except (peewee.DoesNotExist, peewee.DataError):
            return {'code': -2, 'msg': 'invalid quiz'}
        if res != 1:
            return error_response
        return {'data': model_to_dict(qz)}
コード例 #5
0
ファイル: quiz.py プロジェクト: fallaha/quiza
def get_quiz_questions(uuid, current_user: User = Depends(get_current_user)):
    try:
        with database.atomic():
            questions = Quiz.select().join(QuizUsers).where(
                (Quiz.uuid == uuid)
                & ((QuizUsers.user == current_user)
                   | (Quiz.public == True))).get().questions.select().order_by(
                       Question.number)
    except (peewee.DoesNotExist, peewee.DataError):
        return {'code': -2, 'msg': 'invalid quiz'}
    return {'data': list(questions.dicts())}
コード例 #6
0
ファイル: quiz.py プロジェクト: fallaha/quiza
def get_answered_questinos(uuid,
                           current_user: User = Depends(get_current_user)):
    try:
        with database.atomic():
            quiz = Quiz.select().join(QuizUsers).where((Quiz.uuid == uuid) & (
                (QuizUsers.user == current_user)
                | (Quiz.public == True))).get()
    except (peewee.DoesNotExist, peewee.DataError):
        return {'code': -2, 'msg': 'invalid quiz'}
    ans = quiz.answeredquestion_set.select().dicts()
    return {'data': list(ans)}
コード例 #7
0
def register(req: User_Request):
    with database.atomic():
        try:
            newuser = User.create(name=req.name,
                                  password=get_hash_content(req.password),
                                  email=req.email,
                                  role=0)
        except peewee.IntegrityError:
            return {
                'code': -20,
                'msg': 'email already taken by another person'
            }
        else:
            if newuser:
                return {'msg': 'Successfully Registerd'}
            return {'code': -200, 'msg': 'error in register. try again'}
コード例 #8
0
ファイル: manage.py プロジェクト: fallaha/quiza
def quiz_creat(quiz: QuizCreateRequest,
               current_user: User = Depends(get_current_user)):
    with database.atomic():
        quiz_uuid = uuid.uuid4()
        q = Quiz.create(uuid=quiz_uuid,
                        name=quiz.name,
                        public=quiz.public,
                        onlynext=quiz.onlynext,
                        timing=quiz.timing,
                        random=quiz.random,
                        start_time=quiz.start_time,
                        end_time=quiz.end_time,
                        creator=current_user)
    if q:
        return {'data': model_to_dict(q)}
    else:
        return error_response
コード例 #9
0
ファイル: quiz.py プロジェクト: fallaha/quiza
def get_quiz_questions(uuid,
                       number: int,
                       current_user: User = Depends(get_current_user)):
    try:
        with database.atomic():
            quiz = Quiz.select().join(QuizUsers).where((Quiz.uuid == uuid) & (
                (QuizUsers.user == current_user)
                | (Quiz.public == True))).get()
    except (peewee.DoesNotExist, peewee.DataError):
        return {'code': -2, 'msg': 'invalid quiz'}
    if quiz.onlynext:
        return {'code': -6, 'msg': 'only next question support.'}
    question = quiz.questions.select().order_by(
        Question.number).limit(1).offset(abs(number - 1))
    if not len(question):
        return {'code': -7, 'msg': "this question number dose not exist."}
    # Set that user see the question at 'now' time
    AnsweredQuestion.get_or_create(user=current_user,
                                   quiz=quiz,
                                   question=question,
                                   defaults={'start_time': datetime.now()})
    return {'data': list(question.dicts())[0]}
コード例 #10
0
ファイル: manage.py プロジェクト: fallaha/quiza
def put_quiz(uuid,
             quiz: QuizCreateRequest,
             current_user: User = Depends(get_current_user)):
    with database.atomic():
        try:
            qz = current_user.created_quizzes.where(Quiz.uuid == uuid).get()
        except (peewee.DoesNotExist, peewee.DataError):
            return {'code': -2, 'msg': 'invalid quiz'}
        if quiz.public == False:
            if QuizUsers.select().where(QuizUsers.quiz == qz).count():
                return {
                    'code': -13,
                    'msg': "you can not private quiz that have participant(s)"
                }
        qz.public = quiz.public
        qz.onlynext = quiz.onlynext
        qz.timing = quiz.timing
        qz.random = quiz.random
        qz.start_time = quiz.start_time
        qz.end_time = quiz.end_time
        res = qz.save()
        if res != 1:
            return error_response
        return {'data': model_to_dict(qz)}
コード例 #11
0
ファイル: quiz.py プロジェクト: fallaha/quiza
def get_quiz_nex_questions(uuid,
                           current_user: User = Depends(get_current_user)):
    try:
        with database.atomic():
            quiz = Quiz.select().join(
                QuizUsers,
                peewee.JOIN.LEFT_OUTER,
                on=(QuizUsers.quiz == Quiz.uuid)).where((Quiz.uuid == uuid) & (
                    (QuizUsers.user == current_user) |
                    ((Quiz.public == True) & (QuizUsers.user == None)))).get()
    except (peewee.DoesNotExist, peewee.DataError):
        return {'code': -2, 'msg': 'invalid quiz'}
    # Check Quiz Time
    if quiz.end_time is not None and quiz.end_time < datetime.now():
        return {
            'code': -8,
            'msg': 'this quiz has been finished at {}'.format(quiz.end_time)
        }

    # Get Number of Questin that user answered it. so we can calculate this question
    # is nth questino
    nth = AnsweredQuestion.select().where(
        (AnsweredQuestion.user == current_user)
        & (AnsweredQuestion.quiz == quiz)).count()
    try:
        answ_question = AnsweredQuestion.select().where(
            (AnsweredQuestion.user == current_user)
            & (AnsweredQuestion.quiz == quiz)
            & (AnsweredQuestion.closed == False)).get()
        # if last open question has not any time to answer, so next question
        if not answ_question.remaining_time():
            answ_question.closed = True
            answ_question.save()
            raise peewee.DoesNotExist

    except peewee.DoesNotExist:
        # there is not any open question, so go to next question
        all_not_answ_question = Question.select().join(
            AnsweredQuestion,
            peewee.JOIN.FULL).where((Question.quiz == quiz) & (
                AnsweredQuestion.closed == None)).order_by(Question.number)

        if not all_not_answ_question.exists():
            return {'code': -14, 'msg': 'no question remind to answer'}

        if quiz.random:
            question = random.choice(all_not_answ_question)
        else:
            question = all_not_answ_question[0]

        answ_question = AnsweredQuestion.get_or_create(
            user=current_user,
            quiz=quiz,
            question=question,
            defaults={'start_time': datetime.now()})

        data = model_to_dict(question, extra_attrs=['options'], recurse=False)
        data['nth'] = nth + 1
        data['user_start_time'] = answ_question[0].start_time
        return {'data': data}

    else:
        data = model_to_dict(answ_question.question,
                             extra_attrs=['options'],
                             recurse=False)
        data['nth'] = nth
        data['user_start_time'] = answ_question.start_time
        return {'data': data}
コード例 #12
0
ファイル: quiz.py プロジェクト: fallaha/quiza
def set_answer_of_question(uuid,
                           number: int,
                           answer: AnswerRequest,
                           current_user: User = Depends(get_current_user)):
    try:
        with database.atomic():
            quiz = Quiz.select().join(
                QuizUsers,
                peewee.JOIN.LEFT_OUTER,
                on=(QuizUsers.quiz == Quiz.uuid)).where((Quiz.uuid == uuid) & (
                    (QuizUsers.user == current_user) |
                    ((Quiz.public == True) & (QuizUsers.user == None)))).get()
    except (peewee.DoesNotExist, peewee.DataError):
        return {'code': -2, 'msg': 'invalid quiz'}

    if quiz.end_time is not None and quiz.end_time < datetime.now():
        return {
            'code': -8,
            'msg': 'this quiz has been finished at {}'.format(quiz.end_time)
        }

    try:
        question = Question.select().where((Question.quiz == quiz) &
                                           (Question.number == number)).get()
    except peewee.DoesNotExist:
        return {'code': -7, 'msg': "this question number dose not exist."}

    try:
        answ_question = AnsweredQuestion.select().where(
            AnsweredQuestion.quiz == quiz,
            AnsweredQuestion.question == question,
            AnsweredQuestion.user == current_user,
            AnsweredQuestion.closed == False).get()
    except peewee.DoesNotExist:
        return {
            'code': -9,
            'msg': "You cant access this question (closed or not open yet)"
        }

    if not answ_question.remaining_time():
        # Closed The Question to avoid response again
        answ_question.closed = True
        answ_question.save()
        return {'code': -10, 'msg': "The time to answer this question is over"}

    if question.qtype == QuestionType.MultiOption:
        print(answer)
        if answer.option_answer is not None:
            try:
                selected_option = answer.option_answer = MultipleOption.select(
                ).where((MultipleOption.number == answer.option_answer)
                        & (MultipleOption.question == question)).get()
                answ_question.option_answer = selected_option
            except peewee.DoesNotExist:
                return {
                    'code': -11,
                    'msg': "this selected option does not exist"
                }

    if question.qtype == QuestionType.Textual:
        answ_question.text_answer = answer.text_answer
    if question.qtype == QuestionType.FileUpload:
        answ_question.uploded_file_url = answer.uploded_file_url

    # Closed The Question to avoid response again
    answ_question.closed = True
    answ_question.save()

    return {'msg': 'your answer has been saved'}