Skip to content

Commit

Permalink
Fix openapi types for created_by and modified_by
Browse files Browse the repository at this point in the history
  • Loading branch information
hsong-rh committed Jan 15, 2025
1 parent 1d12765 commit 3a20b18
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 16 deletions.
15 changes: 11 additions & 4 deletions src/aap_eda/api/serializers/activation.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
)
from aap_eda.api.serializers.eda_credential import EdaCredentialSerializer
from aap_eda.api.serializers.event_stream import EventStreamOutSerializer
from aap_eda.api.serializers.fields.basic_user import BasicUserFieldSerializer
from aap_eda.api.serializers.fields.yaml import YAMLSerializerField
from aap_eda.api.serializers.organization import OrganizationRefSerializer
from aap_eda.api.serializers.project import (
Expand Down Expand Up @@ -290,6 +291,8 @@ class ActivationListSerializer(serializers.ModelSerializer):
allow_null=True,
child=EventStreamOutSerializer(),
)
created_by = BasicUserFieldSerializer()
modified_by = BasicUserFieldSerializer()

class Meta:
model = models.Activation
Expand All @@ -312,6 +315,8 @@ class Meta:
"rules_fired_count",
"created_at",
"modified_at",
"created_by",
"modified_by",
"status_message",
"awx_token_id",
"log_level",
Expand Down Expand Up @@ -565,6 +570,8 @@ class ActivationReadSerializer(serializers.ModelSerializer):
allow_null=True,
child=EventStreamOutSerializer(),
)
created_by = BasicUserFieldSerializer()
modified_by = BasicUserFieldSerializer()

class Meta:
model = models.Activation
Expand All @@ -590,6 +597,8 @@ class Meta:
"rules_fired_count",
"created_at",
"modified_at",
"created_by",
"modified_by",
"restarted_at",
"status_message",
"awx_token_id",
Expand Down Expand Up @@ -660,8 +669,6 @@ def to_representation(self, activation):
if activation.ruleset_stats
else ""
)
created_by = BasicUserSerializer(activation.created_by).data
modified_by = BasicUserSerializer(activation.modified_by).data

return {
"id": activation.id,
Expand Down Expand Up @@ -696,8 +703,8 @@ def to_representation(self, activation):
"event_streams": event_streams,
"source_mappings": activation.source_mappings,
"skip_audit_events": activation.skip_audit_events,
"created_by": created_by,
"modified_by": modified_by,
"created_by": BasicUserSerializer(activation.created_by).data,
"modified_by": BasicUserSerializer(activation.modified_by).data,
}


Expand Down
12 changes: 8 additions & 4 deletions src/aap_eda/api/serializers/decision_environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,27 +15,31 @@
from rest_framework import serializers

from aap_eda.api.serializers.eda_credential import EdaCredentialRefSerializer
from aap_eda.api.serializers.fields.basic_user import BasicUserFieldSerializer
from aap_eda.api.serializers.organization import OrganizationRefSerializer
from aap_eda.api.serializers.user import BasicUserSerializer
from aap_eda.core import models, validators


class DecisionEnvironmentSerializer(serializers.ModelSerializer):
created_by = BasicUserFieldSerializer()
modified_by = BasicUserFieldSerializer()

class Meta:
model = models.DecisionEnvironment
read_only_fields = [
"id",
"created_at",
"modified_at",
"created_by",
"modified_by",
]
fields = [
"name",
"description",
"image_url",
"organization_id",
"eda_credential_id",
"created_by",
"modified_by",
*read_only_fields,
]

Expand Down Expand Up @@ -93,6 +97,8 @@ class DecisionEnvironmentReadSerializer(serializers.ModelSerializer):
required=False, allow_null=True
)
organization = OrganizationRefSerializer()
created_by = BasicUserFieldSerializer()
modified_by = BasicUserFieldSerializer()

class Meta:
model = models.DecisionEnvironment
Expand All @@ -112,8 +118,6 @@ class Meta:
"id",
"created_at",
"modified_at",
"created_by",
"modified_by",
]

def to_representation(self, decision_environment):
Expand Down
7 changes: 5 additions & 2 deletions src/aap_eda/api/serializers/eda_credential.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from rest_framework import serializers

from aap_eda.api.serializers.credential_type import CredentialTypeRefSerializer
from aap_eda.api.serializers.fields.basic_user import BasicUserFieldSerializer
from aap_eda.api.serializers.organization import OrganizationRefSerializer
from aap_eda.api.serializers.user import BasicUserSerializer
from aap_eda.core import models, validators
Expand Down Expand Up @@ -51,15 +52,15 @@ class EdaCredentialSerializer(serializers.ModelSerializer):
)
organization = OrganizationRefSerializer()
references = EdaCredentialReferenceField(required=False, allow_null=True)
created_by = BasicUserFieldSerializer()
modified_by = BasicUserFieldSerializer()

