Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chore/2267 - remove commentator's name #2320

Merged
merged 7 commits into from
Oct 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions backend/api/mixins/user_mixin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
from rest_framework.serializers import ModelSerializer, SerializerMethodField
from api.models.user_profile import UserProfile
from api.serializers.user import UserBasicSerializer
from api.serializers.organization import OrganizationNameSerializer


def get_user_data(username, request):
if username is None:
return f"{username} does not exist on the object."
user_profile = UserProfile.objects.filter(username=username).first()
if not user_profile:
return {
"display_name": username
} # Return the username if the user profile doesn't exist

is_government = False
if request is not None:
is_government = request.user.is_government

if not user_profile.is_government or is_government:
# If the user is non-government or the requesting user is government, return full data
serializer = UserBasicSerializer(user_profile, read_only=True)
return serializer.data
else:
# If the requesting user is non-government and the user is government, limit info
organization = OrganizationNameSerializer(
user_profile.organization, read_only=True
)
return {
"display_name": "Government User",
"is_government": user_profile.is_government,
"organization": organization.data,
}


class UserSerializerMixin(ModelSerializer):
create_user = SerializerMethodField()

update_user = SerializerMethodField()

def get_create_user(self, obj):
username = getattr(obj, "create_user", None)
request = self.context.get("request")
return get_user_data(username, request)

def get_update_user(self, obj):
username = getattr(obj, "update_user", None)
request = self.context.get("request")
return get_user_data(username, request)
34 changes: 5 additions & 29 deletions backend/api/serializers/credit_agreement.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,29 +9,16 @@
from api.models.credit_agreement_transaction_types import CreditAgreementTransactionTypes
from api.models.credit_class import CreditClass
from api.models.model_year import ModelYear
from api.models.user_profile import UserProfile
from api.serializers.user import MemberSerializer
from api.serializers.credit_agreement_attachment import CreditAgreementAttachmentSerializer
from api.serializers.credit_agreement_comment import CreditAgreementCommentSerializer
from api.serializers.credit_agreement_content import \
CreditAgreementContentSerializer
from .organization import OrganizationSerializer
from api.models.credit_agreement_history import CreditAgreementHistory
from api.services.minio import minio_remove_object
from api.mixins.user_mixin import UserSerializerMixin


class CreditAgreementBaseSerializer:
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)
return serializer.data

return obj.update_user


class CreditAgreementSerializer(ModelSerializer, CreditAgreementBaseSerializer):
class CreditAgreementSerializer(UserSerializerMixin):
organization = OrganizationSerializer(read_only=True)
transaction_type = EnumField(CreditAgreementTransactionTypes)
credit_agreement_content = CreditAgreementContentSerializer(
Expand All @@ -40,25 +27,16 @@ class CreditAgreementSerializer(ModelSerializer, CreditAgreementBaseSerializer):
status = EnumField(CreditAgreementStatuses)
comments = SerializerMethodField()
attachments = SerializerMethodField()
update_user = SerializerMethodField()

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)
return serializer.data

return obj.update_user

def get_comments(self, obj):
request = self.context.get('request')
agreement_comment = CreditAgreementComment.objects.filter(
credit_agreement=obj
).order_by('-create_timestamp')

if agreement_comment.exists():
serializer = CreditAgreementCommentSerializer(
agreement_comment, read_only=True, many=True
agreement_comment, read_only=True, many=True, context={"request": request}
)
return serializer.data

Expand Down Expand Up @@ -287,15 +265,13 @@ class Meta:


class CreditAgreementListSerializer(
ModelSerializer, EnumSupportSerializerMixin, CreditAgreementBaseSerializer
UserSerializerMixin, EnumSupportSerializerMixin
):

organization = OrganizationSerializer()
credit_agreement_content = CreditAgreementContentSerializer(
many=True, read_only=True
)
status = EnumField(CreditAgreementStatuses)
update_user = SerializerMethodField()
transaction_type = EnumField(CreditAgreementTransactionTypes)

class Meta:
Expand Down
19 changes: 3 additions & 16 deletions backend/api/serializers/credit_agreement_comment.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,11 @@
from rest_framework.serializers import ModelSerializer, \
SerializerMethodField
from api.models.credit_agreement_comment import CreditAgreementComment
from api.models.user_profile import UserProfile
from api.serializers.user import MemberSerializer, UserSerializer
from api.mixins.user_mixin import UserSerializerMixin


