Exemple #1
0
def location_edit(request, domain, loc_id=None):
    parent_id = request.GET.get('parent')

    if loc_id:
        try:
            location = Location.get(loc_id)
        except ResourceNotFound:
            raise Http404()
    else:
        location = Location(domain=domain, parent=parent_id)

    if request.method == "POST":
        form = LocationForm(location, request.POST)
        if form.is_valid():
            form.save()
            messages.success(request, 'Location saved!')
            return HttpResponseRedirect('%s?%s' % (
                    reverse('manage_locations', kwargs={'domain': domain}),
                    urllib.urlencode({'selected': form.location._id})
                ))
    else:
        form = LocationForm(location)

    context = {
        'domain': domain,
        'api_root': reverse('api_dispatch_list', kwargs={'domain': domain,
                                                         'resource_name': 'location',
                                                         'api_name': 'v0.3'}),
        'location': location,
        'hierarchy': location_hierarchy_config(domain),
        'form': form,
    }
    return render(request, 'locations/manage/location.html', context)
Exemple #2
0
    def _get_custom_context(self):
        api_root = reverse('api_dispatch_list', kwargs={'domain': self.domain,
                                                        'resource_name': 'location', 
                                                        'api_name': 'v0.3'})
        selected_loc_id = self.request.GET.get('location_id')
        user = WebUser.get_by_username(str(self.request.user))
        domain = Domain.get_by_name(self.domain)

        context = {}
        
        from corehq.apps.commtrack.util import is_commtrack_location
        if is_commtrack_location(user, domain):
            selected_loc_id = user.location_id
            if domain.location_restriction_for_users:
                context.update({'restriction': domain.location_restriction_for_users})

        context.update({
            'api_root': api_root,
            'control_name': self.name,
            'control_slug': self.slug,
            'loc_id': selected_loc_id,
            'locations': json.dumps(load_locs_json(self.domain, selected_loc_id)),
            'hierarchy': location_hierarchy_config(self.domain),
        })

        return context
Exemple #3
0
    def filter_context(self):
        api_root = reverse('api_dispatch_list', kwargs={'domain': self.domain,
                                                        'resource_name': 'location',
                                                        'api_name': 'v0.3'})
        selected_loc_id = self.request.GET.get('location_id')
        user = CouchUser.get_by_username(unicode(self.request.user))
        domain = Domain.get_by_name(self.domain)

        context = {}
        location_id = None

        domain_membership = user.get_domain_membership(self.domain)
        if domain_membership:
            location_id = domain_membership.location_id

        if not selected_loc_id and location_id and domain.commtrack_enabled:
            selected_loc_id = location_id
            if domain.location_restriction_for_users:
                context.update({'restriction': domain.location_restriction_for_users})

        context.update({
            'api_root': api_root,
            'control_name': self.label, # todo: cleanup, don't follow this structure
            'control_slug': self.slug, # todo: cleanup, don't follow this structure
            'loc_id': selected_loc_id,
            'locations': json.dumps(load_locs_json(self.domain, selected_loc_id)),
            'hierarchy': location_hierarchy_config(self.domain)
        })

        return context
Exemple #4
0
    def filter_context(self):
        api_root = reverse('api_dispatch_list',
                           params={'show_administrative': False},
                           kwargs={'domain': self.domain,
                                   'resource_name': 'ews_location',
                                   'api_name': 'v0.3'})
        user = self.request.couch_user
        loc_id = self.request.GET.get('location_id')
        if not loc_id:
            domain_membership = user.get_domain_membership(self.domain)
            if not domain_membership or not domain_membership.location_id:
                loc_id = SQLLocation.objects.filter(
                    domain=self.domain,
                    location_type__name='country'
                ).first().location_id
            else:
                loc_id = domain_membership.location_id

        location = get_object_or_404(SQLLocation, location_id=loc_id)
        if not location.location_type.administrative:
            loc_id = location.parent.location_id
        hier = location_hierarchy_config(self.domain)
        hierarchy = []
        for h in hier:
            if h[0] not in self.reporting_types():
                hierarchy.append(h)

        return {
            'api_root': api_root,
            'control_name': self.label,
            'control_slug': self.slug,
            'loc_id': loc_id,
            'locations': load_locs_json(self.domain, loc_id, only_administrative=self.only_administrative),
            'hierarchy': hierarchy
        }
