我们从Python开源项目中,提取了以下16个代码示例,用于说明如何使用django.utils.timezone.deactivate()。
def test_timestamp_utc_create(self): timezone.activate(pytz.utc) obj = DateTimeTzModel.objects.create(timestamp=aware_datetime()) self.assertEqual(obj.timestamp.tzinfo, aware_datetime().tzinfo) timezone.deactivate()
def test_timestamp_utc_read(self): # Regression test for https://github.com/learningequality/kolibri/issues/1602 timezone.activate(pytz.utc) obj = DateTimeTzModel.objects.create(timestamp=aware_datetime()) obj.refresh_from_db() self.assertEqual(obj.timestamp, aware_datetime()) timezone.deactivate()
def test_timestamp_arbitrary_create(self): tz = pytz.timezone('Africa/Nairobi') timezone.activate(tz) timestamp = aware_datetime() obj = DateTimeTzModel.objects.create(timestamp=timestamp) self.assertEqual(obj.timestamp.tzinfo, timestamp.tzinfo) timezone.deactivate()
def test_timestamp_arbitrary_read(self): # Regression test for https://github.com/learningequality/kolibri/issues/1602 tz = pytz.timezone('Africa/Nairobi') timezone.activate(tz) timestamp = aware_datetime() obj = DateTimeTzModel.objects.create(timestamp=timestamp) obj.refresh_from_db() self.assertEqual(obj.timestamp, timestamp) timezone.deactivate()
def test_default_utc_create(self): timezone.activate(pytz.utc) obj = DateTimeTzModel.objects.create() self.assertEqual(obj.default_timestamp.tzinfo, pytz.utc) timezone.deactivate()
def test_zero_second_fractions_read(self): # Regression test for https://github.com/learningequality/kolibri/issues/1758 timezone.activate(pytz.utc) try: timestamp = parse_timezonestamp('2000-12-11 10:09:08') self.assertEqual(timestamp, aware_datetime()) except ValueError: self.fail('parse_timezonestamp did not parse time data missing fractions of seconds.') finally: timezone.deactivate()
def test_timestamp_utc_parse(self): timezone.activate(pytz.utc) field = DateTimeTzSerializerField() timestamp = aware_datetime() self.assertEqual(field.to_internal_value(timestamp.isoformat()).tzinfo, aware_datetime().tzinfo) timezone.deactivate()
def test_timestamp_arbitrary_parse(self): tz = pytz.timezone('Africa/Nairobi') timezone.activate(tz) field = DateTimeTzSerializerField() timestamp = aware_datetime() self.assertEqual(field.to_internal_value(timestamp.isoformat()).tzinfo, aware_datetime().tzinfo) timezone.deactivate()
def process_request(self, request): if request.user.is_authenticated(): timezone.activate(request.user.timezone) else: timezone.deactivate()
def __call__(self, request): if request.site: tzname = GeneralSetting.for_site(request.site).timezone if tzname: timezone.activate(pytz.timezone(tzname)) else: timezone.deactivate() response = self.get_response(request) return response
def process_request(self, request): tzname = request.session.get('django_timezone') if tzname: timezone.activate(pytz.timezone(tzname)) else: timezone.deactivate()
def process_request(self, request): tzname = get_site_setting('i18n_timezone') if tzname: timezone.activate(pytz.timezone(tzname)) else: timezone.deactivate()
def process_request(self, request): user_time_zone = request.session.get('user_time_zone') try: if not user_time_zone: user_ip = get_real_ip(request) if user_ip: reader = geolite2.reader() ip_details = reader.get(user_ip) user_time_zone = ip_details['location']['time_zone'] geolite2.close() if user_time_zone: request.session['user_time_zone'] = user_time_zone timezone.activate(pytz.timezone(user_time_zone)) except: timezone.deactivate()
def process_request(self, request): if request.user.is_authenticated() and request.user.timezone: timezone.activate(pytz.timezone(request.user.timezone)) else: timezone.deactivate()
def process_request(self, request): if request.user.is_authenticated: if request.user.timezone is not None: timezone.activate(pytz.timezone(request.user.timezone)) else: timezone.activate(pytz.timezone('UTC')) else: timezone.deactivate()