Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sourcery refactored master branch #6

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
156 changes: 99 additions & 57 deletions app/answers.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,15 +66,20 @@ async def add_user_answer(conn, question, answer, user_info, question_type):

async with await conn.start_session(causal_consistency=True) as session:
# вариант вопроса с одним возможным ответом
if question_type == 'shortanswer' or question_type == 'numerical' or question_type == 'multichoice' or question_type == 'truefalse':
if question_type in [
'shortanswer',
'numerical',
'multichoice',
'truefalse',
]:
Comment on lines -69 to +74
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function AnswersDB.add_user_answer refactored with the following changes:

This removes the following comments ( why? ):

# если пользователь не отпралял этот вариант ответа

# удаляем другие наши ответы и добавляем новый
await conn[DATABASE_NAME][QUESTIONS_COLLECTION_NAME].update_many(
{'question': question, 'answers.users': user_info},
{'$pull': {'answers.$.users': user_info}},
session=session
)

if question_type == 'shortanswer' or question_type == 'numerical':
if question_type in ['shortanswer', 'numerical']:
await AnswersDB.delete_empty_answers(conn, question, session)

if await AnswersDB.find_question_by_ans(conn, question, answer, session) is None:
Expand All @@ -96,24 +101,40 @@ async def add_user_answer(conn, question, answer, user_info, question_type):
{'$pull': {'answers.$.users': user_info}}, {"_id": 0},
return_document=ReturnDocument.AFTER, session=session
)
else:
# если пользователь не отпралял этот вариант ответа
if await AnswersDB.is_user_send_answer(conn, question, answer[0], user_info, session) is False:
if await AnswersDB.find_question_by_ans(conn, question, answer[0], session) is None:
result = await conn[DATABASE_NAME][QUESTIONS_COLLECTION_NAME].find_one_and_update(
{'question': question},
{'$push': {'answers': {'answer': answer[0], 'users': [
user_info], 'correct': [], 'not_correct': []}}},
{"_id": 0}, return_document=ReturnDocument.AFTER, session=session
)
else:
result = await conn[DATABASE_NAME][QUESTIONS_COLLECTION_NAME].find_one_and_update(
{'question': question,
'answers.answer': answer[0]},
{'$push': {'answers.$.users': user_info}}, {
"_id": 0},
return_document=ReturnDocument.AFTER, session=session
)
elif await AnswersDB.is_user_send_answer(conn, question, answer[0], user_info, session) is False:
result = (
await conn[DATABASE_NAME][
QUESTIONS_COLLECTION_NAME
].find_one_and_update(
{'question': question},
{
'$push': {
'answers': {
'answer': answer[0],
'users': [user_info],
'correct': [],
'not_correct': [],
}
}
},
{"_id": 0},
return_document=ReturnDocument.AFTER,
session=session,
)
if await AnswersDB.find_question_by_ans(
conn, question, answer[0], session
)
is None
else await conn[DATABASE_NAME][
QUESTIONS_COLLECTION_NAME
].find_one_and_update(
{'question': question, 'answers.answer': answer[0]},
{'$push': {'answers.$.users': user_info}},
{"_id": 0},
return_document=ReturnDocument.AFTER,
session=session,
)
)

if result is None:
return await AnswersDB.find_question(conn, question, session)
Expand Down Expand Up @@ -176,33 +197,53 @@ async def add_user_approve(conn, question, answer, user_info, is_correct):
)