Exemple #5
0
def top_level_location_types(domain):
    """
    Return all location types which do not have
    any potential parents
    """
    from corehq.apps.locations.util import location_hierarchy_config
    hierarchy = location_hierarchy_config(domain)
    return [t[0] for t in hierarchy if t[1] == [None]]
Exemple #6
0
 def main_context(self):
     context = super(BaseLocationView, self).main_context
     context.update({
         'hierarchy': location_hierarchy_config(self.domain),
         'api_root': reverse('api_dispatch_list', kwargs={'domain': self.domain,
                                                          'resource_name': 'location',
                                                          'api_name': 'v0.3'}),
     })
     return context
Exemple #7
0
 def filter_context(self, request_user):
     loc_id = self.user_location_id(request_user)
     return {
         'input_name': self.name,
         'loc_id': loc_id,
         'hierarchy': location_hierarchy_config(self.domain),
         'locations': load_locs_json(self.domain, selected_loc_id=loc_id, user=request_user),
         'loc_url': self.api_root,
         'max_drilldown_levels': self.max_drilldown_levels,
     }
Exemple #8
0
def locations_list(request, domain):
    selected_id = request.GET.get('selected')

    context = {
        'domain': domain,
        'selected_id': selected_id,
        'locations': load_locs_json(domain, selected_id),
        'hierarchy': location_hierarchy_config(domain),
        'api_root': reverse('api_dispatch_list', kwargs={'domain': domain,
                                                         'resource_name': 'location', 
                                                         'api_name': 'v0.3'})
    }
    return render(request, 'locations/manage/locations.html', context)
Exemple #9
0
    def _get_custom_context(self):
        api_root = reverse(
            "api_dispatch_list", kwargs={"domain": self.domain, "resource_name": "location", "api_name": "v0.3"}
        )
        selected_loc_id = self.request.GET.get("location_id")

        return {
            "api_root": api_root,
            "control_name": self.name,
            "control_slug": self.slug,
            "loc_id": selected_loc_id,
            "locations": json.dumps(load_locs_json(self.domain, selected_loc_id)),
            "hierarchy": location_hierarchy_config(self.domain),
        }
Exemple #10
0
    def _get_custom_context(self):
        api_root = reverse('api_dispatch_list', kwargs={'domain': self.domain,
                                                        'resource_name': 'location', 
                                                        'api_name': 'v0.3'})
        selected_loc_id = self.request.GET.get('location_id')

        return {
            'api_root': api_root,
            'control_name': self.name,
            'control_slug': self.slug,
            'loc_id': selected_loc_id,
            'locations': json.dumps(load_locs_json(self.domain, selected_loc_id)),
            'hierarchy': location_hierarchy_config(self.domain),
        }
Exemple #11
0
    def filter_context(self):
        api_root = reverse('api_dispatch_list', kwargs={'domain': self.domain,
                                                        'resource_name': 'location',
                                                        'api_name': 'v0.3'})
        selected_loc_id = self.request.GET.get('location_id')

        return {
            'api_root': api_root,
            'control_name': self.label, # todo: cleanup, don't follow this structure
            'control_slug': self.slug, # todo: cleanup, don't follow this structure
            'loc_id': selected_loc_id,
            'locations': json.dumps(load_locs_json(self.domain, selected_loc_id)),
            'hierarchy': location_hierarchy_config(self.domain),
        }
    def test_filter(self):
        report = ReportConfiguration(domain=self.domain)
        ui_filter = ReportFilterFactory.from_spec({
            "slug": "block_id_drill",
            "type": "location_drilldown",
            "field": "block_id",
            "display": "Drilldown by Location",
            "include_descendants": False,
        }, report)

        self.assertEqual(type(ui_filter), LocationDrilldownFilter)

        # test filter_context
        filter_context_expected = {
            'input_name': 'block_id_drill',
            'loc_id': None,
            'hierarchy': location_hierarchy_config(self.domain),
            'locations': load_locs_json(self.domain),
            'loc_url': '/a/{}/api/v0.5/location_internal/'.format(self.domain),
            'max_drilldown_levels': 99
        }
        self.assertDictEqual(ui_filter.filter_context(self.user), filter_context_expected)

        # test include_descendants=False
        self.assertListEqual(
            ui_filter.value(
                **{ui_filter.name: self.locations.get('Middlesex').location_id, REQUEST_USER_KEY: self.user}
            ),
            [self.locations.get('Middlesex').location_id]
        )

        # test include_descendants=True
        ui_filter = ReportFilterFactory.from_spec({
            "slug": "block_id_drill",
            "type": "location_drilldown",
            "field": "block_id",
            "display": "Drilldown by Location",
            "include_descendants": True,
        }, report)
        self.assertListEqual(
            ui_filter.value(
                **{ui_filter.name: self.locations.get('Middlesex').location_id, REQUEST_USER_KEY: self.user}
            ),
            [self.locations.get(name).location_id
             for name in ['Middlesex', 'Cambridge', 'Somerville']]
        )
