def test_is_active_function_is_cached(self): self.tenant_switch.countries.add(connection.tenant) self.tenant_switch.save() # <- save is necessary to mimic the admin with self.assertNumQueries(2): # First time takes 2 queries (one to get switch, and one to get countries list) switch_active = tenant_switch_is_active(self.tenant_switch.name) self.assertTrue(switch_active) with self.assertNumQueries(0): # Second time, takes zero queries switch_active = tenant_switch_is_active(self.tenant_switch.name) self.assertTrue(switch_active)
def get_queryset(self): q = Intervention.objects.detail_qs() # TODO: remember to add back the location filter after the PRP integration related structural changes are final # .filter(sector_locations__isnull=False).exclude(sector_locations__locations=None)\ # .prefetch_related('sector_locations__locations') query_params = self.request.query_params if query_params: queries = [] if "country_programme" in query_params.keys(): queries.append( Q(agreement__country_programme=query_params.get( "country_programme"))) if "section" in query_params.keys(): if tenant_switch_is_active("prp_mode_off"): sq = Q(sections__pk=query_params.get("section")) else: sq = Q( result_links__ll_results__applied_indicators__section__pk =query_params.get("section")) queries.append(sq) if "status" in query_params.keys(): queries.append(Q(status=query_params.get("status"))) if "partner" in query_params.keys(): queries.append( Q(agreement__partner=query_params.get("partner"))) if queries: expression = functools.reduce(operator.and_, queries) q = q.filter(expression).distinct() return q
def test_is_active_function_switch_on(self): self.tenant_switch.countries.add(connection.tenant) # In tests, we have to manually flush the cache. When created through # the admin, the only way to change countries is to click the 'Save' # button, which flushes the cache self.tenant_switch.save() # <- save is necessary to mimic the admin switch_active = tenant_switch_is_active(self.tenant_switch.name) # tenant in countries, so this should return True self.assertTrue(switch_active)
def list(self, request, *args, **kwargs): """ Override list() to check each flag against this request and return just a list of active flags. """ flag_serializer = TenantFlagSerializer(TenantFlag.objects, many=True) switch_serializer = TenantSwitchSerializer(TenantSwitch.objects, many=True) # use set comprehensions so we never get dupes in this list active_flags = { flag['name'] for flag in flag_serializer.data if tenant_flag_is_active(request, flag['name']) } active_flags.update([ switch['name'] for switch in switch_serializer.data if tenant_switch_is_active(switch['name']) ]) return Response({'active_flags': list(active_flags)})
def prp_server_on(): return tenant_switch_is_active("prp_server_on")
def prp_mode_off(): return tenant_switch_is_active("prp_mode_off")
def test_is_active_function_nonexistent_switch(self): "Nonexistent TenantSwitch should return False." switch_active = tenant_switch_is_active('foo') self.assertFalse(switch_active)
def test_is_active_function_switch_off(self): "Return False if tenant not in countries" switch_active = tenant_switch_is_active(self.tenant_switch.name) self.assertFalse(switch_active)