当前位置: 首页>>代码示例>>Python>>正文


Python timezone.activate方法代码示例

本文整理汇总了Python中django.utils.timezone.activate方法的典型用法代码示例。如果您正苦于以下问题:Python timezone.activate方法的具体用法?Python timezone.activate怎么用?Python timezone.activate使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在django.utils.timezone的用法示例。


在下文中一共展示了timezone.activate方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: profile_settings

# 需要导入模块: from django.utils import timezone [as 别名]
# 或者: from django.utils.timezone import activate [as 别名]
def profile_settings(request):
    user = request.user
    user_profile = user.profile

    if request.method == 'POST':
        user_profile.show_email = 'show_email' in request.POST
        user_profile.send_my_own_events = 'send_my_own_events' in request.POST
        user_profile.location = request.POST.get('location', '')
        if 'geoid' in request.POST:
            tz = get_tz(request.POST['geoid'])
            user_profile.time_zone = tz
            request.session['django_timezone'] = tz
            timezone.activate(pytz.timezone(tz))
        user_profile.save()

        return HttpResponse("OK")

    context = {
        'user_profile': user_profile,
        'geo_suggest_url': settings.GEO_SUGGEST_URL
    }

    return render(request, 'user_settings.html', context) 
开发者ID:znick,项目名称:anytask,代码行数:25,代码来源:views.py

示例2: test_notification_template_rendering_empty_text_body

# 需要导入模块: from django.utils import timezone [as 别名]
# 或者: from django.utils.timezone import activate [as 别名]
def test_notification_template_rendering_empty_text_body(notification_template):
    context = {
        'subject_var': 'bar',
        'body_var': 'baz',
        'html_body_var': 'foo <b>bar</b> baz',
    }

    activate('fi')
    notification_template.body = ''
    notification_template.save()

    rendered = render_notification_template(NotificationType.TEST, context, 'fi')
    assert len(rendered) == 3
    assert rendered['subject'] == "testiotsikko, muuttujan arvo: bar!"
    assert rendered['body'] == "testihötömölöruumis, muuttujan arvo: foo bar baz!"
    assert rendered['html_body'] == "testi<b>hötömölö</b>ruumis, muuttujan arvo: foo <b>bar</b> baz!" 
开发者ID:City-of-Helsinki,项目名称:linkedevents,代码行数:18,代码来源:test_notifications.py

示例3: test_notification_template_rendering_empty_html_body

# 需要导入模块: from django.utils import timezone [as 别名]
# 或者: from django.utils.timezone import activate [as 别名]
def test_notification_template_rendering_empty_html_body(notification_template):
    context = {
        'subject_var': 'bar',
        'body_var': 'baz',
        'html_body_var': 'foo <b>bar</b> baz',
    }

    activate('fi')
    notification_template.html_body = ''
    notification_template.save()

    rendered = render_notification_template(NotificationType.TEST, context, 'fi')
    assert len(rendered) == 3
    assert rendered['subject'] == "testiotsikko, muuttujan arvo: bar!"
    assert rendered['body'] == "testiruumis, muuttujan arvo: baz!"
    assert rendered['html_body'] == "" 
开发者ID:City-of-Helsinki,项目名称:linkedevents,代码行数:18,代码来源:test_notifications.py

示例4: test_notification_template_format_datetime

# 需要导入模块: from django.utils import timezone [as 别名]
# 或者: from django.utils.timezone import activate [as 别名]
def test_notification_template_format_datetime(notification_template):
    notification_template.body_en = "{{ datetime|format_datetime('en') }}"
    notification_template.save()

    dt = datetime(2020, 2, 22, 12, 0, 0, 0, pytz.utc)

    context = {
        'subject_var': 'bar',
        'datetime': dt,
        'html_body_var': 'foo <b>bar</b> baz',
    }

    timezone.activate(pytz.timezone('Europe/Helsinki'))

    rendered = render_notification_template(NotificationType.TEST, context, 'en')
    assert rendered['body'] == '22 Feb 2020 at 14:00' 