Exemple #13
0
    def filter_context(self):
        api_root = self.api_root
        user = self.request.couch_user
        loc_id = self.request.GET.get('location_id')
        if not loc_id:
            domain_membership = user.get_domain_membership(self.domain)
            if domain_membership:
                loc_id = domain_membership.location_id

        return {
            'api_root': api_root,
            'control_name': self.label, # todo: cleanup, don't follow this structure
            'control_slug': self.slug, # todo: cleanup, don't follow this structure
            'loc_id': loc_id,
            'locations': load_locs_json(self.domain, loc_id, user=user),
            'make_optional': self.make_optional,
            'hierarchy': location_hierarchy_config(self.domain)
        }
Exemple #14
0
def get_dashboard_template_context(domain, couch_user):
    context = {}
    context['location_hierarchy'] = location_hierarchy_config(domain)
    context['user_location_id'] = couch_user.get_location_id(domain)
    context['all_user_location_id'] = list(
        couch_user.get_sql_locations(domain).location_ids())
    try:
        context['user_location_type'] = couch_user.get_sql_location(
            domain).location_type_name
    except AttributeError:
        context['user_location_type'] = ''
    context['state_level_access'] = 'state' in set([
        loc.location_type.code for loc in couch_user.get_sql_locations(domain)
    ])
    context['have_access_to_features'] = icds_pre_release_features(couch_user)
    context['show_mobile_apk'] = ICDS_DASHBOARD_SHOW_MOBILE_APK.enabled(
        couch_user.username, namespace=NAMESPACE_USER)
    context['have_access_to_all_locations'] = couch_user.has_permission(
        domain, 'access_all_locations')

    if context['have_access_to_all_locations']:
        context['user_location_id'] = None

    if couch_user.is_web_user():
        context['is_web_user'] = True

    context['nav_metadata'] = _get_nav_metadatada()
    context['sdd_metadata'] = _get_sdd_metadata()
    context['nav_menu_items'] = _get_nav_menu_items()
    context['fact_sheet_sections'] = _get_factsheet_sections()
    context['MAPBOX_ACCESS_TOKEN'] = settings.MAPBOX_ACCESS_TOKEN
    context['support_email'] = settings.SUPPORT_EMAIL

    if couch_user.is_commcare_user() and _has_helpdesk_role(
            domain, couch_user):
        build_id = get_latest_issue_tracker_build_id()
        context['report_an_issue_url'] = webapps_module(
            domain=domain,
            app_id=build_id,
            module_id=0,
        )

    return context
Exemple #15
0
    def filter_context(self):
        api_root = reverse('api_dispatch_list',
                           kwargs={
                               'domain': self.domain,
                               'resource_name': 'location_internal',
                               'api_name': 'v0.5'
                           })
        selected_loc_id = self.request.GET.get('location_id')
        locations = load_locs_json(self.domain, selected_loc_id)

        return {
            'api_root': api_root,
            'control_name': self.label,
            'control_slug': self.slug,
            'loc_id': selected_loc_id if selected_loc_id else '',
            'locations': json.dumps(locations),
            'hierarchy':
            [loc for loc in location_hierarchy_config(self.domain)],
        }
