def _sorting_dates(self): # utility function to get datetime objects for dates used to # compute the is_new flag and the sorting_score announcement = self.announcement if announcement is not None: announcement = time_to_datetime(announcement) try: start = dateutil.parser.parse(self.advertised_start) except (ValueError, AttributeError): start = time_to_datetime(self.start) now = datetime.utcnow() return announcement, start, now
def setUp(self): # Needed for test_is_newish datetime_patcher = patch.object(xmodule.course_module, 'datetime', Mock(wraps=datetime.datetime)) mocked_datetime = datetime_patcher.start() mocked_datetime.utcnow.return_value = time_to_datetime(NOW) self.addCleanup(datetime_patcher.stop)
def __init__(self, system, location, descriptor, model_data): XModule.__init__(self, system, location, descriptor, model_data) due_date = time_to_datetime(self.due) if self.graceperiod is not None and due_date: self.close_date = due_date + self.graceperiod else: self.close_date = due_date if self.seed is None: self.choose_new_seed() # Need the problem location in openendedresponse to send out. Adding # it to the system here seems like the least clunky way to get it # there. self.system.set('location', self.location.url()) try: # TODO (vshnayder): move as much as possible of this work and error # checking to descriptor load time self.lcp = self.new_lcp(self.get_state_for_lcp()) # At this point, we need to persist the randomization seed # so that when the problem is re-loaded (to check/view/save) # it stays the same. # However, we do not want to write to the database # every time the module is loaded. # So we set the seed ONLY when there is not one set already if self.seed is None: self.seed = self.lcp.seed except Exception as err: msg = 'cannot create LoncapaProblem {loc}: {err}'.format( loc=self.location.url(), err=err) # TODO (vshnayder): do modules need error handlers too? # We shouldn't be switching on DEBUG. if self.system.DEBUG: log.warning(msg) # TODO (vshnayder): This logic should be general, not here--and may # want to preserve the data instead of replacing it. # e.g. in the CMS msg = '<p>%s</p>' % msg.replace('<', '<') msg += '<p><pre>%s</pre></p>' % traceback.format_exc().replace( '<', '<') # create a dummy problem with error message instead of failing problem_text = ( '<problem><text><span class="inline-error">' 'Problem %s has an error:</span>%s</text></problem>' % (self.location.url(), msg)) self.lcp = self.new_lcp(self.get_state_for_lcp(), text=problem_text) else: # add extra info and raise raise Exception(msg), None, sys.exc_info()[2] self.set_state_from_lcp() assert self.seed is not None
def __init__(self, system, location, descriptor, model_data): XModule.__init__(self, system, location, descriptor, model_data) due_date = time_to_datetime(self.due) if self.graceperiod is not None and due_date: self.close_date = due_date + self.graceperiod else: self.close_date = due_date if self.seed is None: self.choose_new_seed() # Need the problem location in openendedresponse to send out. Adding # it to the system here seems like the least clunky way to get it # there. self.system.set('location', self.location.url()) try: # TODO (vshnayder): move as much as possible of this work and error # checking to descriptor load time self.lcp = self.new_lcp(self.get_state_for_lcp()) # At this point, we need to persist the randomization seed # so that when the problem is re-loaded (to check/view/save) # it stays the same. # However, we do not want to write to the database # every time the module is loaded. # So we set the seed ONLY when there is not one set already if self.seed is None: self.seed = self.lcp.seed except Exception as err: msg = 'cannot create LoncapaProblem {loc}: {err}'.format( loc=self.location.url(), err=err) # TODO (vshnayder): do modules need error handlers too? # We shouldn't be switching on DEBUG. if self.system.DEBUG: log.warning(msg) # TODO (vshnayder): This logic should be general, not here--and may # want to preserve the data instead of replacing it. # e.g. in the CMS msg = '<p>%s</p>' % msg.replace('<', '<') msg += '<p><pre>%s</pre></p>' % traceback.format_exc().replace( '<', '<') # create a dummy problem with error message instead of failing problem_text = ('<problem><text><span class="inline-error">' 'Problem %s has an error:</span>%s</text></problem>' % (self.location.url(), msg)) self.lcp = self.new_lcp( self.get_state_for_lcp(), text=problem_text) else: # add extra info and raise raise Exception(msg), None, sys.exc_info()[2] self.set_state_from_lcp() assert self.seed is not None
def setUp(self): # Needed for test_is_newish datetime_patcher = patch.object( xmodule.course_module, 'datetime', Mock(wraps=datetime.datetime) ) mocked_datetime = datetime_patcher.start() mocked_datetime.utcnow.return_value = time_to_datetime(NOW) self.addCleanup(datetime_patcher.stop)
def __init__(self, *args, **kwargs): XModule.__init__(self, *args, **kwargs) """ Example: <foldit show_basic_score="true" required_level="4" required_sublevel="3" show_leaderboard="false"/> """ self.due_time = time_to_datetime(self.due)
def __init__(self, due_date, grace_period_string): if due_date is not None: self.display_due_date = time_to_datetime(due_date) else: self.display_due_date = None if grace_period_string is not None and self.display_due_date: try: self.grace_period = parse_timedelta(grace_period_string) self.close_date = self.display_due_date + self.grace_period except: log.error("Error parsing the grace period {0}".format(grace_period_string)) raise else: self.grace_period = None self.close_date = self.display_due_date
def __init__(self, due_date, grace_period_string): if due_date is not None: self.display_due_date = time_to_datetime(due_date) else: self.display_due_date = None if grace_period_string is not None and self.display_due_date: try: self.grace_period = parse_timedelta(grace_period_string) self.close_date = self.display_due_date + self.grace_period except: log.error("Error parsing the grace period {0}".format( grace_period_string)) raise else: self.grace_period = None self.close_date = self.display_due_date
def test_time_to_datetime(): assert_equals(None, date_utils.time_to_datetime(None)) test_time = time.struct_time((1992, 3, 12, 15, 3, 30, 1, 71, 0)) assert_equals( datetime.datetime(1992, 3, 12, 15, 3, 30), date_utils.time_to_datetime(test_time))
def test_time_to_datetime(): assert_equals(None, date_utils.time_to_datetime(None)) test_time = time.struct_time((1992, 3, 12, 15, 3, 30, 1, 71, 0)) assert_equals(datetime.datetime(1992, 3, 12, 15, 3, 30), date_utils.time_to_datetime(test_time))