def test_are_datetimes_close(self) -> None: initial_time = datetime.datetime(2016, 12, 1, 0, 0, 0) with self.swap(feconf, 'PROXIMAL_TIMEDELTA_SECS', 2): self.assertTrue( utils.are_datetimes_close( datetime.datetime(2016, 12, 1, 0, 0, 1), initial_time)) self.assertFalse( utils.are_datetimes_close( datetime.datetime(2016, 12, 1, 0, 0, 3), initial_time))
def __init__(self, request, response): # pylint: disable=super-init-not-called # Set self.request, self.response and self.app. self.initialize(request, response) self.start_time = datetime.datetime.utcnow() # Initializes the return dict for the handlers. self.values = {} # TODO(#13155): Remove the if-else part once all the handlers have had # schema validation implemented. if self.request.get('payload'): self.payload = json.loads(self.request.get('payload')) else: self.payload = None self.iframed = False self.user_id = None self.username = None self.email = None self.partially_logged_in = False self.user_is_scheduled_for_deletion = False self.current_user_is_super_admin = False # Once the attribute `normalized_request` is type annotated here, make # sure to fix all the subclasses using normalized_request.get() method # by removing their type: ignore[union-attr] and using a type cast # instead to eliminate the possibility on union types. # e.g. ClassroomAccessValidationHandler. self.normalized_request = None self.normalized_payload = None try: auth_claims = auth_services.get_auth_claims_from_request(request) except auth_domain.StaleAuthSessionError: auth_services.destroy_auth_session(self.response) self.redirect(user_services.create_login_url(self.request.uri)) return except auth_domain.InvalidAuthSessionError: logging.exception('User session is invalid!') auth_services.destroy_auth_session(self.response) self.redirect(user_services.create_login_url(self.request.uri)) return else: self.current_user_is_super_admin = ( auth_claims is not None and auth_claims.role_is_super_admin) if auth_claims: auth_id = auth_claims.auth_id user_settings = user_services.get_user_settings_by_auth_id(auth_id) if user_settings is None: # If the user settings are not yet created and the request leads # to signup page create a new user settings. Otherwise logout # the not-fully registered user. email = auth_claims.email if 'signup?' in self.request.uri: user_settings = ( user_services.create_new_user(auth_id, email)) else: logging.exception( 'Cannot find user %s with email %s on page %s' % ( auth_id, email, self.request.uri)) auth_services.destroy_auth_session(self.response) return self.email = user_settings.email self.values['user_email'] = user_settings.email self.user_id = user_settings.user_id if user_settings.deleted: self.user_is_scheduled_for_deletion = user_settings.deleted elif (self.REDIRECT_UNFINISHED_SIGNUPS and not user_services.has_fully_registered_account(self.user_id)): self.partially_logged_in = True else: self.username = user_settings.username self.values['username'] = self.username # In order to avoid too many datastore writes, we do not bother # recording a log-in if the current time is sufficiently close # to the last log-in time. if (user_settings.last_logged_in is None or not utils.are_datetimes_close( datetime.datetime.utcnow(), user_settings.last_logged_in)): user_services.record_user_logged_in(self.user_id) self.roles = ( [feconf.ROLE_ID_GUEST] if self.user_id is None else user_settings.roles) self.user = user_services.get_user_actions_info(self.user_id) if not self._is_requested_path_currently_accessible_to_user(): auth_services.destroy_auth_session(self.response) return self.values['is_super_admin'] = self.current_user_is_super_admin