Exemple #16
0
    def filter_context(self):
        api_root = reverse('api_dispatch_list', kwargs={'domain': self.domain,
                                                        'resource_name': 'location',
                                                        'api_name': 'v0.3'})
        user = self.request.couch_user
        loc_id = self.request.GET.get('location_id')
        if not loc_id:
            domain_membership = user.get_domain_membership(self.domain)
            if domain_membership:
                loc_id = domain_membership.location_id

        return {
            'api_root': api_root,
            'control_name': self.label, # todo: cleanup, don't follow this structure
            'control_slug': self.slug, # todo: cleanup, don't follow this structure
            'loc_id': loc_id,
            'locations': load_locs_json(self.domain, loc_id, user=user),
            'hierarchy': location_hierarchy_config(self.domain)
        }
Exemple #17
0
    def filter_context(self):
        api_root = self.api_root
        user = self.request.couch_user
        loc_id = self.request.GET.get('location_id')
        if not loc_id:
            domain_membership = user.get_domain_membership(self.domain)
            if domain_membership:
                loc_id = domain_membership.location_id

        return {
            'api_root': api_root,
            'control_name':
            self.label,  # todo: cleanup, don't follow this structure
            'control_slug':
            self.slug,  # todo: cleanup, don't follow this structure
            'loc_id': loc_id,
            'locations': load_locs_json(self.domain, loc_id, user=user),
            'make_optional': self.make_optional,
            'hierarchy': location_hierarchy_config(self.domain)
        }
def get_dashboard_template_context(domain, couch_user):
    context = {}
    context['location_hierarchy'] = location_hierarchy_config(domain)
    context['user_location_id'] = couch_user.get_location_id(domain)
    context['all_user_location_id'] = list(
        couch_user.get_sql_locations(domain).location_ids())
    context['state_level_access'] = 'state' in set([
        loc.location_type.code for loc in couch_user.get_sql_locations(domain)
    ])
    context['have_access_to_features'] = icds_pre_release_features(couch_user)
    context['have_access_to_all_locations'] = couch_user.has_permission(
        domain, 'access_all_locations')

    if context['have_access_to_all_locations']:
        context['user_location_id'] = None

    if couch_user.is_web_user():
        context['is_web_user'] = True

    return context
Exemple #19
0
    def filter_context(self):
        api_root = reverse('api_dispatch_list',
                           kwargs={
                               'domain': self.domain,
                               'resource_name': 'location',
                               'api_name': 'v0.3'
                           })
        selected_loc_id = self.request.GET.get('location_id')

        return {
            'api_root': api_root,
            'control_name':
            self.label,  # todo: cleanup, don't follow this structure
            'control_slug':
            self.slug,  # todo: cleanup, don't follow this structure
            'loc_id': selected_loc_id,
            'locations':
            json.dumps(load_locs_json(self.domain, selected_loc_id)),
            'hierarchy': location_hierarchy_config(self.domain),
        }
Exemple #20
0
def location_edit(request, domain, loc_id=None):
    parent_id = request.GET.get('parent')

    if loc_id:
        try:
            location = Location.get(loc_id)
        except ResourceNotFound:
            raise Http404()
    else:
        location = Location(domain=domain, parent=parent_id)

    if request.method == "POST":
        form = LocationForm(location, request.POST)
        if form.is_valid():
            form.save()
            messages.success(request, 'Location saved!')
            return HttpResponseRedirect(
                '%s?%s' %
                (reverse('manage_locations', kwargs={'domain': domain}),
                 urllib.urlencode({'selected': form.location._id})))
    else:
        form = LocationForm(location)

    context = {
        'domain':
        domain,
        'api_root':
        reverse('api_dispatch_list',
                kwargs={
                    'domain': domain,
                    'resource_name': 'location',
                    'api_name': 'v0.3'
                }),
        'location':
        location,
        'hierarchy':
        location_hierarchy_config(domain),
        'form':
        form,
    }
    return render(request, 'locations/manage/location.html', context)
