Files
edx-platform/openedx/core/djangoapps/schedules/utils.py
Kyle McCormick 151bd13666 Use full names for common.djangoapps imports; warn when using old style (#25477)
* Generate common/djangoapps import shims for LMS
* Generate common/djangoapps import shims for Studio
* Stop appending project root to sys.path
* Stop appending common/djangoapps to sys.path
* Import from common.djangoapps.course_action_state instead of course_action_state
* Import from common.djangoapps.course_modes instead of course_modes
* Import from common.djangoapps.database_fixups instead of database_fixups
* Import from common.djangoapps.edxmako instead of edxmako
* Import from common.djangoapps.entitlements instead of entitlements
* Import from common.djangoapps.pipline_mako instead of pipeline_mako
* Import from common.djangoapps.static_replace instead of static_replace
* Import from common.djangoapps.student instead of student
* Import from common.djangoapps.terrain instead of terrain
* Import from common.djangoapps.third_party_auth instead of third_party_auth
* Import from common.djangoapps.track instead of track
* Import from common.djangoapps.util instead of util
* Import from common.djangoapps.xblock_django instead of xblock_django
* Add empty common/djangoapps/__init__.py to fix pytest collection
* Fix pylint formatting violations
* Exclude import_shims/ directory tree from linting
2020-11-10 07:02:01 -05:00

68 lines
2.2 KiB
Python

import datetime
import logging
import pytz
from django.db.models import F, Subquery
from django.db.models.functions import Greatest
from django.db import transaction
from openedx.core.djangoapps.schedules.models import Schedule
LOG = logging.getLogger(__name__)
# TODO: consider using a LoggerAdapter instead of this mixin:
# https://docs.python.org/2/library/logging.html#logging.LoggerAdapter
class PrefixedDebugLoggerMixin(object):
log_prefix = None
def __init__(self, *args, **kwargs):
super(PrefixedDebugLoggerMixin, self).__init__(*args, **kwargs)
if self.log_prefix is None:
self.log_prefix = self.__class__.__name__
def log_debug(self, message, *args, **kwargs):
"""
Wrapper around LOG.debug that prefixes the message.
"""
LOG.debug(self.log_prefix + ': ' + message, *args, **kwargs)
def log_info(self, message, *args, **kwargs):
"""
Wrapper around LOG.info that prefixes the message.
"""
LOG.info(self.log_prefix + ': ' + message, *args, **kwargs)
def reset_self_paced_schedule(user, course_key, use_availability_date=False):
"""
Reset the user's schedule if self-paced.
It does not create a new schedule, just resets an existing one.
This is used, for example, when a user requests it or when an enrollment mode changes.
Arguments:
user (User)
course_key (CourseKey or str)
use_availability_date (bool): if False, reset to now, else reset to when user got access to course material
"""
with transaction.atomic(savepoint=False):
try:
schedule = Schedule.objects.select_related('enrollment', 'enrollment__course').get(
enrollment__user=user,
enrollment__course__id=course_key,
enrollment__course__self_paced=True,
)
except Schedule.DoesNotExist:
return
if use_availability_date:
enrollment = schedule.enrollment
schedule.start_date = max(enrollment.created, enrollment.course.start)
schedule.save()
else:
schedule.start_date = datetime.datetime.now(pytz.utc)
schedule.save()