开发者ID:City-of-Helsinki,项目名称:linkedevents,代码行数:18,代码来源:test_notifications.py

示例5: test_localize

# 需要导入模块: from django.utils import timezone [as 别名]
# 或者: from django.utils.timezone import activate [as 别名]
def test_localize(self):
        env = Environment(extensions=[DjangoL10n])
        template = env.from_string("{{ foo }}")
        context1 = {'foo': 1.23}
        date = datetime.datetime(2000, 10, 1, 14, 10, 12, tzinfo=timezone.utc)
        context2 = {'foo': date}

        translation.activate('en')
        self.assertEqual('1.23', template.render(context1))

        translation.activate('de')
        self.assertEqual('1,23', template.render(context1))

        translation.activate('es')
        timezone.activate('America/Argentina/Buenos_Aires')
        self.assertEqual('1 de Octubre de 2000 a las 11:10', template.render(context2))

        timezone.activate('Europe/Berlin')
        self.assertEqual('1 de Octubre de 2000 a las 16:10', template.render(context2))

        translation.activate('de')
        self.assertEqual('1. Oktober 2000 16:10', template.render(context2))

        timezone.activate('America/Argentina/Buenos_Aires')
        self.assertEqual('1. Oktober 2000 11:10', template.render(context2)) 
开发者ID:MoritzS,项目名称:jinja2-django-tags,代码行数:27,代码来源:tests.py

示例6: __call__

# 需要导入模块: from django.utils import timezone [as 别名]
# 或者: from django.utils.timezone import activate [as 别名]
def __call__(self, request):
        # Code to be executed for each request before
        # the view (and later middleware) are called.

        response = self.get_response(request)

        # Code to be executed for each request/response after
        # the view is called.

        tzname = request.session.get('django_timezone')

        if not tzname:
            # Get it from the Account. Should hopefully happens once per session
            user = request.user
            if user and not user.is_anonymous:
                tzname = user.time_zone
                if tzname:
                    request.session['django_timezone'] = tzname

        if tzname:
            timezone.activate(pytz.timezone(tzname))
        else:
            timezone.deactivate()

        return response 
开发者ID:dkarchmer,项目名称:django-aws-template,代码行数:27,代码来源:timezoneMiddleware.py

示例7: __call__

# 需要导入模块: from django.utils import timezone [as 别名]
# 或者: from django.utils.timezone import activate [as 别名]
def __call__(self, request):
        if "HTTP_X_FORWARDED_FOR" in request.META:
            request.META["HTTP_X_PROXY_REMOTE_ADDR"] = request.META["REMOTE_ADDR"]
            parts = request.META["HTTP_X_FORWARDED_FOR"].split(",", 1)
            request.META["REMOTE_ADDR"] = parts[0]
        ip = request.META["REMOTE_ADDR"]
        g = GeoIP2()
        try:
            ip_response = g.city(ip)
            time_zone = ip_response['time_zone']
        except AddressNotFoundError:
            time_zone = None
        if time_zone:
            timezone_object = pytz.timezone(time_zone)
            timezone.activate(timezone_object)
        else:
            timezone.deactivate()
        return self.get_response(request) 
开发者ID:meneses-pt,项目名称:goals.zone,代码行数:20,代码来源:timezone.py

示例8: test_override

# 需要导入模块: from django.utils import timezone [as 别名]
# 或者: from django.utils.timezone import activate [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() 
开发者ID:nesdis,项目名称:djongo,代码行数:26,代码来源:test_timezone.py

示例9: process_request

# 需要导入模块: from django.utils import timezone [as 别名]
# 或者: from django.utils.timezone import activate [as 别名]
def process_request(self, request):
        tzname = request.session.get('django_timezone')
        if tzname:
            timezone.activate(pytz.timezone(tzname))
        else:
            timezone.deactivate() 
开发者ID:fpsw,项目名称:Servo,代码行数:8,代码来源:middleware.py

示例10: process_request

# 需要导入模块: from django.utils import timezone [as 别名]
# 或者: from django.utils.timezone import activate [as 别名]
def process_request(self, request):
        zone = self.get_timezone_from_request(request)
        if zone:
            timezone.activate(zone) 
