def test_is_authenticated(self): permission = IsAuthenticated() authenticated_user = Namespace(is_authenticated=lambda: True) authenticated_request = Namespace(user=authenticated_user) self.assertTrue(permission.has_permission(authenticated_request)) unauthenticated_user = Namespace(is_authenticated=lambda: False) unauthenticated_request = Namespace(user=unauthenticated_user) self.assertFalse(permission.has_permission(unauthenticated_request))
class IndigenousVillageSerializer(FieldPermissionSerializerMixin, GeoBaseMixinSerializer): position_precision = serializers.SerializerMethodField() population = serializers.SerializerMethodField() guarani_presence = serializers.SerializerMethodField() projects = SimpleProjectSerializer(many=True) land = serializers.SerializerMethodField() # Private fields private_comments = fields.ReadOnlyField( permission_classes=(IsAuthenticated(), )) class Meta: model = IndigenousVillage list_serializer_class = ListIndigenousVillageSerializer depth = 1 fields = '__all__' @staticmethod def get_position_precision(obj): if obj.position_precision: return dict(IndigenousVillage.POSITION_PRECISION).get( obj.position_precision) @staticmethod def get_population(obj): try: population = obj.population_annual_series.latest() return PopulationSerializer(population).data except Population.DoesNotExist: return None @staticmethod def get_guarani_presence(obj): try: presence = obj.guarani_presence_annual_series.latest() except GuaraniPresence.DoesNotExist: presence = GuaraniPresence(presence=False) return GuaraniPresenceSerializer(presence).data # @staticmethod # def get_villages(obj): # return SimpleIndigenousVillageSerializer(obj.villages, many=True).data @staticmethod def get_land(obj): if obj.land: land = obj.land[0] return SimpleIndigenousLandSerializer(land).data
def test_single_permission_checking(self): field = BooleanField(permission_classes=(AllowAny(),)) self.assertTrue(field.check_permission({})) field = BooleanField(permission_classes=(AllowNone(),)) self.assertFalse(field.check_permission({})) field = BooleanField(permission_classes=(IsAuthenticated(),)) authenticated_user = Namespace(is_authenticated=lambda: True) authenticated_request = Namespace(user=authenticated_user) self.assertTrue(field.check_permission(authenticated_request)) unauthenticated_user = Namespace(is_authenticated=lambda: False) unauthenticated_request = Namespace(user=unauthenticated_user) self.assertFalse(field.check_permission(unauthenticated_request))
class IndigenousLandGeojsonSerializer(IndigenousLandExportSerializer, GeoFeatureModelSerializer, CachedSerializerMixin): # Trick to avoid fiona error cti_id = fields.ReadOnlyField(source='id', permission_classes=(IsAuthenticated(), )) class Meta: model = IndigenousLand geo_field = 'geometry' fields = [ 'cti_id', 'name', 'other_names', 'ethnic_groups', 'prominent_subgroup', 'villages', 'population', 'guarani_presence', 'official_area', 'land_tenure', 'land_tenure_status', 'associated_land', 'guarani_exclusive_possession_area_portion', 'others_exclusive_possession_area_portion', 'documents', 'cities', 'states', 'demand', 'claim', 'public_comments', 'source', 'private_comments', 'layer' ]
class IndigenousLandSerializer(FieldPermissionSerializerMixin, GeoBaseMixinSerializer): associated_land = serializers.PrimaryKeyRelatedField(read_only=True) bbox = serializers.SerializerMethodField() guarani_presence = serializers.SerializerMethodField() villages = serializers.SerializerMethodField() population = serializers.ReadOnlyField() # calculated_area = serializers.ReadOnlyField() cities = serializers.SerializerMethodField() states = serializers.SerializerMethodField() private_comments = fields.ReadOnlyField( permission_classes=(IsAuthenticated(), )) claim = fields.ReadOnlyField(permission_classes=(IsAuthenticated(), )) demand = fields.ReadOnlyField(permission_classes=(IsAuthenticated(), )) projects = SimpleProjectSerializer(many=True) country = serializers.SerializerMethodField() class Meta: model = IndigenousLand list_serializer_class = IndigenousLandListSerializer exclude = ['geometry'] depth = 1 @staticmethod def get_bbox(obj): if obj.geometry.extent: return [[obj.geometry.extent[0], obj.geometry.extent[1]], [obj.geometry.extent[2], obj.geometry.extent[3]]] @staticmethod def get_guarani_presence(obj): guarani_presence = False # presences = [] for village in obj.villages: try: presence = village.guarani_presence_annual_series.latest() guarani_presence = guarani_presence or presence.presence # presences.append(GuaraniPresenceSerializer(presence).data) except GuaraniPresence.DoesNotExist: pass return guarani_presence @staticmethod def get_villages(obj): return SimpleIndigenousVillageSerializer(obj.villages, many=True).data @staticmethod def get_cities(obj): cities = obj.get_cities_intersected() if cities: return ", ".join([city.name for city in cities]) @staticmethod def get_states(obj): states = obj.get_states_intersected() if states: return ", ".join([state.name or state.acronym for state in states]) @staticmethod def get_country(obj): if obj.country: return obj.country.name