Skip to content

Commit

Permalink
fix: removes unecessary data from transfer -- build-on-dev (#2241)
Browse files Browse the repository at this point in the history
* fix: removes unecessary data from transfer

* fix: new user serializer passes only basic user info to frontend
-get history modified so that approved and disapproved statuses created by government are  excluded so that communications/status changes within government are not shown in network tab

* removes id from user basic serializer
  • Loading branch information
emi-hi authored Sep 13, 2024
1 parent 7cfadf9 commit 1ab09ab
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 11 deletions.
22 changes: 12 additions & 10 deletions backend/api/serializers/credit_transfer.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
CreditTransferCommentSerializer
from api.serializers.credit_transfer_content import \
CreditTransferContentSerializer, CreditTransferContentSaveSerializer
from api.serializers.user import MemberSerializer, UserSerializer
from api.serializers.organization import OrganizationSerializer
from api.serializers.user import UserBasicSerializer
from api.serializers.organization import OrganizationNameSerializer
from api.services.credit_transaction import calculate_insufficient_credits
from api.services.send_email import notifications_credit_transfers

Expand All @@ -29,15 +29,15 @@ def get_update_user(self, obj):
user_profile = UserProfile.objects.filter(username=obj.update_user)

if user_profile.exists():
serializer = MemberSerializer(user_profile.first(), read_only=True)
serializer = UserBasicSerializer(user_profile.first(), read_only=True)
return serializer.data

return obj.update_user

def get_create_user(self, obj):
user_profile = UserProfile.objects.filter(username=obj.create_user)
if user_profile.exists():
serializer = UserSerializer(user_profile.first(), read_only=True)
serializer = UserBasicSerializer(user_profile.first(), read_only=True)
return serializer.data
return obj.create_user

Expand All @@ -47,6 +47,8 @@ def get_history(self, obj):
history = CreditTransferHistory.objects.filter(
transfer_id=obj.id)
else:
create_user_subquery = UserProfile.objects.filter(organization__is_government=True).values_list('username', flat=True)

history = CreditTransferHistory.objects.filter(
transfer_id=obj.id,
status__in=[
Expand All @@ -58,8 +60,8 @@ def get_history(self, obj):
CreditTransferStatuses.RESCIND_PRE_APPROVAL,
CreditTransferStatuses.REJECTED,
CreditTransferStatuses.VALIDATED
])
serializer = CreditTransferHistorySerializer(history, many=True, read_only=True)
]).exclude(create_user__in=create_user_subquery, status__in=[CreditTransferStatuses.APPROVED, CreditTransferStatuses.DISAPPROVED,])
serializer = CreditTransferHistorySerializer(history, many=True, read_only=True, context={'request': request})
return serializer.data


Expand Down Expand Up @@ -97,11 +99,11 @@ class CreditTransferListSerializer(
CreditTransferBaseSerializer
):
history = SerializerMethodField()
credit_to = OrganizationSerializer()
credit_to = OrganizationNameSerializer()
credit_transfer_content = CreditTransferContentSerializer(
many=True, read_only=True
)
debit_from = OrganizationSerializer()
debit_from = OrganizationNameSerializer()
status = SerializerMethodField()
update_user = SerializerMethodField()

Expand All @@ -128,11 +130,11 @@ class CreditTransferSerializer(
CreditTransferBaseSerializer
):
history = SerializerMethodField()
credit_to = OrganizationSerializer()
credit_to = OrganizationNameSerializer()
credit_transfer_content = CreditTransferContentSerializer(
many=True, read_only=True
)
debit_from = OrganizationSerializer()
debit_from = OrganizationNameSerializer()
status = SerializerMethodField()
update_user = SerializerMethodField()
sufficient_credits = SerializerMethodField()
Expand Down
10 changes: 10 additions & 0 deletions backend/api/serializers/organization.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,16 @@
OrganizationLDVSalesSerializer
from api.models.model_year import ModelYear

class OrganizationNameSerializer(serializers.ModelSerializer):
"""
Serializer for passing just the necessary info for credit transfers etc
"""
class Meta:
model = Organization
fields = (
'name', 'short_name', 'is_government'
)


class OrganizationSerializer(serializers.ModelSerializer):
"""
Expand Down
13 changes: 12 additions & 1 deletion backend/api/serializers/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from api.models.role import Role
from api.models.user_profile import UserProfile
from api.services.user import update_roles, create_default_user_notification_settings
from .organization import OrganizationSerializer
from .organization import OrganizationSerializer, OrganizationNameSerializer
from .permission import PermissionSerializer
from .role import RoleSerializer

Expand All @@ -30,6 +30,17 @@ class Meta:
)


class UserBasicSerializer(serializers.ModelSerializer):
"""
Serializer for the basic details of a user
"""
organization = OrganizationNameSerializer(read_only=True)
class Meta:
model = UserProfile
fields = (
'display_name', 'organization'
)

class UserSerializer(serializers.ModelSerializer):
"""
Serializer for the full details of the User
Expand Down

0 comments on commit 1ab09ab

Please sign in to comment.