예제 #1
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
        }
예제 #2
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)
        }
예제 #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
예제 #4
0
    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 = list(filter(f, locations))
            if districts:
                PPS = list(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'
            ],
        }
예제 #5
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
예제 #6
0
 def filter_context(self):
     return {
         'input_name': self.name,
         'loc_id': None,
         'hierarchy': location_hierarchy_config(self.domain),
         'locations': load_locs_json(self.domain),
         'loc_url': self.api_root
     }
예제 #7
0
 def render(self, name, value, attrs=None):
     return get_template(
         'locations/manage/partials/parent_loc_widget.html').render(
             Context({
                 'name': name,
                 'value': value,
                 'locations': load_locs_json(self.domain, value),
             }))
예제 #8
0
파일: views.py 프로젝트: atinus/commcare-hq
 def page_context(self):
     selected_id = self.request.GET.get('selected')
     return {
         'selected_id': selected_id,
         'locations': load_locs_json(
             self.domain, selected_id, self.show_inactive
         ),
         'show_inactive': self.show_inactive,
     }
예제 #9
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,
     }
예제 #10
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,
     }
예제 #11
0
    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']
                ])
예제 #12
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),
        'api_root': reverse('api_dispatch_list', kwargs={'domain': domain,
                                                         'resource_name': 'location', 
                                                         'api_name': 'v0.3'})
    }
    return render(request, 'locations/manage/locations.html', context)
예제 #13
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)
예제 #14
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)),
        }
예제 #15
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),
        }
예제 #16
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),
        }
예제 #17
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),
        }
예제 #18
0
    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']]
        )
예제 #19
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)
        }
예제 #20
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),
        }
예제 #21
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)
        }
예제 #22
0
    def filter_context(self):
        from custom.ewsghana import ROOT_SITE_CODE
        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.get(
                    domain=self.domain, site_code=ROOT_SITE_CODE).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
        }
예제 #23
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)
        }
예제 #24
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)],
        }
예제 #25
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),
        }
예제 #26
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)
        }
예제 #27
0
    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)
예제 #28
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'],
        }
예제 #29
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"],
        }
예제 #30
0
 def load_locations_json(self, loc_id):
     return load_locs_json(self.domain, loc_id, user=self.request.couch_user)
예제 #31
0
 def load_locations_json(self, loc_id):
     return load_locs_json(self.domain,
                           loc_id,
                           user=self.request.couch_user)
예제 #32
0
 def render(self, name, value, attrs=None):
     return get_template('locations/manage/partials/parent_loc_widget.html').render(Context({
                 'name': name,
                 'value': value,
                 'locations': load_locs_json(self.domain, value),
             }))
예제 #33
0
 def page_context(self):
     selected_id = self.request.GET.get('selected')
     return {
         'selected_id': selected_id,
         'locations': load_locs_json(self.domain, selected_id),
     }