本文整理汇总了Python中django.utils.timezone.override方法的典型用法代码示例。如果您正苦于以下问题:Python timezone.override方法的具体用法?Python timezone.override怎么用?Python timezone.override使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类django.utils.timezone
的用法示例。
在下文中一共展示了timezone.override方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: daily_activity_notifications
# 需要导入模块: from django.utils import timezone [as 别名]
# 或者: from django.utils.timezone import override [as 别名]
def daily_activity_notifications():
with timer() as t:
for group in Group.objects.all():
with timezone.override(group.timezone):
if timezone.localtime().hour != 20: # only at 8pm local time
continue
for data in fetch_activity_notification_data_for_group(group):
prepare_activity_notification_email(**data).send()
stats.activity_notification_email(
group=data['group'], **{k: v.count()
for k, v in data.items() if isinstance(v, QuerySet)}
)
stats_utils.periodic_task('activities__daily_activity_notifications', seconds=t.elapsed_seconds)
示例2: notify_message_push_subscribers_with_language
# 需要导入模块: from django.utils import timezone [as 别名]
# 或者: from django.utils.timezone import override [as 别名]
def notify_message_push_subscribers_with_language(message, subscriptions, language):
conversation = message.conversation
if not translation.check_for_language(language):
language = 'en'
with translation.override(language):
message_title = get_message_title(message, language)
if message.is_thread_reply():
click_action = frontend_urls.thread_url(message.thread)
else:
click_action = frontend_urls.conversation_url(conversation, message.author)
notify_subscribers_by_device(
subscriptions,
click_action=click_action,
fcm_options={
'message_title': message_title,
'message_body': Truncator(message.content).chars(num=1000),
# this causes each notification for a given conversation to replace previous notifications
# fancier would be to make the new notifications show a summary not just the latest message
'tag': 'conversation:{}'.format(conversation.id),
}
)
示例3: prepare_application_message_notification
# 需要导入模块: from django.utils import timezone [as 别名]
# 或者: from django.utils.timezone import override [as 别名]
def prepare_application_message_notification(user, messages):
application = target_from_messages(messages)
with translation.override(language_for_user(user)):
reply_to_name = application.user.display_name
if application.user == user:
conversation_name = _('New message in your application to %(group_name)s') % {
'group_name': application.group.name
}
else:
conversation_name = _('New message in application of %(user_name)s to %(group_name)s') % {
'user_name': application.user.display_name,
'group_name': application.group.name,
}
return prepare_message_notification(
user,
messages,
reply_to_name=reply_to_name,
group=application.group,
conversation_name=conversation_name,
conversation_url=application_url(application),
stats_category='application_message'
)
示例4: now
# 需要导入模块: from django.utils import timezone [as 别名]
# 或者: from django.utils.timezone import override [as 别名]
def now(self, request):
data = TimezoneForm(request.query_params)
if not data.is_valid():
raise DRFValidationError(data.errors)
requested_timezone = data.cleaned_data.get('timezone')
tz = requested_timezone or pytz.utc
now = utils.timezone.now()
transmissions = Transmission.at(now)
try:
transmission = next(transmissions)
except StopIteration:
return Response(None)
else:
serializer = self.get_serializer(transmission, many=False)
with override(timezone=tz):
return Response(serializer.data)
示例5: test_get_current_timezone_templatetag
# 需要导入模块: from django.utils import timezone [as 别名]
# 或者: from django.utils.timezone import override [as 别名]
def test_get_current_timezone_templatetag(self):
"""
Test the {% get_current_timezone %} templatetag.
"""
tpl = Template("{% load tz %}{% get_current_timezone as time_zone %}{{ time_zone }}")
self.assertEqual(tpl.render(Context()), "Africa/Nairobi")
with timezone.override(UTC):
self.assertEqual(tpl.render(Context()), "UTC")
tpl = Template(
"{% load tz %}{% timezone tz %}{% get_current_timezone as time_zone %}"
"{% endtimezone %}{{ time_zone }}"
)
self.assertEqual(tpl.render(Context({'tz': ICT})), "+0700")
with timezone.override(UTC):
self.assertEqual(tpl.render(Context({'tz': ICT})), "+0700")
示例6: test_override
# 需要导入模块: from django.utils import timezone [as 别名]
# 或者: from django.utils.timezone import override [as 别名]
def test_override(self):
default = timezone.get_default_timezone()
try:
timezone.activate(ICT)
with timezone.override(EAT):
self.assertIs(EAT, timezone.get_current_timezone())
self.assertIs(ICT, timezone.get_current_timezone())
with timezone.override(None):
self.assertIs(default, timezone.get_current_timezone())
self.assertIs(ICT, timezone.get_current_timezone())
timezone.deactivate()
with timezone.override(EAT):
self.assertIs(EAT, timezone.get_current_timezone())
self.assertIs(default, timezone.get_current_timezone())
with timezone.override(None):
self.assertIs(default, timezone.get_current_timezone())
self.assertIs(default, timezone.get_current_timezone())
finally:
timezone.deactivate()
示例7: test_localdate
# 需要导入模块: from django.utils import timezone [as 别名]
# 或者: from django.utils.timezone import override [as 别名]
def test_localdate(self):
naive = datetime.datetime(2015, 1, 1, 0, 0, 1)
with self.assertRaisesMessage(ValueError, 'localtime() cannot be applied to a naive datetime'):
timezone.localdate(naive)
with self.assertRaisesMessage(ValueError, 'localtime() cannot be applied to a naive datetime'):
timezone.localdate(naive, timezone=EAT)
aware = datetime.datetime(2015, 1, 1, 0, 0, 1, tzinfo=ICT)
self.assertEqual(timezone.localdate(aware, timezone=EAT), datetime.date(2014, 12, 31))
with timezone.override(EAT):
self.assertEqual(timezone.localdate(aware), datetime.date(2014, 12, 31))
with mock.patch('django.utils.timezone.now', return_value=aware):
self.assertEqual(timezone.localdate(timezone=EAT), datetime.date(2014, 12, 31))
with timezone.override(EAT):
self.assertEqual(timezone.localdate(), datetime.date(2014, 12, 31))
示例8: render
# 需要导入模块: from django.utils import timezone [as 别名]
# 或者: from django.utils.timezone import override [as 别名]
def render(self, context):
with timezone.override(self.tz.resolve(context)):
output = self.nodelist.render(context)
return output
示例9: calculate_group_summary_dates
# 需要导入模块: from django.utils import timezone [as 别名]
# 或者: from django.utils.timezone import override [as 别名]
def calculate_group_summary_dates(group):
with timezone.override(group.timezone):
tz = get_current_timezone()
# midnight last night in the groups local timezone
midnight = tz.localize(timezone.now().replace(tzinfo=None, hour=0, minute=0, second=0, microsecond=0))
# 7 days before that
from_date = midnight - relativedelta(days=7)
# a week after from date
to_date = from_date + relativedelta(days=7)
return from_date, to_date
示例10: test_summary_email_dates_printed_correctly
# 需要导入模块: from django.utils import timezone [as 别名]
# 或者: from django.utils.timezone import override [as 别名]
def test_summary_email_dates_printed_correctly(self):
mail.outbox = []
with timezone.override(timezone.utc), freeze_time(datetime.datetime(2018, 8, 19)): # Sunday
group = GroupFactory()
self.make_activity_in_group(group)
from_date, to_date = calculate_group_summary_dates(group)
context = prepare_group_summary_data(group, from_date, to_date)
emails = prepare_group_summary_emails(group, context)
self.assertGreater(len(emails), 0)
email = emails[0]
expected_format = 'Sunday, August 12, 2018 to Saturday, August 18, 2018'
self.assertIn(expected_format, email.body)
示例11: test_summary_emails_send_at_8am_localtime
# 需要导入模块: from django.utils import timezone [as 别名]
# 或者: from django.utils.timezone import override [as 别名]
def test_summary_emails_send_at_8am_localtime(self):
group = GroupFactory(timezone=pytz.timezone('Europe/Berlin'))
# 6am UTC is 8am in this timezone
with timezone.override(timezone.utc), freeze_time(datetime.datetime(2018, 8, 19, 6, 0, 0, tzinfo=pytz.utc)):
self.make_activity_in_group(group)
mail.outbox = []
send_summary_emails()
self.assertEqual(len(mail.outbox), self.new_user_count)
示例12: test_summary_emails_do_not_send_at_other_times
# 需要导入模块: from django.utils import timezone [as 别名]
# 或者: from django.utils.timezone import override [as 别名]
def test_summary_emails_do_not_send_at_other_times(self):
group = GroupFactory(timezone=pytz.timezone('Europe/Berlin'))
# 6am UTC is 8am in this timezone
with timezone.override(timezone.utc), freeze_time(datetime.datetime(2018, 8, 19, 7, 0, 0, tzinfo=pytz.utc)):
self.make_activity_in_group(group)
mail.outbox = []
send_summary_emails()
self.assertEqual(len(mail.outbox), 0)
示例13: test_time_filter_uses_timezone
# 需要导入模块: from django.utils import timezone [as 别名]
# 或者: from django.utils.timezone import override [as 别名]
def test_time_filter_uses_timezone(self):
hour = 5
datetime = timezone.now().replace(
tzinfo=pytz.utc,
hour=hour,
minute=0,
second=0,
microsecond=0,
)
tz = pytz.timezone('Europe/Berlin')
offset_hours = int(tz.utcoffset(datetime.utcnow()).total_seconds() / 3600)
with timezone.override(tz), translation.override('en'):
val = time_filter(datetime)
self.assertEqual(val, '{}:00 AM'.format(hour + offset_hours))
示例14: prepare_email_content
# 需要导入模块: from django.utils import timezone [as 别名]
# 或者: from django.utils.timezone import override [as 别名]
def prepare_email_content(template, context, tz, language='en'):
if not translation.check_for_language(language):
language = 'en'
with timezone.override(tz), translation.override(language):
html_content = None
try:
html_template = get_template('{}.html.jinja2'.format(template))
html_content = html_template.render(context)
except TemplateDoesNotExist:
pass
try:
text_template = get_template('{}.text.jinja2'.format(template))
text_content = text_template.render(context)
except TemplateDoesNotExist:
if html_content:
text_content = generate_plaintext_from_html(html_content)
else:
raise Exception('Nothing to use for text content, no text or html templates available.')
subject = render_to_string('{}.subject.jinja2'.format(template), context).replace('\n', '')
return subject, text_content, html_content
示例15: group_timezone_at
# 需要导入模块: from django.utils import timezone [as 别名]
# 或者: from django.utils.timezone import override [as 别名]
def group_timezone_at(group, **kwargs):
with timezone.override(group.timezone):
datetime = timezone.localtime(timezone=group.timezone).replace(**kwargs)
with freeze_time(datetime, tick=True):
yield