<s> import json <EOL> from django . contrib import messages <EOL> from django . http import HttpResponse , HttpResponseRedirect , Http404 <EOL> from django . http . response import HttpResponseServerError <EOL> from django . shortcuts import render <EOL> from django . utils . decorators import method_decorator <EOL> from django . utils . safestring import mark_safe <EOL> from django . utils . translation import ugettext as _ , ugettext_noop , ugettext_lazy <EOL> from django . views . decorators . http import require_POST , require_http_methods <EOL> from couchdbkit import ResourceNotFound <EOL> from corehq . apps . style . decorators import use_bootstrap3 , use_jquery_ui <EOL> from corehq . util . files import file_extention_from_filename <EOL> from couchexport . models import Format <EOL> from dimagi . utils . decorators . memoized import memoized <EOL> from dimagi . utils . web import json_response <EOL> from soil . exceptions import TaskFailedError <EOL> from soil . util import expose_cached_download , get_download_context <EOL> from corehq import toggles <EOL> from corehq . apps . commtrack . tasks import import_locations_async <EOL> from corehq . apps . commtrack . util import unicode_slug <EOL> from corehq . apps . consumption . shortcuts import get_default_monthly_consumption <EOL> from corehq . apps . custom_data_fields import CustomDataModelMixin <EOL> from corehq . apps . domain . decorators import domain_admin_required <EOL> from corehq . apps . domain . views import BaseDomainView <EOL> from corehq . apps . hqwebapp . utils import get_bulk_upload_form <EOL> from corehq . apps . products . models import Product , SQLProduct <EOL> from corehq . apps . users . forms import MultipleSelectionForm <EOL> from corehq . util import reverse , get_document_or_404 <EOL> from custom . openlmis . tasks import bootstrap_domain_task <EOL> from . analytics import users_have_locations <EOL> from . dbaccessors import get_users_assigned_to_locations <EOL> from . permissions import ( <EOL> locations_access_required , <EOL> is_locations_admin , <EOL> can_edit_location , <EOL> can_edit_location_types , <EOL> user_can_edit_any_location , <EOL> can_edit_any_location , <EOL> ) <EOL> from . models import Location , LocationType , SQLLocation <EOL> from . forms import LocationForm , UsersAtLocationForm <EOL> from . util import load_locs_json , location_hierarchy_config , dump_locations <EOL> @ locations_access_required <EOL> def default ( request , domain ) : <EOL> return HttpResponseRedirect ( reverse ( LocationsListView . urlname , args = [ domain ] ) ) <EOL> class BaseLocationView ( BaseDomainView ) : <EOL> @ method_decorator ( locations_access_required ) <EOL> def dispatch ( self , request , * args , ** kwargs ) : <EOL> return super ( BaseLocationView , self ) . dispatch ( request , * args , ** kwargs ) <EOL> @ property <EOL> def section_url ( self ) : <EOL> return reverse ( LocationsListView . urlname , args = [ self . domain ] ) <EOL> @ property <EOL> def main_context ( self ) : <EOL> context = super ( BaseLocationView , self ) . main_context <EOL> context . update ( { <EOL> '<STR_LIT>' : location_hierarchy_config ( self . domain ) , <EOL> '<STR_LIT>' : reverse ( '<STR_LIT>' , kwargs = { '<STR_LIT>' : self . domain , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' } ) , <EOL> } ) <EOL> return context <EOL> class LocationsListView ( BaseLocationView ) : <EOL> urlname = '<STR_LIT>' <EOL> page_title = ugettext_noop ( "<STR_LIT>" ) <EOL> template_name = '<STR_LIT>' <EOL> @ use_bootstrap3 <EOL> @ use_jquery_ui <EOL> def dispatch ( self , request , * args , ** kwargs ) : <EOL> return super ( LocationsListView , self ) . dispatch ( request , * args , ** kwargs ) <EOL> @ property <EOL> def show_inactive ( self ) : <EOL> return json . loads ( self . request . GET . get ( '<STR_LIT>' , '<STR_LIT:false>' ) ) <EOL> @ property <EOL> def page_context ( self ) : <EOL> selected_id = self . request . GET . get ( '<STR_LIT>' ) <EOL> has_location_types = len ( self . domain_object . location_types ) > <NUM_LIT:0> <EOL> loc_restricted = self . request . project . location_restriction_for_users <EOL> return { <EOL> '<STR_LIT>' : selected_id , <EOL> '<STR_LIT>' : load_locs_json ( self . domain , selected_id , self . show_inactive , self . request . couch_user ) , <EOL> '<STR_LIT>' : self . show_inactive , <EOL> '<STR_LIT>' : has_location_types , <EOL> '<STR_LIT>' : ( <EOL> not loc_restricted or ( loc_restricted and not self . request . couch_user . get_location ( self . domain ) ) ) , <EOL> '<STR_LIT>' : user_can_edit_any_location ( self . request . couch_user , self . request . project ) , <EOL> } <EOL> class LocationFieldsView ( CustomDataModelMixin , BaseLocationView ) : <EOL> urlname = '<STR_LIT>' <EOL> field_type = '<STR_LIT>' <EOL> entity_string = ugettext_lazy ( "<STR_LIT>" ) <EOL> template_name = "<STR_LIT>" <EOL> @ method_decorator ( is_locations_admin ) <EOL> @ use_bootstrap3 <EOL> @ use_jquery_ui <EOL> def dispatch ( self , request , * args , ** kwargs ) : <EOL> return super ( LocationFieldsView , self ) . dispatch ( request , * args , ** kwargs ) <EOL> class LocationTypesView ( BaseLocationView ) : <EOL> urlname = '<STR_LIT>' <EOL> page_title = ugettext_noop ( "<STR_LIT>" ) <EOL> template_name = '<STR_LIT>' <EOL> @ method_decorator ( can_edit_location_types ) <EOL> @ use_bootstrap3 <EOL> @ use_jquery_ui <EOL> def dispatch ( self , request , * args , ** kwargs ) : <EOL> return super ( LocationTypesView , self ) . dispatch ( request , * args , ** kwargs ) <EOL> @ property <EOL> def page_context ( self ) : <EOL> return { <EOL> '<STR_LIT>' : self . _get_loc_types ( ) , <EOL> '<STR_LIT>' : self . domain_object . commtrack_enabled , <EOL> } <EOL> def _get_loc_types ( self ) : <EOL> return [ { <EOL> '<STR_LIT>' : loc_type . pk , <EOL> '<STR_LIT:name>' : loc_type . name , <EOL> '<STR_LIT>' : ( loc_type . parent_type . pk <EOL> if loc_type . parent_type else None ) , <EOL> '<STR_LIT>' : loc_type . administrative , <EOL> '<STR_LIT>' : loc_type . shares_cases , <EOL> '<STR_LIT>' : loc_type . view_descendants , <EOL> '<STR_LIT:code>' : loc_type . code , <EOL> } for loc_type in LocationType . objects . by_domain ( self . domain ) ] <EOL> def post ( self , request , * args , ** kwargs ) : <EOL> payload = json . loads ( request . POST . get ( '<STR_LIT>' ) ) <EOL> sql_loc_types = { } <EOL> def _is_fake_pk ( pk ) : <EOL> return isinstance ( pk , basestring ) and pk . startswith ( "<STR_LIT>" ) <EOL> def mk_loctype ( name , parent_type , administrative , <EOL> shares_cases , view_descendants , pk , code ) : <EOL> parent = sql_loc_types [ parent_type ] if parent_type else None <EOL> loc_type = None <EOL> if not _is_fake_pk ( pk ) : <EOL> try : <EOL> loc_type = LocationType . objects . get ( domain = self . domain , pk = pk ) <EOL> except LocationType . DoesNotExist : <EOL> pass <EOL> if loc_type is None : <EOL> loc_type = LocationType ( domain = self . domain ) <EOL> loc_type . name = name <EOL> loc_type . administrative = administrative <EOL> loc_type . parent_type = parent <EOL> loc_type . shares_cases = shares_cases <EOL> loc_type . view_descendants = view_descendants <EOL> loc_type . code = unicode_slug ( code ) <EOL> loc_type . save ( ) <EOL> sql_loc_types [ pk ] = loc_type <EOL> loc_types = payload [ '<STR_LIT>' ] <EOL> pks = [ ] <EOL> for loc_type in loc_types : <EOL> for prop in [ '<STR_LIT:name>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] : <EOL> assert prop in loc_type , "<STR_LIT>" <EOL> pk = loc_type [ '<STR_LIT>' ] <EOL> if not _is_fake_pk ( pk ) : <EOL> pks . append ( loc_type [ '<STR_LIT>' ] ) <EOL> hierarchy = self . get_hierarchy ( loc_types ) <EOL> if not self . remove_old_location_types ( pks ) : <EOL> return self . get ( request , * args , ** kwargs ) <EOL> for loc_type in hierarchy : <EOL> mk_loctype ( ** loc_type ) <EOL> return HttpResponseRedirect ( reverse ( self . urlname , args = [ self . domain ] ) ) <EOL> def remove_old_location_types ( self , pks ) : <EOL> existing_pks = ( LocationType . objects . filter ( domain = self . domain ) <EOL> . values_list ( '<STR_LIT>' , flat = True ) ) <EOL> to_delete = [ ] <EOL> for pk in existing_pks : <EOL> if pk not in pks : <EOL> if ( SQLLocation . objects . filter ( domain = self . domain , <EOL> location_type = pk ) <EOL> . exists ( ) ) : <EOL> msg = _ ( "<STR_LIT>" ) <EOL> messages . warning ( self . request , msg ) <EOL> return False <EOL> to_delete . append ( pk ) <EOL> ( LocationType . objects . filter ( domain = self . domain , pk__in = to_delete ) <EOL> . delete ( ) ) <EOL> return True <EOL> def get_hierarchy ( self , loc_types ) : <EOL> """<STR_LIT>""" <EOL> lt_dict = { lt [ '<STR_LIT>' ] : lt for lt in loc_types } <EOL> for loc_type in loc_types : <EOL> visited = set ( ) <EOL> def step ( lt ) : <EOL> assert lt [ '<STR_LIT:name>' ] not in visited , "<STR_LIT>" <EOL> visited . add ( lt [ '<STR_LIT:name>' ] ) <EOL> if lt [ '<STR_LIT>' ] : <EOL> step ( lt_dict [ lt [ '<STR_LIT>' ] ] ) <EOL> step ( loc_type ) <EOL> hierarchy = { } <EOL> def insert_loc_type ( loc_type ) : <EOL> """<STR_LIT>""" <EOL> name = loc_type [ '<STR_LIT:name>' ] <EOL> parent = lt_dict . get ( loc_type [ '<STR_LIT>' ] , None ) <EOL> if not parent : <EOL> lt_hierarchy = hierarchy <EOL> else : <EOL> lt_hierarchy = insert_loc_type ( parent ) <EOL> if name not in lt_hierarchy : <EOL> lt_hierarchy [ name ] = ( loc_type , { } ) <EOL> return lt_hierarchy [ name ] [ <NUM_LIT:1> ] <EOL> for loc_type in loc_types : <EOL> insert_loc_type ( loc_type ) <EOL> ordered_loc_types = [ ] <EOL> def step_through_graph ( hierarchy ) : <EOL> for name , ( loc_type , children ) in hierarchy . items ( ) : <EOL> ordered_loc_types . append ( loc_type ) <EOL> step_through_graph ( children ) <EOL> step_through_graph ( hierarchy ) <EOL> return ordered_loc_types <EOL> class NewLocationView ( BaseLocationView ) : <EOL> urlname = '<STR_LIT>' <EOL> page_title = ugettext_noop ( "<STR_LIT>" ) <EOL> template_name = '<STR_LIT>' <EOL> creates_new_location = True <EOL> form_tab = '<STR_LIT>' <EOL> @ use_bootstrap3 <EOL> @ use_jquery_ui <EOL> def dispatch ( self , request , * args , ** kwargs ) : <EOL> return super ( NewLocationView , self ) . dispatch ( request , * args , ** kwargs ) <EOL> @ property <EOL> def parent_pages ( self ) : <EOL> selected = self . location . location_id or self . location . parent_id <EOL> breadcrumbs = [ { <EOL> '<STR_LIT:title>' : LocationsListView . page_title , <EOL> '<STR_LIT:url>' : reverse ( <EOL> LocationsListView . urlname , <EOL> args = [ self . domain ] , <EOL> params = { "<STR_LIT>" : selected } if selected else None , <EOL> ) <EOL> } ] <EOL> if self . location . parent : <EOL> sql_parent = self . location . parent . sql_location <EOL> for loc in sql_parent . get_ancestors ( include_self = True ) : <EOL> breadcrumbs . append ( { <EOL> '<STR_LIT:title>' : loc . name , <EOL> '<STR_LIT:url>' : reverse ( <EOL> EditLocationView . urlname , <EOL> args = [ self . domain , loc . location_id ] , <EOL> ) <EOL> } ) <EOL> return breadcrumbs <EOL> @ property <EOL> @ memoized <EOL> def location ( self ) : <EOL> parent_id = self . request . GET . get ( '<STR_LIT>' ) <EOL> parent = ( get_document_or_404 ( Location , self . domain , parent_id ) <EOL> if parent_id else None ) <EOL> return Location ( domain = self . domain , parent = parent ) <EOL> @ property <EOL> def consumption ( self ) : <EOL> return None <EOL> @ property <EOL> @ memoized <EOL> def location_form ( self ) : <EOL> data = self . request . POST if self . request . method == '<STR_LIT:POST>' else None <EOL> return LocationForm ( <EOL> self . location , <EOL> bound_data = data , <EOL> user = self . request . couch_user , <EOL> is_new = self . creates_new_location , <EOL> ) <EOL> @ property <EOL> def page_context ( self ) : <EOL> return { <EOL> '<STR_LIT>' : self . location_form , <EOL> '<STR_LIT:location>' : self . location , <EOL> '<STR_LIT>' : self . consumption , <EOL> '<STR_LIT>' : load_locs_json ( self . domain , self . location . parent_id , <EOL> user = self . request . couch_user ) , <EOL> '<STR_LIT>' : self . form_tab , <EOL> } <EOL> def form_valid ( self ) : <EOL> messages . success ( self . request , _ ( '<STR_LIT>' ) ) <EOL> return HttpResponseRedirect ( <EOL> reverse ( EditLocationView . urlname , <EOL> args = [ self . domain , self . location_form . location . location_id ] ) , <EOL> ) <EOL> def settings_form_post ( self , request , * args , ** kwargs ) : <EOL> if self . location_form . is_valid ( ) : <EOL> self . location_form . save ( ) <EOL> return self . form_valid ( ) <EOL> return self . get ( request , * args , ** kwargs ) <EOL> def post ( self , request , * args , ** kwargs ) : <EOL> return self . settings_form_post ( request , * args , ** kwargs ) <EOL> @ can_edit_location <EOL> def archive_location ( request , domain , loc_id ) : <EOL> loc = Location . get ( loc_id ) <EOL> if loc . domain != domain : <EOL> raise Http404 ( ) <EOL> loc . archive ( ) <EOL> return json_response ( { <EOL> '<STR_LIT:success>' : True , <EOL> '<STR_LIT:message>' : _ ( "<STR_LIT>" ) . format ( <EOL> location_name = loc . name , <EOL> action = _ ( "<STR_LIT>" ) , <EOL> ) <EOL> } ) <EOL> @ require_http_methods ( [ '<STR_LIT>' ] ) <EOL> @ can_edit_location <EOL> def delete_location ( request , domain , loc_id ) : <EOL> loc = Location . get ( loc_id ) <EOL> if loc . domain != domain : <EOL> raise Http404 ( ) <EOL> loc . full_delete ( ) <EOL> return json_response ( { <EOL> '<STR_LIT:success>' : True , <EOL> '<STR_LIT:message>' : _ ( "<STR_LIT>" ) . format ( <EOL> location_name = loc . name , <EOL> action = _ ( "<STR_LIT>" ) , <EOL> ) <EOL> } ) <EOL> def location_descendants_count ( request , domain , loc_id ) : <EOL> location = SQLLocation . objects . get ( location_id = loc_id ) <EOL> if location . domain != domain : <EOL> raise Http404 ( ) <EOL> count = len ( location . get_descendants ( include_self = True ) ) <EOL> return json_response ( { <EOL> '<STR_LIT:count>' : count <EOL> } ) <EOL> @ can_edit_location <EOL> def unarchive_location ( request , domain , loc_id ) : <EOL> loc = Location . get ( loc_id , db = Location . get_db ( ) ) <EOL> if loc . domain != domain : <EOL> raise Http404 ( ) <EOL> loc . unarchive ( ) <EOL> return json_response ( { <EOL> '<STR_LIT:success>' : True , <EOL> '<STR_LIT:message>' : _ ( "<STR_LIT>" ) . format ( <EOL> location_name = loc . name , <EOL> action = _ ( "<STR_LIT>" ) , <EOL> ) <EOL> } ) <EOL> class EditLocationView ( NewLocationView ) : <EOL> urlname = '<STR_LIT>' <EOL> page_title = ugettext_noop ( "<STR_LIT>" ) <EOL> creates_new_location = False <EOL> @ method_decorator ( can_edit_location ) <EOL> def dispatch ( self , request , * args , ** kwargs ) : <EOL> return super ( EditLocationView , self ) . dispatch ( request , * args , ** kwargs ) <EOL> @ property <EOL> def location_id ( self ) : <EOL> return self . kwargs [ '<STR_LIT>' ] <EOL> @ property <EOL> @ memoized <EOL> def location ( self ) : <EOL> try : <EOL> location = Location . get ( self . location_id ) <EOL> if location . domain != self . domain : <EOL> raise Http404 ( ) <EOL> except ResourceNotFound : <EOL> raise Http404 ( ) <EOL> else : <EOL> return location <EOL> @ property <EOL> @ memoized <EOL> def sql_location ( self ) : <EOL> try : <EOL> location = SQLLocation . objects . get ( location_id = self . location_id ) <EOL> if location . domain != self . domain : <EOL> raise Http404 ( ) <EOL> except ResourceNotFound : <EOL> raise Http404 ( ) <EOL> else : <EOL> return location <EOL> @ property <EOL> @ memoized <EOL> def supply_point ( self ) : <EOL> return self . location . linked_supply_point ( ) <EOL> @ property <EOL> def page_url ( self ) : <EOL> return reverse ( self . urlname , args = [ self . domain , self . location_id ] ) <EOL> @ property <EOL> def consumption ( self ) : <EOL> consumptions = [ ] <EOL> for product in Product . by_domain ( self . domain ) : <EOL> consumption = get_default_monthly_consumption ( <EOL> self . domain , <EOL> product . _id , <EOL> self . location . location_type , <EOL> self . supply_point . case_id if self . supply_point else None , <EOL> ) <EOL> if consumption : <EOL> consumptions . append ( ( product . name , consumption ) ) <EOL> return consumptions <EOL> @ property <EOL> @ memoized <EOL> def products_form ( self ) : <EOL> if ( <EOL> self . location . location_type_object . administrative or <EOL> not toggles . PRODUCTS_PER_LOCATION . enabled ( self . request . domain ) <EOL> ) : <EOL> return None <EOL> form = MultipleSelectionForm ( <EOL> initial = { '<STR_LIT>' : self . products_at_location } , <EOL> submit_label = _ ( "<STR_LIT>" ) , <EOL> prefix = "<STR_LIT>" , <EOL> ) <EOL> form . fields [ '<STR_LIT>' ] . choices = self . active_products <EOL> return form <EOL> @ property <EOL> @ memoized <EOL> def users_form ( self ) : <EOL> form = UsersAtLocationForm ( <EOL> domain_object = self . domain_object , <EOL> location = self . location , <EOL> data = self . request . POST if self . request . method == "<STR_LIT:POST>" else None , <EOL> submit_label = _ ( "<STR_LIT>" ) , <EOL> prefix = "<STR_LIT>" , <EOL> ) <EOL> return form <EOL> @ property <EOL> def active_products ( self ) : <EOL> return [ ( p . product_id , p . name ) <EOL> for p in SQLProduct . objects . filter ( domain = self . domain , is_archived = False ) . all ( ) ] <EOL> @ property <EOL> def products_at_location ( self ) : <EOL> return [ p . product_id for p in self . sql_location . products . all ( ) ] <EOL> @ property <EOL> def page_name ( self ) : <EOL> return mark_safe ( _ ( "<STR_LIT>" ) . format ( <EOL> name = self . location . name , type = self . location . location_type <EOL> ) ) <EOL> @ property <EOL> def page_context ( self ) : <EOL> context = super ( EditLocationView , self ) . page_context <EOL> context . update ( { <EOL> '<STR_LIT>' : self . products_form , <EOL> '<STR_LIT>' : self . users_form , <EOL> } ) <EOL> return context <EOL> def users_form_post ( self , request , * args , ** kwargs ) : <EOL> if self . users_form . is_valid ( ) : <EOL> self . users_form . save ( ) <EOL> return self . form_valid ( ) <EOL> else : <EOL> self . request . method = "<STR_LIT:GET>" <EOL> self . form_tab = '<STR_LIT>' <EOL> return self . get ( request , * args , ** kwargs ) <EOL> def products_form_post ( self , request , * args , ** kwargs ) : <EOL> products = SQLProduct . objects . filter ( <EOL> product_id__in = request . POST . getlist ( '<STR_LIT>' , [ ] ) <EOL> ) <EOL> self . sql_location . products = products <EOL> self . sql_location . save ( ) <EOL> return self . form_valid ( ) <EOL> def post ( self , request , * args , ** kwargs ) : <EOL> if self . request . POST [ '<STR_LIT>' ] == "<STR_LIT>" : <EOL> return self . settings_form_post ( request , * args , ** kwargs ) <EOL> elif self . request . POST [ '<STR_LIT>' ] == "<STR_LIT>" : <EOL> return self . users_form_post ( request , * args , ** kwargs ) <EOL> elif ( self . request . POST [ '<STR_LIT>' ] == "<STR_LIT>" <EOL> and toggles . PRODUCTS_PER_LOCATION . enabled ( request . domain ) ) : <EOL> return self . products_form_post ( request , * args , ** kwargs ) <EOL> else : <EOL> raise Http404 ( ) <EOL> class BaseSyncView ( BaseLocationView ) : <EOL> source = "<STR_LIT>" <EOL> sync_urlname = None <EOL> @ property <EOL> def page_context ( self ) : <EOL> return { <EOL> '<STR_LIT>' : self . settings_context , <EOL> '<STR_LIT:source>' : self . source , <EOL> '<STR_LIT>' : self . sync_urlname <EOL> } <EOL> @ property <EOL> def settings_context ( self ) : <EOL> key = "<STR_LIT>" % self . source <EOL> if hasattr ( self . domain_object . commtrack_settings , key ) : <EOL> return { <EOL> "<STR_LIT>" : getattr ( self . domain_object . commtrack_settings , key ) . _doc , <EOL> } <EOL> else : <EOL> return { } <EOL> def post ( self , request , * args , ** kwargs ) : <EOL> payload = json . loads ( request . POST . get ( '<STR_LIT>' ) ) <EOL> key = "<STR_LIT>" % self . source <EOL> if "<STR_LIT>" in payload : <EOL> for item in payload [ '<STR_LIT>' ] : <EOL> if hasattr ( self . domain_object . commtrack_settings , key ) : <EOL> setattr ( <EOL> getattr ( self . domain_object . commtrack_settings , key ) , <EOL> item , <EOL> payload [ '<STR_LIT>' ] [ item ] <EOL> ) <EOL> self . domain_object . commtrack_settings . save ( ) <EOL> return self . get ( request , * args , ** kwargs ) <EOL> class FacilitySyncView ( BaseSyncView ) : <EOL> urlname = '<STR_LIT>' <EOL> sync_urlname = '<STR_LIT>' <EOL> page_title = ugettext_noop ( "<STR_LIT>" ) <EOL> template_name = '<STR_LIT>' <EOL> source = '<STR_LIT>' <EOL> class LocationImportStatusView ( BaseLocationView ) : <EOL> urlname = '<STR_LIT>' <EOL> page_title = ugettext_noop ( '<STR_LIT>' ) <EOL> template_name = '<STR_LIT>' <EOL> @ use_bootstrap3 <EOL> def dispatch ( self , request , * args , ** kwargs ) : <EOL> return super ( LocationImportStatusView , self ) . dispatch ( request , * args , ** kwargs ) <EOL> def get ( self , request , * args , ** kwargs ) : <EOL> context = super ( LocationImportStatusView , self ) . main_context <EOL> context . update ( { <EOL> '<STR_LIT>' : self . domain , <EOL> '<STR_LIT>' : kwargs [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : reverse ( '<STR_LIT>' , args = [ self . domain , kwargs [ '<STR_LIT>' ] ] ) , <EOL> '<STR_LIT:title>' : _ ( "<STR_LIT>" ) , <EOL> '<STR_LIT>' : _ ( "<STR_LIT>" ) , <EOL> '<STR_LIT>' : _ ( "<STR_LIT>" ) , <EOL> } ) <EOL> return render ( request , self . template_name , context ) <EOL> def page_url ( self ) : <EOL> return reverse ( self . urlname , args = self . args , kwargs = self . kwargs ) <EOL> class LocationImportView ( BaseLocationView ) : <EOL> urlname = '<STR_LIT>' <EOL> page_title = ugettext_noop ( '<STR_LIT>' ) <EOL> template_name = '<STR_LIT>' <EOL> @ method_decorator ( can_edit_any_location ) <EOL> @ use_bootstrap3 <EOL> def dispatch ( self , request , * args , ** kwargs ) : <EOL> return super ( LocationImportView , self ) . dispatch ( request , * args , ** kwargs ) <EOL> @ property <EOL> def page_context ( self ) : <EOL> def _get_manage_consumption ( ) : <EOL> if self . domain_object . commtrack_settings : <EOL> return self . domain_object . commtrack_settings . individual_consumption_defaults <EOL> else : <EOL> return False <EOL> context = { <EOL> '<STR_LIT>' : { <EOL> "<STR_LIT>" : reverse ( <EOL> "<STR_LIT>" , args = ( self . domain , ) ) , <EOL> "<STR_LIT>" : _ ( "<STR_LIT>" ) , <EOL> "<STR_LIT>" : _ ( "<STR_LIT>" ) , <EOL> } , <EOL> "<STR_LIT>" : _get_manage_consumption ( ) , <EOL> } <EOL> context . update ( { <EOL> '<STR_LIT>' : get_bulk_upload_form ( context ) , <EOL> } ) <EOL> return context <EOL> def post ( self , request , * args , ** kwargs ) : <EOL> upload = request . FILES . get ( '<STR_LIT>' ) <EOL> if not upload : <EOL> messages . error ( request , _ ( '<STR_LIT>' ) ) <EOL> return self . get ( request , * args , ** kwargs ) <EOL> if not args : <EOL> messages . error ( request , _ ( '<STR_LIT>' ) ) <EOL> return self . get ( request , * args , ** kwargs ) <EOL> domain = args [ <NUM_LIT:0> ] <EOL> file_ref = expose_cached_download ( <EOL> upload . read ( ) , <EOL> expiry = <NUM_LIT:1> * <NUM_LIT> * <NUM_LIT> , <EOL> file_extension = file_extention_from_filename ( upload . name ) , <EOL> ) <EOL> task = import_locations_async . delay ( <EOL> domain , <EOL> file_ref . download_id , <EOL> ) <EOL> file_ref . set_task ( task ) <EOL> return HttpResponseRedirect ( <EOL> reverse ( <EOL> LocationImportStatusView . urlname , <EOL> args = [ domain , file_ref . download_id ] <EOL> ) <EOL> ) <EOL> @ locations_access_required <EOL> def location_importer_job_poll ( request , domain , download_id , <EOL> template = "<STR_LIT>" ) : <EOL> try : <EOL> context = get_download_context ( download_id , check_state = True ) <EOL> except TaskFailedError : <EOL> return HttpResponseServerError ( ) <EOL> context . update ( { <EOL> '<STR_LIT>' : _ ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' : _ ( '<STR_LIT>' ) , <EOL> } ) <EOL> return render ( request , template , context ) <EOL> @ locations_access_required <EOL> def location_export ( request , domain ) : <EOL> if not LocationType . objects . filter ( domain = domain ) . exists ( ) : <EOL> messages . error ( request , _ ( "<STR_LIT>" <EOL> "<STR_LIT>" ) ) <EOL> return HttpResponseRedirect ( reverse ( LocationsListView . urlname , args = [ domain ] ) ) <EOL> include_consumption = request . GET . get ( '<STR_LIT>' ) == '<STR_LIT:true>' <EOL> include_ids = request . GET . get ( '<STR_LIT>' ) == '<STR_LIT:true>' <EOL> response = HttpResponse ( content_type = Format . from_format ( '<STR_LIT>' ) . mimetype ) <EOL> response [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> dump_locations ( response , domain , include_consumption = include_consumption , <EOL> include_ids = include_ids ) <EOL> return response <EOL> @ is_locations_admin <EOL> @ require_POST <EOL> def sync_openlmis ( request , domain ) : <EOL> bootstrap_domain_task . delay ( domain ) <EOL> return HttpResponse ( '<STR_LIT:OK>' ) <EOL> @ locations_access_required <EOL> def child_locations_for_select2 ( request , domain ) : <EOL> id = request . GET . get ( '<STR_LIT:id>' ) <EOL> ids = request . GET . get ( '<STR_LIT>' ) <EOL> query = request . GET . get ( '<STR_LIT:name>' , '<STR_LIT>' ) . lower ( ) <EOL> user = request . couch_user <EOL> def loc_to_payload ( loc ) : <EOL> return { '<STR_LIT:id>' : loc . location_id , '<STR_LIT:name>' : loc . display_name } <EOL> if id : <EOL> try : <EOL> loc = SQLLocation . objects . get ( location_id = id ) <EOL> if loc . domain != domain : <EOL> raise SQLLocation . DoesNotExist ( ) <EOL> except SQLLocation . DoesNotExist : <EOL> return json_response ( <EOL> { '<STR_LIT:message>' : '<STR_LIT>' % id } , <EOL> status_code = <NUM_LIT> , <EOL> ) <EOL> else : <EOL> return json_response ( loc_to_payload ( loc ) ) <EOL> elif ids : <EOL> from corehq . apps . locations . util import get_locations_from_ids <EOL> ids = json . loads ( ids ) <EOL> try : <EOL> locations = get_locations_from_ids ( ids , domain ) <EOL> except SQLLocation . DoesNotExist : <EOL> return json_response ( <EOL> { '<STR_LIT:message>' : '<STR_LIT>' } , <EOL> status_code = <NUM_LIT> , <EOL> ) <EOL> return json_response ( [ loc_to_payload ( loc ) for loc in locations ] ) <EOL> else : <EOL> locs = [ ] <EOL> user_loc = user . get_sql_location ( domain ) <EOL> if user_can_edit_any_location ( user , request . project ) : <EOL> locs = SQLLocation . objects . filter ( domain = domain , is_archived = False ) <EOL> elif user_loc : <EOL> locs = user_loc . get_descendants ( include_self = True ) <EOL> if locs != [ ] and query : <EOL> locs = locs . filter ( name__icontains = query ) <EOL> return json_response ( map ( loc_to_payload , locs [ : <NUM_LIT:10> ] ) ) <EOL> class DowngradeLocationsView ( BaseDomainView ) : <EOL> """<STR_LIT>""" <EOL> template_name = '<STR_LIT>' <EOL> urlname = '<STR_LIT>' <EOL> def dispatch ( self , * args , ** kwargs ) : <EOL> if not users_have_locations ( self . domain ) : <EOL> redirect_url = reverse ( '<STR_LIT>' , args = [ self . domain ] ) <EOL> return HttpResponseRedirect ( redirect_url ) <EOL> return super ( DowngradeLocationsView , self ) . dispatch ( * args , ** kwargs ) <EOL> @ property <EOL> def section_url ( self ) : <EOL> return self . page_url <EOL> @ domain_admin_required <EOL> def unassign_users ( request , domain ) : <EOL> """<STR_LIT>""" <EOL> for user in get_users_assigned_to_locations ( domain ) : <EOL> if user . is_web_user ( ) : <EOL> user . unset_location ( domain ) <EOL> elif user . is_commcare_user ( ) : <EOL> user . unset_location ( ) <EOL> messages . success ( request , <EOL> _ ( "<STR_LIT>" ) ) <EOL> fallback_url = reverse ( '<STR_LIT>' , args = [ domain ] ) <EOL> return HttpResponseRedirect ( request . POST . get ( '<STR_LIT>' , fallback_url ) ) </s>
