<s> from datetime import datetime <EOL> import string <EOL> import babel <EOL> import babel . dates <EOL> from django . conf import settings <EOL> from django import shortcuts <EOL> from django . utils import encoding <EOL> from django . utils import translation <EOL> from django . utils . translation import ugettext_lazy as _ <EOL> import pytz <EOL> from horizon import forms <EOL> from horizon import messages <EOL> def _one_year ( ) : <EOL> now = datetime . utcnow ( ) <EOL> return datetime ( now . year + <NUM_LIT:1> , now . month , now . day , now . hour , <EOL> now . minute , now . second , now . microsecond , now . tzinfo ) <EOL> class UserSettingsForm ( forms . SelfHandlingForm ) : <EOL> language = forms . ChoiceField ( label = _ ( "<STR_LIT>" ) ) <EOL> timezone = forms . ChoiceField ( label = _ ( "<STR_LIT>" ) ) <EOL> pagesize = forms . IntegerField ( label = _ ( "<STR_LIT>" ) , <EOL> min_value = <NUM_LIT:1> , <EOL> max_value = getattr ( settings , <EOL> '<STR_LIT>' , <EOL> <NUM_LIT:1000> ) , <EOL> help_text = _ ( "<STR_LIT>" <EOL> "<STR_LIT>" ) ) <EOL> @ staticmethod <EOL> def _sorted_zones ( ) : <EOL> d = datetime ( datetime . today ( ) . year , <NUM_LIT:1> , <NUM_LIT:1> ) <EOL> zones = [ ( tz , pytz . timezone ( tz ) . localize ( d ) . strftime ( '<STR_LIT>' ) ) <EOL> for tz in pytz . common_timezones ] <EOL> zones . sort ( key = lambda zone : int ( zone [ <NUM_LIT:1> ] ) ) <EOL> return zones <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> super ( UserSettingsForm , self ) . __init__ ( * args , ** kwargs ) <EOL> def get_language_display_name ( code , desc ) : <EOL> try : <EOL> desc = translation . get_language_info ( code ) [ '<STR_LIT>' ] <EOL> desc = string . capwords ( desc ) <EOL> except KeyError : <EOL> pass <EOL> return "<STR_LIT>" % ( desc , code ) <EOL> languages = [ ( k , get_language_display_name ( k , v ) ) <EOL> for k , v in settings . LANGUAGES ] <EOL> self . fields [ '<STR_LIT>' ] . choices = languages <EOL> timezones = [ ] <EOL> language = translation . get_language ( ) <EOL> current_locale = translation . to_locale ( language ) <EOL> babel_locale = babel . Locale . parse ( current_locale ) <EOL> for tz , offset in self . _sorted_zones ( ) : <EOL> try : <EOL> utc_offset = _ ( "<STR_LIT>" ) % { "<STR_LIT>" : offset [ : <NUM_LIT:3> ] , <EOL> "<STR_LIT>" : offset [ <NUM_LIT:3> : ] } <EOL> except Exception : <EOL> utc_offset = "<STR_LIT>" <EOL> if tz == "<STR_LIT>" : <EOL> tz_name = _ ( "<STR_LIT>" ) <EOL> elif tz == "<STR_LIT>" : <EOL> tz_name = _ ( "<STR_LIT>" ) <EOL> else : <EOL> tz_label = babel . dates . get_timezone_location ( <EOL> tz , locale = babel_locale ) <EOL> tz_name = _ ( "<STR_LIT>" ) % { "<STR_LIT>" : utc_offset , <EOL> "<STR_LIT:label>" : tz_label } <EOL> timezones . append ( ( tz , tz_name ) ) <EOL> self . fields [ '<STR_LIT>' ] . choices = timezones <EOL> def handle ( self , request , data ) : <EOL> response = shortcuts . redirect ( request . build_absolute_uri ( ) ) <EOL> lang_code = data [ '<STR_LIT>' ] <EOL> if lang_code and translation . check_for_language ( lang_code ) : <EOL> if hasattr ( request , '<STR_LIT>' ) : <EOL> request . session [ '<STR_LIT>' ] = lang_code <EOL> response . set_cookie ( settings . LANGUAGE_COOKIE_NAME , lang_code , <EOL> expires = _one_year ( ) ) <EOL> request . session [ '<STR_LIT>' ] = pytz . timezone ( <EOL> data [ '<STR_LIT>' ] ) . zone <EOL> response . set_cookie ( '<STR_LIT>' , data [ '<STR_LIT>' ] , <EOL> expires = _one_year ( ) ) <EOL> request . session [ '<STR_LIT>' ] = data [ '<STR_LIT>' ] <EOL> response . set_cookie ( '<STR_LIT>' , data [ '<STR_LIT>' ] , <EOL> expires = _one_year ( ) ) <EOL> with translation . override ( lang_code ) : <EOL> messages . success ( request , <EOL> encoding . force_text ( _ ( "<STR_LIT>" ) ) ) <EOL> return response </s>
