Ran pyupgrade on lms/djangoapps

Ran pyupgrade on lms/djangoapps/instructor_analytics
Ran pyugprade on lms/djangoapps/instructor_task
Ran pyupgrade on lms/djangoapps/learner_dashboard
This commit is contained in:
usamasadiq
2021-02-16 12:55:05 +05:00
parent ba16e05899
commit 3f1df8eb2a
39 changed files with 518 additions and 592 deletions

View File

@@ -10,17 +10,16 @@ import hashlib
import json
import logging
import six
from celery.result import AsyncResult
from celery.states import FAILURE, READY_STATES, REVOKED, SUCCESS
from django.utils.translation import ugettext as _
from opaque_keys.edx.keys import UsageKey
import six
from six import text_type
from common.djangoapps.util.db import outer_atomic
from lms.djangoapps.courseware.courses import get_problems_in_section
from lms.djangoapps.courseware.module_render import get_xqueue_callback_url_prefix
from lms.djangoapps.instructor_task.models import PROGRESS, InstructorTask
from common.djangoapps.util.db import outer_atomic
from xmodule.modulestore.django import modulestore
log = logging.getLogger(__name__)
@@ -34,8 +33,8 @@ class AlreadyRunningError(Exception):
def __init__(self, message=None):
if not message:
message = self.message
super(AlreadyRunningError, self).__init__(message) # lint-amnesty, pylint: disable=super-with-arguments
message = self.message # pylint: disable=exception-message-attribute
super().__init__(message)
class QueueConnectionError(Exception):
@@ -46,8 +45,8 @@ class QueueConnectionError(Exception):
def __init__(self, message=None):
if not message:
message = self.message
super(QueueConnectionError, self).__init__(message) # lint-amnesty, pylint: disable=super-with-arguments
message = self.message # pylint: disable=exception-message-attribute
super().__init__(message)
def _task_is_running(course_id, task_type, task_key):
@@ -78,7 +77,7 @@ def _reserve_task(course_id, task_type, task_key, task_input, requester):
"""
if _task_is_running(course_id, task_type, task_key):
log.warning(u"Duplicate task found for task_type %s and task_key %s", task_type, task_key)
log.warning("Duplicate task found for task_type %s and task_key %s", task_type, task_key)
error_message = generate_already_running_error_message(task_type)
raise AlreadyRunningError(error_message)
@@ -88,7 +87,7 @@ def _reserve_task(course_id, task_type, task_key, task_input, requester):
most_recent_id = "None found"
finally:
log.warning(
u"No duplicate tasks found: task_type %s, task_key %s, and most recent task_id = %s",
"No duplicate tasks found: task_type %s, task_key %s, and most recent task_id = %s",
task_type,
task_key,
most_recent_id
@@ -120,7 +119,7 @@ def generate_already_running_error_message(task_type):
if report_types.get(task_type):
message = _(
u"The {report_type} report is being created. "
"The {report_type} report is being created. "
"To view the status of the report, see Pending Tasks below. "
"You will be able to download the report when it is complete."
).format(report_type=report_types.get(task_type))
@@ -215,20 +214,20 @@ def _update_instructor_task(instructor_task, task_result):
elif result_state in [PROGRESS, SUCCESS]:
# construct a status message directly from the task result's result:
# it needs to go back with the entry passed in.
log.info(u"background task (%s), state %s: result: %s", task_id, result_state, returned_result)
log.info("background task (%s), state %s: result: %s", task_id, result_state, returned_result)
task_output = InstructorTask.create_output_for_success(returned_result)
elif result_state == FAILURE:
# on failure, the result's result contains the exception that caused the failure
exception = returned_result
traceback = result_traceback if result_traceback is not None else ''
log.warning(u"background task (%s) failed: %s %s", task_id, returned_result, traceback)
log.warning("background task (%s) failed: %s %s", task_id, returned_result, traceback)
task_output = InstructorTask.create_output_for_failure(exception, result_traceback)
elif result_state == REVOKED:
# on revocation, the result's result doesn't contain anything
# but we cannot rely on the worker thread to set this status,
# so we set it here.
entry_needs_saving = True
log.warning(u"background task (%s) revoked.", task_id)
log.warning("background task (%s) revoked.", task_id)
task_output = InstructorTask.create_output_for_revoked()
# save progress and state into the entry, even if it's not being saved:
@@ -258,8 +257,8 @@ def _handle_instructor_task_failure(instructor_task, error):
"""
Do required operations if task creation was not complete.
"""
log.info(u"instructor task (%s) failed, result: %s", instructor_task.task_id, text_type(error))
_update_instructor_task_state(instructor_task, FAILURE, text_type(error))
log.info("instructor task (%s) failed, result: %s", instructor_task.task_id, str(error))
_update_instructor_task_state(instructor_task, FAILURE, str(error))
raise QueueConnectionError()
@@ -282,7 +281,7 @@ def get_updated_instructor_task(task_id):
try:
instructor_task = InstructorTask.objects.get(task_id=task_id)
except InstructorTask.DoesNotExist:
log.warning(u"query for InstructorTask status failed: task_id=(%s) not found", task_id)
log.warning("query for InstructorTask status failed: task_id=(%s) not found", task_id)
return None
# if the task is not already known to be done, then we need to query
@@ -390,11 +389,11 @@ def encode_problem_and_student_input(usage_key, student=None):
assert isinstance(usage_key, UsageKey)
if student is not None:
task_input = {'problem_url': text_type(usage_key), 'student': student.username}
task_key_stub = "{student}_{problem}".format(student=student.id, problem=text_type(usage_key))
task_input = {'problem_url': str(usage_key), 'student': student.username}
task_key_stub = "{student}_{problem}".format(student=student.id, problem=str(usage_key))
else:
task_input = {'problem_url': text_type(usage_key)}
task_key_stub = "_{problem}".format(problem=text_type(usage_key))
task_input = {'problem_url': str(usage_key)}
task_key_stub = "_{problem}".format(problem=str(usage_key))
# create the key value by using MD5 hash:
task_key = hashlib.md5(six.b(task_key_stub)).hexdigest()
@@ -412,11 +411,11 @@ def encode_entrance_exam_and_student_input(usage_key, student=None):
"""
assert isinstance(usage_key, UsageKey)
if student is not None:
task_input = {'entrance_exam_url': text_type(usage_key), 'student': student.username}
task_key_stub = "{student}_{entranceexam}".format(student=student.id, entranceexam=text_type(usage_key))
task_input = {'entrance_exam_url': str(usage_key), 'student': student.username}
task_key_stub = "{student}_{entranceexam}".format(student=student.id, entranceexam=str(usage_key))
else:
task_input = {'entrance_exam_url': text_type(usage_key)}
task_key_stub = "_{entranceexam}".format(entranceexam=text_type(usage_key))
task_input = {'entrance_exam_url': str(usage_key)}
task_key_stub = "_{entranceexam}".format(entranceexam=str(usage_key))
# create the key value by using MD5 hash:
task_key = hashlib.md5(task_key_stub.encode('utf-8')).hexdigest()