-
Notifications
You must be signed in to change notification settings - Fork 2
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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', | ||
]: | ||
# удаляем другие наши ответы и добавляем новый | ||
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: | ||
|
@@ -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) | ||
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
||
@staticmethod | ||
async def add_new_viewer(conn, question, user_info): | ||
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
else: | ||
await AnswersDB.add_new_question(conn, question, [], [user_info], session) | ||
return {'question': question, 'answers': [], 'viewers': [user_info]} | ||
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
||
return False | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
||
result = await AnswersDB.add_user_approve(await get_database(), | ||
data['question'], data['answer'], data['user_info'], data['is_correct']) | ||
|
There was a problem hiding this comment.
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:in
operator [×2] (merge-comparisons
)merge-else-if-into-elif
)assign-if-exp
)This removes the following comments ( why? ):