Python django.utils.timezone.deactivate() Examples

The following are 26 code examples for showing how to use django.utils.timezone.deactivate(). These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example.

You may check out the related API usage on the sidebar.

You may also want to check out all available functions/classes of the module django.utils.timezone , or try the search function .

Example 1
Project: django-aws-template   Author: dkarchmer   File: timezoneMiddleware.py    License: MIT License 6 votes vote down vote up
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 
Example 2
Project: goals.zone   Author: meneses-pt   File: timezone.py    License: GNU General Public License v3.0 6 votes vote down vote up
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) 
Example 3
Project: djongo   Author: nesdis   File: test_timezone.py    License: GNU Affero General Public License v3.0 6 votes vote down vote up
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() 
Example 4
Project: djongo   Author: nesdis   File: test_timezone.py    License: GNU Affero General Public License v3.0 6 votes vote down vote up
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() 
Example 5
Project: Servo   Author: fpsw   File: middleware.py    License: BSD 2-Clause "Simplified" License 5 votes vote down vote up
def process_request(self, request):
        tzname = request.session.get('django_timezone')
        if tzname:
            timezone.activate(pytz.timezone(tzname))
        else:
            timezone.deactivate() 
Example 6
Project: django-sundial   Author: charettes   File: test_middleware.py    License: MIT License 5 votes vote down vote up
def tearDown(self):
        timezone.deactivate() 
Example 7
Project: anytask   Author: znick   File: timezone_middleware.py    License: MIT License 5 votes vote down vote up
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() 
Example 8
Project: anytask   Author: znick   File: send_notifications.py    License: MIT License 5 votes vote down vote up
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] 
Example 9
Project: hawkpost   Author: whitesmith   File: middleware.py    License: MIT License 5 votes vote down vote up
def process_request(self, request):
        if request.user.is_authenticated():
            timezone.activate(request.user.timezone)
        else:
            timezone.deactivate() 
Example 10
Project: palanaeum   Author: Palanaeum   File: middleware.py    License: GNU Affero General Public License v3.0 5 votes vote down vote up
def __call__(self, request):
        if request.user.is_authenticated:
            if hasattr(request.user, 'settings'):
                settings = request.user.settings
            else:
                from palanaeum.models import UserSettings
                settings = UserSettings.objects.create(user=request.user)
            tzname = settings.timezone
            timezone.activate(pytz.timezone(tzname))
        else:
            timezone.deactivate()

        response = self.get_response(request)

        return response 
Example 11
Project: Spirit   Author: nitely   File: tests.py    License: MIT License 5 votes vote down vote up
def setUp(self):
        timezone.deactivate()
        utils.cache_clear()
        self.user = utils.create_user() 
Example 12
Project: Spirit   Author: nitely   File: tests.py    License: MIT License 5 votes vote down vote up
def test_timezone(self):
        """
        Should activate the user timezone
        """
        timezone.deactivate()
        utils.login(self)
        req = RequestFactory().get('/')
        req.user = self.user
        time_zone = 'America/Argentina/Buenos_Aires'
        self.user.st.timezone = time_zone

        self.assertEqual(timezone.get_current_timezone().zone, 'UTC')
        middleware.TimezoneMiddleware().process_request(req)
        self.assertEqual(timezone.get_current_timezone().zone, time_zone) 
Example 13
Project: Spirit   Author: nitely   File: tests.py    License: MIT License 5 votes vote down vote up
def test_timezone_bad_tz(self):
        timezone.deactivate()
        utils.login(self)
        req = RequestFactory().get('/')
        req.user = self.user
        self.user.st.timezone = 'badtimezone'

        time_zone = 'America/Argentina/Buenos_Aires'
        timezone.activate(time_zone)
        self.assertEqual(timezone.get_current_timezone().zone, time_zone)
        middleware.TimezoneMiddleware().process_request(req)
        self.assertEqual(timezone.get_current_timezone().zone, 'UTC') 
Example 14
Project: Spirit   Author: nitely   File: tests.py    License: MIT License 5 votes vote down vote up
def test_timezone_anonymous_user(self):
        class AnonymUserMock(object):
            @property
            def is_authenticated(self):
                return False

        timezone.deactivate()
        req = RequestFactory().get('/')
        req.user = AnonymUserMock()

        time_zone = 'America/Argentina/Buenos_Aires'
        timezone.activate(time_zone)
        self.assertEqual(timezone.get_current_timezone().zone, time_zone)
        middleware.TimezoneMiddleware().process_request(req)
        self.assertEqual(timezone.get_current_timezone().zone, 'UTC') 