class Meta:
model = models.EdaCredential
read_only_fields = [
"id",
"created_at",
"modified_at",
"created_by",
"modified_by",
"managed",
"organization",
]
Expand All @@ -69,6 +70,8 @@ class Meta:
"inputs",
"credential_type",
"references",
"created_by",
"modified_by",
*read_only_fields,
]

Expand Down
7 changes: 5 additions & 2 deletions src/aap_eda/api/serializers/event_stream.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
from rest_framework import serializers

from aap_eda.api.serializers.eda_credential import EdaCredentialRefSerializer
from aap_eda.api.serializers.fields.basic_user import BasicUserFieldSerializer
from aap_eda.api.serializers.organization import OrganizationRefSerializer
from aap_eda.api.serializers.user import BasicUserSerializer
from aap_eda.core import enums, models, validators
Expand Down Expand Up @@ -83,6 +84,8 @@ class EventStreamOutSerializer(serializers.ModelSerializer):
required=True, allow_null=False
)
url = serializers.SerializerMethodField()
created_by = BasicUserFieldSerializer()
modified_by = BasicUserFieldSerializer()

class Meta:
model = models.EventStream
Expand All @@ -98,8 +101,6 @@ class Meta:
"test_headers",
"events_received",
"last_event_received_at",
"created_by",
"modified_by",
]
fields = [
"name",
Expand All @@ -108,6 +109,8 @@ class Meta:
"organization",
"eda_credential",
"event_stream_type",
"created_by",
"modified_by",
*read_only_fields,
]

Expand Down
34 changes: 34 additions & 0 deletions src/aap_eda/api/serializers/fields/basic_user.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Copyright 2024 Red Hat, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
from drf_spectacular.utils import extend_schema_field
from rest_framework import serializers


@extend_schema_field(
{
"type": "string",
"properties": {
"id": {"type": "integer"},
"username": {"type": "string"},
"first_name": {"type": "string"},
"last_name": {"type": "string"},
},
}
)
class BasicUserFieldSerializer(serializers.JSONField):
"""Serializer for Basic User Field."""

def to_representation(self, value) -> dict:
return value
15 changes: 11 additions & 4 deletions src/aap_eda/api/serializers/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
from rest_framework.validators import UniqueValidator

from aap_eda.api.serializers.eda_credential import EdaCredentialRefSerializer
from aap_eda.api.serializers.fields.basic_user import BasicUserFieldSerializer
from aap_eda.api.serializers.organization import OrganizationRefSerializer
from aap_eda.api.serializers.user import BasicUserSerializer
from aap_eda.core import models, validators
Expand All @@ -43,6 +44,8 @@ class ProjectSerializer(serializers.ModelSerializer, ProxyFieldMixin):
)

proxy = serializers.SerializerMethodField()
created_by = BasicUserFieldSerializer()
modified_by = BasicUserFieldSerializer()

class Meta:
model = models.Project
Expand All @@ -55,8 +58,6 @@ class Meta:
"import_error",
"created_at",
"modified_at",
"created_by",
"modified_by",
]
fields = [
"name",
Expand All @@ -68,6 +69,8 @@ class Meta:
"scm_refspec",
"verify_ssl",
"proxy",
"created_by",
"modified_by",
*read_only_fields,
]

Expand Down Expand Up @@ -187,6 +190,8 @@ class ProjectUpdateRequestSerializer(serializers.ModelSerializer):
allow_null=True,
help_text="Proxy server for http or https connection",
)
created_by = BasicUserFieldSerializer()
modified_by = BasicUserFieldSerializer()

class Meta:
model = models.Project
Expand Down Expand Up @@ -238,6 +243,8 @@ class ProjectReadSerializer(serializers.ModelSerializer, ProxyFieldMixin):
required=False, allow_null=True
)
proxy = serializers.SerializerMethodField()
created_by = BasicUserFieldSerializer()
modified_by = BasicUserFieldSerializer()

class Meta:
model = models.Project
Expand All @@ -250,8 +257,6 @@ class Meta:
"import_error",
"created_at",
"modified_at",
"created_by",
"modified_by",
]
fields = [
"name",
Expand All @@ -263,6 +268,8 @@ class Meta:
"scm_branch",
"scm_refspec",
"proxy",
"created_by",
"modified_by",
*read_only_fields,
]

Expand Down

0 comments on commit 3a20b18

Please sign in to comment.