class CreditAgreementCommentSerializer(ModelSerializer):
class CreditAgreementCommentSerializer(UserSerializerMixin):
"""
Serializer for credit agreement comments
"""
create_user = SerializerMethodField()

def get_create_user(self, obj):
user = UserProfile.objects.filter(username=obj.create_user).first()
if user is None:
return obj.create_user

serializer = MemberSerializer(user, read_only=True)
return serializer.data


class Meta:
model = CreditAgreementComment
fields = (
Expand Down
40 changes: 10 additions & 30 deletions backend/api/serializers/credit_transfer.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,29 +18,12 @@
CreditTransferCommentSerializer
from api.serializers.credit_transfer_content import \
CreditTransferContentSerializer, CreditTransferContentSaveSerializer
from api.serializers.user import UserBasicSerializer
from api.serializers.organization import OrganizationNameSerializer, OrganizationSerializer
from api.services.credit_transaction import calculate_insufficient_credits
from api.services.send_email import notifications_credit_transfers
from api.mixins.user_mixin import UserSerializerMixin


class CreditTransferBaseSerializer:
def get_update_user(self, obj):
user_profile = UserProfile.objects.filter(username=obj.update_user)

if user_profile.exists():
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 = UserBasicSerializer(user_profile.first(), read_only=True)
return serializer.data
return obj.create_user

class CreditTransferBaseSerializer(UserSerializerMixin):
def get_history(self, obj):
request = self.context.get('request')
if request.user.is_government:
Expand All @@ -66,22 +49,21 @@ def get_history(self, obj):


class CreditTransferHistorySerializer(
ModelSerializer, EnumSupportSerializerMixin,
CreditTransferBaseSerializer
CreditTransferBaseSerializer,
EnumSupportSerializerMixin,
):
create_user = SerializerMethodField()
update_user = SerializerMethodField()
status = EnumField(CreditTransferStatuses)
comment = SerializerMethodField()

def get_comment(self, obj):
request = self.context.get('request')
credit_transfer_comment = CreditTransferComment.objects.filter(
credit_transfer_history=obj
).first()

if credit_transfer_comment:
serializer = CreditTransferCommentSerializer(
credit_transfer_comment, read_only=True, many=False
credit_transfer_comment, read_only=True, many=False, context={'request': request}
)
return serializer.data
return None
Expand All @@ -95,8 +77,8 @@ class Meta:


class CreditTransferListSerializer(
ModelSerializer, EnumSupportSerializerMixin,
CreditTransferBaseSerializer
CreditTransferBaseSerializer,
EnumSupportSerializerMixin
):
history = SerializerMethodField()
credit_to = OrganizationNameSerializer()
Expand All @@ -105,7 +87,6 @@ class CreditTransferListSerializer(
)
debit_from = OrganizationNameSerializer()
status = SerializerMethodField()
update_user = SerializerMethodField()

def get_status(self, obj):
request = self.context.get('request')
Expand All @@ -126,8 +107,8 @@ class Meta:


class CreditTransferSerializer(
ModelSerializer, EnumSupportSerializerMixin,
CreditTransferBaseSerializer
CreditTransferBaseSerializer,
EnumSupportSerializerMixin,
):
history = SerializerMethodField()
credit_to = OrganizationNameSerializer()
Expand All @@ -136,7 +117,6 @@ class CreditTransferSerializer(
)
debit_from = OrganizationNameSerializer()
status = SerializerMethodField()
update_user = SerializerMethodField()
sufficient_credits = SerializerMethodField()
pending = SerializerMethodField()

Expand Down
16 changes: 2 additions & 14 deletions backend/api/serializers/credit_transfer_comment.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,14 @@
"""
Credit Transfer Comment Serializer
"""
from rest_framework.serializers import ModelSerializer, SerializerMethodField

from api.models.user_profile import UserProfile
from api.models.credit_transfer_comment import CreditTransferComment
from api.serializers.user import MemberSerializer
from api.mixins.user_mixin import UserSerializerMixin


class CreditTransferCommentSerializer(ModelSerializer):
class CreditTransferCommentSerializer(UserSerializerMixin):
"""
Serializer for Credit Transfer comments
"""
create_user = SerializerMethodField()

def get_create_user(self, obj):
user = UserProfile.objects.filter(username=obj.create_user).first()
if user is None:
return obj.create_user

serializer = MemberSerializer(user, read_only=True)
return serializer.data

class Meta:
model = CreditTransferComment
Expand Down
4 changes: 2 additions & 2 deletions backend/api/serializers/model_year_report.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ def get_makes(self, obj):
def get_statuses(self, obj):
request = self.context.get("request")

return get_model_year_report_statuses(obj, request.user)
return get_model_year_report_statuses(obj, request)

def get_model_year_report_history(self, obj):
request = self.context.get("request")
Expand Down Expand Up @@ -192,7 +192,7 @@ def get_model_year_report_history(self, obj):
create_user__in=users,
)

