<s> """<STR_LIT>""" <EOL> from django . http import * <EOL> from django . core import serializers <EOL> from django . core . exceptions import ValidationError , SuspiciousOperation , ObjectDoesNotExist <EOL> from django . db import IntegrityError , connection , transaction <EOL> from django . shortcuts import render_to_response <EOL> from django . core . urlresolvers import reverse <EOL> from django . core . context_processors import csrf <EOL> from django . contrib . comments . models import Comment <EOL> from django . contrib . comments . forms import CommentForm <EOL> from django . contrib . contenttypes . models import ContentType <EOL> from django . contrib . auth . decorators import login_required , user_passes_test <EOL> from django . contrib . sessions . models import Session <EOL> from django . contrib . sessions . backends . db import SessionStore <EOL> from django . contrib . gis . geos . collections import MultiPolygon <EOL> from django . contrib . gis . geos import GEOSGeometry <EOL> from django . contrib . gis . gdal import * <EOL> from django . contrib . gis . gdal . libgdal import lgdal <EOL> from django . contrib . sites . models import Site <EOL> from django . contrib import humanize <EOL> from django . template import loader , Context as DjangoContext , RequestContext <EOL> from django . utils import simplejson as json , translation <EOL> from django . utils . translation import ugettext as _ , ungettext as _n <EOL> from django . template . defaultfilters import slugify , force_escape <EOL> from django . conf import settings <EOL> from tagging . utils import parse_tag_input <EOL> from tagging . models import Tag , TaggedItem <EOL> from datetime import datetime , time , timedelta <EOL> from decimal import * <EOL> from functools import wraps <EOL> from redistricting . calculators import * <EOL> from redistricting . models import * <EOL> from redistricting . tasks import * <EOL> import random , string , math , types , copy , time , threading , traceback , os <EOL> import commands , sys , tempfile , csv , hashlib , inflect , logging <EOL> import ModestMaps <EOL> from PIL import Image , ImageChops , ImageMath <EOL> import urllib , urllib2 <EOL> from xhtml2pdf . pisa import CreatePDF <EOL> import StringIO <EOL> logger = logging . getLogger ( __name__ ) <EOL> UNASSIGNED_DISTRICT_ID = <NUM_LIT:0> <EOL> def using_unique_session ( u ) : <EOL> """<STR_LIT>""" <EOL> if u . is_anonymous ( ) or u . is_superuser : <EOL> return True <EOL> sessions = Session . objects . all ( ) <EOL> count = <NUM_LIT:0> <EOL> for session in sessions : <EOL> try : <EOL> decoded = session . get_decoded ( ) <EOL> if '<STR_LIT>' in decoded and decoded [ '<STR_LIT>' ] == u . id : <EOL> if '<STR_LIT>' in decoded and decoded [ '<STR_LIT>' ] < datetime . now ( ) : <EOL> Session . objects . filter ( session_key = session . session_key ) . delete ( ) <EOL> else : <EOL> count += <NUM_LIT:1> <EOL> except SuspiciousOperation : <EOL> logger . debug ( "<STR_LIT>" , session . session_key ) <EOL> for session in sessions : <EOL> try : <EOL> decoded = session . get_decoded ( ) <EOL> if '<STR_LIT>' in decoded and decoded [ '<STR_LIT>' ] == u . id : <EOL> websession = SessionStore ( session_key = session . session_key ) <EOL> websession [ '<STR_LIT:count>' ] = count <EOL> websession . save ( ) <EOL> except SuspiciousOperation : <EOL> logger . debug ( "<STR_LIT>" , session . session_key ) <EOL> return ( count <= <NUM_LIT:1> ) <EOL> def unique_session_or_json_redirect ( function ) : <EOL> """<STR_LIT>""" <EOL> def decorator ( request , * args , ** kwargs ) : <EOL> def return_nonunique_session_result ( ) : <EOL> status = { '<STR_LIT:success>' : False } <EOL> status [ '<STR_LIT:message>' ] = _ ( <EOL> "<STR_LIT>" ) <EOL> status [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> return HttpResponse ( json . dumps ( status ) , mimetype = '<STR_LIT:application/json>' ) <EOL> if not using_unique_session ( request . user ) : <EOL> return return_nonunique_session_result ( ) <EOL> else : <EOL> return function ( request , * args , ** kwargs ) <EOL> return wraps ( function ) ( decorator ) <EOL> def is_session_available ( req ) : <EOL> """<STR_LIT>""" <EOL> if req . user . is_superuser or req . user . is_staff : <EOL> return True <EOL> sessions = Session . objects . filter ( expire_date__gt = datetime . now ( ) ) <EOL> count = <NUM_LIT:0> <EOL> for session in sessions : <EOL> try : <EOL> decoded = session . get_decoded ( ) <EOL> if ( not req . user . is_anonymous ( ) ) and '<STR_LIT>' in decoded and decoded [ '<STR_LIT>' ] > datetime . now ( ) : <EOL> count += <NUM_LIT:1> <EOL> except SuspiciousOperation : <EOL> logger . debug ( "<STR_LIT>" , session . session_key ) <EOL> avail = count < settings . CONCURRENT_SESSIONS <EOL> req . session [ '<STR_LIT>' ] = avail <EOL> return avail <EOL> def note_session_activity ( req ) : <EOL> """<STR_LIT>""" <EOL> window = timedelta ( <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> , settings . SESSION_TIMEOUT ) <EOL> req . session [ '<STR_LIT>' ] = datetime . now ( ) + window <EOL> @ login_required <EOL> def unloadplan ( request , planid ) : <EOL> """<STR_LIT>""" <EOL> note_session_activity ( request ) <EOL> status = { '<STR_LIT:success>' : False } <EOL> ps = Plan . objects . filter ( pk = planid ) <EOL> if len ( ps ) > <NUM_LIT:0> : <EOL> p = ps [ <NUM_LIT:0> ] <EOL> if not can_copy ( request . user , p ) : <EOL> status [ '<STR_LIT:message>' ] = _ ( "<STR_LIT>" ) % { '<STR_LIT:user>' : request . user . username } <EOL> return HttpResponse ( json . dumps ( status ) , mimetype = '<STR_LIT:application/json>' ) <EOL> if settings . MAX_UNDOS_AFTER_EDIT > <NUM_LIT:0> : <EOL> p . purge_beyond_nth_step ( settings . MAX_UNDOS_AFTER_EDIT ) <EOL> status [ '<STR_LIT:success>' ] = True <EOL> return HttpResponse ( json . dumps ( status ) , mimetype = '<STR_LIT:application/json>' ) <EOL> @ login_required <EOL> @ unique_session_or_json_redirect <EOL> def copyplan ( request , planid ) : <EOL> """<STR_LIT>""" <EOL> note_session_activity ( request ) <EOL> if not is_plan_ready ( planid ) : <EOL> return HttpResponseRedirect ( '<STR_LIT:/>' ) <EOL> status = { '<STR_LIT:success>' : False } <EOL> p = Plan . objects . get ( pk = planid ) <EOL> if not can_copy ( request . user , p ) : <EOL> status [ '<STR_LIT:message>' ] = _ ( "<STR_LIT>" "<STR_LIT>" % { '<STR_LIT:username>' : request . user . username } ) <EOL> return HttpResponse ( json . dumps ( status ) , mimetype = '<STR_LIT:application/json>' ) <EOL> newname = p . name + "<STR_LIT:U+0020>" + str ( random . random ( ) ) <EOL> if ( request . method == "<STR_LIT:POST>" ) : <EOL> newname = request . POST [ "<STR_LIT:name>" ] [ <NUM_LIT:0> : <NUM_LIT:200> ] <EOL> shared = request . POST . get ( "<STR_LIT>" , False ) <EOL> plan_copy = Plan . objects . filter ( name = newname , owner = request . user , legislative_body = p . legislative_body ) <EOL> if len ( plan_copy ) > <NUM_LIT:0> : <EOL> status [ '<STR_LIT:message>' ] = _ ( "<STR_LIT>" "<STR_LIT>" ) <EOL> return HttpResponse ( json . dumps ( status ) , mimetype = '<STR_LIT:application/json>' ) <EOL> plan_copy = Plan ( name = newname , owner = request . user , is_shared = shared , legislative_body = p . legislative_body , processing_state = ProcessingState . READY ) <EOL> plan_copy . create_unassigned = False <EOL> plan_copy . save ( ) <EOL> districts = p . get_districts_at_version ( p . version , include_geom = True ) <EOL> for district in districts : <EOL> district_copy = copy . copy ( district ) <EOL> district_copy . id = None <EOL> district_copy . version = <NUM_LIT:0> <EOL> district_copy . is_locked = False <EOL> district_copy . plan = plan_copy <EOL> try : <EOL> district_copy . save ( ) <EOL> except Exception as inst : <EOL> status [ "<STR_LIT:message>" ] = _ ( "<STR_LIT>" ) <EOL> status [ "<STR_LIT>" ] = inst . message <EOL> return HttpResponse ( json . dumps ( status ) , mimetype = '<STR_LIT:application/json>' ) <EOL> district_copy . clone_relations_from ( district ) <EOL> data = serializers . serialize ( "<STR_LIT>" , [ plan_copy ] ) <EOL> return HttpResponse ( data , mimetype = '<STR_LIT:application/json>' ) <EOL> @ login_required <EOL> @ unique_session_or_json_redirect <EOL> def scoreplan ( request , planid ) : <EOL> """<STR_LIT>""" <EOL> note_session_activity ( request ) <EOL> status = { '<STR_LIT:success>' : False } <EOL> plan = Plan . objects . get ( pk = planid ) <EOL> criterion = ValidationCriteria . objects . filter ( legislative_body = plan . legislative_body ) <EOL> status [ '<STR_LIT:success>' ] = True <EOL> for criteria in criterion : <EOL> try : <EOL> score = ComputedPlanScore . compute ( criteria . function , plan ) <EOL> except : <EOL> logger . debug ( traceback . format_exc ( ) ) <EOL> if not score or not score [ '<STR_LIT:value>' ] : <EOL> status [ '<STR_LIT:success>' ] = False <EOL> status [ '<STR_LIT:message>' ] = '<STR_LIT>' % ( criteria . get_short_label ( ) , criteria . get_long_description ( ) or criteria . function . get_long_description ( ) ) <EOL> break <EOL> if status [ '<STR_LIT:success>' ] : <EOL> status [ '<STR_LIT:success>' ] = True <EOL> status [ '<STR_LIT:message>' ] = _ ( "<STR_LIT>" ) <EOL> plan . is_valid = True <EOL> plan . save ( ) <EOL> return HttpResponse ( json . dumps ( status ) , mimetype = '<STR_LIT:application/json>' ) <EOL> def get_user_info ( user ) : <EOL> """<STR_LIT>""" <EOL> if user . is_anonymous ( ) : <EOL> return None <EOL> profile = user . get_profile ( ) <EOL> return { <EOL> '<STR_LIT:username>' : user . username , <EOL> '<STR_LIT:email>' : user . email , <EOL> '<STR_LIT>' : profile . pass_hint , <EOL> '<STR_LIT>' : user . first_name , <EOL> '<STR_LIT>' : user . last_name , <EOL> '<STR_LIT>' : profile . organization , <EOL> '<STR_LIT:id>' : user . id <EOL> } <EOL> def commonplan ( request , planid ) : <EOL> """<STR_LIT>""" <EOL> note_session_activity ( request ) <EOL> plan = Plan . objects . filter ( id = planid ) <EOL> if plan . count ( ) == <NUM_LIT:1> : <EOL> plan = plan [ <NUM_LIT:0> ] <EOL> plan . edited = getutc ( plan . edited ) <EOL> levels = plan . legislative_body . get_geolevels ( ) <EOL> districts = plan . get_districts_at_version ( plan . version , include_geom = False ) <EOL> editable = can_edit ( request . user , plan ) <EOL> default_demo = plan . legislative_body . get_default_subject ( ) <EOL> max_dists = plan . legislative_body . max_districts <EOL> body_member_short_label = plan . legislative_body . get_short_label ( ) <EOL> body_member_long_label = plan . legislative_body . get_label ( ) <EOL> body_members = plan . legislative_body . get_members_label ( ) <EOL> reporting_template = '<STR_LIT>' % plan . legislative_body . name if not plan . is_community ( ) else None <EOL> index = body_member_short_label . find ( '<STR_LIT>' ) <EOL> if index >= <NUM_LIT:0> : <EOL> body_member_short_label = body_member_short_label [ <NUM_LIT:0> : index ] <EOL> index = body_member_long_label . find ( '<STR_LIT>' ) <EOL> if index >= <NUM_LIT:0> : <EOL> body_member_long_label = body_member_long_label [ <NUM_LIT:0> : index ] <EOL> if not editable and not can_view ( request . user , plan ) : <EOL> plan = { } <EOL> tags = [ ] <EOL> calculator_reports = [ ] <EOL> else : <EOL> tags = Tag . objects . filter ( name__startswith = '<STR_LIT>' ) . order_by ( '<STR_LIT:id>' ) . values_list ( '<STR_LIT:name>' , flat = True ) <EOL> tags = map ( lambda x : x [ <NUM_LIT:5> : ] , tags ) <EOL> calculator_reports = [ ] <EOL> if settings . REPORTS_ENABLED == '<STR_LIT>' : <EOL> report_displays = ScoreDisplay . objects . filter ( name = "<STR_LIT>" % plan . legislative_body . name ) <EOL> if len ( report_displays ) > <NUM_LIT:0> : <EOL> calculator_reports = map ( lambda p : { <EOL> '<STR_LIT:title>' : p . __unicode__ ( ) , <EOL> '<STR_LIT>' : map ( lambda f : { <EOL> '<STR_LIT:label>' : f . get_label ( ) , <EOL> '<STR_LIT:id>' : f . id <EOL> } , p . score_functions . all ( ) . filter ( selectable_bodies = plan . legislative_body ) ) <EOL> } , report_displays [ <NUM_LIT:0> ] . scorepanel_set . all ( ) . order_by ( '<STR_LIT>' ) ) <EOL> else : <EOL> plan = { } <EOL> levels = list ( ) <EOL> districts = { } <EOL> editable = False <EOL> default_demo = None <EOL> max_dists = <NUM_LIT:0> <EOL> body_member_short_label = '<STR_LIT>' <EOL> body_member_long_label = _ ( '<STR_LIT>' ) + '<STR_LIT:U+0020>' <EOL> body_members = _n ( '<STR_LIT>' , '<STR_LIT>' , <NUM_LIT:2> ) <EOL> reporting_template = None <EOL> tags = [ ] <EOL> calculator_reports = [ ] <EOL> demos = Subject . objects . all ( ) . order_by ( '<STR_LIT>' ) [ <NUM_LIT:0> : <NUM_LIT:3> ] <EOL> layers = [ ] <EOL> snaplayers = [ ] <EOL> if len ( levels ) > <NUM_LIT:0> : <EOL> study_area_extent = list ( levels [ <NUM_LIT:0> ] . geounit_set . extent ( field_name = '<STR_LIT>' ) ) <EOL> else : <EOL> for lb in LegislativeBody . objects . all ( ) : <EOL> biglevel = lb . get_geolevels ( ) [ <NUM_LIT:0> ] <EOL> if biglevel . geounit_set . count ( ) > <NUM_LIT:0> : <EOL> study_area_extent = biglevel . geounit_set . extent ( field_name = '<STR_LIT>' ) <EOL> break <EOL> for level in levels : <EOL> snaplayers . append ( { <EOL> '<STR_LIT>' : level . id , <EOL> '<STR_LIT>' : level . name , <EOL> '<STR_LIT>' : '<STR_LIT>' + level . name , <EOL> '<STR_LIT>' : level . get_long_description ( ) , <EOL> '<STR_LIT>' : level . min_zoom <EOL> } ) <EOL> default_selected = False <EOL> for demo in demos : <EOL> isdefault = str ( ( not default_demo is None ) and ( demo . id == default_demo . id ) ) . lower ( ) <EOL> if isdefault == '<STR_LIT:true>' : <EOL> default_selected = True <EOL> layers . append ( { <EOL> '<STR_LIT:id>' : demo . id , <EOL> '<STR_LIT:text>' : demo . get_short_label ( ) , <EOL> '<STR_LIT:value>' : demo . name , <EOL> '<STR_LIT>' : isdefault , <EOL> '<STR_LIT>' : str ( demo . is_displayed ) . lower ( ) <EOL> } ) <EOL> if default_demo and not default_selected : <EOL> layers . insert ( <NUM_LIT:0> , { <EOL> '<STR_LIT:id>' : default_demo . id , <EOL> '<STR_LIT:text>' : default_demo . get_short_label ( ) , <EOL> '<STR_LIT:value>' : default_demo . name , <EOL> '<STR_LIT>' : str ( True ) . lower ( ) , <EOL> '<STR_LIT>' : str ( default_demo . is_displayed ) . lower ( ) <EOL> } ) <EOL> if '<STR_LIT>' in settings . __members__ : <EOL> mapserver_protocol = settings . MAP_SERVER_PROTOCOL <EOL> else : <EOL> mapserver_protocol = '<STR_LIT>' <EOL> short_label = body_member_short_label . strip ( ) . lower ( ) <EOL> long_label = body_member_long_label . strip ( ) . lower ( ) <EOL> has_regions = Region . objects . all ( ) . count ( ) > <NUM_LIT:1> <EOL> bodies = LegislativeBody . objects . all ( ) . order_by ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> l_bodies = [ b for b in bodies if b in [ sd . legislative_body for sd in ScoreDisplay . objects . filter ( is_page = True ) ] ] <EOL> try : <EOL> loader . get_template ( reporting_template ) <EOL> except : <EOL> reporting_template = None <EOL> return RequestContext ( request , { <EOL> '<STR_LIT>' : bodies , <EOL> '<STR_LIT>' : has_regions , <EOL> '<STR_LIT>' : l_bodies , <EOL> '<STR_LIT>' : plan , <EOL> '<STR_LIT>' : districts , <EOL> '<STR_LIT>' : settings . MAP_SERVER , <EOL> '<STR_LIT>' : mapserver_protocol , <EOL> '<STR_LIT>' : settings . BASE_MAPS , <EOL> '<STR_LIT>' : settings . MAP_SERVER_NS , <EOL> '<STR_LIT>' : settings . MAP_SERVER_NSHREF , <EOL> '<STR_LIT>' : settings . FEATURE_LIMIT , <EOL> '<STR_LIT>' : settings . ADJACENCY , <EOL> '<STR_LIT>' : settings . CONVEX_CHOROPLETH , <EOL> '<STR_LIT>' : layers , <EOL> '<STR_LIT>' : snaplayers , <EOL> '<STR_LIT>' : UNASSIGNED_DISTRICT_ID , <EOL> '<STR_LIT>' : request . user . username != '<STR_LIT>' and request . user . username != '<STR_LIT>' , <EOL> '<STR_LIT>' : settings . DEBUG and request . user . is_staff , <EOL> '<STR_LIT>' : get_user_info ( request . user ) , <EOL> '<STR_LIT>' : editable , <EOL> '<STR_LIT>' : max_dists + <NUM_LIT:1> , <EOL> '<STR_LIT>' : settings . GA_ACCOUNT , <EOL> '<STR_LIT>' : settings . GA_DOMAIN , <EOL> '<STR_LIT>' : short_label , <EOL> '<STR_LIT>' : long_label , <EOL> '<STR_LIT>' : body_members , <EOL> '<STR_LIT>' : reporting_template , <EOL> '<STR_LIT>' : study_area_extent , <EOL> '<STR_LIT>' : len ( ScoreDisplay . objects . filter ( is_page = True ) ) > <NUM_LIT:0> , <EOL> '<STR_LIT>' : json . dumps ( calculator_reports ) , <EOL> '<STR_LIT>' : ( '<STR_LIT>' in settings . __members__ ) , <EOL> '<STR_LIT>' : tags , <EOL> '<STR_LIT>' : Site . objects . get_current ( ) , <EOL> '<STR_LIT>' : _ ( "<STR_LIT>" ) if ( plan and plan . is_community ( ) ) else _ ( "<STR_LIT>" ) , <EOL> '<STR_LIT>' : translation . get_language ( ) , <EOL> '<STR_LIT>' : settings . LANGUAGES <EOL> } ) <EOL> def is_plan_ready ( planid ) : <EOL> """<STR_LIT>""" <EOL> planid = int ( planid ) <EOL> return planid == <NUM_LIT:0> or len ( Plan . objects . filter ( id = planid , processing_state = ProcessingState . READY ) ) > <NUM_LIT:0> <EOL> @ user_passes_test ( using_unique_session ) <EOL> def viewplan ( request , planid ) : <EOL> """<STR_LIT>""" <EOL> if not is_session_available ( request ) or not is_plan_ready ( planid ) : <EOL> return HttpResponseRedirect ( '<STR_LIT:/>' ) <EOL> if not request . user . is_anonymous ( ) and ( int ( planid ) == <NUM_LIT:0> ) and ( settings . MAX_UNDOS_AFTER_EDIT > <NUM_LIT:0> ) : <EOL> for p in Plan . objects . filter ( owner = request . user ) : <EOL> p . purge_beyond_nth_step ( settings . MAX_UNDOS_AFTER_EDIT ) <EOL> return render_to_response ( '<STR_LIT>' , commonplan ( request , planid ) ) <EOL> @ user_passes_test ( using_unique_session ) <EOL> def editplan ( request , planid ) : <EOL> """<STR_LIT>""" <EOL> if request . user . is_anonymous ( ) or not is_session_available ( request ) or not is_plan_ready ( planid ) : <EOL> return HttpResponseRedirect ( '<STR_LIT:/>' ) <EOL> cfg = commonplan ( request , planid ) <EOL> if cfg [ '<STR_LIT>' ] == False : <EOL> return HttpResponseRedirect ( '<STR_LIT>' % planid ) <EOL> plan = Plan . objects . get ( id = planid , owner = request . user ) <EOL> cfg [ '<STR_LIT>' ] = len ( cfg [ '<STR_LIT>' ] ) > plan . legislative_body . max_districts <EOL> cfg [ '<STR_LIT>' ] = plan . get_available_districts ( ) <EOL> if settings . MAX_UNDOS_AFTER_EDIT > <NUM_LIT:0> : <EOL> plan . purge_beyond_nth_step ( settings . MAX_UNDOS_AFTER_EDIT ) <EOL> return render_to_response ( '<STR_LIT>' , cfg ) <EOL> @ user_passes_test ( using_unique_session ) <EOL> def printplan ( request , planid ) : <EOL> """<STR_LIT>""" <EOL> if not is_session_available ( request ) : <EOL> return HttpResponseRedirect ( '<STR_LIT:/>' ) <EOL> cfg = commonplan ( request , planid ) <EOL> sha = hashlib . sha1 ( ) <EOL> sha . update ( str ( planid ) + str ( datetime . now ( ) ) ) <EOL> cfg [ '<STR_LIT>' ] = '<STR_LIT>' % sha . hexdigest ( ) <EOL> cfg [ '<STR_LIT>' ] = '<STR_LIT>' % request . META [ '<STR_LIT>' ] <EOL> if request . method == '<STR_LIT:POST>' : <EOL> if not '<STR_LIT>' in request . REQUEST or not '<STR_LIT>' in request . REQUEST or not '<STR_LIT>' in request . REQUEST or not '<STR_LIT>' in request . REQUEST or not '<STR_LIT>' in request . REQUEST : <EOL> logger . warning ( '<STR_LIT>' ) <EOL> return HttpResponseRedirect ( '<STR_LIT>' ) <EOL> height = <NUM_LIT> * <NUM_LIT:2> <EOL> if '<STR_LIT>' in request . REQUEST : <EOL> height = int ( request . REQUEST [ '<STR_LIT>' ] ) * <NUM_LIT:2> <EOL> width = <NUM_LIT> * <NUM_LIT:2> <EOL> if '<STR_LIT:width>' in request . REQUEST : <EOL> width = int ( request . REQUEST [ '<STR_LIT:width>' ] ) * <NUM_LIT:2> <EOL> opacity = <NUM_LIT> <EOL> if '<STR_LIT>' in request . REQUEST : <EOL> opacity = float ( request . REQUEST [ '<STR_LIT>' ] ) <EOL> full_legend = json . loads ( request . REQUEST [ '<STR_LIT>' ] ) <EOL> cfg [ '<STR_LIT>' ] = request . REQUEST [ '<STR_LIT>' ] <EOL> cfg [ '<STR_LIT>' ] = request . REQUEST [ '<STR_LIT>' ] <EOL> cfg [ '<STR_LIT>' ] = request . REQUEST [ '<STR_LIT>' ] <EOL> cfg [ '<STR_LIT>' ] = request . REQUEST [ '<STR_LIT>' ] <EOL> cfg [ '<STR_LIT>' ] = full_legend [ '<STR_LIT>' ] <EOL> cfg [ '<STR_LIT>' ] = full_legend [ '<STR_LIT>' ] <EOL> cfg [ '<STR_LIT>' ] = full_legend [ '<STR_LIT>' ] <EOL> cfg [ '<STR_LIT>' ] = full_legend [ '<STR_LIT>' ] <EOL> cfg [ '<STR_LIT>' ] = Plan . objects . get ( id = int ( request . REQUEST [ '<STR_LIT>' ] ) ) <EOL> cfg [ '<STR_LIT>' ] = datetime . now ( ) <EOL> bbox = request . REQUEST [ '<STR_LIT>' ] . split ( '<STR_LIT:U+002C>' ) <EOL> pt1 = Point ( float ( bbox [ <NUM_LIT:0> ] ) , float ( bbox [ <NUM_LIT:1> ] ) , srid = <NUM_LIT> ) <EOL> pt1 . transform ( SpatialReference ( '<STR_LIT>' ) ) <EOL> ll = ModestMaps . Geo . Location ( pt1 . y , pt1 . x ) <EOL> pt2 = Point ( float ( bbox [ <NUM_LIT:2> ] ) , float ( bbox [ <NUM_LIT:3> ] ) , srid = <NUM_LIT> ) <EOL> pt2 . transform ( SpatialReference ( '<STR_LIT>' ) ) <EOL> ur = ModestMaps . Geo . Location ( pt2 . y , pt2 . x ) <EOL> dims = ModestMaps . Core . Point ( width , height ) <EOL> provider = ModestMaps . OpenStreetMap . Provider ( ) <EOL> basemap = ModestMaps . mapByExtent ( provider , ll , ur , dims ) <EOL> fullImg = basemap . draw ( ) <EOL> provider = ModestMaps . WMS . Provider ( cfg [ '<STR_LIT>' ] , { <EOL> '<STR_LIT>' : cfg [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : '<STR_LIT:true>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT>' : <NUM_LIT> <EOL> } ) <EOL> overlayImg = ModestMaps . mapByExtent ( provider , ll , ur , dims ) . draw ( ) <EOL> maskImg = ImageChops . invert ( overlayImg ) <EOL> provider = ModestMaps . WMS . Provider ( cfg [ '<STR_LIT>' ] , { <EOL> '<STR_LIT>' : cfg [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : '<STR_LIT:false>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : request . REQUEST [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT>' : <NUM_LIT> <EOL> } ) <EOL> overlayImg = Image . blend ( overlayImg , ModestMaps . mapByExtent ( provider , ll , ur , dims ) . draw ( ) , <NUM_LIT:0.5> ) <EOL> fullImg = Image . composite ( fullImg , Image . blend ( fullImg , overlayImg , opacity ) , maskImg ) <EOL> provider = ModestMaps . WMS . Provider ( cfg [ '<STR_LIT>' ] , { <EOL> '<STR_LIT>' : cfg [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : '<STR_LIT:true>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : request . REQUEST [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT>' : <NUM_LIT> <EOL> } ) <EOL> overlayImg = ModestMaps . mapByExtent ( provider , ll , ur , dims ) . draw ( ) <EOL> maskImg = ImageChops . invert ( overlayImg ) <EOL> fullImg = Image . composite ( fullImg , Image . blend ( fullImg , overlayImg , opacity ) , maskImg ) <EOL> fullImg . save ( settings . WEB_TEMP + ( '<STR_LIT>' % sha . hexdigest ( ) ) , '<STR_LIT>' , quality = <NUM_LIT:100> ) <EOL> t = loader . get_template ( '<STR_LIT>' ) <EOL> page = t . render ( DjangoContext ( cfg ) ) <EOL> result = StringIO . StringIO ( ) <EOL> CreatePDF ( page , result , show_error_as_pdf = True ) <EOL> response = HttpResponse ( result . getvalue ( ) , mimetype = '<STR_LIT>' ) <EOL> response [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> return response <EOL> else : <EOL> return HttpResponseRedirect ( '<STR_LIT>' ) <EOL> @ login_required <EOL> @ unique_session_or_json_redirect <EOL> def createplan ( request ) : <EOL> """<STR_LIT>""" <EOL> note_session_activity ( request ) <EOL> status = { '<STR_LIT:success>' : False } <EOL> if request . method == "<STR_LIT:POST>" : <EOL> name = request . POST [ '<STR_LIT:name>' ] [ <NUM_LIT:0> : <NUM_LIT:200> ] <EOL> body = LegislativeBody . objects . get ( id = int ( request . POST [ '<STR_LIT>' ] ) ) <EOL> plan = Plan ( name = name , owner = request . user , legislative_body = body , processing_state = ProcessingState . READY ) <EOL> try : <EOL> plan . save ( ) <EOL> status = serializers . serialize ( "<STR_LIT>" , [ plan ] ) <EOL> except : <EOL> status = { '<STR_LIT:success>' : False , '<STR_LIT:message>' : _ ( "<STR_LIT>" ) } <EOL> return HttpResponse ( json . dumps ( status ) , mimetype = '<STR_LIT:application/json>' ) <EOL> @ unique_session_or_json_redirect <EOL> def uploadfile ( request ) : <EOL> """<STR_LIT>""" <EOL> note_session_activity ( request ) <EOL> if request . user . is_anonymous ( ) : <EOL> return HttpResponseRedirect ( '<STR_LIT:/>' ) <EOL> status = commonplan ( request , <NUM_LIT:0> ) <EOL> status [ '<STR_LIT>' ] = True <EOL> status [ '<STR_LIT>' ] = True <EOL> index_file = request . FILES . get ( '<STR_LIT>' , False ) <EOL> if not index_file : <EOL> status [ '<STR_LIT>' ] = False <EOL> return render_to_response ( '<STR_LIT>' , status ) <EOL> else : <EOL> filename = index_file . name <EOL> if index_file . size > settings . MAX_UPLOAD_SIZE : <EOL> logger . error ( '<STR_LIT>' ) <EOL> status [ '<STR_LIT>' ] = False <EOL> return render_to_response ( '<STR_LIT>' , status ) <EOL> if not filename . endswith ( ( '<STR_LIT>' , '<STR_LIT>' ) ) : <EOL> logger . error ( '<STR_LIT>' ) <EOL> status [ '<STR_LIT>' ] = False <EOL> elif request . POST [ '<STR_LIT>' ] == '<STR_LIT>' : <EOL> logger . error ( '<STR_LIT>' ) <EOL> status [ '<STR_LIT>' ] = False <EOL> else : <EOL> try : <EOL> dest = tempfile . NamedTemporaryFile ( mode = '<STR_LIT>' , delete = False ) <EOL> for chunk in request . FILES [ '<STR_LIT>' ] . chunks ( ) : <EOL> dest . write ( chunk ) <EOL> dest . close ( ) <EOL> if request . FILES [ '<STR_LIT>' ] . name . endswith ( '<STR_LIT>' ) : <EOL> os . rename ( dest . name , '<STR_LIT>' % ( dest . name , '<STR_LIT>' ) ) <EOL> filename = '<STR_LIT>' % ( dest . name , '<STR_LIT>' ) <EOL> else : <EOL> filename = dest . name <EOL> except Exception as ex : <EOL> logger . error ( '<STR_LIT>' ) <EOL> logger . error ( '<STR_LIT>' , ex ) <EOL> status [ '<STR_LIT>' ] = False <EOL> return render_to_response ( '<STR_LIT>' , status ) <EOL> DistrictIndexFile . index2plan . delay ( request . POST [ '<STR_LIT>' ] , request . POST [ '<STR_LIT>' ] , filename , owner = request . user , template = False , purge = True , email = request . POST [ '<STR_LIT>' ] , language = translation . get_language ( ) ) <EOL> return render_to_response ( '<STR_LIT>' , status ) <EOL> def generate_report_hash ( qdict ) : <EOL> """<STR_LIT>""" <EOL> params = qdict . get ( '<STR_LIT>' , '<STR_LIT:U+0020>' ) + qdict . get ( '<STR_LIT>' , '<STR_LIT:U+0020>' ) + qdict . get ( '<STR_LIT>' , '<STR_LIT:U+0020>' ) + qdict . get ( '<STR_LIT>' , '<STR_LIT:U+0020>' ) + qdict . get ( '<STR_LIT>' , '<STR_LIT>' ) + qdict . get ( '<STR_LIT>' , '<STR_LIT:U+0020>' ) + qdict . get ( '<STR_LIT>' , '<STR_LIT:U+0020>' ) + qdict . get ( '<STR_LIT>' , '<STR_LIT:U+0020>' ) + qdict . get ( '<STR_LIT>' , '<STR_LIT:U+0020>' ) <EOL> sha = hashlib . sha1 ( ) <EOL> sha . update ( params ) <EOL> return sha . hexdigest ( ) <EOL> @ unique_session_or_json_redirect <EOL> def getreport ( request , planid ) : <EOL> """<STR_LIT>""" <EOL> note_session_activity ( request ) <EOL> status = { '<STR_LIT:success>' : False } <EOL> try : <EOL> plan = Plan . objects . get ( pk = planid ) <EOL> except : <EOL> status [ '<STR_LIT:message>' ] = _ ( '<STR_LIT>' ) <EOL> return HttpResponse ( json . dumps ( status ) , mimetype = '<STR_LIT:application/json>' ) <EOL> if not can_view ( request . user , plan ) : <EOL> status [ '<STR_LIT:message>' ] = _ ( "<STR_LIT>" ) <EOL> return HttpResponse ( json . dumps ( status ) , mimetype = '<STR_LIT:application/json>' ) <EOL> if not settings . REPORTS_ENABLED is None : <EOL> status [ '<STR_LIT:message>' ] = _ ( '<STR_LIT>' ) <EOL> return HttpResponse ( json . dumps ( status ) , mimetype = '<STR_LIT:application/json>' ) <EOL> if request . method != '<STR_LIT:POST>' : <EOL> status [ '<STR_LIT:message>' ] = _ ( "<STR_LIT>" ) <EOL> return HttpResponse ( json . dumps ( status ) , mimetype = '<STR_LIT:application/json>' ) <EOL> stamp = request . POST . get ( '<STR_LIT>' , generate_report_hash ( request . POST ) ) <EOL> rptstatus = PlanReport . checkreport ( planid , stamp ) <EOL> if rptstatus == '<STR_LIT>' : <EOL> status = { <EOL> '<STR_LIT:success>' : True , <EOL> '<STR_LIT:url>' : PlanReport . getreport ( planid , stamp ) , <EOL> '<STR_LIT>' : <NUM_LIT:0> , <EOL> '<STR_LIT:message>' : _ ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' : stamp <EOL> } <EOL> elif rptstatus == '<STR_LIT>' : <EOL> status = { <EOL> '<STR_LIT:success>' : True , <EOL> '<STR_LIT:url>' : reverse ( getreport , args = [ planid ] ) , <EOL> '<STR_LIT>' : <NUM_LIT:10> , <EOL> '<STR_LIT:message>' : _ ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' : stamp <EOL> } <EOL> elif rptstatus == '<STR_LIT>' : <EOL> status = { <EOL> '<STR_LIT:success>' : True , <EOL> '<STR_LIT:url>' : reverse ( getreport , args = [ planid ] ) , <EOL> '<STR_LIT>' : <NUM_LIT:10> , <EOL> '<STR_LIT:message>' : _ ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' : stamp <EOL> } <EOL> req = { <EOL> '<STR_LIT>' : request . POST . get ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> '<STR_LIT>' : request . POST . get ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> '<STR_LIT>' : request . POST . getlist ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' : request . POST . get ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> '<STR_LIT>' : request . POST . get ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> '<STR_LIT>' : request . POST . get ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> '<STR_LIT>' : request . POST . get ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> '<STR_LIT>' : request . POST . get ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> '<STR_LIT>' : request . POST . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> } <EOL> PlanReport . markpending ( planid , stamp ) <EOL> PlanReport . createreport . delay ( planid , stamp , req , language = translation . get_language ( ) ) <EOL> else : <EOL> status [ '<STR_LIT:message>' ] = _ ( <EOL> '<STR_LIT>' ) <EOL> return HttpResponse ( json . dumps ( status ) , mimetype = '<STR_LIT:application/json>' ) <EOL> @ unique_session_or_json_redirect <EOL> def getcalculatorreport ( request , planid ) : <EOL> """<STR_LIT>""" <EOL> note_session_activity ( request ) <EOL> status = { '<STR_LIT:success>' : False } <EOL> try : <EOL> plan = Plan . objects . get ( pk = planid ) <EOL> except : <EOL> status [ '<STR_LIT:message>' ] = _ ( '<STR_LIT>' ) <EOL> return HttpResponse ( json . dumps ( status ) , mimetype = '<STR_LIT:application/json>' ) <EOL> if not can_view ( request . user , plan ) : <EOL> status [ '<STR_LIT:message>' ] = _ ( "<STR_LIT>" ) <EOL> return HttpResponse ( json . dumps ( status ) , mimetype = '<STR_LIT:application/json>' ) <EOL> if request . method != '<STR_LIT:POST>' : <EOL> status [ '<STR_LIT:message>' ] = _ ( "<STR_LIT>" ) <EOL> return HttpResponse ( json . dumps ( status ) , mimetype = '<STR_LIT:application/json>' ) <EOL> function_ids = request . POST . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> sha = hashlib . sha1 ( ) <EOL> sha . update ( function_ids ) <EOL> stamp = request . POST . get ( '<STR_LIT>' , sha . hexdigest ( ) ) <EOL> rptstatus = CalculatorReport . checkreport ( planid , stamp ) <EOL> if rptstatus == '<STR_LIT>' : <EOL> status = { <EOL> '<STR_LIT:success>' : True , <EOL> '<STR_LIT:url>' : CalculatorReport . getreport ( planid , stamp ) , <EOL> '<STR_LIT>' : <NUM_LIT:0> , <EOL> '<STR_LIT:message>' : _ ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' : stamp <EOL> } <EOL> elif rptstatus == '<STR_LIT>' : <EOL> status = { <EOL> '<STR_LIT:success>' : True , <EOL> '<STR_LIT:url>' : reverse ( getcalculatorreport , args = [ planid ] ) , <EOL> '<STR_LIT>' : <NUM_LIT:5> , <EOL> '<STR_LIT:message>' : _ ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' : stamp <EOL> } <EOL> elif rptstatus == '<STR_LIT>' : <EOL> status = { <EOL> '<STR_LIT:success>' : True , <EOL> '<STR_LIT:url>' : reverse ( getcalculatorreport , args = [ planid ] ) , <EOL> '<STR_LIT>' : <NUM_LIT:5> , <EOL> '<STR_LIT:message>' : _ ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' : stamp <EOL> } <EOL> req = { '<STR_LIT>' : function_ids } <EOL> CalculatorReport . markpending ( planid , stamp ) <EOL> CalculatorReport . createcalculatorreport . delay ( planid , stamp , req , language = translation . get_language ( ) ) <EOL> else : <EOL> status [ '<STR_LIT:message>' ] = _ ( <EOL> '<STR_LIT>' ) <EOL> return HttpResponse ( json . dumps ( status ) , mimetype = '<STR_LIT:application/json>' ) <EOL> @ login_required <EOL> @ unique_session_or_json_redirect <EOL> def newdistrict ( request , planid ) : <EOL> """<STR_LIT>""" <EOL> note_session_activity ( request ) <EOL> status = { '<STR_LIT:success>' : False } <EOL> if len ( request . REQUEST . items ( ) ) >= <NUM_LIT:3> : <EOL> plan = Plan . objects . get ( pk = planid , owner = request . user ) <EOL> if '<STR_LIT>' in request . REQUEST : <EOL> geolevel = request . REQUEST [ '<STR_LIT>' ] <EOL> else : <EOL> geolevel = None <EOL> if '<STR_LIT>' in request . REQUEST : <EOL> geounit_ids = string . split ( request . REQUEST [ '<STR_LIT>' ] , '<STR_LIT:|>' ) <EOL> else : <EOL> geounit_ids = None <EOL> if '<STR_LIT>' in request . REQUEST : <EOL> district_id = int ( request . REQUEST [ '<STR_LIT>' ] ) <EOL> else : <EOL> district_id = None <EOL> if '<STR_LIT>' in request . REQUEST : <EOL> district_short = request . REQUEST [ '<STR_LIT>' ] [ <NUM_LIT:0> : <NUM_LIT:10> ] <EOL> elif not district_id is None : <EOL> district_short = plan . legislative_body . get_short_label ( ) % { '<STR_LIT>' : district_id } <EOL> else : <EOL> district_short = None <EOL> if '<STR_LIT>' in request . REQUEST : <EOL> district_long = request . REQUEST [ '<STR_LIT>' ] [ <NUM_LIT:0> : <NUM_LIT> ] <EOL> elif not district_id is None : <EOL> district_long = plan . legislative_body . get_label ( ) % { '<STR_LIT>' : district_id } <EOL> else : <EOL> district_long = None <EOL> if '<STR_LIT:version>' in request . REQUEST : <EOL> version = request . REQUEST [ '<STR_LIT:version>' ] <EOL> else : <EOL> version = plan . version <EOL> if geolevel and geounit_ids and district_id : <EOL> try : <EOL> fixed = plan . add_geounits ( ( district_id , district_short , district_long , ) , geounit_ids , geolevel , version ) <EOL> district = plan . district_set . filter ( district_id = district_id , short_label = district_short , long_label = district_long ) [ <NUM_LIT:0> ] <EOL> if plan . legislative_body . multi_members_allowed : <EOL> district . num_members = plan . legislative_body . min_multi_district_members <EOL> district . save ( ) <EOL> ct = ContentType . objects . get ( app_label = '<STR_LIT>' , model = '<STR_LIT>' ) <EOL> if '<STR_LIT>' in request . POST and request . POST [ '<STR_LIT>' ] != '<STR_LIT>' : <EOL> comment = Comment ( <EOL> object_pk = district . id , <EOL> content_type = ct , <EOL> site_id = Site . objects . get_current ( ) . id , <EOL> user_name = request . user . username , <EOL> user_email = request . user . email , <EOL> comment = request . POST [ '<STR_LIT>' ] ) <EOL> comment . save ( ) <EOL> if len ( request . REQUEST . getlist ( '<STR_LIT>' ) ) > <NUM_LIT:0> : <EOL> strtags = request . REQUEST . getlist ( '<STR_LIT>' ) <EOL> for strtag in strtags : <EOL> if strtag == '<STR_LIT>' : <EOL> continue <EOL> if strtag . count ( '<STR_LIT:U+0020>' ) > <NUM_LIT:0> : <EOL> strtag = '<STR_LIT>' % strtag <EOL> else : <EOL> strtag = '<STR_LIT>' % strtag <EOL> Tag . objects . add_tag ( district , strtag ) <EOL> status [ '<STR_LIT:success>' ] = True <EOL> status [ '<STR_LIT:message>' ] = _ ( '<STR_LIT>' ) <EOL> plan = Plan . objects . get ( pk = planid , owner = request . user ) <EOL> status [ '<STR_LIT>' ] = getutc ( plan . edited ) . isoformat ( ) <EOL> status [ '<STR_LIT>' ] = district_id <EOL> status [ '<STR_LIT:version>' ] = plan . version <EOL> except ValidationError : <EOL> status [ '<STR_LIT:message>' ] = _ ( '<STR_LIT>' ) <EOL> except Exception , ex : <EOL> logger . warn ( '<STR_LIT>' ) <EOL> logger . debug ( '<STR_LIT>' , ex ) <EOL> status [ '<STR_LIT:message>' ] = _ ( "<STR_LIT>" ) <EOL> else : <EOL> status [ '<STR_LIT:message>' ] = _ ( '<STR_LIT>' '<STR_LIT>' ) <EOL> return HttpResponse ( json . dumps ( status ) , mimetype = '<STR_LIT:application/json>' ) <EOL> @ login_required <EOL> @ unique_session_or_json_redirect <EOL> @ transaction . commit_manually <EOL> def add_districts_to_plan ( request , planid ) : <EOL> """<STR_LIT>""" <EOL> status = { '<STR_LIT:success>' : False } <EOL> try : <EOL> plan = Plan . objects . get ( pk = planid ) <EOL> except : <EOL> status [ '<STR_LIT:message>' ] = _ ( '<STR_LIT>' ) <EOL> return HttpResponse ( json . dumps ( status ) , mimetype = '<STR_LIT:application/json>' ) <EOL> if not can_edit ( request . user , plan ) : <EOL> status [ '<STR_LIT:message>' ] = _ ( "<STR_LIT>" ) <EOL> return HttpResponse ( json . dumps ( status ) , mimetype = '<STR_LIT:application/json>' ) <EOL> district_list = request . POST . getlist ( '<STR_LIT>' ) <EOL> if len ( district_list ) == <NUM_LIT:0> : <EOL> status [ '<STR_LIT:message>' ] = _ ( "<STR_LIT>" ) <EOL> return HttpResponse ( json . dumps ( status ) , mimetype = '<STR_LIT:application/json>' ) <EOL> else : <EOL> districts = District . objects . filter ( id__in = district_list ) <EOL> version = int ( request . POST . get ( '<STR_LIT:version>' , None ) ) <EOL> status [ '<STR_LIT:message>' ] = _ ( '<STR_LIT>' '<STR_LIT>' ) % { '<STR_LIT>' : len ( districts ) } <EOL> allowed_districts = plan . get_available_districts ( version = version ) <EOL> if len ( districts ) > allowed_districts : <EOL> status [ '<STR_LIT:message>' ] = _ ( '<STR_LIT>' '<STR_LIT>' ) % { '<STR_LIT>' : allowed_districts } <EOL> try : <EOL> results = plan . paste_districts ( districts , version = version ) <EOL> transaction . commit ( ) <EOL> status [ '<STR_LIT:success>' ] = True <EOL> status [ '<STR_LIT:message>' ] = _ ( '<STR_LIT>' ) % { '<STR_LIT>' : len ( results ) } <EOL> status [ '<STR_LIT:version>' ] = plan . version <EOL> except Exception as ex : <EOL> transaction . rollback ( ) <EOL> status [ '<STR_LIT:message>' ] = str ( ex ) <EOL> status [ '<STR_LIT>' ] = traceback . format_exc ( ) <EOL> return HttpResponse ( json . dumps ( status ) , mimetype = '<STR_LIT:application/json>' ) <EOL> @ login_required <EOL> @ unique_session_or_json_redirect <EOL> @ transaction . commit_manually <EOL> def assign_district_members ( request , planid ) : <EOL> """<STR_LIT>""" <EOL> status = { '<STR_LIT:success>' : False } <EOL> try : <EOL> plan = Plan . objects . get ( pk = planid ) <EOL> except : <EOL> status [ '<STR_LIT:message>' ] = _ ( '<STR_LIT>' ) <EOL> return HttpResponse ( json . dumps ( status ) , mimetype = '<STR_LIT:application/json>' ) <EOL> if not can_edit ( request . user , plan ) : <EOL> status [ '<STR_LIT:message>' ] = _ ( "<STR_LIT>" ) <EOL> return HttpResponse ( json . dumps ( status ) , mimetype = '<STR_LIT:application/json>' ) <EOL> leg_bod = plan . legislative_body <EOL> if ( not leg_bod . multi_members_allowed ) : <EOL> status [ '<STR_LIT:message>' ] = _ ( <EOL> '<STR_LIT>' ) <EOL> return HttpResponse ( json . dumps ( status ) , mimetype = '<STR_LIT:application/json>' ) <EOL> districts = request . POST . getlist ( '<STR_LIT>' ) <EOL> counts = request . POST . getlist ( '<STR_LIT>' ) <EOL> version = int ( request . POST . get ( '<STR_LIT:version>' , None ) ) <EOL> try : <EOL> changed = <NUM_LIT:0> <EOL> for i in range ( <NUM_LIT:0> , len ( districts ) ) : <EOL> id = int ( districts [ i ] ) <EOL> count = int ( counts [ i ] ) <EOL> district = District . objects . filter ( plan = plan , district_id = id , version__lte = version ) . order_by ( '<STR_LIT:version>' ) . reverse ( ) [ <NUM_LIT:0> ] <EOL> if district . num_members != count : <EOL> if ( changed == <NUM_LIT:0> ) : <EOL> if version != plan . version : <EOL> plan . purge ( after = version ) <EOL> plan . version = plan . version + <NUM_LIT:1> <EOL> plan . save ( ) <EOL> plan . update_num_members ( district , count ) <EOL> changed += <NUM_LIT:1> <EOL> transaction . commit ( ) <EOL> status [ '<STR_LIT:success>' ] = True <EOL> status [ '<STR_LIT:version>' ] = plan . version <EOL> status [ '<STR_LIT>' ] = changed <EOL> status [ '<STR_LIT:message>' ] = _ ( '<STR_LIT>' <EOL> '<STR_LIT>' ) % { '<STR_LIT>' : changed } <EOL> except Exception , ex : <EOL> transaction . rollback ( ) <EOL> status [ '<STR_LIT:message>' ] = str ( ex ) <EOL> status [ '<STR_LIT>' ] = traceback . format_exc ( ) <EOL> logger . warn ( '<STR_LIT>' ) <EOL> logger . debug ( '<STR_LIT>' , ex ) <EOL> return HttpResponse ( json . dumps ( status ) , mimetype = '<STR_LIT:application/json>' ) <EOL> @ login_required <EOL> @ unique_session_or_json_redirect <EOL> def combine_districts ( request , planid ) : <EOL> """<STR_LIT>""" <EOL> status = { '<STR_LIT:success>' : False } <EOL> try : <EOL> plan = Plan . objects . get ( pk = planid ) <EOL> except : <EOL> status [ '<STR_LIT:message>' ] = _ ( '<STR_LIT>' ) <EOL> return HttpResponse ( json . dumps ( status ) , mimetype = '<STR_LIT:application/json>' ) <EOL> if not can_edit ( request . user , plan ) : <EOL> status [ '<STR_LIT:message>' ] = _ ( "<STR_LIT>" ) <EOL> return HttpResponse ( json . dumps ( status ) , mimetype = '<STR_LIT:application/json>' ) <EOL> version = int ( request . POST . get ( '<STR_LIT:version>' , plan . version ) ) <EOL> from_id = int ( request . POST . get ( '<STR_LIT>' , - <NUM_LIT:1> ) ) <EOL> to_id = int ( request . POST . get ( '<STR_LIT>' , None ) ) <EOL> try : <EOL> all_districts = plan . get_districts_at_version ( version , include_geom = True ) <EOL> from_districts = filter ( lambda d : True if d . district_id == from_id else False , all_districts ) <EOL> to_district = filter ( lambda d : True if d . district_id == to_id else False , all_districts ) [ <NUM_LIT:0> ] <EOL> locked = to_district . is_locked <EOL> for district in from_districts : <EOL> if district . is_locked : <EOL> locked = True <EOL> if locked : <EOL> status [ '<STR_LIT:message>' ] = _ ( "<STR_LIT>" ) <EOL> return HttpResponse ( json . dumps ( status ) , mimetype = '<STR_LIT:application/json>' ) <EOL> result = plan . combine_districts ( to_district , from_districts , version = version ) <EOL> if result [ <NUM_LIT:0> ] == True : <EOL> status [ '<STR_LIT:success>' ] = True <EOL> status [ '<STR_LIT:message>' ] = _ ( '<STR_LIT>' ) <EOL> status [ '<STR_LIT:version>' ] = result [ <NUM_LIT:1> ] <EOL> except Exception , ex : <EOL> status [ '<STR_LIT:message>' ] = _ ( '<STR_LIT>' ) <EOL> status [ '<STR_LIT>' ] = traceback . format_exc ( ) <EOL> logger . warn ( '<STR_LIT>' ) <EOL> logger . debug ( '<STR_LIT>' , ex ) <EOL> return HttpResponse ( json . dumps ( status ) , mimetype = '<STR_LIT:application/json>' ) <EOL> @ login_required <EOL> @ unique_session_or_json_redirect <EOL> def fix_unassigned ( request , planid ) : <EOL> """<STR_LIT>""" <EOL> status = { '<STR_LIT:success>' : False } <EOL> try : <EOL> plan = Plan . objects . get ( pk = planid ) <EOL> except : <EOL> status [ '<STR_LIT:message>' ] = _ ( '<STR_LIT>' ) <EOL> return HttpResponse ( json . dumps ( status ) , mimetype = '<STR_LIT:application/json>' ) <EOL> if not can_edit ( request . user , plan ) : <EOL> status [ '<STR_LIT:message>' ] = _ ( "<STR_LIT>" ) <EOL> return HttpResponse ( json . dumps ( status ) , mimetype = '<STR_LIT:application/json>' ) <EOL> try : <EOL> version = int ( request . POST . get ( '<STR_LIT:version>' , plan . version ) ) <EOL> result = plan . fix_unassigned ( version ) <EOL> status [ '<STR_LIT:success>' ] = result [ <NUM_LIT:0> ] <EOL> status [ '<STR_LIT:message>' ] = result [ <NUM_LIT:1> ] <EOL> status [ '<STR_LIT:version>' ] = plan . version <EOL> except Exception , ex : <EOL> status [ '<STR_LIT:message>' ] = _ ( '<STR_LIT>' ) <EOL> status [ '<STR_LIT>' ] = traceback . format_exc ( ) <EOL> logger . warn ( '<STR_LIT>' ) <EOL> logger . debug ( '<STR_LIT>' , ex ) <EOL> return HttpResponse ( json . dumps ( status ) , mimetype = '<STR_LIT:application/json>' ) <EOL> @ unique_session_or_json_redirect <EOL> def get_splits ( request , planid , otherid , othertype ) : <EOL> """<STR_LIT>""" <EOL> otherid = int ( otherid ) <EOL> status = { '<STR_LIT:success>' : False } <EOL> try : <EOL> plan = Plan . objects . get ( pk = planid ) <EOL> except : <EOL> status [ '<STR_LIT:message>' ] = _ ( '<STR_LIT>' ) <EOL> return HttpResponse ( json . dumps ( status ) , mimetype = '<STR_LIT:application/json>' ) <EOL> if not can_view ( request . user , plan ) : <EOL> status [ '<STR_LIT:message>' ] = _ ( "<STR_LIT>" ) <EOL> return HttpResponse ( json . dumps ( status ) , mimetype = '<STR_LIT:application/json>' ) <EOL> version = int ( request . REQUEST [ '<STR_LIT:version>' ] if '<STR_LIT:version>' in request . REQUEST else plan . version ) <EOL> try : <EOL> if othertype == '<STR_LIT>' : <EOL> try : <EOL> otherplan = Plan . objects . get ( pk = otherid ) <EOL> except : <EOL> status [ '<STR_LIT:message>' ] = _ ( '<STR_LIT>' ) <EOL> return HttpResponse ( json . dumps ( status ) , mimetype = '<STR_LIT:application/json>' ) <EOL> if not can_view ( request . user , otherplan ) : <EOL> status [ '<STR_LIT:message>' ] = _ ( "<STR_LIT>" ) <EOL> return HttpResponse ( json . dumps ( status ) , mimetype = '<STR_LIT:application/json>' ) <EOL> otherversion = int ( request . REQUEST [ '<STR_LIT>' ] if '<STR_LIT>' in request . REQUEST else otherplan . version ) <EOL> splits = plan . find_plan_splits ( otherplan , version , otherversion ) <EOL> elif othertype == '<STR_LIT>' : <EOL> splits = plan . find_geolevel_splits ( otherid , version ) <EOL> else : <EOL> status [ '<STR_LIT:message>' ] = _ ( '<STR_LIT>' ) % { '<STR_LIT>' : othertype } <EOL> return HttpResponse ( json . dumps ( status ) , mimetype = '<STR_LIT:application/json>' ) <EOL> split_word = _ ( '<STR_LIT>' ) if len ( splits ) == <NUM_LIT:1> else inflect . engine ( ) . plural ( _ ( '<STR_LIT>' ) ) <EOL> status [ '<STR_LIT:success>' ] = True <EOL> status [ '<STR_LIT:message>' ] = _ ( '<STR_LIT>' ) % { '<STR_LIT>' : len ( splits ) , '<STR_LIT>' : split_word } <EOL> status [ '<STR_LIT>' ] = splits <EOL> status [ '<STR_LIT>' ] = list ( set ( [ i [ <NUM_LIT:0> ] for i in splits ] ) ) <EOL> status [ '<STR_LIT>' ] = list ( set ( [ i [ <NUM_LIT:1> ] for i in splits ] ) ) <EOL> except Exception , ex : <EOL> status [ '<STR_LIT:message>' ] = _ ( '<STR_LIT>' ) <EOL> status [ '<STR_LIT>' ] = traceback . format_exc ( ) <EOL> logger . warn ( '<STR_LIT>' ) <EOL> logger . debug ( '<STR_LIT>' , ex ) <EOL> return HttpResponse ( json . dumps ( status ) , mimetype = '<STR_LIT:application/json>' ) <EOL> def get_processing_status ( request ) : <EOL> """<STR_LIT>""" <EOL> status = { '<STR_LIT:success>' : False } <EOL> plan_ids = request . REQUEST . getlist ( '<STR_LIT>' ) <EOL> if len ( plan_ids ) == <NUM_LIT:0> : <EOL> status [ '<STR_LIT:message>' ] = _ ( '<STR_LIT>' ) <EOL> else : <EOL> statuses = { } <EOL> for p in Plan . objects . filter ( id__in = plan_ids ) : <EOL> statuses [ str ( p . id ) ] = p . get_processing_state_display ( ) <EOL> status [ '<STR_LIT:success>' ] = True <EOL> status [ '<STR_LIT:message>' ] = statuses <EOL> return HttpResponse ( json . dumps ( status ) , mimetype = '<STR_LIT:application/json>' ) <EOL> def get_splits_report ( request , planid ) : <EOL> """<STR_LIT>""" <EOL> note_session_activity ( request ) <EOL> try : <EOL> plan = Plan . objects . get ( pk = planid ) <EOL> except : <EOL> return HttpResponse ( _ ( '<STR_LIT>' ) , mimetype = '<STR_LIT>' ) <EOL> if not using_unique_session ( request . user ) or not can_view ( request . user , plan ) : <EOL> return HttpResponseForbidden ( ) <EOL> version = int ( request . REQUEST [ '<STR_LIT:version>' ] if '<STR_LIT:version>' in request . REQUEST else plan . version ) <EOL> inverse = request . REQUEST [ '<STR_LIT>' ] == '<STR_LIT:true>' if '<STR_LIT>' in request . REQUEST else False <EOL> extended = request . REQUEST [ '<STR_LIT>' ] == '<STR_LIT:true>' if '<STR_LIT>' in request . REQUEST else False <EOL> layers = request . REQUEST . getlist ( '<STR_LIT>' ) <EOL> if len ( layers ) == <NUM_LIT:0> : <EOL> return HttpResponse ( _ ( '<STR_LIT>' ) , mimetype = '<STR_LIT>' ) <EOL> try : <EOL> report = loader . get_template ( '<STR_LIT>' ) <EOL> html = '<STR_LIT>' <EOL> for layer in layers : <EOL> my_context = { '<STR_LIT>' : extended } <EOL> my_context . update ( plan . compute_splits ( layer , version = version , inverse = inverse , extended = extended ) ) <EOL> last_item = layer is layers [ - <NUM_LIT:1> ] <EOL> community_info = plan . get_community_type_info ( layer , version = version , inverse = inverse , include_counts = last_item ) <EOL> if community_info is not None : <EOL> my_context . update ( community_info ) <EOL> calc_context = DjangoContext ( my_context ) <EOL> html += report . render ( calc_context ) <EOL> if not last_item : <EOL> html += '<STR_LIT>' <EOL> return HttpResponse ( html , mimetype = '<STR_LIT>' ) <EOL> except Exception , ex : <EOL> logger . warn ( '<STR_LIT>' ) <EOL> logger . debug ( '<STR_LIT>' , ex ) <EOL> return HttpResponse ( str ( ex ) , mimetype = '<STR_LIT>' ) <EOL> @ login_required <EOL> @ unique_session_or_json_redirect <EOL> def addtodistrict ( request , planid , districtid ) : <EOL> """<STR_LIT>""" <EOL> note_session_activity ( request ) <EOL> status = { '<STR_LIT:success>' : False } <EOL> if len ( request . REQUEST . items ( ) ) >= <NUM_LIT:2> : <EOL> try : <EOL> geolevel = request . REQUEST [ "<STR_LIT>" ] <EOL> geounit_ids = string . split ( request . REQUEST [ "<STR_LIT>" ] , "<STR_LIT:|>" ) <EOL> plan = Plan . objects . get ( pk = planid , owner = request . user ) <EOL> except : <EOL> status [ '<STR_LIT>' ] = traceback . format_exc ( ) <EOL> status [ '<STR_LIT:message>' ] = _ ( '<STR_LIT>' ) <EOL> if '<STR_LIT:version>' in request . REQUEST : <EOL> version = request . REQUEST [ '<STR_LIT:version>' ] <EOL> else : <EOL> version = plan . version <EOL> try : <EOL> fixed = plan . add_geounits ( districtid , geounit_ids , geolevel , version ) <EOL> status [ '<STR_LIT:success>' ] = True ; <EOL> status [ '<STR_LIT:message>' ] = _ ( '<STR_LIT>' ) % { '<STR_LIT>' : fixed } <EOL> status [ '<STR_LIT>' ] = fixed <EOL> plan = Plan . objects . get ( pk = planid , owner = request . user ) <EOL> status [ '<STR_LIT>' ] = getutc ( plan . edited ) . isoformat ( ) <EOL> status [ '<STR_LIT:version>' ] = plan . version <EOL> except Exception , ex : <EOL> status [ '<STR_LIT>' ] = traceback . format_exc ( ) <EOL> status [ '<STR_LIT:message>' ] = _ ( '<STR_LIT>' ) <EOL> logger . warn ( '<STR_LIT>' ) <EOL> logger . debug ( '<STR_LIT>' , ex ) <EOL> else : <EOL> status [ '<STR_LIT:message>' ] = _ ( "<STR_LIT>" ) <EOL> return HttpResponse ( json . dumps ( status ) , mimetype = '<STR_LIT:application/json>' ) <EOL> @ unique_session_or_json_redirect <EOL> @ login_required <EOL> def setdistrictlock ( request , planid , district_id ) : <EOL> """<STR_LIT>""" <EOL> note_session_activity ( request ) <EOL> status = { '<STR_LIT:success>' : False } <EOL> if request . method != '<STR_LIT:POST>' : <EOL> return HttpResponseForbidden ( ) <EOL> lock = request . POST . get ( '<STR_LIT>' ) . lower ( ) == '<STR_LIT:true>' <EOL> version = request . POST . get ( '<STR_LIT:version>' ) <EOL> if lock == None : <EOL> status [ '<STR_LIT:message>' ] = _ ( '<STR_LIT>' ) <EOL> elif version == None : <EOL> status [ '<STR_LIT:message>' ] = _ ( '<STR_LIT>' ) <EOL> try : <EOL> plan = Plan . objects . get ( pk = planid ) <EOL> district = plan . district_set . filter ( district_id = district_id , version__lte = version ) . order_by ( '<STR_LIT:version>' ) . reverse ( ) [ <NUM_LIT:0> ] <EOL> except ObjectDoesNotExist : <EOL> status [ '<STR_LIT:message>' ] = _ ( '<STR_LIT>' ) <EOL> return HttpResponse ( json . dumps ( status ) , mimetype = '<STR_LIT:application/json>' ) <EOL> if plan . owner != request . user : <EOL> return HttpResponseForbidden ( ) <EOL> district . is_locked = lock <EOL> district . save ( ) <EOL> status [ '<STR_LIT:success>' ] = True <EOL> status [ '<STR_LIT:message>' ] = _ ( '<STR_LIT>' ) % { '<STR_LIT>' : _ ( '<STR_LIT>' ) if lock else _ ( '<STR_LIT>' ) } <EOL> return HttpResponse ( json . dumps ( status ) , mimetype = '<STR_LIT:application/json>' ) <EOL> @ unique_session_or_json_redirect <EOL> def getdistricts ( request , planid ) : <EOL> """<STR_LIT>""" <EOL> note_session_activity ( request ) <EOL> status = { '<STR_LIT:success>' : False } <EOL> plan = Plan . objects . filter ( id = planid ) <EOL> if plan . count ( ) == <NUM_LIT:1> : <EOL> plan = plan [ <NUM_LIT:0> ] <EOL> if '<STR_LIT:version>' in request . REQUEST : <EOL> version = int ( request . REQUEST [ '<STR_LIT:version>' ] ) <EOL> else : <EOL> version = plan . version <EOL> districts = plan . get_districts_at_version ( version , include_geom = False ) <EOL> status [ '<STR_LIT>' ] = [ ] <EOL> status [ '<STR_LIT>' ] = plan . legislative_body . max_districts - len ( districts ) + <NUM_LIT:1> <EOL> max_version = max ( [ d . version for d in districts ] ) <EOL> can_undo = max_version > plan . min_version <EOL> for district in districts : <EOL> status [ '<STR_LIT>' ] . append ( { <EOL> '<STR_LIT:id>' : district . district_id , <EOL> '<STR_LIT>' : '<STR_LIT:U+0020>' . join ( map ( _ , district . short_label . split ( '<STR_LIT:U+0020>' ) ) ) , <EOL> '<STR_LIT>' : '<STR_LIT:U+0020>' . join ( map ( _ , district . long_label . split ( '<STR_LIT:U+0020>' ) ) ) , <EOL> '<STR_LIT:version>' : district . version <EOL> } ) <EOL> status [ '<STR_LIT>' ] = can_undo <EOL> status [ '<STR_LIT:success>' ] = True <EOL> else : <EOL> status [ '<STR_LIT:message>' ] = _ ( '<STR_LIT>' ) <EOL> return HttpResponse ( json . dumps ( status ) , mimetype = '<STR_LIT:application/json>' ) <EOL> def simple_district_versioned ( request , planid , district_ids = None ) : <EOL> """<STR_LIT>""" <EOL> note_session_activity ( request ) <EOL> status = { '<STR_LIT:type>' : '<STR_LIT>' } <EOL> plan = Plan . objects . filter ( id = planid ) <EOL> if plan . count ( ) == <NUM_LIT:1> : <EOL> plan = plan [ <NUM_LIT:0> ] <EOL> if '<STR_LIT>' in request . REQUEST : <EOL> version = request . REQUEST [ '<STR_LIT>' ] <EOL> else : <EOL> version = plan . version <EOL> subject_id = None <EOL> if '<STR_LIT>' in request . REQUEST : <EOL> subject_id = request . REQUEST [ '<STR_LIT>' ] <EOL> elif plan . legislative_body . get_default_subject ( ) : <EOL> subject_id = plan . legislative_body . get_default_subject ( ) . id <EOL> geolevel = plan . legislative_body . get_geolevels ( ) [ <NUM_LIT:0> ] . id <EOL> if '<STR_LIT>' in request . REQUEST : <EOL> geolevel = int ( request . REQUEST [ '<STR_LIT>' ] ) <EOL> if '<STR_LIT>' in request . REQUEST : <EOL> district_ids = request . REQUEST [ '<STR_LIT>' ] <EOL> if len ( district_ids ) > <NUM_LIT:0> : <EOL> district_ids = district_ids . split ( '<STR_LIT:U+002C>' ) <EOL> else : <EOL> district_ids = [ ] <EOL> if subject_id : <EOL> bbox = None <EOL> if '<STR_LIT>' in request . REQUEST : <EOL> bbox = request . REQUEST [ '<STR_LIT>' ] <EOL> bbox = tuple ( map ( lambda x : float ( x ) , bbox . split ( '<STR_LIT:U+002C>' ) ) ) <EOL> else : <EOL> bbox = plan . district_set . all ( ) . extent ( field_name = '<STR_LIT>' ) <EOL> status [ '<STR_LIT>' ] = plan . get_wfs_districts ( version , subject_id , bbox , geolevel , district_ids ) <EOL> else : <EOL> status [ '<STR_LIT>' ] = [ ] <EOL> status [ '<STR_LIT:message>' ] = _ ( '<STR_LIT>' ) <EOL> else : <EOL> status [ '<STR_LIT>' ] = [ ] <EOL> status [ '<STR_LIT:message>' ] = _ ( '<STR_LIT>' ) <EOL> return HttpResponse ( json . dumps ( status ) , mimetype = '<STR_LIT:application/json>' ) <EOL> def get_unlocked_simple_geometries ( request , planid ) : <EOL> """<STR_LIT>""" <EOL> note_session_activity ( request ) <EOL> status = { '<STR_LIT:type>' : '<STR_LIT>' } <EOL> plan = Plan . objects . filter ( id = planid ) <EOL> if plan . count ( ) == <NUM_LIT:1> : <EOL> plan = plan [ <NUM_LIT:0> ] <EOL> version = request . POST . get ( '<STR_LIT>' , plan . version ) <EOL> geolevel = request . POST . get ( '<STR_LIT>' , plan . legislative_body . get_geolevels ( ) [ <NUM_LIT:0> ] . id ) <EOL> geom = request . POST . get ( '<STR_LIT>' , None ) <EOL> if geom is not None : <EOL> try : <EOL> wkt = request . POST . get ( '<STR_LIT>' , None ) <EOL> geom = GEOSGeometry ( wkt ) <EOL> except GEOSException : <EOL> wkt = request . REQUEST [ '<STR_LIT>' ] . replace ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> wkt = wkt . replace ( '<STR_LIT>' , '<STR_LIT:(>' ) . replace ( '<STR_LIT>' , '<STR_LIT:)>' ) <EOL> try : <EOL> geom = GEOSGeometry ( wkt ) <EOL> except GEOSException : <EOL> geom = None <EOL> selection = Q ( geom__intersects = geom ) <EOL> districts = [ d . id for d in plan . get_districts_at_version ( version , include_geom = True ) if d . is_locked ] <EOL> locked = District . objects . filter ( id__in = districts ) . collect ( ) <EOL> locked_buffered = locked . simplify ( <NUM_LIT:100> , True ) . buffer ( <NUM_LIT:100> ) if locked else None <EOL> filtered = Geolevel . objects . get ( id = geolevel ) . geounit_set . filter ( selection ) <EOL> features = [ ] <EOL> for feature in filtered : <EOL> geom = feature . simple <EOL> if locked and geom . intersects ( locked_buffered ) : <EOL> if feature . geom . within ( locked ) : <EOL> continue <EOL> if feature . geom . overlaps ( locked ) : <EOL> geom = geom . difference ( locked_buffered ) <EOL> features . append ( { <EOL> '<STR_LIT:id>' : '<STR_LIT>' % feature . id , <EOL> '<STR_LIT>' : json . loads ( geom . json ) , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT:name>' : feature . name , <EOL> '<STR_LIT>' : geolevel , <EOL> '<STR_LIT:id>' : feature . id <EOL> } <EOL> } ) <EOL> status [ '<STR_LIT>' ] = features <EOL> return HttpResponse ( json . dumps ( status ) , mimetype = '<STR_LIT:application/json>' ) <EOL> else : <EOL> status [ '<STR_LIT>' ] = [ ] <EOL> status [ '<STR_LIT:message>' ] = _ ( '<STR_LIT>' ) <EOL> else : <EOL> status [ '<STR_LIT>' ] = [ ] <EOL> status [ '<STR_LIT:message>' ] = _ ( '<STR_LIT>' ) <EOL> return HttpResponse ( json . dumps ( status ) , mimetype = '<STR_LIT:application/json>' ) <EOL> @ unique_session_or_json_redirect <EOL> def get_statistics ( request , planid ) : <EOL> note_session_activity ( request ) <EOL> status = { '<STR_LIT:success>' : False } <EOL> try : <EOL> plan = Plan . objects . get ( pk = planid ) <EOL> except : <EOL> status [ '<STR_LIT:message>' ] = _ ( <EOL> "<STR_LIT>" ) <EOL> return HttpResponse ( json . dumps ( status ) , mimetype = '<STR_LIT:application/json>' , status = <NUM_LIT> ) <EOL> if '<STR_LIT:version>' in request . REQUEST : <EOL> try : <EOL> version = int ( request . REQUEST [ '<STR_LIT:version>' ] ) <EOL> except : <EOL> version = plan . version <EOL> else : <EOL> version = plan . version <EOL> try : <EOL> display = ScoreDisplay . objects . get ( legislative_body = plan . legislative_body , name = "<STR_LIT>" % plan . legislative_body . name ) <EOL> except : <EOL> status [ '<STR_LIT:message>' ] = _ ( '<STR_LIT>' ) <EOL> status [ '<STR_LIT>' ] = traceback . format_exc ( ) <EOL> if '<STR_LIT>' in request . REQUEST : <EOL> try : <EOL> display = ScoreDisplay . objects . get ( pk = request . POST [ '<STR_LIT>' ] ) <EOL> except : <EOL> status [ '<STR_LIT:message>' ] = _ ( '<STR_LIT>' ) <EOL> status [ '<STR_LIT>' ] = traceback . format_exc ( ) <EOL> else : <EOL> logger . warn ( '<STR_LIT>' ) <EOL> logger . warn ( str ( request . POST ) ) <EOL> try : <EOL> html = display . render ( plan , request , version = version ) <EOL> return HttpResponse ( html , mimetype = '<STR_LIT>' ) <EOL> except Exception , ex : <EOL> status [ '<STR_LIT:message>' ] = _ ( "<STR_LIT>" ) <EOL> status [ '<STR_LIT>' ] = traceback . format_exc ( ) <EOL> logger . warn ( "<STR_LIT>" ) <EOL> logger . debug ( '<STR_LIT>' , ex ) <EOL> return HttpResponse ( json . dumps ( status ) , mimetype = '<STR_LIT:application/json>' , status = <NUM_LIT> ) <EOL> def getutc ( t ) : <EOL> """<STR_LIT>""" <EOL> t_tuple = t . timetuple ( ) <EOL> t_seconds = time . mktime ( t_tuple ) <EOL> return t . utcfromtimestamp ( t_seconds ) <EOL> @ unique_session_or_json_redirect <EOL> def getdistrictfilestatus ( request , planid ) : <EOL> """<STR_LIT>""" <EOL> note_session_activity ( request ) <EOL> status = { '<STR_LIT:success>' : False } <EOL> plan = Plan . objects . get ( pk = planid ) <EOL> if not can_copy ( request . user , plan ) : <EOL> return HttpResponseForbidden ( ) <EOL> try : <EOL> is_shape = '<STR_LIT:type>' in request . REQUEST and request . REQUEST [ '<STR_LIT:type>' ] == '<STR_LIT>' <EOL> file_status = DistrictFile . get_file_status ( plan , shape = is_shape ) <EOL> status [ '<STR_LIT:success>' ] = True <EOL> status [ '<STR_LIT:status>' ] = file_status <EOL> except Exception as ex : <EOL> status [ '<STR_LIT:message>' ] = _ ( '<STR_LIT>' ) <EOL> status [ '<STR_LIT>' ] = ex <EOL> return HttpResponse ( json . dumps ( status ) , mimetype = '<STR_LIT:application/json>' ) <EOL> @ unique_session_or_json_redirect <EOL> def getdistrictfile ( request , planid ) : <EOL> """<STR_LIT>""" <EOL> note_session_activity ( request ) <EOL> plan = Plan . objects . get ( pk = planid ) <EOL> if not can_copy ( request . user , plan ) : <EOL> return HttpResponseForbidden ( ) <EOL> is_shape = '<STR_LIT:type>' in request . REQUEST and request . REQUEST [ '<STR_LIT:type>' ] == '<STR_LIT>' <EOL> file_status = DistrictFile . get_file_status ( plan , shape = is_shape ) <EOL> if file_status == '<STR_LIT>' : <EOL> if is_shape : <EOL> archive = DistrictShapeFile . plan2shape ( plan ) <EOL> else : <EOL> archive = DistrictIndexFile . plan2index ( plan ) <EOL> response = HttpResponse ( open ( archive . name ) . read ( ) , content_type = '<STR_LIT>' ) <EOL> response [ '<STR_LIT>' ] = '<STR_LIT>' % plan . get_friendly_name ( ) <EOL> else : <EOL> if is_shape : <EOL> DistrictShapeFile . plan2shape . delay ( plan ) <EOL> else : <EOL> DistrictIndexFile . plan2index . delay ( plan ) <EOL> response = HttpResponse ( _ ( '<STR_LIT>' <EOL> '<STR_LIT>' ) ) <EOL> return response <EOL> @ unique_session_or_json_redirect <EOL> def emaildistrictindexfile ( request , planid ) : <EOL> """<STR_LIT>""" <EOL> note_session_activity ( request ) <EOL> if request . method != '<STR_LIT:POST>' : <EOL> return HttpResponseForbidden ( ) <EOL> plan = Plan . objects . get ( pk = planid ) <EOL> if not can_copy ( request . user , plan ) : <EOL> return HttpResponseForbidden ( ) <EOL> DistrictIndexFile . emailfile . delay ( plan , request . user , request . POST , translation . get_language ( ) ) <EOL> return HttpResponse ( json . dumps ( { <EOL> '<STR_LIT:success>' : True , <EOL> '<STR_LIT:message>' : _ ( '<STR_LIT>' ) } ) , <EOL> mimetype = '<STR_LIT:application/json>' ) <EOL> def getvalidplans ( leg_body , owner = None ) : <EOL> """<STR_LIT>""" <EOL> pfilter = Q ( legislative_body = leg_body ) & Q ( is_valid = True ) <EOL> if owner is not None : <EOL> pfilter = pfilter & Q ( owner = owner ) <EOL> return list ( Plan . objects . filter ( pfilter ) ) <EOL> def getleaderboarddisplay ( leg_body , owner_filter ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> return ScoreDisplay . objects . get ( name = "<STR_LIT>" % ( leg_body . name , owner_filter ) ) <EOL> except : <EOL> return None <EOL> def getleaderboard ( request ) : <EOL> """<STR_LIT>""" <EOL> note_session_activity ( request ) <EOL> if not using_unique_session ( request . user ) : <EOL> return HttpResponseForbidden ( ) <EOL> owner_filter = request . REQUEST [ '<STR_LIT>' ] <EOL> body_pk = int ( request . REQUEST [ '<STR_LIT>' ] ) ; <EOL> leg_body = LegislativeBody . objects . get ( pk = body_pk ) <EOL> display = getleaderboarddisplay ( leg_body , owner_filter ) <EOL> if display is None : <EOL> return HttpResponse ( _ ( '<STR_LIT>' ) , mimetype = '<STR_LIT>' ) <EOL> plans = getvalidplans ( leg_body , request . user if owner_filter == '<STR_LIT>' else None ) <EOL> try : <EOL> html = display . render ( plans , request ) <EOL> return HttpResponse ( html , mimetype = '<STR_LIT>' ) <EOL> except Exception , ex : <EOL> logger . warn ( '<STR_LIT>' ) <EOL> logger . debug ( '<STR_LIT>' , ex ) <EOL> return HttpResponse ( str ( ex ) , mimetype = '<STR_LIT>' ) <EOL> def getleaderboardcsv ( request ) : <EOL> """<STR_LIT>""" <EOL> note_session_activity ( request ) <EOL> if not using_unique_session ( request . user ) : <EOL> return HttpResponseForbidden ( ) <EOL> owner_filter = request . REQUEST [ '<STR_LIT>' ] <EOL> body_pk = int ( request . REQUEST [ '<STR_LIT>' ] ) ; <EOL> leg_body = LegislativeBody . objects . get ( pk = body_pk ) <EOL> plans = getvalidplans ( leg_body , request . user if owner_filter == '<STR_LIT>' else None ) <EOL> display = getleaderboarddisplay ( leg_body , owner_filter ) <EOL> plans = getvalidplans ( leg_body , request . user if owner_filter == '<STR_LIT>' else None ) <EOL> panels = display . scorepanel_set . all ( ) . order_by ( '<STR_LIT>' ) <EOL> try : <EOL> response = HttpResponse ( mimetype = '<STR_LIT>' ) <EOL> response [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> writer = csv . writer ( response ) <EOL> writer . writerow ( [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] + [ p . __unicode__ ( ) for p in panels ] ) <EOL> for plan in plans : <EOL> row = [ plan . id , plan . name , plan . owner . username ] <EOL> for panel in panels : <EOL> function = panel . score_functions . all ( ) [ <NUM_LIT:0> ] <EOL> score = ComputedPlanScore . compute ( function , plan ) <EOL> row . append ( score [ '<STR_LIT:value>' ] ) <EOL> writer . writerow ( row ) <EOL> return response <EOL> except Exception , ex : <EOL> logger . warn ( "<STR_LIT>" ) <EOL> logger . debug ( '<STR_LIT>' , ex ) <EOL> return HttpResponse ( str ( ex ) , mimetype = '<STR_LIT>' ) <EOL> def getplans ( request ) : <EOL> """<STR_LIT>""" <EOL> note_session_activity ( request ) <EOL> if not using_unique_session ( request . user ) : <EOL> return HttpResponseForbidden ( ) <EOL> if request . method == '<STR_LIT:POST>' : <EOL> page = int ( request . POST . get ( '<STR_LIT>' , <NUM_LIT:1> ) ) <EOL> rows = int ( request . POST . get ( '<STR_LIT>' , <NUM_LIT:10> ) ) <EOL> sidx = request . POST . get ( '<STR_LIT>' , '<STR_LIT:id>' ) <EOL> sord = request . POST . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> owner_filter = request . POST . get ( '<STR_LIT>' ) ; <EOL> body_pk = request . POST . get ( '<STR_LIT>' ) ; <EOL> body_pk = int ( body_pk ) if body_pk else body_pk ; <EOL> search = request . POST . get ( '<STR_LIT>' , False ) ; <EOL> search_string = request . POST . get ( '<STR_LIT>' , '<STR_LIT>' ) ; <EOL> is_community = request . POST . get ( '<STR_LIT>' , False ) == '<STR_LIT:true>' ; <EOL> else : <EOL> return HttpResponseForbidden ( ) <EOL> end = page * rows <EOL> start = end - rows <EOL> if owner_filter == '<STR_LIT>' : <EOL> available = Q ( is_template = True ) <EOL> elif owner_filter == '<STR_LIT>' : <EOL> available = Q ( is_shared = True ) <EOL> elif owner_filter == '<STR_LIT>' : <EOL> if request . user . is_anonymous ( ) : <EOL> return HttpResponseForbidden ( ) <EOL> else : <EOL> available = Q ( owner__exact = request . user ) <EOL> elif owner_filter == '<STR_LIT>' : <EOL> available = Q ( is_template = True ) | Q ( is_shared = True ) <EOL> if not request . user . is_anonymous ( ) : <EOL> available = available | Q ( owner__exact = request . user ) <EOL> else : <EOL> return HttpResponseBadRequest ( _ ( "<STR_LIT>" ) ) <EOL> not_creating = ~ Q ( processing_state = ProcessingState . CREATING ) & ~ Q ( processing_state = ProcessingState . UNKNOWN ) <EOL> if sidx . startswith ( '<STR_LIT>' ) : <EOL> sidx = sidx [ len ( '<STR_LIT>' ) : ] <EOL> if sidx == '<STR_LIT>' : <EOL> sidx = '<STR_LIT>' <EOL> if sidx == '<STR_LIT>' : <EOL> sidx = '<STR_LIT>' <EOL> if sord == '<STR_LIT>' : <EOL> sidx = '<STR_LIT:->' + sidx <EOL> if search : <EOL> search_filter = Q ( name__icontains = search_string ) | Q ( description__icontains = search_string ) | Q ( owner__username__icontains = search_string ) <EOL> else : <EOL> search_filter = None <EOL> if body_pk : <EOL> body_filter = Q ( legislative_body = body_pk ) <EOL> all_plans = Plan . objects . filter ( available , not_creating , body_filter , search_filter ) . order_by ( sidx ) <EOL> else : <EOL> community_filter = Q ( legislative_body__is_community = is_community ) <EOL> all_plans = Plan . objects . filter ( available , not_creating , search_filter , community_filter ) . order_by ( sidx ) <EOL> if all_plans . count ( ) > <NUM_LIT:0> : <EOL> total_pages = math . ceil ( all_plans . count ( ) / float ( rows ) ) <EOL> else : <EOL> total_pages = <NUM_LIT:1> <EOL> plans = all_plans [ start : end ] <EOL> plans_list = list ( ) <EOL> for plan in plans : <EOL> plans_list . append ( { <EOL> '<STR_LIT>' : plan . id , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT:name>' : plan . name , <EOL> '<STR_LIT:description>' : plan . description , <EOL> '<STR_LIT>' : time . mktime ( plan . edited . timetuple ( ) ) , <EOL> '<STR_LIT>' : plan . is_template , <EOL> '<STR_LIT>' : plan . is_shared , <EOL> '<STR_LIT>' : plan . owner . username , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : can_edit ( request . user , plan ) , <EOL> '<STR_LIT>' : plan . legislative_body . get_long_description ( ) , <EOL> '<STR_LIT>' : plan . get_processing_state_display ( ) <EOL> } <EOL> } ) <EOL> json_response = "<STR_LIT>" % ( total_pages , page , len ( all_plans ) , json . dumps ( plans_list ) ) <EOL> return HttpResponse ( json_response , mimetype = '<STR_LIT:application/json>' ) <EOL> def get_shared_districts ( request , planid ) : <EOL> """<STR_LIT>""" <EOL> note_session_activity ( request ) <EOL> if not using_unique_session ( request . user ) : <EOL> return HttpResponseForbidden ( ) <EOL> if request . method == '<STR_LIT:POST>' : <EOL> page = int ( request . POST . get ( '<STR_LIT>' , <NUM_LIT:1> ) ) <EOL> rows = int ( request . POST . get ( '<STR_LIT>' , <NUM_LIT:10> ) ) <EOL> else : <EOL> return HttpResponseForbidden ( ) <EOL> end = page * rows <EOL> start = end - rows <EOL> try : <EOL> plan = Plan . objects . get ( pk = planid ) <EOL> if not can_copy ( request . user , plan ) : <EOL> return HttpResponseForbidden ( ) <EOL> all_districts = plan . get_districts_at_version ( plan . version , include_geom = False ) <EOL> except : <EOL> plan = None <EOL> all_districts = ( ) <EOL> if len ( all_districts ) > <NUM_LIT:0> : <EOL> total_pages = math . ceil ( len ( all_districts ) / float ( rows ) ) <EOL> else : <EOL> total_pages = <NUM_LIT:1> <EOL> districts = all_districts [ start : end ] <EOL> districts_list = list ( ) <EOL> for district in districts : <EOL> if not district . is_unassigned : <EOL> districts_list . append ( { <EOL> '<STR_LIT>' : district . id , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : district . short_label , <EOL> '<STR_LIT>' : district . long_label , <EOL> '<STR_LIT>' : district . district_id , <EOL> } <EOL> } ) <EOL> json_response = "<STR_LIT>" % ( total_pages , page , len ( all_districts ) , json . dumps ( districts_list ) ) <EOL> return HttpResponse ( json_response , mimetype = '<STR_LIT:application/json>' ) <EOL> @ login_required <EOL> @ unique_session_or_json_redirect <EOL> def editplanattributes ( request , planid ) : <EOL> """<STR_LIT>""" <EOL> note_session_activity ( request ) <EOL> status = { '<STR_LIT:success>' : False } <EOL> if request . method != '<STR_LIT:POST>' : <EOL> return HttpResponseNotAllowed ( [ '<STR_LIT:POST>' ] ) <EOL> new_name = request . POST . get ( '<STR_LIT:name>' , None ) <EOL> new_description = request . POST . get ( '<STR_LIT:description>' , '<STR_LIT>' ) <EOL> if not planid or not ( new_name or new_description ) : <EOL> return HttpResponseBadRequest ( <EOL> _ ( '<STR_LIT>' ) ) <EOL> plan = Plan . objects . filter ( pk = planid , owner = request . user ) <EOL> if plan . count ( ) == <NUM_LIT:1> : <EOL> plan = plan [ <NUM_LIT:0> ] <EOL> if not new_name is None : <EOL> plan . name = new_name <EOL> plan . description = new_description <EOL> try : <EOL> plan . save ( ) <EOL> status [ '<STR_LIT:success>' ] = True <EOL> status [ '<STR_LIT:message>' ] = _ ( '<STR_LIT>' ) <EOL> except Exception , ex : <EOL> status [ '<STR_LIT:message>' ] = _ ( '<STR_LIT>' ) <EOL> status [ '<STR_LIT>' ] = ex <EOL> logger . warn ( '<STR_LIT>' ) <EOL> logger . debug ( '<STR_LIT>' , ex ) <EOL> else : <EOL> status [ '<STR_LIT:message>' ] = _ ( "<STR_LIT>" ) <EOL> return HttpResponse ( json . dumps ( status ) , mimetype = '<STR_LIT:application/json>' ) <EOL> @ login_required <EOL> @ unique_session_or_json_redirect <EOL> def deleteplan ( request , planid ) : <EOL> """<STR_LIT>""" <EOL> note_session_activity ( request ) <EOL> status = { '<STR_LIT:success>' : False } <EOL> if request . method != '<STR_LIT:POST>' : <EOL> return HttpResponseNotAllowed ( [ '<STR_LIT:POST>' ] ) <EOL> if not planid : <EOL> return HttpResponseBadRequest ( _ ( '<STR_LIT>' ) ) <EOL> plan = Plan . objects . filter ( pk = planid , owner = request . user ) <EOL> if plan . count ( ) == <NUM_LIT:1> : <EOL> plan = plan [ <NUM_LIT:0> ] <EOL> try : <EOL> plan . delete ( ) <EOL> status [ '<STR_LIT:success>' ] = True <EOL> status [ '<STR_LIT:message>' ] = _ ( '<STR_LIT>' ) <EOL> except Exception , ex : <EOL> status [ '<STR_LIT:message>' ] = _ ( '<STR_LIT>' ) <EOL> status [ '<STR_LIT>' ] = ex <EOL> logger . warn ( '<STR_LIT>' ) <EOL> logger . debug ( '<STR_LIT>' , ex ) <EOL> else : <EOL> status [ '<STR_LIT:message>' ] = _ ( "<STR_LIT>" ) <EOL> return HttpResponse ( json . dumps ( status ) , mimetype = '<STR_LIT:application/json>' ) <EOL> @ login_required <EOL> @ unique_session_or_json_redirect <EOL> def reaggregateplan ( request , planid ) : <EOL> """<STR_LIT>""" <EOL> note_session_activity ( request ) <EOL> status = { '<STR_LIT:success>' : False } <EOL> if request . method != '<STR_LIT:POST>' : <EOL> return HttpResponseNotAllowed ( [ '<STR_LIT:POST>' ] ) <EOL> if not planid : <EOL> return HttpResponseBadRequest ( _ ( '<STR_LIT>' ) ) <EOL> plan = Plan . objects . filter ( pk = planid , owner = request . user ) <EOL> if plan . count ( ) == <NUM_LIT:1> : <EOL> plan = plan [ <NUM_LIT:0> ] <EOL> try : <EOL> reaggregate_plan . delay ( plan . id ) <EOL> plan . processing_state = ProcessingState . REAGGREGATING <EOL> plan . save ( ) <EOL> status [ '<STR_LIT:success>' ] = True <EOL> status [ '<STR_LIT:message>' ] = _ ( '<STR_LIT>' ) <EOL> except Exception , ex : <EOL> status [ '<STR_LIT:message>' ] = _ ( '<STR_LIT>' ) <EOL> status [ '<STR_LIT>' ] = ex <EOL> logger . warn ( '<STR_LIT>' ) <EOL> logger . debug ( '<STR_LIT>' , ex ) <EOL> else : <EOL> status [ '<STR_LIT:message>' ] = _ ( "<STR_LIT>" ) <EOL> return HttpResponse ( json . dumps ( status ) , mimetype = '<STR_LIT:application/json>' ) <EOL> def get_health ( request ) : <EOL> def num_users ( minutes ) : <EOL> users = <NUM_LIT:0> <EOL> for session in Session . objects . all ( ) : <EOL> try : <EOL> decoded = session . get_decoded ( ) <EOL> except : <EOL> session . delete ( ) <EOL> continue <EOL> if '<STR_LIT>' in decoded : <EOL> activity_delta = decoded [ '<STR_LIT>' ] - timedelta ( <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> , settings . SESSION_TIMEOUT ) <EOL> if activity_delta > ( datetime . now ( ) - timedelta ( <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> , minutes ) ) : <EOL> users += <NUM_LIT:1> <EOL> return users <EOL> try : <EOL> result = _ ( '<STR_LIT>' ) % { '<STR_LIT:time>' : datetime . now ( ) } <EOL> result += _ ( '<STR_LIT>' ) % { '<STR_LIT>' : Plan . objects . all ( ) . count ( ) } <EOL> result += _ ( '<STR_LIT>' ) % { '<STR_LIT>' : Session . objects . all ( ) . count ( ) , <EOL> '<STR_LIT>' : settings . CONCURRENT_SESSIONS } <EOL> result += _ ( '<STR_LIT>' ) % { '<STR_LIT>' : num_users ( <NUM_LIT:10> ) } <EOL> space = os . statvfs ( '<STR_LIT>' ) <EOL> result += _ ( '<STR_LIT>' ) % { '<STR_LIT>' : ( ( space . f_bsize * space . f_bavail ) / ( <NUM_LIT> * <NUM_LIT> ) ) } <EOL> result += _ ( '<STR_LIT>' ) % { '<STR_LIT>' : commands . getoutput ( '<STR_LIT>' ) } <EOL> return HttpResponse ( result , mimetype = '<STR_LIT>' ) <EOL> except : <EOL> return HttpResponse ( _ ( "<STR_LIT>" ) % traceback . format_exc ( ) ) <EOL> def statistics_sets ( request , planid ) : <EOL> result = { '<STR_LIT:success>' : False } <EOL> plan = Plan . objects . filter ( id = planid ) <EOL> if plan . count ( ) == <NUM_LIT:0> : <EOL> result [ '<STR_LIT:message>' ] = _ ( '<STR_LIT>' ) <EOL> return HttpResponse ( json . dumps ( result ) , mimetype = '<STR_LIT:application/json>' ) <EOL> else : <EOL> plan = plan [ <NUM_LIT:0> ] <EOL> if request . method == '<STR_LIT:GET>' : <EOL> sets = [ ] <EOL> scorefunctions = [ ] <EOL> user_functions = ScoreFunction . objects . filter ( selectable_bodies = plan . legislative_body ) . order_by ( '<STR_LIT:name>' ) <EOL> for f in user_functions : <EOL> if '<STR_LIT>' not in f . name . lower ( ) and '<STR_LIT>' not in f . name . lower ( ) : <EOL> scorefunctions . append ( { '<STR_LIT:id>' : f . id , '<STR_LIT:name>' : force_escape ( f . get_label ( ) ) } ) <EOL> result [ '<STR_LIT>' ] = scorefunctions <EOL> admin_display_names = [ <EOL> "<STR_LIT>" % plan . legislative_body . name , <EOL> ] <EOL> if plan . legislative_body . is_community : <EOL> admin_display_names . append ( "<STR_LIT>" % <EOL> plan . legislative_body . name ) <EOL> else : <EOL> admin_display_names . append ( "<STR_LIT>" % <EOL> plan . legislative_body . name ) <EOL> admin_displays = ScoreDisplay . objects . filter ( <EOL> owner__is_superuser = True , <EOL> legislative_body = plan . legislative_body , <EOL> name__in = admin_display_names <EOL> ) <EOL> for admin_display in admin_displays : <EOL> sets . append ( { <EOL> '<STR_LIT:id>' : admin_display . id , <EOL> '<STR_LIT:name>' : force_escape ( admin_display . get_label ( ) ) , <EOL> '<STR_LIT>' : [ ] , <EOL> '<STR_LIT>' : False <EOL> } ) <EOL> try : <EOL> user_displays = ScoreDisplay . objects . filter ( <EOL> owner = request . user , <EOL> legislative_body = plan . legislative_body , <EOL> is_page = False ) . order_by ( '<STR_LIT:title>' ) <EOL> result [ '<STR_LIT>' ] = len ( user_displays ) <EOL> for display in user_displays : <EOL> functions = [ ] <EOL> for panel in display . scorepanel_set . all ( ) : <EOL> if panel . type == '<STR_LIT>' : <EOL> functions = map ( lambda x : x . id , panel . score_functions . all ( ) ) <EOL> if len ( functions ) == <NUM_LIT:0> : <EOL> result [ '<STR_LIT:message>' ] = _ ( "<STR_LIT>" ) % { '<STR_LIT>' : panel } <EOL> sets . append ( { '<STR_LIT:id>' : display . id , '<STR_LIT:name>' : force_escape ( display . __unicode__ ( ) ) , '<STR_LIT>' : functions , '<STR_LIT>' : display . owner == request . user } ) <EOL> except Exception , ex : <EOL> result [ '<STR_LIT:message>' ] = _ ( '<STR_LIT>' ) % { '<STR_LIT:user>' : request . user } <EOL> logger . warn ( '<STR_LIT>' ) <EOL> logger . debug ( '<STR_LIT>' , ex ) <EOL> result [ '<STR_LIT>' ] = sets <EOL> result [ '<STR_LIT:success>' ] = True <EOL> elif request . method == '<STR_LIT:POST>' and '<STR_LIT>' in request . POST : <EOL> try : <EOL> display = ScoreDisplay . objects . get ( pk = request . REQUEST . get ( '<STR_LIT:id>' , - <NUM_LIT:1> ) ) <EOL> result [ '<STR_LIT>' ] = { '<STR_LIT:name>' : force_escape ( display . __unicode__ ( ) ) , '<STR_LIT:id>' : display . id } <EOL> qset = display . scorepanel_set . all ( ) <EOL> for panel in qset : <EOL> if panel . displays . count ( ) == <NUM_LIT:1> : <EOL> panel . delete ( ) <EOL> display . delete ( ) <EOL> result [ '<STR_LIT:success>' ] = True <EOL> except Exception , ex : <EOL> result [ '<STR_LIT:message>' ] = _ ( "<STR_LIT>" ) <EOL> result [ '<STR_LIT>' ] = traceback . format_exc ( ) <EOL> logger . warn ( "<STR_LIT>" ) <EOL> logger . debug ( '<STR_LIT>' , ex ) <EOL> elif request . method == '<STR_LIT:POST>' : <EOL> def validate_num ( user , limit = <NUM_LIT:3> ) : <EOL> return ScoreDisplay . objects . filter ( owner = user , legislative_body = plan . legislative_body , is_page = False ) . count ( ) < limit <EOL> if '<STR_LIT>' in request . POST : <EOL> functions = request . POST . getlist ( '<STR_LIT>' ) <EOL> functions = map ( lambda x : int ( x ) , functions ) <EOL> try : <EOL> display = ScoreDisplay . objects . get ( title = request . POST . get ( '<STR_LIT:name>' ) , owner = request . user ) <EOL> display = display . copy_from ( display = display , functions = functions ) <EOL> except : <EOL> limit = <NUM_LIT:3> <EOL> if validate_num ( request . user , limit ) : <EOL> demo = ScoreDisplay . objects . filter ( <EOL> owner__is_superuser = True , <EOL> legislative_body = plan . legislative_body , <EOL> is_page = False , <EOL> title = "<STR_LIT>" <EOL> ) <EOL> for disp in demo : <EOL> has_comments = False <EOL> for pnl in disp . scorepanel_set . all ( ) : <EOL> for fn in pnl . score_functions . all ( ) : <EOL> has_comments = has_comments or fn . calculator . endswith ( '<STR_LIT>' ) <EOL> if not has_comments : <EOL> demo = disp <EOL> break <EOL> display = ScoreDisplay ( ) <EOL> display = display . copy_from ( display = demo , title = request . POST . get ( '<STR_LIT:name>' ) , owner = request . user , functions = functions ) <EOL> result [ '<STR_LIT>' ] = True <EOL> else : <EOL> result [ '<STR_LIT:message>' ] = _ ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) % { '<STR_LIT>' : limit } <EOL> result [ '<STR_LIT:error>' ] = '<STR_LIT>' <EOL> return HttpResponse ( json . dumps ( result ) , mimetype = '<STR_LIT:application/json>' ) <EOL> result [ '<STR_LIT>' ] = { '<STR_LIT:name>' : force_escape ( display . __unicode__ ( ) ) , '<STR_LIT:id>' : display . id , '<STR_LIT>' : functions , '<STR_LIT>' : display . owner == request . user } <EOL> result [ '<STR_LIT:success>' ] = True <EOL> else : <EOL> result [ '<STR_LIT:message>' ] = _ ( "<STR_LIT>" ) <EOL> return HttpResponse ( json . dumps ( result ) , mimetype = '<STR_LIT:application/json>' ) <EOL> def purge_plan_clear_cache ( district , version ) : <EOL> """<STR_LIT>""" <EOL> district . plan . purge ( after = version ) <EOL> district . plan . version = version <EOL> district . plan . save ( ) <EOL> cache = district . computeddistrictscore_set . filter ( function__calculator__endswith = '<STR_LIT>' ) <EOL> cache . delete ( ) <EOL> @ unique_session_or_json_redirect <EOL> def district_info ( request , planid , district_id ) : <EOL> """<STR_LIT>""" <EOL> status = { '<STR_LIT:success>' : False } <EOL> plan = Plan . objects . filter ( id = planid ) <EOL> if plan . count ( ) == <NUM_LIT:0> : <EOL> status [ '<STR_LIT:message>' ] = _ ( '<STR_LIT>' ) <EOL> else : <EOL> plan = plan [ <NUM_LIT:0> ] <EOL> version = plan . version <EOL> if '<STR_LIT:version>' in request . REQUEST : <EOL> try : <EOL> version = int ( request . REQUEST [ '<STR_LIT:version>' ] ) <EOL> version = min ( plan . version , int ( version ) ) <EOL> except : <EOL> pass <EOL> district_id = int ( district_id ) <EOL> district = plan . get_districts_at_version ( version , include_geom = False ) <EOL> district = filter ( lambda d : d . district_id == district_id , district ) <EOL> if request . method == '<STR_LIT:POST>' : <EOL> district = plan . district_set . get ( id = request . POST [ '<STR_LIT>' ] ) <EOL> district . short_label = request . POST [ '<STR_LIT>' ] [ <NUM_LIT:0> : <NUM_LIT:10> ] <EOL> district . long_label = request . POST [ '<STR_LIT>' ] [ <NUM_LIT:0> : <NUM_LIT> ] <EOL> if district . version < version : <EOL> district_copy = copy . copy ( district ) <EOL> district_copy . id = None <EOL> district_copy . version = version <EOL> district_copy . save ( ) <EOL> district_copy . clone_relations_from ( district ) <EOL> district = district_copy <EOL> else : <EOL> district . save ( ) <EOL> has_comment = '<STR_LIT>' in request . POST and request . POST [ '<STR_LIT>' ] != '<STR_LIT>' <EOL> if has_comment : <EOL> ct = ContentType . objects . get ( app_label = '<STR_LIT>' , model = '<STR_LIT>' ) <EOL> Comment . objects . filter ( object_pk = district . id , content_type = ct ) . delete ( ) <EOL> comment = Comment ( <EOL> object_pk = district . id , <EOL> content_type = ct , <EOL> site_id = Site . objects . get_current ( ) . id , <EOL> user_name = request . user . username , <EOL> user_email = request . user . email , <EOL> comment = request . POST [ '<STR_LIT>' ] ) <EOL> comment . save ( ) <EOL> else : <EOL> district . save ( ) <EOL> tset = Tag . objects . get_for_object ( district ) . filter ( name__startswith = '<STR_LIT:type>' ) <EOL> TaggedItem . objects . filter ( tag__in = tset , object_id = district . id ) . delete ( ) <EOL> purge_plan_clear_cache ( district , version ) <EOL> if len ( request . REQUEST . getlist ( '<STR_LIT>' ) ) > <NUM_LIT:0> : <EOL> strtags = request . REQUEST . getlist ( '<STR_LIT>' ) <EOL> for strtag in strtags : <EOL> if strtag == '<STR_LIT>' : <EOL> continue <EOL> if strtag . count ( '<STR_LIT:U+0020>' ) > <NUM_LIT:0> : <EOL> strtag = '<STR_LIT>' % strtag <EOL> else : <EOL> strtag = '<STR_LIT>' % strtag <EOL> Tag . objects . add_tag ( district , strtag ) <EOL> status [ '<STR_LIT:version>' ] = version <EOL> status [ '<STR_LIT:success>' ] = True <EOL> return HttpResponse ( json . dumps ( status ) , mimetype = '<STR_LIT:application/json>' ) <EOL> def plan_feed ( request ) : <EOL> feed = loader . get_template ( '<STR_LIT>' ) <EOL> plans = Plan . objects . all ( ) . order_by ( '<STR_LIT>' ) [ <NUM_LIT:0> : <NUM_LIT:10> ] <EOL> geolevel = plans [ <NUM_LIT:0> ] . legislative_body . get_geolevels ( ) [ <NUM_LIT:0> ] <EOL> extent = geolevel . geounit_set . collect ( ) . extent <EOL> if extent [ <NUM_LIT:2> ] - extent [ <NUM_LIT:0> ] > extent [ <NUM_LIT:3> ] - extent [ <NUM_LIT:1> ] : <EOL> width = <NUM_LIT> <EOL> height = int ( <NUM_LIT> * ( extent [ <NUM_LIT:3> ] - extent [ <NUM_LIT:1> ] ) / ( extent [ <NUM_LIT:2> ] - extent [ <NUM_LIT:0> ] ) ) <EOL> else : <EOL> width = int ( <NUM_LIT> * ( extent [ <NUM_LIT:2> ] - extent [ <NUM_LIT:0> ] ) / ( extent [ <NUM_LIT:3> ] - extent [ <NUM_LIT:1> ] ) ) <EOL> height = <NUM_LIT> <EOL> mapserver = settings . MAP_SERVER if settings . MAP_SERVER != '<STR_LIT>' else request . META [ '<STR_LIT>' ] <EOL> context = { <EOL> '<STR_LIT>' : plans , <EOL> '<STR_LIT>' : mapserver , <EOL> '<STR_LIT>' : settings . MAP_SERVER_NS , <EOL> '<STR_LIT>' : extent , <EOL> '<STR_LIT:width>' : width , <EOL> '<STR_LIT>' : height <EOL> } <EOL> xml = feed . render ( DjangoContext ( context ) ) <EOL> return HttpResponse ( xml , mimetype = '<STR_LIT>' ) <EOL> def share_feed ( request ) : <EOL> feed = loader . get_template ( '<STR_LIT>' ) <EOL> plans = Plan . objects . filter ( is_shared = True ) . order_by ( '<STR_LIT>' ) [ <NUM_LIT:0> : <NUM_LIT:10> ] <EOL> if plans . count ( ) < <NUM_LIT:0> : <EOL> geolevel = plans [ <NUM_LIT:0> ] . legislative_body . get_geolevels ( ) [ <NUM_LIT:0> ] <EOL> extent = geolevel . geounit_set . collect ( ) . extent <EOL> if extent [ <NUM_LIT:2> ] - extent [ <NUM_LIT:0> ] > extent [ <NUM_LIT:3> ] - extent [ <NUM_LIT:1> ] : <EOL> width = <NUM_LIT> <EOL> height = int ( <NUM_LIT> * ( extent [ <NUM_LIT:3> ] - extent [ <NUM_LIT:1> ] ) / ( extent [ <NUM_LIT:2> ] - extent [ <NUM_LIT:0> ] ) ) <EOL> else : <EOL> width = int ( <NUM_LIT> * ( extent [ <NUM_LIT:2> ] - extent [ <NUM_LIT:0> ] ) / ( extent [ <NUM_LIT:3> ] - extent [ <NUM_LIT:1> ] ) ) <EOL> height = <NUM_LIT> <EOL> else : <EOL> extent = ( <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> , ) <EOL> width = <NUM_LIT:1> <EOL> height = <NUM_LIT:1> <EOL> mapserver = settings . MAP_SERVER if settings . MAP_SERVER != '<STR_LIT>' else request . META [ '<STR_LIT>' ] <EOL> context = { <EOL> '<STR_LIT>' : plans , <EOL> '<STR_LIT>' : mapserver , <EOL> '<STR_LIT>' : settings . MAP_SERVER_NS , <EOL> '<STR_LIT>' : extent , <EOL> '<STR_LIT:width>' : width , <EOL> '<STR_LIT>' : height <EOL> } <EOL> xml = feed . render ( DjangoContext ( context ) ) <EOL> return HttpResponse ( xml , mimetype = '<STR_LIT>' ) </s>
