Python django.utils.timezone.deactivate() Examples

The following are 26 code examples of django.utils.timezone.deactivate(). 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 also want to check out all available functions/classes of the module django.utils.timezone , or try the search function .
Example #1
Source File: timezoneMiddleware.py    From django-aws-template with 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
Source File: test_timezone.py    From djongo with 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 #3
Source File: test_timezone.py    From djongo with 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
Source File: timezone.py    From goals.zone with 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 #5
Source File: timezone_middleware.py    From iguana with Creative Commons Attribution Share Alike 4.0 International 5 votes vote down vote up
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 #6
Source File: middleware.py    From horas with 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() 
Example #7
Source File: timezone.py    From brutaldon with 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 #8
Source File: i18n.py    From timestrap with 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 #9
Source File: test_multiday_event.py    From ls.joyous with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def tearDown(self):
        timezone.deactivate() 
Example #10
Source File: middleware.py    From ls.joyous with 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 #11
Source File: test_timezone.py    From djongo with 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 #12
Source File: test_timezone.py    From djongo with 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 #13
Source File: middleware.py    From colossus with 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 #14
Source File: middleware.py    From diting with 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 #15
Source File: timezone_middleware.py    From approval_frame with 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 #16
Source File: middleware.py    From Servo with 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 #17
Source File: middleware.py    From Spirit with 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 #18
Source File: tests.py    From Spirit with 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 #19
Source File: tests.py    From Spirit with 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 #20
Source File: tests.py    From Spirit with 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 #21
Source File: tests.py    From Spirit with MIT License 5 votes vote down vote up
def setUp(self):
        timezone.deactivate()
        utils.cache_clear()
        self.user = utils.create_user() 
Example #22
Source File: middleware.py    From palanaeum with 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 #23
Source File: middleware.py    From hawkpost with 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 #24
Source File: send_notifications.py    From anytask with 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 #25
Source File: timezone_middleware.py    From anytask with 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 #26
Source File: test_middleware.py    From django-sundial with MIT License 5 votes vote down vote up
def tearDown(self):
        timezone.deactivate()