开发者ID:charettes,项目名称:django-sundial,代码行数:6,代码来源:middleware.py

示例11: process_request

# 需要导入模块: from django.utils import timezone [as 别名]
# 或者: from django.utils.timezone import activate [as 别名]
def process_request(self, request):
        if request.user.is_authenticated():
            tz = request.session.get('django_timezone',
                                     default=request.user.profile.time_zone) or settings.TIME_ZONE
            timezone.activate(tz)
        else:
            timezone.deactivate() 
开发者ID:znick,项目名称:anytask,代码行数:9,代码来源:timezone_middleware.py

示例12: get_message

# 需要导入模块: from django.utils import timezone [as 别名]
# 或者: from django.utils.timezone import activate [as 别名]
def get_message(user, user_type, issue, events, from_email, domain):
    user_profile = user.profile

    if not user_profile.send_my_own_events:
        events = events.exclude(author_id=user.id)

    if not events:
        return ()

    lang = user_profile.language
    translation.activate(lang)
    timezone.activate(user_profile.time_zone)

    subject = (_(u'kurs') + u': {0} | ' + _(u'zadacha') + u': {1} | ' + _(u'student') + u': {2} {3}'). \
        format(issue.task.course.name, issue.task.get_title(lang), issue.student.last_name, issue.student.first_name)

    context = {
        "user": user,
        "domain": domain,
        "title": subject,
        "user_type": user_type,
        "issue": issue,
        "events": events,
        "STATIC_URL": settings.STATIC_URL,
    }

    plain_text = render_to_string('email_issue_notification.txt', context)
    html = render_to_string('email_issue_notification.html', context)
    translation.deactivate()
    timezone.deactivate()

    return subject, plain_text, html, from_email, [user.email] 
开发者ID:znick,项目名称:anytask,代码行数:34,代码来源:send_notifications.py

示例13: notification_template

# 需要导入模块: from django.utils import timezone [as 别名]
# 或者: from django.utils.timezone import activate [as 别名]
def notification_template(notification_type):
    template = NotificationTemplate.objects.create(
        type=NotificationType.TEST,
        subject_en="test subject, variable value: {{ subject_var }}!",
        body_en="test body, variable value: {{ body_var }}!",
        html_body_en="test <b>HTML</b> body, variable value: {{ html_body_var }}!",
    )
    activate('fi')
    template.subject = "testiotsikko, muuttujan arvo: {{ subject_var }}!"
    template.body = "testiruumis, muuttujan arvo: {{ body_var }}!"
    template.html_body = "testi<b>hötömölö</b>ruumis, muuttujan arvo: {{ html_body_var }}!"
    template.save()

    return template 
开发者ID:City-of-Helsinki,项目名称:linkedevents,代码行数:16,代码来源:test_notifications.py

示例14: test_existing_finalize

# 需要导入模块: from django.utils import timezone [as 别名]
# 或者: from django.utils.timezone import activate [as 别名]
def test_existing_finalize(self):
        finalize_mock = mock.Mock(side_effect=lambda s: s)

        class TestExtension(Extension):
            def __init__(self, environment):
                environment.finalize = finalize_mock

        env = Environment(extensions=[TestExtension, DjangoL10n])
        template = env.from_string("{{ foo }}")

        translation.activate('de')
        self.assertEqual('1,23', template.render({'foo': 1.23}))
        finalize_mock.assert_called_with(1.23) 
开发者ID:MoritzS,项目名称:jinja2-django-tags,代码行数:15,代码来源:tests.py

示例15: localnow

# 需要导入模块: from django.utils import timezone [as 别名]
# 或者: from django.utils.timezone import activate [as 别名]
def localnow():
    """ Get the current datetime in the local timezone for the user
    (timezone set by timezone.activate())."""
    return timezone.localtime(utcnow(), timezone=timezone.get_current_timezone()) 
开发者ID:andersroos,项目名称:rankedftw,代码行数:6,代码来源:utils.py


注:本文中的django.utils.timezone.activate方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。