Exemple #21
0
    def filter_context(self):
        api_root = reverse('api_dispatch_list',
                           params={'show_administrative': True},
                           kwargs={'domain': self.domain,
                                   'resource_name': 'ews_location',
                                   'api_name': 'v0.3'})
        user = self.request.couch_user
        loc_id = self.request.GET.get('location_id')
        if not loc_id:
            domain_membership = user.get_domain_membership(self.domain)
            if domain_membership:
                loc_id = domain_membership.location_id

        return {
            'api_root': api_root,
            'control_name': self.label,
            'control_slug': self.slug,
            'loc_id': loc_id,
            'locations': load_locs_json(self.domain, loc_id, only_administrative=self.only_administrative),
            'hierarchy': location_hierarchy_config(self.domain)
        }
    def test_filter(self):
        report = ReportConfiguration(domain=self.domain)
        filter = ReportFilterFactory.from_spec(
            {
                "slug": "block_id_drill",
                "type": "location_drilldown",
                "field": "block_id",
                "display": "Drilldown by Location",
            }, report)

        self.assertEqual(type(filter), LocationDrilldownFilter)

        filter_context_expected = {
            'input_name': 'block_id_drill',
            'loc_id': None,
            'hierarchy': location_hierarchy_config(self.domain),
            'locations': load_locs_json(self.domain),
            'loc_url': '/a/{}/api/v0.5/location_internal/'.format(self.domain)
        }

        self.assertDictEqual(filter.filter_context(), filter_context_expected)
Exemple #23
0
    def ctx(self):
        api_root = reverse('api_dispatch_list', kwargs={'domain': self.domain,
                                                        'resource_name': 'location',
                                                        'api_name': 'v0.3'})
        selected_loc_id = self.request.GET.get('location_id')

        locations = load_locs_json(self.domain, selected_loc_id)

        f = lambda y: 'children' in y
        districts = filter(f, locations)
        if districts:
            PPS = filter(f, districts[0]['children'])
            if PPS:
                del PPS[0]['children']
        return {
            'api_root': api_root,
            'control_name': self.label,
            'control_slug': self.slug,
            'loc_id': selected_loc_id,
            'locations': json.dumps(locations),
            'hierarchy': [loc for loc in location_hierarchy_config(self.domain) if loc[0] != 'PPS'],
        }
Exemple #24
0
    def ctx(self):
        api_root = reverse(
            "api_dispatch_list", kwargs={"domain": self.domain, "resource_name": "location", "api_name": "v0.3"}
        )
        selected_loc_id = self.request.GET.get("location_id")

        locations = load_locs_json(self.domain, selected_loc_id)

        if self.required != 2:
            f = lambda y: "children" in y
            districts = filter(f, locations)
            if districts:
                PPS = filter(f, districts[0]["children"])
                if PPS:
                    del PPS[0]["children"]
        return {
            "api_root": api_root,
            "control_name": self.label,
            "control_slug": self.slug,
            "loc_id": selected_loc_id,
            "locations": json.dumps(locations),
            "hierarchy": [loc for loc in location_hierarchy_config(self.domain) if loc[0] != "PPS"],
        }
    def ctx(self):
        api_root = reverse('api_dispatch_list', kwargs={'domain': self.domain,
                                                        'resource_name': 'location_internal',
                                                        'api_name': 'v0.5'})
        selected_loc_id = self.request.GET.get('location_id')

        locations = load_locs_json(self.domain, selected_loc_id)

        if self.required != 2:
            f = lambda y: 'children' in y
            districts = filter(f, locations)
            if districts:
                PPS = filter(f, districts[0]['children'])
                if PPS:
                    del PPS[0]['children']
        return {
            'api_root': api_root,
            'control_name': self.label,
            'control_slug': self.slug,
            'loc_id': selected_loc_id,
            'locations': json.dumps(locations),
            'hierarchy': [loc for loc in location_hierarchy_config(self.domain) if loc[0] != 'PPS'],
        }
Exemple #26
0
 def location_hierarchy_config(self):
     return location_hierarchy_config(self.domain)
Exemple #27
0
 def filter_context(self):
     context = super(RecapPassageLocationFilter, self).filter_context
     context.update(dict(hierarchy=location_hierarchy_config(self.domain)))
     return context
Exemple #28
0
 def filter_context(self):
     context = super(RecapPassageLocationFilter, self).filter_context
     context.update(dict(hierarchy=location_hierarchy_config(self.domain)))
     return context
Exemple #29
0
 def location_hierarchy_config(self):
     return location_hierarchy_config(self.domain)