<s> """<STR_LIT>""" <EOL> import os , hashlib , logging , httplib , string , base64 , json , traceback , types <EOL> from datetime import datetime , timedelta , tzinfo <EOL> from django . conf import settings <EOL> from django . db . models import Model , Avg <EOL> from django . core . exceptions import ObjectDoesNotExist <EOL> from django . contrib . sessions . models import Session <EOL> from django . contrib . auth . models import User <EOL> from django . utils . translation import ugettext as _ <EOL> from models import * <EOL> from rosetta import polib <EOL> from djsld import generator <EOL> import sld <EOL> logger = logging . getLogger ( __name__ ) <EOL> def check_and_update ( a_model , unique_id_field = '<STR_LIT:name>' , overwrite = False , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> name = kwargs [ unique_id_field ] <EOL> object_name = '<STR_LIT>' % ( a_model . __name__ , name ) <EOL> try : <EOL> id_args = { unique_id_field : name } <EOL> current_object = a_model . objects . get ( ** id_args ) <EOL> except ObjectDoesNotExist : <EOL> new = a_model ( ** kwargs ) <EOL> new . save ( ) <EOL> return new , True , False , '<STR_LIT>' % object_name <EOL> different = [ ] <EOL> changed = False <EOL> message = '<STR_LIT>' <EOL> for key in kwargs : <EOL> current_value = current_object . __getattribute__ ( key ) <EOL> config_value = kwargs [ key ] <EOL> if not ( isinstance ( current_value , types . StringTypes ) ) and not ( isinstance ( current_value , Model ) ) : <EOL> config_value = type ( current_value ) ( config_value ) <EOL> if current_value != config_value : <EOL> if overwrite : <EOL> current_object . __setattr__ ( key , config_value ) <EOL> changed = True <EOL> message = '<STR_LIT>' <EOL> else : <EOL> message = '<STR_LIT>' <EOL> different . append ( key ) <EOL> if overwrite and changed : <EOL> current_object . save ( ) <EOL> return current_object , False , len ( different ) > <NUM_LIT:0> , message % ( object_name , '<STR_LIT:U+002CU+0020>' . join ( different ) ) <EOL> class Utils : <EOL> """<STR_LIT>""" <EOL> @ staticmethod <EOL> def purge_sessions ( ) : <EOL> """<STR_LIT>""" <EOL> qset = Session . objects . all ( ) <EOL> logger . debug ( '<STR_LIT>' , qset . count ( ) ) <EOL> try : <EOL> qset . delete ( ) <EOL> except Exception , e : <EOL> logger . info ( '<STR_LIT>' ) <EOL> return False <EOL> return True <EOL> class ConfigImporter : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , store ) : <EOL> """<STR_LIT>""" <EOL> self . store = store <EOL> self . poutils = { } <EOL> for locale in [ l [ <NUM_LIT:0> ] for l in settings . LANGUAGES ] : <EOL> self . poutils [ locale ] = PoUtils ( locale ) <EOL> def save ( self ) : <EOL> """<STR_LIT>""" <EOL> for locale in self . poutils : <EOL> self . poutils [ locale ] . save ( ) <EOL> def import_superuser ( self , force ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> admcfg = self . store . get_admin ( ) <EOL> admin_attributes = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : True , <EOL> '<STR_LIT>' : True , <EOL> '<STR_LIT>' : True , <EOL> '<STR_LIT:username>' : admcfg . get ( '<STR_LIT:user>' ) [ : <NUM_LIT:30> ] , <EOL> '<STR_LIT:email>' : admcfg . get ( '<STR_LIT:email>' ) [ : <NUM_LIT> ] <EOL> } <EOL> admin , created , changed , message = check_and_update ( User , unique_id_field = '<STR_LIT:username>' , overwrite = force , ** admin_attributes ) <EOL> if changed and not force : <EOL> logger . info ( message ) <EOL> else : <EOL> logger . debug ( message ) <EOL> if created or changed or force : <EOL> m = hashlib . sha1 ( ) <EOL> m . update ( admcfg . get ( '<STR_LIT:password>' ) ) <EOL> admin . set_password ( m . hexdigest ( ) ) <EOL> admin . save ( ) <EOL> return True <EOL> except : <EOL> logger . info ( '<STR_LIT>' ) <EOL> logger . info ( traceback . format_exc ( ) ) <EOL> return False <EOL> def import_regions ( self , force ) : <EOL> """<STR_LIT>""" <EOL> regions = self . store . filter_regions ( ) <EOL> for region in regions : <EOL> attributes = { <EOL> '<STR_LIT:name>' : region . get ( '<STR_LIT:id>' ) [ <NUM_LIT:0> : <NUM_LIT> ] , <EOL> '<STR_LIT>' : region . get ( '<STR_LIT>' ) <EOL> } <EOL> obj , created , changed , message = check_and_update ( Region , overwrite = force , ** attributes ) <EOL> for locale in [ l [ <NUM_LIT:0> ] for l in settings . LANGUAGES ] : <EOL> po = self . poutils [ locale ] <EOL> po . add_or_update ( <EOL> msgid = u'<STR_LIT>' % attributes [ '<STR_LIT:name>' ] , <EOL> msgstr = region . get ( '<STR_LIT:name>' ) or '<STR_LIT>' , <EOL> occurs = [ ( os . path . abspath ( self . store . datafile ) , region . sourceline , ) ] <EOL> ) <EOL> po . add_or_update ( <EOL> msgid = u'<STR_LIT>' % attributes [ '<STR_LIT:name>' ] , <EOL> msgstr = region . get ( '<STR_LIT:label>' ) or '<STR_LIT>' , <EOL> occurs = [ ( os . path . abspath ( self . store . datafile ) , region . sourceline , ) ] <EOL> ) <EOL> po . add_or_update ( <EOL> msgid = u'<STR_LIT>' % attributes [ '<STR_LIT:name>' ] , <EOL> msgstr = region . get ( '<STR_LIT:description>' ) or '<STR_LIT>' , <EOL> occurs = [ ( os . path . abspath ( self . store . datafile ) , region . sourceline , ) ] <EOL> ) <EOL> if changed and not force : <EOL> logger . info ( message ) <EOL> else : <EOL> logger . debug ( message ) <EOL> return True <EOL> def import_legislative_bodies ( self , force ) : <EOL> """<STR_LIT>""" <EOL> bodies = self . store . filter_legislative_bodies ( ) <EOL> for body in bodies : <EOL> attributes = { <EOL> '<STR_LIT:name>' : body . get ( '<STR_LIT:id>' ) [ : <NUM_LIT> ] , <EOL> '<STR_LIT>' : body . get ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' : body . get ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' : body . get ( '<STR_LIT>' ) == '<STR_LIT:true>' <EOL> } <EOL> body_by_region = self . store . filter_nodes ( '<STR_LIT>' % body . get ( '<STR_LIT:id>' ) ) <EOL> if len ( body_by_region ) != <NUM_LIT:1> : <EOL> logger . info ( "<STR_LIT>" , attributes [ '<STR_LIT:name>' ] ) <EOL> continue <EOL> else : <EOL> region_name = body_by_region [ <NUM_LIT:0> ] . getparent ( ) . getparent ( ) . get ( '<STR_LIT:id>' ) <EOL> attributes [ '<STR_LIT>' ] = Region . objects . get ( name = region_name ) <EOL> obj , created , changed , message = check_and_update ( LegislativeBody , overwrite = force , ** attributes ) <EOL> for locale in [ l [ <NUM_LIT:0> ] for l in settings . LANGUAGES ] : <EOL> po = self . poutils [ locale ] <EOL> po . add_or_update ( <EOL> msgid = u'<STR_LIT>' % attributes [ '<STR_LIT:name>' ] , <EOL> msgstr = body . get ( '<STR_LIT>' ) or '<STR_LIT>' , <EOL> occurs = [ ( os . path . abspath ( self . store . datafile ) , body . sourceline , ) ] <EOL> ) <EOL> po . add_or_update ( <EOL> msgid = u'<STR_LIT>' % attributes [ '<STR_LIT:name>' ] , <EOL> msgstr = body . get ( '<STR_LIT>' ) or '<STR_LIT>' , <EOL> occurs = [ ( os . path . abspath ( self . store . datafile ) , body . sourceline , ) ] <EOL> ) <EOL> po . add_or_update ( <EOL> msgid = u'<STR_LIT>' % attributes [ '<STR_LIT:name>' ] , <EOL> msgstr = body . get ( '<STR_LIT:name>' ) , <EOL> occurs = [ ( os . path . abspath ( self . store . datafile ) , body . sourceline , ) ] <EOL> ) <EOL> po . add_or_update ( <EOL> msgid = u'<STR_LIT>' % attributes [ '<STR_LIT:name>' ] , <EOL> msgstr = body . get ( '<STR_LIT>' ) , <EOL> occurs = [ ( os . path . abspath ( self . store . datafile ) , body . sourceline , ) ] <EOL> ) <EOL> if changed and not force : <EOL> logger . info ( message ) <EOL> else : <EOL> logger . debug ( message ) <EOL> if obj is None : <EOL> continue <EOL> mmconfig = self . store . get_node ( '<STR_LIT>' % body . get ( '<STR_LIT:id>' ) ) <EOL> if mmconfig : <EOL> obj . multi_members_allowed = True <EOL> obj . multi_district_label_format = mmconfig . get ( '<STR_LIT>' ) [ : <NUM_LIT:32> ] <EOL> obj . min_multi_districts = mmconfig . get ( '<STR_LIT>' ) <EOL> obj . max_multi_districts = mmconfig . get ( '<STR_LIT>' ) <EOL> obj . min_multi_district_members = mmconfig . get ( '<STR_LIT>' ) <EOL> obj . max_multi_district_members = mmconfig . get ( '<STR_LIT>' ) <EOL> obj . min_plan_members = mmconfig . get ( '<STR_LIT>' ) <EOL> obj . max_plan_members = mmconfig . get ( '<STR_LIT>' ) <EOL> logger . debug ( '<STR_LIT>' , body . get ( '<STR_LIT:name>' ) ) <EOL> else : <EOL> obj . multi_members_allowed = False <EOL> obj . multi_district_label_format = '<STR_LIT>' <EOL> obj . min_multi_districts = <NUM_LIT:0> <EOL> obj . max_multi_districts = <NUM_LIT:0> <EOL> obj . min_multi_district_members = <NUM_LIT:0> <EOL> obj . max_multi_district_members = <NUM_LIT:0> <EOL> obj . min_plan_members = <NUM_LIT:0> <EOL> obj . max_plan_members = <NUM_LIT:0> <EOL> logger . debug ( '<STR_LIT>' , body . get ( '<STR_LIT:name>' ) ) <EOL> obj . save ( ) <EOL> return True <EOL> def import_subjects ( self , force ) : <EOL> """<STR_LIT>""" <EOL> subjs = self . store . filter_subjects ( ) <EOL> for subj in subjs : <EOL> if '<STR_LIT>' in subj . attrib : <EOL> continue <EOL> attributes = { <EOL> '<STR_LIT:name>' : subj . get ( '<STR_LIT:id>' ) . lower ( ) [ : <NUM_LIT:50> ] , <EOL> '<STR_LIT>' : subj . get ( '<STR_LIT>' ) == '<STR_LIT:true>' , <EOL> '<STR_LIT>' : subj . get ( '<STR_LIT>' ) <EOL> } <EOL> obj , created , changed , message = check_and_update ( Subject , overwrite = force , ** attributes ) <EOL> for locale in [ l [ <NUM_LIT:0> ] for l in settings . LANGUAGES ] : <EOL> po = self . poutils [ locale ] <EOL> po . add_or_update ( <EOL> msgid = u'<STR_LIT>' % attributes [ '<STR_LIT:name>' ] , <EOL> msgstr = subj . get ( '<STR_LIT>' ) or '<STR_LIT>' , <EOL> occurs = [ ( os . path . abspath ( self . store . datafile ) , subj . sourceline , ) ] <EOL> ) <EOL> po . add_or_update ( <EOL> msgid = u'<STR_LIT>' % attributes [ '<STR_LIT:name>' ] , <EOL> msgstr = subj . get ( '<STR_LIT:name>' ) or '<STR_LIT>' , <EOL> occurs = [ ( os . path . abspath ( self . store . datafile ) , subj . sourceline , ) ] <EOL> ) <EOL> po . add_or_update ( <EOL> msgid = u'<STR_LIT>' % attributes [ '<STR_LIT:name>' ] , <EOL> msgstr = '<STR_LIT>' , <EOL> occurs = [ ( os . path . abspath ( self . store . datafile ) , subj . sourceline , ) ] <EOL> ) <EOL> if changed and not force : <EOL> logger . info ( message ) <EOL> else : <EOL> logger . debug ( message ) <EOL> for subj in subjs : <EOL> numerator_name = name = subj . get ( '<STR_LIT:id>' ) . lower ( ) [ : <NUM_LIT:50> ] <EOL> try : <EOL> numerator = Subject . objects . get ( name = numerator_name ) <EOL> except Exception , ex : <EOL> logger . info ( '<STR_LIT>' , numerator_name ) <EOL> raise <EOL> denominator = None <EOL> denominator_name = subj . get ( '<STR_LIT>' ) <EOL> if denominator_name : <EOL> denominator_name = denominator_name . lower ( ) [ : <NUM_LIT:50> ] <EOL> try : <EOL> denominator = Subject . objects . get ( name = denominator_name ) <EOL> except Exception , ex : <EOL> logger . info ( '<STR_LIT>' , denominator_name ) <EOL> raise <EOL> numerator . percentage_denominator = denominator <EOL> numerator . save ( ) <EOL> logger . debug ( '<STR_LIT>' , numerator . name , denominator_name ) <EOL> return True <EOL> def import_geolevels ( self , force ) : <EOL> """<STR_LIT>""" <EOL> geolevels = self . store . filter_geolevels ( ) <EOL> for geolevel in geolevels : <EOL> attributes = { <EOL> '<STR_LIT:name>' : geolevel . get ( '<STR_LIT:id>' ) . lower ( ) [ : <NUM_LIT:50> ] , <EOL> '<STR_LIT>' : geolevel . get ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' : geolevel . get ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' : geolevel . get ( '<STR_LIT>' ) <EOL> } <EOL> glvl , created , changed , message = check_and_update ( Geolevel , overwrite = force , ** attributes ) <EOL> for locale in [ l [ <NUM_LIT:0> ] for l in settings . LANGUAGES ] : <EOL> po = self . poutils [ locale ] <EOL> po . add_or_update ( <EOL> msgid = u'<STR_LIT>' % attributes [ '<STR_LIT:name>' ] , <EOL> msgstr = geolevel . get ( '<STR_LIT:name>' ) or '<STR_LIT>' , <EOL> occurs = [ ( os . path . abspath ( self . store . datafile ) , geolevel . sourceline , ) ] <EOL> ) <EOL> po . add_or_update ( <EOL> msgid = u'<STR_LIT>' % attributes [ '<STR_LIT:name>' ] , <EOL> msgstr = geolevel . get ( '<STR_LIT:label>' ) or '<STR_LIT>' , <EOL> occurs = [ ( os . path . abspath ( self . store . datafile ) , geolevel . sourceline , ) ] <EOL> ) <EOL> po . add_or_update ( <EOL> msgid = u'<STR_LIT>' % attributes [ '<STR_LIT:name>' ] , <EOL> msgstr = '<STR_LIT>' , <EOL> occurs = [ ( os . path . abspath ( self . store . datafile ) , geolevel . sourceline , ) ] <EOL> ) <EOL> if changed and not force : <EOL> logger . info ( message ) <EOL> else : <EOL> logger . debug ( message ) <EOL> return True <EOL> def import_regional_geolevels ( self , force ) : <EOL> """<STR_LIT>""" <EOL> regions = self . store . filter_regions ( ) <EOL> for region in regions : <EOL> regional_geolevels = self . store . filter_regional_geolevel ( region ) <EOL> zero_geolevel_config = self . store . get_geolevel ( regional_geolevels [ len ( regional_geolevels ) - <NUM_LIT:1> ] . get ( '<STR_LIT>' ) ) <EOL> zero_zoom = int ( zero_geolevel_config . get ( '<STR_LIT>' ) ) <EOL> for geolevel in regional_geolevels : <EOL> try : <EOL> geolevel_obj = Geolevel . objects . get ( name = geolevel . get ( '<STR_LIT>' ) ) <EOL> except : <EOL> logger . debug ( "<STR_LIT>" , name , region . get ( '<STR_LIT:name>' ) ) <EOL> return <EOL> attributes = { <EOL> '<STR_LIT:name>' : '<STR_LIT>' % ( region . get ( '<STR_LIT:name>' ) , geolevel . get ( '<STR_LIT>' ) ) , <EOL> '<STR_LIT>' : geolevel_obj . min_zoom - zero_zoom , <EOL> '<STR_LIT>' : geolevel_obj . tolerance <EOL> } <EOL> obj , created , changed , message = check_and_update ( Geolevel , overwrite = force , ** attributes ) <EOL> for locale in [ l [ <NUM_LIT:0> ] for l in settings . LANGUAGES ] : <EOL> po = self . poutils [ locale ] <EOL> po . add_or_update ( <EOL> msgid = u'<STR_LIT>' % attributes [ '<STR_LIT:name>' ] , <EOL> msgstr = geolevel . get ( '<STR_LIT:name>' ) or '<STR_LIT>' , <EOL> occurs = [ ( os . path . abspath ( self . store . datafile ) , geolevel . sourceline , ) ] <EOL> ) <EOL> po . add_or_update ( <EOL> msgid = u'<STR_LIT>' % attributes [ '<STR_LIT:name>' ] , <EOL> msgstr = geolevel . get ( '<STR_LIT:label>' ) or '<STR_LIT>' , <EOL> occurs = [ ( os . path . abspath ( self . store . datafile ) , geolevel . sourceline , ) ] <EOL> ) <EOL> po . add_or_update ( <EOL> msgid = u'<STR_LIT>' % attributes [ '<STR_LIT:name>' ] , <EOL> msgstr = '<STR_LIT>' , <EOL> occurs = [ ( os . path . abspath ( self . store . datafile ) , geolevel . sourceline , ) ] <EOL> ) <EOL> if changed and not force : <EOL> logger . info ( message ) <EOL> else : <EOL> logger . debug ( message ) <EOL> for region in regions : <EOL> lbodies = self . store . filter_regional_legislative_bodies ( region ) <EOL> for lbody in lbodies : <EOL> legislative_body = LegislativeBody . objects . get ( name = lbody . get ( '<STR_LIT>' ) [ : <NUM_LIT> ] ) <EOL> sconfig = self . store . get_legislative_body_default_subject ( lbody ) <EOL> if not sconfig . get ( '<STR_LIT>' ) is None : <EOL> sconfig = self . store . get_subject ( sconfig . get ( '<STR_LIT>' ) ) <EOL> subject = Subject . objects . get ( name = sconfig . get ( '<STR_LIT:id>' ) . lower ( ) [ : <NUM_LIT:50> ] ) <EOL> def add_legislative_level_for_geolevel ( node , body , subject , parent ) : <EOL> """<STR_LIT>""" <EOL> geolevel_node = self . store . get_geolevel ( node . get ( '<STR_LIT>' ) ) <EOL> geolevel_name = "<STR_LIT>" % ( region . get ( '<STR_LIT:id>' ) , geolevel_node . get ( '<STR_LIT:id>' ) ) <EOL> geolevel = Geolevel . objects . get ( name = geolevel_name ) <EOL> obj , created = LegislativeLevel . objects . get_or_create ( <EOL> legislative_body = body , <EOL> geolevel = geolevel , <EOL> subject = subject , <EOL> parent = parent ) <EOL> if created : <EOL> logger . debug ( '<STR_LIT>' , legislative_body . name , geolevel . name ) <EOL> else : <EOL> logger . debug ( '<STR_LIT>' , legislative_body . name , geolevel . name ) <EOL> if len ( node ) > <NUM_LIT:0> : <EOL> add_legislative_level_for_geolevel ( node [ <NUM_LIT:0> ] , body , subject , obj ) <EOL> parentless = self . store . get_top_regional_geolevel ( region ) <EOL> if parentless is not None : <EOL> add_legislative_level_for_geolevel ( parentless , legislative_body , subject , None ) <EOL> return True <EOL> def import_scoring ( self , force ) : <EOL> """<STR_LIT>""" <EOL> result = True <EOL> if not self . store . has_scoring ( ) : <EOL> logger . debug ( '<STR_LIT>' ) <EOL> admin = User . objects . filter ( is_superuser = True ) <EOL> if admin . count ( ) == <NUM_LIT:0> : <EOL> logger . debug ( '<STR_LIT>' ) <EOL> return False <EOL> else : <EOL> admin = admin [ <NUM_LIT:0> ] <EOL> for sd in self . store . filter_scoredisplays ( ) : <EOL> lb = LegislativeBody . objects . get ( name = sd . get ( '<STR_LIT>' ) ) <EOL> sd_obj , created = ScoreDisplay . objects . get_or_create ( <EOL> name = sd . get ( '<STR_LIT:id>' ) [ : <NUM_LIT:50> ] , <EOL> title = sd . get ( '<STR_LIT:title>' ) [ : <NUM_LIT:50> ] , <EOL> legislative_body = lb , <EOL> is_page = sd . get ( '<STR_LIT:type>' ) == '<STR_LIT>' , <EOL> cssclass = ( sd . get ( '<STR_LIT>' ) or '<STR_LIT>' ) [ : <NUM_LIT:50> ] , <EOL> owner = admin <EOL> ) <EOL> for locale in [ l [ <NUM_LIT:0> ] for l in settings . LANGUAGES ] : <EOL> po = self . poutils [ locale ] <EOL> po . add_or_update ( <EOL> msgid = u'<STR_LIT>' % sd . get ( '<STR_LIT:id>' ) , <EOL> msgstr = sd . get ( '<STR_LIT:title>' ) or '<STR_LIT>' , <EOL> occurs = [ ( os . path . abspath ( self . store . datafile ) , sd . sourceline , ) ] <EOL> ) <EOL> po . add_or_update ( <EOL> msgid = u'<STR_LIT>' % sd . get ( '<STR_LIT:id>' ) , <EOL> msgstr = sd . get ( '<STR_LIT:title>' ) or '<STR_LIT>' , <EOL> occurs = [ ( os . path . abspath ( self . store . datafile ) , sd . sourceline , ) ] <EOL> ) <EOL> po . add_or_update ( <EOL> msgid = u'<STR_LIT>' % sd . get ( '<STR_LIT:id>' ) , <EOL> msgstr = '<STR_LIT>' , <EOL> occurs = [ ( os . path . abspath ( self . store . datafile ) , sd . sourceline , ) ] <EOL> ) <EOL> if created : <EOL> logger . debug ( '<STR_LIT>' , sd . get ( '<STR_LIT:title>' ) ) <EOL> else : <EOL> logger . debug ( '<STR_LIT>' , sd . get ( '<STR_LIT:title>' ) ) <EOL> for spref in self . store . filter_displayed_score_panels ( sd ) : <EOL> sp = self . store . get_score_panel ( spref . get ( '<STR_LIT>' ) ) <EOL> name = sp . get ( '<STR_LIT:id>' ) [ : <NUM_LIT:50> ] <EOL> position = int ( sp . get ( '<STR_LIT>' ) ) <EOL> template = sp . get ( '<STR_LIT>' ) [ : <NUM_LIT> ] <EOL> cssclass = ( sp . get ( '<STR_LIT>' ) or '<STR_LIT>' ) [ : <NUM_LIT:50> ] <EOL> pnltype = sp . get ( '<STR_LIT:type>' ) [ : <NUM_LIT:20> ] <EOL> is_ascending = sp . get ( '<STR_LIT>' ) <EOL> if is_ascending is None : <EOL> is_ascending = True <EOL> ascending = sp . get ( '<STR_LIT>' ) <EOL> sp_obj , created = ScorePanel . objects . get_or_create ( <EOL> type = pnltype , <EOL> position = position , <EOL> name = name , <EOL> template = template , <EOL> cssclass = cssclass , <EOL> is_ascending = ( ascending is None or ascending == '<STR_LIT:true>' ) , <EOL> ) <EOL> if created : <EOL> sd_obj . scorepanel_set . add ( sp_obj ) <EOL> logger . debug ( '<STR_LIT>' , name ) <EOL> else : <EOL> attached = sd_obj . scorepanel_set . filter ( id = sp_obj . id ) . count ( ) == <NUM_LIT:1> <EOL> if not attached : <EOL> sd_obj . scorepanel_set . add ( sp_obj ) <EOL> logger . debug ( '<STR_LIT>' , name ) <EOL> for locale in [ l [ <NUM_LIT:0> ] for l in settings . LANGUAGES ] : <EOL> po = self . poutils [ locale ] <EOL> po . add_or_update ( <EOL> msgid = u'<STR_LIT>' % sp_obj . name , <EOL> msgstr = sp . get ( '<STR_LIT:title>' ) or '<STR_LIT>' , <EOL> occurs = [ ( os . path . abspath ( self . store . datafile ) , sp . sourceline , ) ] <EOL> ) <EOL> po . add_or_update ( <EOL> msgid = u'<STR_LIT>' % sp_obj . name , <EOL> msgstr = sp . get ( '<STR_LIT:title>' ) or '<STR_LIT>' , <EOL> occurs = [ ( os . path . abspath ( self . store . datafile ) , sp . sourceline , ) ] <EOL> ) <EOL> po . add_or_update ( <EOL> msgid = u'<STR_LIT>' % sp_obj . name , <EOL> msgstr = '<STR_LIT>' , <EOL> occurs = [ ( os . path . abspath ( self . store . datafile ) , sp . sourceline , ) ] <EOL> ) <EOL> for sfref in self . store . filter_paneled_score_functions ( sp ) : <EOL> sf_node = self . store . get_score_function ( sfref . get ( '<STR_LIT>' ) ) <EOL> sf_obj = self . import_function ( sf_node , force ) <EOL> sp_obj . score_functions . add ( sf_obj ) <EOL> for sf_node in self . store . filter_score_functions ( ) : <EOL> self . import_function ( sf_node , force ) <EOL> if not self . store . has_validation ( ) : <EOL> logger . debug ( '<STR_LIT>' ) <EOL> return False ; <EOL> for vc in self . store . filter_criteria ( ) : <EOL> lb = LegislativeBody . objects . get ( name = vc . get ( '<STR_LIT>' ) ) <EOL> for crit in self . store . filter_criteria_criterion ( vc ) : <EOL> sfref = self . store . get_criterion_score ( crit ) <EOL> try : <EOL> sf = self . store . get_score_function ( sfref . get ( '<STR_LIT>' ) ) <EOL> except : <EOL> logger . info ( "<STR_LIT>" , crit . get ( '<STR_LIT:name>' ) ) <EOL> result = False <EOL> sf_obj = self . import_function ( sf , force ) <EOL> attributes = { <EOL> '<STR_LIT:name>' : crit . get ( '<STR_LIT:id>' ) [ <NUM_LIT:0> : <NUM_LIT:50> ] , <EOL> '<STR_LIT>' : sf_obj , <EOL> '<STR_LIT>' : lb <EOL> } <EOL> crit_obj , created , changed , message = check_and_update ( ValidationCriteria , overwrite = force , ** attributes ) <EOL> for locale in [ l [ <NUM_LIT:0> ] for l in settings . LANGUAGES ] : <EOL> po = self . poutils [ locale ] <EOL> po . add_or_update ( <EOL> msgid = u'<STR_LIT>' % attributes [ '<STR_LIT:name>' ] , <EOL> msgstr = crit . get ( '<STR_LIT:name>' ) or '<STR_LIT>' , <EOL> occurs = [ ( os . path . abspath ( self . store . datafile ) , crit . sourceline , ) ] <EOL> ) <EOL> po . add_or_update ( <EOL> msgid = u'<STR_LIT>' % attributes [ '<STR_LIT:name>' ] , <EOL> msgstr = crit . get ( '<STR_LIT:name>' ) or '<STR_LIT>' , <EOL> occurs = [ ( os . path . abspath ( self . store . datafile ) , crit . sourceline , ) ] <EOL> ) <EOL> po . add_or_update ( <EOL> msgid = u'<STR_LIT>' % attributes [ '<STR_LIT:name>' ] , <EOL> msgstr = crit . get ( '<STR_LIT:description>' ) or '<STR_LIT>' , <EOL> occurs = [ ( os . path . abspath ( self . store . datafile ) , crit . sourceline , ) ] <EOL> ) <EOL> if changed and not force : <EOL> logger . info ( message ) <EOL> else : <EOL> logger . debug ( message ) <EOL> return result <EOL> def import_function ( self , node , force ) : <EOL> """<STR_LIT>""" <EOL> attributes = { <EOL> '<STR_LIT>' : node . get ( '<STR_LIT>' ) [ : <NUM_LIT> ] , <EOL> '<STR_LIT:name>' : node . get ( '<STR_LIT:id>' ) [ : <NUM_LIT:50> ] , <EOL> '<STR_LIT>' : node . get ( '<STR_LIT:type>' ) == '<STR_LIT>' <EOL> } <EOL> fn_obj , created , changed , message = check_and_update ( ScoreFunction , overwrite = force , ** attributes ) <EOL> for locale in [ l [ <NUM_LIT:0> ] for l in settings . LANGUAGES ] : <EOL> po = self . poutils [ locale ] <EOL> po . add_or_update ( <EOL> msgid = u'<STR_LIT>' % attributes [ '<STR_LIT:name>' ] , <EOL> msgstr = node . get ( '<STR_LIT:label>' ) or '<STR_LIT>' , <EOL> occurs = [ ( os . path . abspath ( self . store . datafile ) , node . sourceline , ) ] <EOL> ) <EOL> po . add_or_update ( <EOL> msgid = u'<STR_LIT>' % attributes [ '<STR_LIT:name>' ] , <EOL> msgstr = node . get ( '<STR_LIT:label>' ) or '<STR_LIT>' , <EOL> occurs = [ ( os . path . abspath ( self . store . datafile ) , node . sourceline , ) ] <EOL> ) <EOL> po . add_or_update ( <EOL> msgid = u'<STR_LIT>' % attributes [ '<STR_LIT:name>' ] , <EOL> msgstr = node . get ( '<STR_LIT:description>' ) or '<STR_LIT>' , <EOL> occurs = [ ( os . path . abspath ( self . store . datafile ) , node . sourceline , ) ] <EOL> ) <EOL> lbodies = [ ] <EOL> for lb in self . store . filter_function_legislative_bodies ( node ) : <EOL> lbodies . append ( lb . get ( '<STR_LIT>' ) ) <EOL> lbodies = list ( LegislativeBody . objects . filter ( name__in = lbodies ) ) <EOL> fn_obj . selectable_bodies . add ( * lbodies ) <EOL> if changed and not force : <EOL> logger . info ( message ) <EOL> else : <EOL> logger . debug ( message ) <EOL> self . import_arguments ( fn_obj , node , force ) <EOL> return fn_obj <EOL> def import_arguments ( self , score_function , node , force ) : <EOL> """<STR_LIT>""" <EOL> for arg in self . store . filter_function_arguments ( node ) : <EOL> name = arg . get ( '<STR_LIT:name>' ) [ : <NUM_LIT:50> ] <EOL> arg_obj , created = ScoreArgument . objects . get_or_create ( <EOL> function = score_function , <EOL> type = '<STR_LIT>' , <EOL> argument = name , <EOL> ) <EOL> config_value = arg . get ( '<STR_LIT:value>' ) [ : <NUM_LIT:50> ] <EOL> if created : <EOL> arg_obj . value = config_value <EOL> arg_obj . save ( ) <EOL> logger . debug ( '<STR_LIT>' , name ) <EOL> else : <EOL> if arg_obj . value == config_value : <EOL> logger . debug ( '<STR_LIT>' , name ) <EOL> elif force : <EOL> arg_obj . value = config_value <EOL> arg_obj . save ( ) <EOL> logger . info ( '<STR_LIT>' , name ) <EOL> else : <EOL> logger . info ( '<STR_LIT>' , name ) <EOL> for subarg in self . store . filter_function_subject_arguments ( node ) : <EOL> name = subarg . get ( '<STR_LIT:name>' ) [ : <NUM_LIT:50> ] <EOL> config_value = subarg . get ( '<STR_LIT>' ) [ : <NUM_LIT:50> ] <EOL> subarg_obj , created = ScoreArgument . objects . get_or_create ( <EOL> function = score_function , <EOL> type = '<STR_LIT>' , <EOL> argument = name , <EOL> ) <EOL> if created : <EOL> subarg_obj . value = config_value <EOL> subarg_obj . save ( ) <EOL> logger . debug ( '<STR_LIT>' , name ) <EOL> else : <EOL> if subarg_obj . value == config_value : <EOL> logger . debug ( '<STR_LIT>' , name ) <EOL> elif force : <EOL> subarg_obj . value = config_value <EOL> subarg_obj . save ( ) <EOL> logger . info ( '<STR_LIT>' , name ) <EOL> else : <EOL> logger . info ( '<STR_LIT>' , name ) <EOL> for scorearg in self . store . filter_function_score_arguments ( node ) : <EOL> argfn = self . store . get_score_function ( scorearg . get ( '<STR_LIT>' ) ) <EOL> if argfn is None : <EOL> logger . info ( "<STR_LIT>" , scorearg . get ( '<STR_LIT>' ) , score_function . name ) <EOL> continue <EOL> self . import_function ( argfn , force ) <EOL> config_value = scorearg . get ( '<STR_LIT>' ) [ : <NUM_LIT:50> ] <EOL> name = scorearg . get ( '<STR_LIT:name>' ) [ : <NUM_LIT:50> ] <EOL> scorearg_obj , created = ScoreArgument . objects . get_or_create ( <EOL> function = score_function , <EOL> type = '<STR_LIT>' , <EOL> argument = name <EOL> ) <EOL> if created : <EOL> scorearg_obj . value = config_value <EOL> scorearg_obj . save ( ) <EOL> logger . debug ( '<STR_LIT>' , name ) <EOL> else : <EOL> if scorearg_obj . value == config_value : <EOL> logger . debug ( '<STR_LIT>' , name ) <EOL> elif force : <EOL> scorearg_obj . value = config_value <EOL> scorearg_obj . save ( ) <EOL> logger . info ( '<STR_LIT>' , name ) <EOL> else : <EOL> logger . info ( '<STR_LIT>' , name ) <EOL> return True <EOL> def import_contiguity_overrides ( self ) : <EOL> """<STR_LIT>""" <EOL> ContiguityOverride . objects . all ( ) . delete ( ) <EOL> if not self . store . has_contiguity_overrides ( ) : <EOL> logger . debug ( '<STR_LIT>' ) <EOL> for co in self . store . filter_contiguity_overrides ( ) : <EOL> portable_id = co . get ( '<STR_LIT:id>' ) <EOL> temp = Geounit . objects . filter ( portable_id = portable_id ) <EOL> if ( len ( temp ) == <NUM_LIT:0> ) : <EOL> raise Exception ( '<STR_LIT>' % portable_id ) <EOL> override_geounit = temp [ <NUM_LIT:0> ] <EOL> portable_id = co . get ( '<STR_LIT>' ) <EOL> temp = Geounit . objects . filter ( portable_id = portable_id ) <EOL> if ( len ( temp ) == <NUM_LIT:0> ) : <EOL> raise Exception ( '<STR_LIT>' % portable_id ) <EOL> connect_to_geounit = temp [ <NUM_LIT:0> ] <EOL> co_obj , created = ContiguityOverride . objects . get_or_create ( <EOL> override_geounit = override_geounit , <EOL> connect_to_geounit = connect_to_geounit <EOL> ) <EOL> if created : <EOL> logger . debug ( '<STR_LIT>' , str ( co_obj ) ) <EOL> else : <EOL> logger . debug ( '<STR_LIT>' , str ( co_obj ) ) <EOL> return True <EOL> class SpatialUtils : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , store = None , config = None ) : <EOL> """<STR_LIT>""" <EOL> if not store is None : <EOL> self . store = store <EOL> mapconfig = self . store . get_mapserver ( ) <EOL> self . host = mapconfig . get ( '<STR_LIT>' ) <EOL> self . port = <NUM_LIT> <EOL> self . ns = mapconfig . get ( '<STR_LIT>' ) <EOL> self . nshref = mapconfig . get ( '<STR_LIT>' ) <EOL> user_pass = '<STR_LIT>' % ( mapconfig . get ( '<STR_LIT>' ) , mapconfig . get ( '<STR_LIT>' ) ) <EOL> self . styledir = mapconfig . get ( '<STR_LIT>' ) <EOL> elif isinstance ( config , dict ) : <EOL> try : <EOL> self . host = config [ '<STR_LIT:host>' ] <EOL> self . port = <NUM_LIT> <EOL> self . ns = config [ '<STR_LIT>' ] <EOL> self . nshref = config [ '<STR_LIT>' ] <EOL> user_pass = '<STR_LIT>' % ( config [ '<STR_LIT>' ] , config [ '<STR_LIT>' ] ) <EOL> self . styledir = config [ '<STR_LIT>' ] <EOL> except : <EOL> logger . error ( '<STR_LIT>' ) <EOL> raise <EOL> else : <EOL> logger . error ( '<STR_LIT>' ) <EOL> raise Exception ( ) <EOL> if self . host == '<STR_LIT>' : <EOL> self . host = '<STR_LIT:localhost>' <EOL> auth = '<STR_LIT>' % base64 . b64encode ( user_pass ) <EOL> self . headers = { <EOL> '<STR_LIT:default>' : { <EOL> '<STR_LIT>' : auth , <EOL> '<STR_LIT:Content-Type>' : '<STR_LIT:application/json>' , <EOL> '<STR_LIT>' : '<STR_LIT:application/json>' <EOL> } , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : auth , <EOL> '<STR_LIT:Content-Type>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' <EOL> } <EOL> } <EOL> def purge_geoserver ( self ) : <EOL> """<STR_LIT>""" <EOL> ws_cfg = self . _read_config ( '<STR_LIT>' % self . ns , '<STR_LIT>' % self . ns ) <EOL> if ws_cfg is None : <EOL> logger . debug ( "<STR_LIT>" , self . ns ) <EOL> return True <EOL> wsds_cfg = self . _read_config ( ws_cfg [ '<STR_LIT>' ] [ '<STR_LIT>' ] , '<STR_LIT>' % ws_cfg [ '<STR_LIT>' ] [ '<STR_LIT:name>' ] ) <EOL> if wsds_cfg is None : <EOL> logger . debug ( "<STR_LIT>" , self . ns ) <EOL> return False <EOL> ds_cfg = self . _read_config ( wsds_cfg [ '<STR_LIT>' ] [ '<STR_LIT>' ] [ <NUM_LIT:0> ] [ '<STR_LIT>' ] , "<STR_LIT>" % wsds_cfg [ '<STR_LIT>' ] [ '<STR_LIT>' ] [ <NUM_LIT:0> ] [ '<STR_LIT:name>' ] ) <EOL> if ds_cfg is None : <EOL> logger . debug ( "<STR_LIT>" ) <EOL> return False <EOL> fts_cfg = self . _read_config ( ds_cfg [ '<STR_LIT>' ] [ '<STR_LIT>' ] + '<STR_LIT>' , "<STR_LIT>" % wsds_cfg [ '<STR_LIT>' ] [ '<STR_LIT>' ] [ <NUM_LIT:0> ] [ '<STR_LIT:name>' ] ) <EOL> if fts_cfg is None : <EOL> logger . debug ( "<STR_LIT>" , wsds_cfg [ '<STR_LIT>' ] [ '<STR_LIT>' ] [ <NUM_LIT:0> ] [ '<STR_LIT:name>' ] ) <EOL> return False <EOL> if not '<STR_LIT>' in fts_cfg [ '<STR_LIT>' ] : <EOL> fts_cfg [ '<STR_LIT>' ] = { '<STR_LIT>' : [ ] } <EOL> for ft_cfg in fts_cfg [ '<STR_LIT>' ] [ '<STR_LIT>' ] : <EOL> if not self . _rest_config ( '<STR_LIT>' , '<STR_LIT>' % ft_cfg [ '<STR_LIT:name>' ] ) : <EOL> logger . debug ( "<STR_LIT>" , ft_cfg [ '<STR_LIT:name>' ] ) <EOL> continue <EOL> if self . _rest_config ( '<STR_LIT>' , ft_cfg [ '<STR_LIT>' ] ) : <EOL> logger . debug ( "<STR_LIT>" , ft_cfg [ '<STR_LIT:name>' ] ) <EOL> else : <EOL> logger . debug ( "<STR_LIT>" , ft_cfg [ '<STR_LIT:name>' ] ) <EOL> if not self . _rest_config ( '<STR_LIT>' , wsds_cfg [ '<STR_LIT>' ] [ '<STR_LIT>' ] [ <NUM_LIT:0> ] [ '<STR_LIT>' ] ) : <EOL> logger . debug ( "<STR_LIT>" , wsds_cfg [ '<STR_LIT>' ] [ '<STR_LIT>' ] [ <NUM_LIT:0> ] [ '<STR_LIT:name>' ] ) <EOL> return False <EOL> if not self . _rest_config ( '<STR_LIT>' , '<STR_LIT>' % self . ns ) : <EOL> logger . debug ( "<STR_LIT>" , self . ns ) <EOL> return False <EOL> sts_cfg = self . _read_config ( '<STR_LIT>' , "<STR_LIT>" ) <EOL> if not sts_cfg is None : <EOL> includes = [ '<STR_LIT>' % self . ns ] <EOL> for st_cfg in sts_cfg [ '<STR_LIT>' ] [ '<STR_LIT>' ] : <EOL> skip = False <EOL> for inc in includes : <EOL> skip = skip or re . compile ( inc ) . match ( st_cfg [ '<STR_LIT:name>' ] ) is None <EOL> if skip : <EOL> continue <EOL> if not self . _rest_config ( '<STR_LIT>' , st_cfg [ '<STR_LIT>' ] ) : <EOL> logger . debug ( "<STR_LIT>" , st_cfg [ '<STR_LIT:name>' ] ) <EOL> else : <EOL> logger . debug ( "<STR_LIT>" , st_cfg [ '<STR_LIT:name>' ] ) <EOL> return True <EOL> def configure_geoserver ( self ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> srid = Geounit . objects . all ( ) [ <NUM_LIT:0> ] . geom . srid <EOL> except : <EOL> srid = <NUM_LIT:0> <EOL> logger . debug ( '<STR_LIT>' , srid ) <EOL> namespace_url = '<STR_LIT>' <EOL> namespace_obj = { '<STR_LIT>' : { '<STR_LIT>' : self . ns , '<STR_LIT>' : self . nshref } } <EOL> if self . _check_spatial_resource ( namespace_url , self . ns , namespace_obj ) : <EOL> logger . debug ( '<STR_LIT>' % self . ns ) <EOL> else : <EOL> logger . warn ( '<STR_LIT>' ) <EOL> return <EOL> if self . store is None : <EOL> logger . warning ( '<STR_LIT>' ) <EOL> return <EOL> dbconfig = self . store . get_database ( ) <EOL> data_store_url = '<STR_LIT>' % self . ns <EOL> data_store_name = '<STR_LIT>' <EOL> data_store_obj = { <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT:name>' : data_store_name , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT:host>' : dbconfig . get ( '<STR_LIT:host>' , self . host ) , <EOL> '<STR_LIT:port>' : <NUM_LIT> , <EOL> '<STR_LIT>' : dbconfig . get ( '<STR_LIT:name>' ) , <EOL> '<STR_LIT:user>' : dbconfig . get ( '<STR_LIT:user>' ) , <EOL> '<STR_LIT>' : dbconfig . get ( '<STR_LIT:password>' ) , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : self . ns , <EOL> '<STR_LIT>' : dbconfig . get ( '<STR_LIT:user>' ) <EOL> } <EOL> } <EOL> } <EOL> if self . _check_spatial_resource ( data_store_url , data_store_name , data_store_obj ) : <EOL> logger . debug ( '<STR_LIT>' % data_store_name ) <EOL> else : <EOL> logger . warn ( '<STR_LIT>' ) <EOL> return <EOL> if self . create_featuretype ( '<STR_LIT>' ) : <EOL> logger . debug ( '<STR_LIT>' ) <EOL> else : <EOL> logger . warn ( '<STR_LIT>' ) <EOL> for geolevel in Geolevel . objects . all ( ) : <EOL> if geolevel . legislativelevel_set . all ( ) . count ( ) == <NUM_LIT:0> : <EOL> continue <EOL> if self . create_featuretype ( '<STR_LIT>' % geolevel . name ) : <EOL> logger . debug ( '<STR_LIT>' % geolevel . name ) <EOL> else : <EOL> logger . warn ( '<STR_LIT>' % geolevel . name ) <EOL> if self . create_featuretype ( '<STR_LIT>' % geolevel . name ) : <EOL> logger . debug ( '<STR_LIT>' % geolevel . name ) <EOL> else : <EOL> logger . warn ( '<STR_LIT>' % geolevel . name ) <EOL> all_subjects = Subject . objects . all ( ) . order_by ( '<STR_LIT>' ) <EOL> if all_subjects . count ( ) > <NUM_LIT:0> : <EOL> subject = all_subjects [ <NUM_LIT:0> ] <EOL> featuretype_name = get_featuretype_name ( geolevel . name ) <EOL> if self . create_featuretype ( featuretype_name , alias = get_featuretype_name ( geolevel . name , subject . name ) ) : <EOL> logger . debug ( '<STR_LIT>' % featuretype_name ) <EOL> else : <EOL> logger . warn ( '<STR_LIT>' % featuretype_name ) <EOL> if self . create_style ( featuretype_name ) : <EOL> logger . debug ( '<STR_LIT>' % featuretype_name ) <EOL> else : <EOL> logger . warn ( '<STR_LIT>' % featuretype_name ) <EOL> try : <EOL> sld_content = SpatialUtils . generate_style ( geolevel , geolevel . geounit_set . all ( ) , <NUM_LIT:1> , layername = '<STR_LIT:none>' ) <EOL> self . write_style ( geolevel . name + '<STR_LIT>' , sld_content ) <EOL> except Exception , ex : <EOL> logger . error ( traceback . format_exc ( ) ) <EOL> if self . set_style ( featuretype_name , sld_content ) : <EOL> logger . debug ( '<STR_LIT>' % featuretype_name ) <EOL> else : <EOL> logger . warn ( '<STR_LIT>' % featuretype_name ) <EOL> if self . assign_style ( featuretype_name , featuretype_name ) : <EOL> logger . debug ( '<STR_LIT>' % featuretype_name ) <EOL> else : <EOL> logger . warn ( '<STR_LIT>' % featuretype_name ) <EOL> featuretype_name = '<STR_LIT>' % geolevel . name <EOL> if self . create_featuretype ( featuretype_name , alias = get_featuretype_name ( geolevel . name , subject . name ) ) : <EOL> logger . debug ( '<STR_LIT>' % featuretype_name ) <EOL> else : <EOL> logger . warn ( '<STR_LIT>' % featuretype_name ) <EOL> if self . create_style ( featuretype_name ) : <EOL> logger . debug ( '<STR_LIT>' % featuretype_name ) <EOL> else : <EOL> logger . warn ( '<STR_LIT>' % featuretype_name ) <EOL> try : <EOL> sld_content = SpatialUtils . generate_style ( geolevel , geolevel . geounit_set . all ( ) , <NUM_LIT:1> , layername = '<STR_LIT>' ) <EOL> self . write_style ( geolevel . name + '<STR_LIT>' , sld_content ) <EOL> except Exception , ex : <EOL> logger . error ( traceback . format_exc ( ) ) <EOL> if self . set_style ( featuretype_name , sld_content ) : <EOL> logger . debug ( '<STR_LIT>' % featuretype_name ) <EOL> else : <EOL> logger . warn ( '<STR_LIT>' % featuretype_name ) <EOL> if self . assign_style ( featuretype_name , featuretype_name ) : <EOL> logger . debug ( '<STR_LIT>' % featuretype_name ) <EOL> else : <EOL> logger . warn ( '<STR_LIT>' % featuretype_name ) <EOL> for subject in all_subjects : <EOL> featuretype_name = get_featuretype_name ( geolevel . name , subject . name ) <EOL> if self . create_featuretype ( featuretype_name ) : <EOL> logger . debug ( '<STR_LIT>' % featuretype_name ) <EOL> else : <EOL> logger . warn ( '<STR_LIT>' % featuretype_name ) <EOL> if self . create_style ( featuretype_name ) : <EOL> logger . debug ( '<STR_LIT>' % featuretype_name ) <EOL> else : <EOL> logger . warn ( '<STR_LIT>' % featuretype_name ) <EOL> try : <EOL> sld_content = SpatialUtils . generate_style ( geolevel , geolevel . geounit_set . all ( ) , <NUM_LIT:5> , subject = subject ) <EOL> self . write_style ( geolevel . name + '<STR_LIT:_>' + subject . name , sld_content ) <EOL> except Exception , ex : <EOL> logger . error ( traceback . format_exc ( ) ) <EOL> if self . set_style ( featuretype_name , sld_content ) : <EOL> logger . debug ( '<STR_LIT>' % featuretype_name ) <EOL> else : <EOL> logger . warn ( '<STR_LIT>' % featuretype_name ) <EOL> if self . assign_style ( featuretype_name , featuretype_name ) : <EOL> logger . debug ( '<STR_LIT>' % featuretype_name ) <EOL> else : <EOL> logger . warn ( '<STR_LIT>' % featuretype_name ) <EOL> ngeolevels_map = [ ] <EOL> for lbody in LegislativeBody . objects . all ( ) : <EOL> geolevels = lbody . get_geolevels ( ) <EOL> ngeolevels_map . append ( ( len ( geolevels ) , lbody , geolevels [ <NUM_LIT:0> ] , ) ) <EOL> ngeolevels_map . sort ( lambda x , y : cmp ( y [ <NUM_LIT:0> ] , x [ <NUM_LIT:0> ] ) ) <EOL> lbody = ngeolevels_map [ <NUM_LIT:0> ] [ <NUM_LIT:1> ] <EOL> geolevel = ngeolevels_map [ <NUM_LIT:0> ] [ <NUM_LIT:2> ] <EOL> if self . create_featuretype ( '<STR_LIT>' , alias = '<STR_LIT>' % geolevel . name ) : <EOL> logger . debug ( '<STR_LIT>' ) <EOL> else : <EOL> logger . warn ( '<STR_LIT>' ) <EOL> if self . assign_style ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> logger . debug ( '<STR_LIT>' ) <EOL> else : <EOL> logger . warn ( '<STR_LIT>' ) <EOL> try : <EOL> intervals = self . store . filter_nodes ( '<STR_LIT>' ) <EOL> for interval in intervals : <EOL> subject_name = interval . xpath ( '<STR_LIT>' ) [ <NUM_LIT:0> ] . get ( '<STR_LIT>' ) <EOL> lbody_name = interval . xpath ( '<STR_LIT>' ) [ <NUM_LIT:0> ] . get ( '<STR_LIT>' ) <EOL> interval_avg = float ( interval . xpath ( '<STR_LIT>' ) [ <NUM_LIT:0> ] . get ( '<STR_LIT:value>' ) ) <EOL> interval_bnd1 = float ( interval . xpath ( '<STR_LIT>' ) [ <NUM_LIT:0> ] . get ( '<STR_LIT:value>' ) ) <EOL> interval_bnd2 = float ( interval . xpath ( '<STR_LIT>' ) [ <NUM_LIT:0> ] . get ( '<STR_LIT:value>' ) ) <EOL> intervals = [ <EOL> ( interval_avg + interval_avg * interval_bnd2 , None , <EOL> _ ( '<STR_LIT>' ) , { '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' } ) , <EOL> ( interval_avg + interval_avg * interval_bnd1 , interval_avg + interval_avg * interval_bnd2 , <EOL> _ ( '<STR_LIT>' ) , { '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' } ) , <EOL> ( interval_avg - interval_avg * interval_bnd1 , interval_avg + interval_avg * interval_bnd1 , <EOL> _ ( '<STR_LIT>' ) , { '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' } ) , <EOL> ( interval_avg - interval_avg * interval_bnd2 , interval_avg - interval_avg * interval_bnd1 , <EOL> _ ( '<STR_LIT>' ) , { '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' } ) , <EOL> ( None , interval_avg - interval_avg * interval_bnd2 , <EOL> _ ( '<STR_LIT>' ) , { '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' } ) ] <EOL> doc = sld . StyledLayerDescriptor ( ) <EOL> fts = doc . create_namedlayer ( subject_name ) . create_userstyle ( ) . create_featuretypestyle ( ) <EOL> for interval in intervals : <EOL> imin , imax , ititle , ifill = interval <EOL> rule = fts . create_rule ( ititle , sld . PolygonSymbolizer ) <EOL> if imin is None : <EOL> rule . create_filter ( '<STR_LIT>' , '<STR_LIT:<>' , str ( int ( round ( imax ) ) ) ) <EOL> elif imax is None : <EOL> rule . create_filter ( '<STR_LIT>' , '<STR_LIT>' , str ( int ( round ( imin ) ) ) ) <EOL> else : <EOL> f1 = sld . Filter ( rule ) <EOL> f1 . PropertyIsGreaterThanOrEqualTo = sld . PropertyCriterion ( f1 , '<STR_LIT>' ) <EOL> f1 . PropertyIsGreaterThanOrEqualTo . PropertyName = '<STR_LIT>' <EOL> f1 . PropertyIsGreaterThanOrEqualTo . Literal = str ( int ( round ( imin ) ) ) <EOL> f2 = sld . Filter ( rule ) <EOL> f2 . PropertyIsLessThan = sld . PropertyCriterion ( f2 , '<STR_LIT>' ) <EOL> f2 . PropertyIsLessThan . PropertyName = '<STR_LIT>' <EOL> f2 . PropertyIsLessThan . Literal = str ( int ( round ( imax ) ) ) <EOL> rule . Filter = f1 + f2 <EOL> ps = rule . PolygonSymbolizer <EOL> ps . Fill . CssParameters [ <NUM_LIT:0> ] . Value = ifill [ '<STR_LIT>' ] <EOL> ps . Fill . create_cssparameter ( '<STR_LIT>' , ifill [ '<STR_LIT>' ] ) <EOL> ps . Stroke . CssParameters [ <NUM_LIT:0> ] . Value = '<STR_LIT>' <EOL> ps . Stroke . CssParameters [ <NUM_LIT:1> ] . Value = '<STR_LIT:2>' <EOL> ps . Stroke . create_cssparameter ( '<STR_LIT>' , '<STR_LIT:1>' ) <EOL> self . write_style ( lbody_name + '<STR_LIT:_>' + subject_name , doc . as_sld ( pretty_print = True ) ) <EOL> except Exception , ex : <EOL> logger . debug ( traceback . format_exc ( ) ) <EOL> logger . warn ( '<STR_LIT>' ) <EOL> logger . info ( "<STR_LIT>" ) <EOL> return True <EOL> def create_featuretype ( self , feature_type_name , data_store_name = '<STR_LIT>' , alias = None ) : <EOL> """<STR_LIT>""" <EOL> feature_type_url = '<STR_LIT>' % ( self . ns , data_store_name ) <EOL> feature_type_obj = SpatialUtils . feature_template ( feature_type_name , alias = alias ) <EOL> return self . _check_spatial_resource ( feature_type_url , feature_type_name , feature_type_obj ) <EOL> def _check_spatial_resource ( self , url , name , dictionary , update = False ) : <EOL> """<STR_LIT>""" <EOL> if self . _rest_check ( '<STR_LIT>' % ( url , name ) ) : <EOL> if update : <EOL> if not self . _rest_config ( '<STR_LIT>' , url , data = json . dumps ( dictionary ) ) : <EOL> return False <EOL> else : <EOL> if not self . _rest_config ( '<STR_LIT:POST>' , url , data = json . dumps ( dictionary ) ) : <EOL> return False <EOL> return True <EOL> @ staticmethod <EOL> def feature_template ( name , title = None , alias = None ) : <EOL> """<STR_LIT>""" <EOL> nativeName = name <EOL> if not alias is None : <EOL> nativeName = alias <EOL> return { <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT:name>' : name , <EOL> '<STR_LIT:title>' : name if title is None else title , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : '<STR_LIT>' % - <NUM_LIT> , <EOL> '<STR_LIT>' : '<STR_LIT>' % - <NUM_LIT> , <EOL> '<STR_LIT>' : '<STR_LIT>' % <NUM_LIT> , <EOL> '<STR_LIT>' : '<STR_LIT>' % <NUM_LIT> <EOL> } , <EOL> '<STR_LIT>' : settings . FEATURE_LIMIT + <NUM_LIT:1> , <EOL> '<STR_LIT>' : nativeName <EOL> } <EOL> } <EOL> def _rest_check ( self , url ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> conn = httplib . HTTPConnection ( self . host , self . port ) <EOL> conn . request ( '<STR_LIT:GET>' , url , None , self . headers [ '<STR_LIT:default>' ] ) <EOL> rsp = conn . getresponse ( ) <EOL> rsp . read ( ) <EOL> conn . close ( ) <EOL> return rsp . status == <NUM_LIT:200> <EOL> except : <EOL> return False <EOL> def _rest_config ( self , method , url , data = None , headers = None ) : <EOL> """<STR_LIT>""" <EOL> if headers is None : <EOL> headers = self . headers [ '<STR_LIT:default>' ] <EOL> try : <EOL> conn = httplib . HTTPConnection ( self . host , self . port ) <EOL> conn . request ( method , url , data , headers ) <EOL> rsp = conn . getresponse ( ) <EOL> rsp . read ( ) <EOL> conn . close ( ) <EOL> if rsp . status != <NUM_LIT> and rsp . status != <NUM_LIT:200> : <EOL> logger . debug ( '<STR_LIT>' % ( rsp . status , method , url , ) ) <EOL> logger . debug ( data ) <EOL> return False <EOL> except Exception , ex : <EOL> logger . debug ( ex ) <EOL> return False <EOL> return True <EOL> def _read_config ( self , url , msg , headers = None ) : <EOL> """<STR_LIT>""" <EOL> if headers is None : <EOL> headers = self . headers [ '<STR_LIT:default>' ] <EOL> try : <EOL> conn = httplib . HTTPConnection ( self . host , self . port ) <EOL> conn . request ( '<STR_LIT:GET>' , url , None , headers ) <EOL> rsp = conn . getresponse ( ) <EOL> response = rsp . read ( ) <EOL> conn . close ( ) <EOL> if rsp . status != <NUM_LIT> and rsp . status != <NUM_LIT:200> : <EOL> return None <EOL> return json . loads ( response ) <EOL> except Exception , ex : <EOL> return None <EOL> def create_style ( self , featuretype ) : <EOL> """<STR_LIT>""" <EOL> nsfeaturetype = '<STR_LIT>' % ( self . ns , featuretype , ) <EOL> style_obj = { '<STR_LIT>' : { <EOL> '<STR_LIT:name>' : nsfeaturetype , <EOL> '<STR_LIT:filename>' : '<STR_LIT>' % nsfeaturetype <EOL> } } <EOL> style_url = '<STR_LIT>' <EOL> return self . _check_spatial_resource ( style_url , nsfeaturetype , style_obj ) <EOL> @ staticmethod <EOL> def generate_style ( geolevel , qset , nclasses , subject = None , layername = None ) : <EOL> """<STR_LIT>""" <EOL> if subject : <EOL> qset = qset . filter ( characteristic__subject = subject ) <EOL> us_title = subject . get_short_label ( ) <EOL> else : <EOL> us_title = layername <EOL> qset = qset . annotate ( Avg ( '<STR_LIT>' ) ) <EOL> doc = generator . as_quantiles ( qset , '<STR_LIT>' , nclasses , propertyname = '<STR_LIT>' , <EOL> userstyletitle = us_title , colorbrewername = '<STR_LIT>' , invertgradient = False ) <EOL> strokes = doc . _node . xpath ( '<STR_LIT>' , namespaces = doc . _nsmap ) <EOL> for stroke in strokes : <EOL> node = stroke . xpath ( '<STR_LIT>' , namespaces = doc . _nsmap ) [ <NUM_LIT:0> ] <EOL> node . text = '<STR_LIT>' <EOL> if subject : <EOL> node = doc . _node . xpath ( '<STR_LIT>' , namespaces = doc . _nsmap ) [ <NUM_LIT:0> ] <EOL> node . text = subject . name <EOL> elif nclasses == <NUM_LIT:1> : <EOL> fill = doc . _node . xpath ( '<STR_LIT>' , namespaces = doc . _nsmap ) [ <NUM_LIT:0> ] <EOL> fill . getparent ( ) . remove ( fill ) <EOL> if layername : <EOL> name = doc . _node . xpath ( '<STR_LIT>' , namespaces = doc . _nsmap ) [ <NUM_LIT:0> ] <EOL> name . text = layername <EOL> name = doc . _node . xpath ( '<STR_LIT>' , namespaces = doc . _nsmap ) [ <NUM_LIT:0> ] <EOL> name . text = geolevel . get_long_description ( ) <EOL> name = doc . _node . xpath ( '<STR_LIT>' , namespaces = doc . _nsmap ) [ <NUM_LIT:0> ] <EOL> name . text = _ ( '<STR_LIT>' ) <EOL> if layername == '<STR_LIT>' : <EOL> stroke = doc . _node . xpath ( '<STR_LIT>' , namespaces = doc . _nsmap ) [ <NUM_LIT:0> ] <EOL> node = stroke . xpath ( '<STR_LIT>' , namespaces = doc . _nsmap ) [ <NUM_LIT:0> ] <EOL> node . text = '<STR_LIT:3>' <EOL> node = stroke . xpath ( '<STR_LIT>' , namespaces = doc . _nsmap ) [ <NUM_LIT:0> ] <EOL> node . text = '<STR_LIT>' <EOL> strokeopacity = { '<STR_LIT:name>' : '<STR_LIT>' } <EOL> node = stroke . makeelement ( '<STR_LIT>' % doc . _nsmap [ '<STR_LIT>' ] , attrib = strokeopacity , nsmap = doc . _nsmap ) <EOL> node . text = '<STR_LIT>' <EOL> stroke . append ( node ) <EOL> return doc . as_sld ( ) <EOL> def set_style ( self , featuretype , sld_content ) : <EOL> """<STR_LIT>""" <EOL> return self . _rest_config ( '<STR_LIT>' , '<STR_LIT>' % ( self . ns , featuretype , ) , <EOL> data = sld_content , headers = self . headers [ '<STR_LIT>' ] ) <EOL> def assign_style ( self , featuretype , style_name ) : <EOL> """<STR_LIT>""" <EOL> if not style_name == '<STR_LIT>' : <EOL> style_name = '<STR_LIT>' % ( self . ns , style_name ) <EOL> layer = { '<STR_LIT>' : { <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT:name>' : style_name <EOL> } , <EOL> '<STR_LIT>' : True <EOL> } } <EOL> if self . _rest_config ( '<STR_LIT>' , '<STR_LIT>' % ( self . ns , featuretype , ) , data = json . dumps ( layer ) ) : <EOL> return True <EOL> return False <EOL> def write_style ( self , name , body ) : <EOL> """<STR_LIT>""" <EOL> dest = '<STR_LIT>' % ( settings . SLD_ROOT , self . ns , name , ) <EOL> try : <EOL> sld = open ( dest , '<STR_LIT:w>' ) <EOL> sld . write ( body ) <EOL> sld . close ( ) <EOL> logger . debug ( '<STR_LIT>' % dest ) <EOL> return True <EOL> except Exception , ex : <EOL> logger . warn ( '<STR_LIT>' % dest ) <EOL> return False <EOL> def renest_geolevel ( self , glconf , subject = None ) : <EOL> """<STR_LIT>""" <EOL> parent = None <EOL> for region in self . store . filter_regions ( ) : <EOL> llevel = self . store . get_regional_geolevel ( region , glconf . get ( '<STR_LIT:id>' ) ) <EOL> if llevel is None : <EOL> continue <EOL> parent = llevel . getparent ( ) <EOL> while parent . getparent ( ) is not None and parent . getparent ( ) . tag != '<STR_LIT>' : <EOL> parent = parent . getparent ( ) <EOL> break <EOL> parent_geolevel = self . store . get_geolevel ( parent . get ( '<STR_LIT>' ) ) <EOL> if not parent_geolevel is None : <EOL> parent = Geolevel . objects . get ( name = parent_geolevel . get ( '<STR_LIT:name>' ) . lower ( ) [ : <NUM_LIT:50> ] ) <EOL> else : <EOL> return False <EOL> geolevel = Geolevel . objects . get ( name = glconf . get ( '<STR_LIT:name>' ) . lower ( ) [ : <NUM_LIT:50> ] ) <EOL> return geolevel . renest ( parent , subject = None , spatial = True ) <EOL> class PoUtils : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , locale ) : <EOL> """<STR_LIT>""" <EOL> self . popath = '<STR_LIT>' % { '<STR_LIT>' : locale } <EOL> self . mopath = '<STR_LIT>' % { '<STR_LIT>' : locale } <EOL> if os . path . exists ( self . popath ) : <EOL> logger . debug ( '<STR_LIT>' ) <EOL> self . pofile = polib . pofile ( self . popath , check_for_duplicates = True ) <EOL> else : <EOL> logger . debug ( '<STR_LIT>' ) <EOL> now = datetime . utcnow ( ) <EOL> now = now . replace ( tzinfo = UTC ( ) ) <EOL> self . pofile = polib . POFile ( check_for_duplicates = True ) <EOL> self . pofile . metadata = { <EOL> '<STR_LIT>' : '<STR_LIT:1.0>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : now . strftime ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' : now . strftime ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' : '<STR_LIT>' % ( settings . ADMINS [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] , settings . ADMINS [ <NUM_LIT:0> ] [ <NUM_LIT:1> ] ) , <EOL> '<STR_LIT>' : '<STR_LIT>' % ( settings . ADMINS [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] , settings . ADMINS [ <NUM_LIT:0> ] [ <NUM_LIT:1> ] ) , <EOL> '<STR_LIT>' : locale , <EOL> '<STR_LIT>' : '<STR_LIT:1.0>' , <EOL> '<STR_LIT:Content-Type>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' <EOL> } <EOL> def add_or_update ( self , msgid = '<STR_LIT>' , msgstr = '<STR_LIT>' , occurs = [ ] ) : <EOL> """<STR_LIT>""" <EOL> entry = self . pofile . find ( msgid ) <EOL> if entry is None : <EOL> entry = polib . POEntry ( msgid = msgid , msgstr = msgstr , occurrences = occurs ) <EOL> self . pofile . append ( entry ) <EOL> else : <EOL> entry . msgstr = msgstr <EOL> entry . occurences = occurs <EOL> def save ( self ) : <EOL> """<STR_LIT>""" <EOL> logger . debug ( '<STR_LIT>' , { '<STR_LIT>' : self . popath } ) <EOL> self . pofile . save ( self . popath ) <EOL> logger . debug ( '<STR_LIT>' , { '<STR_LIT>' : self . mopath } ) <EOL> self . pofile . save_as_mofile ( self . mopath ) <EOL> class UTC ( tzinfo ) : <EOL> """<STR_LIT>""" <EOL> def utcoffset ( self , dt ) : <EOL> return timedelta ( <NUM_LIT:0> ) <EOL> def tzname ( self , dt ) : <EOL> return '<STR_LIT>' <EOL> def dst ( self , dt ) : <EOL> return timedelta ( <NUM_LIT:0> ) </s>