Example 15
Project: Spirit   Author: nitely   File: middleware.py    License: MIT License 5 votes vote down vote up
def process_request(self, request):
        if not request.user.is_authenticated:
            timezone.deactivate()
            return

        try:
            timezone.activate(request.user.st.timezone)
        except pytz.exceptions.UnknownTimeZoneError:
            timezone.deactivate()
            logger.error(
                '%s is not a valid timezone.', request.user.st.timezone) 
Example 16
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() 
Example 17
Project: approval_frame   Author: electionscience   File: timezone_middleware.py    License: GNU General Public License v3.0 5 votes vote down vote up
def process_request(self, request):
        tzname = request.session.get('django_timezone')
        if tzname:
            timezone.activate(tzname)
        else:
            timezone.deactivate() 
Example 18
Project: diting   Author: getway   File: middleware.py    License: GNU General Public License v2.0 5 votes vote down vote up
def __call__(self, request):
        tzname = request.META.get('TZ')
        if tzname:
            timezone.activate(pytz.timezone(tzname))
        else:
            timezone.deactivate()
        response = self.get_response(request)
        return response 
Example 19
Project: colossus   Author: vitorfs   File: middleware.py    License: MIT License 5 votes vote down vote up
def __call__(self, request):
        if request.user.is_authenticated:
            try:
                timezone.activate(pytz.timezone(request.user.timezone))
            except UnknownTimeZoneError:
                timezone.deactivate()

        response = self.get_response(request)
        return response 
Example 20
Project: djongo   Author: nesdis   File: test_timezone.py    License: GNU Affero General Public License v3.0 5 votes vote down vote up
def test_override_decorator(self):
        default = timezone.get_default_timezone()

        @timezone.override(EAT)
        def func_tz_eat():
            self.assertIs(EAT, timezone.get_current_timezone())

        @timezone.override(None)
        def func_tz_none():
            self.assertIs(default, timezone.get_current_timezone())

        try:
            timezone.activate(ICT)

            func_tz_eat()
            self.assertIs(ICT, timezone.get_current_timezone())

            func_tz_none()
            self.assertIs(ICT, timezone.get_current_timezone())

            timezone.deactivate()

            func_tz_eat()
            self.assertIs(default, timezone.get_current_timezone())

            func_tz_none()
            self.assertIs(default, timezone.get_current_timezone())
        finally:
            timezone.deactivate() 
Example 21
Project: djongo   Author: nesdis   File: test_timezone.py    License: GNU Affero General Public License v3.0 5 votes vote down vote up
def test_override_decorator(self):
        default = timezone.get_default_timezone()

        @timezone.override(EAT)
        def func_tz_eat():
            self.assertIs(EAT, timezone.get_current_timezone())

        @timezone.override(None)
        def func_tz_none():
            self.assertIs(default, timezone.get_current_timezone())

        try:
            timezone.activate(ICT)

            func_tz_eat()
            self.assertIs(ICT, timezone.get_current_timezone())

            func_tz_none()
            self.assertIs(ICT, timezone.get_current_timezone())

            timezone.deactivate()

            func_tz_eat()
            self.assertIs(default, timezone.get_current_timezone())

            func_tz_none()
            self.assertIs(default, timezone.get_current_timezone())
        finally:
            timezone.deactivate() 
Example 22
Project: ls.joyous   Author: linuxsoftware   File: middleware.py    License: BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def process_request(self, request):
        try:
            tzname = request.user.wagtail_userprofile.current_time_zone
        except AttributeError:
            tzname = None
        if tzname:
            timezone.activate(pytz.timezone(tzname))
        else:
            timezone.deactivate() 
Example 23
Project: ls.joyous   Author: linuxsoftware   File: test_multiday_event.py    License: BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def tearDown(self):
        timezone.deactivate() 
Example 24
Project: timestrap   Author: overshard   File: i18n.py    License: BSD 2-Clause "Simplified" License 5 votes vote down vote up
def process_request(self, request):
        tzname = get_site_setting("i18n_timezone")
        if tzname:
            timezone.activate(pytz.timezone(tzname))
        else:
            timezone.deactivate() 
Example 25
Project: brutaldon   Author: jfmcbrayer   File: timezone.py    License: GNU Affero General Public License v3.0 5 votes vote down vote up
def process_request(self, request):
        tzname = request.session.get("timezone", "UTC")
        if tzname:
            timezone.activate(pytz.timezone(tzname))
        else:
            timezone.deactivate() 
Example 26
Project: horas   Author: SoPR   File: middleware.py    License: MIT License 5 votes vote down vote up
def process_request(self, request):
        tzname = request.session.get("django_timezone")

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

        elif request.user.is_authenticated:
            try:
                tzname = request.user.timezone
                timezone.activate(pytz.timezone(tzname))
            except Exception as e:
                print("Failed to set timezone", e)
        else:
            timezone.deactivate()