Skip to main content
Notice removed Draw attention by user103180
Bounty Ended with ferada's answer chosen by CommunityBot
Tweeted twitter.com/StackCodeReview/status/855714864710463489
Notice added Draw attention by user103180
Bounty Started worth 50 reputation by CommunityBot
edited tags
Link
200_success
  • 144.2k
  • 22
  • 188
  • 473
edited title
Link
Phrancis
  • 20.4k
  • 6
  • 68
  • 154

Correct way to check for a local variable Set time zone from cookie

Source Link
user103180
user103180

Correct way to check for a local variable

Is the set_cookie variable the correct way to signal that the cookie should be set? It does not seems pythonic to me:

cookie = request.COOKIES.get(TIMEZONE_COOKIE_NAME)
set_cookie = False
if cookie and cookie in pytz.all_timezones_set:
    timezone.activate(pytz.timezone(cookie))
elif request.ip:
    geo = GeoIP2()
    try:
        time_zone = geo.city(request.ip).time_zone
        if time_zone:
            timezone.activate(pytz.timezone(time_zone))
            set_cookie = True
    except GeoIP2Error:
        self.logger.warning('Could not determine time zone of ip address: %s', request.ip)

response = self.get_response(request)
if set_cookie:
    response.set_cookie(TIMEZONE_COOKIE_NAME, time_zone)
return response