Skip to content

Commit

Permalink
feat(events): add has_user_editable_resources to API
Browse files Browse the repository at this point in the history
Refs: LINK-2163
  • Loading branch information
harriris-vincit committed Sep 13, 2024
1 parent 8177832 commit d802de2
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 7 deletions.
24 changes: 17 additions & 7 deletions events/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ class KeywordViewSet(
viewsets.GenericViewSet,
):
queryset = Keyword.objects.all()
queryset = queryset.select_related("publisher")
queryset = queryset.select_related("data_source", "publisher")
serializer_class = KeywordSerializer
permission_classes = [
DataSourceResourceEditPermission & OrganizationUserEditPermission
Expand Down Expand Up @@ -392,8 +392,13 @@ class KeywordListViewSet(
mixins.CreateModelMixin,
viewsets.GenericViewSet,
):
# publisher relation performs better with prefetch than selected
queryset = Keyword.objects.all().prefetch_related("publisher", "alt_labels")
# publisher relation performs better with prefetch than selected,
# while data_source has less queries with select
queryset = (
Keyword.objects.all()
.select_related("data_source")
.prefetch_related("publisher", "alt_labels")
)
serializer_class = KeywordSerializer
filter_backends = (filters.OrderingFilter,)
ordering_fields = ("n_events", "id", "name", "data_source")
Expand Down Expand Up @@ -549,7 +554,7 @@ class KeywordSetViewSet(
AuditLogApiViewMixin,
viewsets.ModelViewSet,
):
queryset = KeywordSet.objects.all()
queryset = KeywordSet.objects.all().select_related("data_source")
serializer_class = KeywordSetSerializer
permission_classes = [
DataSourceResourceEditPermission & OrganizationUserEditPermission
Expand Down Expand Up @@ -712,7 +717,7 @@ class PlaceRetrieveViewSet(
viewsets.GenericViewSet,
):
queryset = Place.objects.all()
queryset = queryset.select_related("publisher")
queryset = queryset.select_related("data_source", "publisher")
serializer_class = PlaceSerializer
permission_classes = [
DataSourceResourceEditPermission & OrganizationUserEditPermission
Expand Down Expand Up @@ -1356,7 +1361,12 @@ class ImageViewSet(
viewsets.ModelViewSet,
):
queryset = Image.objects.all().select_related(
"publisher", "data_source", "created_by", "last_modified_by", "license"
"data_source",
"publisher",
"data_source",
"created_by",
"last_modified_by",
"license",
)
serializer_class = ImageSerializer
pagination_class = LargeResultsSetPagination
Expand Down Expand Up @@ -2360,7 +2370,7 @@ class EventViewSet(
# sub_event prefetches are handled in get_queryset()
queryset = (
Event.objects.all()
.select_related("publisher", "created_by", "last_modified_by")
.select_related("data_source", "publisher", "created_by", "last_modified_by")
.prefetch_related(
"audience",
"external_links",
Expand Down
7 changes: 7 additions & 0 deletions events/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,10 @@ class Meta:


class EditableLinkedEventsObjectSerializer(LinkedEventsSerializer):
has_user_editable_resources = serializers.BooleanField(
source="is_user_editable_resources", read_only=True
)

def create(self, validated_data):
if "data_source" not in validated_data:
validated_data["data_source"] = self.context["data_source"]
Expand Down Expand Up @@ -299,6 +303,9 @@ class KeywordSetSerializer(LinkedEventsSerializer):
last_modified_time = DateTimeField(
default_timezone=ZoneInfo("UTC"), required=False, allow_null=True
)
has_user_editable_resources = serializers.BooleanField(
source="is_user_editable_resources", read_only=True
)

def to_internal_value(self, data):
# extracting ids from the '@id':'http://testserver/v1/keyword/system:tunnettu_avainsana/' type record
Expand Down
1 change: 1 addition & 0 deletions events/tests/test_event_get.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ def assert_event_fields_exist(data, version="v1"):
"images",
"in_language",
"info_url",
"has_user_editable_resources",
"keywords",
"last_modified_time",
"location",
Expand Down
1 change: 1 addition & 0 deletions events/tests/test_event_images_v1.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ def assert_image_fields_exist(data, version="v1"):
"created_time",
"cropping",
"id",
"has_user_editable_resources",
"url",
"last_modified_time",
"license",
Expand Down

0 comments on commit d802de2

Please sign in to comment.