if is_correct:
if await AnswersDB.find_question_by_ans(conn, question, answer, session) is None:
return await conn[DATABASE_NAME][QUESTIONS_COLLECTION_NAME].find_one_and_update(
return (
await conn[DATABASE_NAME][
QUESTIONS_COLLECTION_NAME
].find_one_and_update(
{'question': question},
{'$push': {'answers': {'answer': answer, 'users': [], 'correct': [
user_info], 'not_correct': []}}}, {"_id": 0},
return_document=ReturnDocument.AFTER, session=session
)
else:
return await conn[DATABASE_NAME][QUESTIONS_COLLECTION_NAME].find_one_and_update(
{'question': question, 'answers.answer': answer},
{'$push': {'answers.$.correct': user_info}}, {"_id": 0},
return_document=ReturnDocument.AFTER, session=session
{
'$push': {
'answers': {
'answer': answer,
'users': [],
'correct': [user_info],
'not_correct': [],
}
}
},
{"_id": 0},
return_document=ReturnDocument.AFTER,
session=session,
)
else:
if await AnswersDB.find_question_by_ans(conn, question, answer, session) is None:
return await conn[DATABASE_NAME][QUESTIONS_COLLECTION_NAME].find_one_and_update(
{'question': question},
{'$push': {'answers': {'answer': answer, 'users': [],
'correct': [], 'not_correct': [user_info]}}}, {"_id": 0},
return_document=ReturnDocument.AFTER, session=session
if await AnswersDB.find_question_by_ans(
conn, question, answer, session
)
else:
return await conn[DATABASE_NAME][QUESTIONS_COLLECTION_NAME].find_one_and_update(
is None
else await conn[DATABASE_NAME][
QUESTIONS_COLLECTION_NAME
].find_one_and_update(
{'question': question, 'answers.answer': answer},
{'$push': {'answers.$.not_correct': user_info}}, {"_id": 0},
return_document=ReturnDocument.AFTER, session=session
{'$push': {'answers.$.correct': user_info}},
{"_id": 0},
return_document=ReturnDocument.AFTER,
session=session,
)
)

if await AnswersDB.find_question_by_ans(conn, question, answer, session) is None:
return await conn[DATABASE_NAME][QUESTIONS_COLLECTION_NAME].find_one_and_update(
{'question': question},
{'$push': {'answers': {'answer': answer, 'users': [],
'correct': [], 'not_correct': [user_info]}}}, {"_id": 0},
return_document=ReturnDocument.AFTER, session=session
)
else:
return await conn[DATABASE_NAME][QUESTIONS_COLLECTION_NAME].find_one_and_update(
{'question': question, 'answers.answer': answer},
{'$push': {'answers.$.not_correct': user_info}}, {"_id": 0},
return_document=ReturnDocument.AFTER, session=session
)
Comment on lines -179 to +246
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function AnswersDB.add_user_approve refactored with the following changes:


@staticmethod
async def add_new_viewer(conn, question, user_info):
Expand All @@ -219,15 +260,14 @@ async def add_new_viewer(conn, question, user_info):
async with await conn.start_session(causal_consistency=True) as session:
question_db = await AnswersDB.find_question(conn, question, session)
if question_db is not None:
if user_info not in question_db['viewers']:
document = await conn[DATABASE_NAME][QUESTIONS_COLLECTION_NAME].find_one_and_update(
{'question': question}, {
'$push': {'viewers': user_info}},
{"_id": 0}, return_document=ReturnDocument.AFTER, session=session
)
return document
else:
if user_info in question_db['viewers']:
return question_db
document = await conn[DATABASE_NAME][QUESTIONS_COLLECTION_NAME].find_one_and_update(
{'question': question}, {
'$push': {'viewers': user_info}},
{"_id": 0}, return_document=ReturnDocument.AFTER, session=session
)
return document
Comment on lines -222 to +270
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function AnswersDB.add_new_viewer refactored with the following changes:

else:
await AnswersDB.add_new_question(conn, question, [], [user_info], session)
return {'question': question, 'answers': [], 'viewers': [user_info]}
Expand All @@ -242,14 +282,16 @@ async def is_user_send_answer(conn, question, answer, user_info, session):
for answer_ in question['answers']:
# для типа вопроса 'match'
if 'subquestion' in answer_:
if answer_['subquestion'] == answer[0] and answer_['answer'] == answer[1]:
if user_info in answer_['users']:
if (
answer_['subquestion'] == answer[0]
and answer_['answer'] == answer[1]
and user_info in answer_['users']
):
return True
elif answer_['answer'] == answer:
for user in answer_['users']:
if user == user_info:
return True
else:
if answer_['answer'] == answer:
for user in answer_['users']:
if user == user_info:
return True
Comment on lines -245 to -252
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function AnswersDB.is_user_send_answer refactored with the following changes:


return False

Expand Down
5 changes: 2 additions & 3 deletions app/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,9 +152,8 @@ async def add_approve(sid, data):
# у checkbox-подобных типов отправляется ещё и состояние
# выбора этого ответа (checked), поэтому берём только
# сам текст ответа
if isinstance(data['answer'], list):
if len(data['answer']) == 2:
data['answer'] = 0
if isinstance(data['answer'], list) and len(data['answer']) == 2:
data['answer'] = 0
Comment on lines -155 to +156
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function add_approve refactored with the following changes:


result = await AnswersDB.add_user_approve(await get_database(),
data['question'], data['answer'], data['user_info'], data['is_correct'])
Expand Down