serializer = ModelYearReportHistorySerializer(history, many=True)
serializer = ModelYearReportHistorySerializer(history, many=True, context={"request": request})

return serializer.data

Expand Down
2 changes: 1 addition & 1 deletion backend/api/serializers/model_year_report_assessment.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ def get_assessment_comment(self, obj):
if not assessment_comment:
return []
serializer = ModelYearReportAssessmentCommentSerializer(
assessment_comment, read_only=True, many=True
assessment_comment, read_only=True, many=True, context={'request': request}
)
return serializer.data

Expand Down
19 changes: 3 additions & 16 deletions backend/api/serializers/model_year_report_assessment_comment.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,11 @@
from rest_framework.serializers import ModelSerializer, \
SerializerMethodField
from api.models.model_year_report_assessment_comment import ModelYearReportAssessmentComment
from api.models.user_profile import UserProfile
from api.serializers.user import MemberSerializer
from api.mixins.user_mixin import UserSerializerMixin


class ModelYearReportAssessmentCommentSerializer(ModelSerializer):
class ModelYearReportAssessmentCommentSerializer(UserSerializerMixin):
"""
Serializer for assessment comments
"""
create_user = SerializerMethodField()

def get_create_user(self, obj):
user = UserProfile.objects.filter(username=obj.create_user).first()
if user is None:
return obj.create_user

serializer = MemberSerializer(user, read_only=True)
return serializer.data


class Meta:
model = ModelYearReportAssessmentComment
fields = (
Expand Down
15 changes: 2 additions & 13 deletions backend/api/serializers/model_year_report_confirmation.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,13 @@
from rest_framework.serializers import ModelSerializer, SerializerMethodField

from api.models.model_year_report_confirmation import \
ModelYearReportConfirmation
from api.models.user_profile import UserProfile
from api.serializers.user import UserSerializer
from api.serializers.signing_authority_assertion import \
SigningAuthorityAssertionSerializer
from api.mixins.user_mixin import UserSerializerMixin


class ModelYearReportConfirmationSerializer(ModelSerializer):
create_user = SerializerMethodField()
class ModelYearReportConfirmationSerializer(UserSerializerMixin):
signing_authority_assertion = SigningAuthorityAssertionSerializer()

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)
return serializer.data

return obj.create_user

class Meta:
model = ModelYearReportConfirmation
Expand Down
20 changes: 4 additions & 16 deletions backend/api/serializers/model_year_report_history.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,13 @@
from enumfields.drf import EnumField
from rest_framework.serializers import ModelSerializer, SerializerMethodField

from api.models.model_year_report_history import ModelYearReportHistory
from api.models.model_year_report_statuses import ModelYearReportStatuses
from api.models.user_profile import UserProfile
from api.serializers.user import MemberSerializer

from api.mixins.user_mixin import UserSerializerMixin

class ModelYearReportHistorySerializer(ModelSerializer):
create_user = SerializerMethodField()
class ModelYearReportHistorySerializer(UserSerializerMixin):

validation_status = EnumField(ModelYearReportStatuses, read_only=True)

def get_create_user(self, obj):
user_profile = UserProfile.objects.filter(username=obj.create_user)

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

return obj.create_user


class Meta:
model = ModelYearReportHistory
fields = ('create_timestamp', 'create_user', 'validation_status')
Loading