Skip to content

Commit

Permalink
-refactors mixin and updates the files that use it
Browse files Browse the repository at this point in the history
-adds extra check to credit transfers details page
  • Loading branch information
emi-hi committed Oct 26, 2024
1 parent df6cdbf commit 3d8b89f
Show file tree
Hide file tree
Showing 13 changed files with 121 additions and 186 deletions.
35 changes: 35 additions & 0 deletions backend/api/mixins/user_mixin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
from rest_framework import serializers
from ..models.user_profile import UserProfile
from ..serializers.user import UserBasicSerializer
from ..serializers.organization import OrganizationNameSerializer

def get_user_data(obj, user_attr, request):
user_attr_value = getattr(obj, user_attr, None)

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

if not user_profile.is_government or request.user.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 UserMixin:
def get_create_user(self, obj):
# Call get_user_data with the appropriate user attribute 'create_user'
return get_user_data(obj, 'create_user', self.context.get('request'))

def get_update_user(self, obj):
# Call get_user_data with the appropriate user attribute 'update_user'
return get_user_data(obj, 'update_user', self.context.get('request'))
21 changes: 3 additions & 18 deletions backend/api/serializers/credit_agreement.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,11 @@
from .organization import OrganizationSerializer
from api.models.credit_agreement_history import CreditAgreementHistory
from api.services.minio import minio_remove_object
from ..mixins.user_mixin import get_user_data


class CreditAgreementBaseSerializer:
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
return get_user_data(obj, 'update_user', self.context.get('request'))


class CreditAgreementSerializer(ModelSerializer, CreditAgreementBaseSerializer):
Expand All @@ -42,15 +36,6 @@ class CreditAgreementSerializer(ModelSerializer, CreditAgreementBaseSerializer):
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):
agreement_comment = CreditAgreementComment.objects.filter(
credit_agreement=obj
Expand Down
15 changes: 4 additions & 11 deletions backend/api/serializers/credit_agreement_comment.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,17 @@
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 ..mixins.user_mixin import get_user_data


class CreditAgreementCommentSerializer(ModelSerializer):
class CreditAgreementCommentSerializer(ModelSerializer,):
"""
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

return get_user_data(obj, 'create_user', self.context.get('request'))

class Meta:
model = CreditAgreementComment
fields = (
Expand Down
17 changes: 3 additions & 14 deletions backend/api/serializers/credit_transfer.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,24 +22,13 @@
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 ..mixins.user_mixin import get_user_data

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

return get_user_data(obj, 'update_user', self.context.get('request'))
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
return get_user_data(obj, 'create_user', self.context.get('request'))

def get_history(self, obj):
request = self.context.get('request')
Expand Down
10 changes: 2 additions & 8 deletions backend/api/serializers/credit_transfer_comment.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from api.models.user_profile import UserProfile
from api.models.credit_transfer_comment import CreditTransferComment
from api.serializers.user import MemberSerializer

from ..mixins.user_mixin import get_user_data

class CreditTransferCommentSerializer(ModelSerializer):
"""
Expand All @@ -15,13 +15,7 @@ class CreditTransferCommentSerializer(ModelSerializer):
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

return get_user_data(obj, 'create_user', self.context.get('request'))
class Meta:
model = CreditTransferComment
fields = (
Expand Down
13 changes: 3 additions & 10 deletions backend/api/serializers/model_year_report_assessment_comment.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
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 ..mixins.user_mixin import get_user_data

class ModelYearReportAssessmentCommentSerializer(ModelSerializer):
"""
Expand All @@ -12,13 +10,8 @@ class ModelYearReportAssessmentCommentSerializer(ModelSerializer):
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

return get_user_data(obj, 'create_user', self.context.get('request'))

class Meta:
model = ModelYearReportAssessmentComment
fields = (
Expand Down
22 changes: 7 additions & 15 deletions backend/api/serializers/model_year_report_history.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,17 @@
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 ..models.model_year_report_history import ModelYearReportHistory
from ..models.model_year_report_statuses import ModelYearReportStatuses
from ..mixins.user_mixin import get_user_data

class ModelYearReportHistorySerializer(ModelSerializer):

create_user = SerializerMethodField()
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')

def get_create_user(self, obj):
return get_user_data(obj, 'create_user', self.context.get('request'))
36 changes: 14 additions & 22 deletions backend/api/serializers/model_year_report_noa.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,18 @@
from api.models.supplemental_report import SupplementalReport
from api.models.user_profile import UserProfile
from api.serializers.user import MemberSerializer
from ..mixins.user_mixin import get_user_data
from django.db.models import Q
from api.utilities.report_history import exclude_from_history

class ModelYearReportNOABaseSerializer:
def get_update_user(self, obj):
return get_user_data(obj, 'update_user', self.context.get('request'))

def get_create_user(self, obj):
return get_user_data(obj, 'create_user', self.context.get('request'))

class ModelYearReportNoaSerializer(ModelSerializer):
class ModelYearReportNoaSerializer(ModelSerializer,):
validation_status = SerializerMethodField()

def get_validation_status(self, obj):
Expand All @@ -27,21 +34,14 @@ class Meta:
)


class SupplementalNOASerializer(ModelSerializer):
class SupplementalNOASerializer(ModelSerializer, ModelYearReportNOABaseSerializer):
status = SerializerMethodField()
update_user = SerializerMethodField()
is_reassessment = SerializerMethodField()
display_superseded_text = SerializerMethodField()

def get_status(self, obj):
return obj.validation_status.value

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

def get_display_superseded_text(self, obj):
if obj.validation_status == ModelYearReportStatuses.ASSESSED:
model_year_report_id = SupplementalReport.objects.filter(
Expand Down Expand Up @@ -71,12 +71,12 @@ def get_is_reassessment(self, obj):
class Meta:
model = SupplementalReportHistory
fields = (
'update_timestamp', 'status', 'id', 'update_user', 'supplemental_report_id', 'display_superseded_text',
'update_timestamp', 'status', 'id', 'supplemental_report_id', 'display_superseded_text',
'is_reassessment'
)


class ModelYearReportHistorySerializer(ModelSerializer):
class ModelYearReportHistorySerializer(ModelSerializer, ModelYearReportNOABaseSerializer):
status = SerializerMethodField()
create_user = SerializerMethodField()
is_reassessment = SerializerMethodField()
Expand All @@ -91,15 +91,6 @@ def get_is_reassessment(self, obj):
def get_status(self, obj):
return obj.validation_status.value

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

serializer = MemberSerializer(user)

return serializer.data

class Meta:
model = ModelYearReportHistory
fields = (
Expand All @@ -113,7 +104,7 @@ class Meta:
fields = ModelYearReportHistorySerializer.Meta.fields + ('supplemental_report_id',)


class SupplementalReportSerializer(ModelSerializer):
class SupplementalReportSerializer(ModelSerializer, ModelYearReportNOABaseSerializer):
status = SerializerMethodField()
history = SerializerMethodField()
is_supplementary = SerializerMethodField()
Expand Down Expand Up @@ -156,11 +147,12 @@ class Meta:
)


class SupplementalModelYearReportSerializer(ModelSerializer):
class SupplementalModelYearReportSerializer(ModelSerializer, ModelYearReportNOABaseSerializer):
status = SerializerMethodField()
history = SerializerMethodField()
supplemental_id = SerializerMethodField()
is_supplementary = SerializerMethodField()
update_user = SerializerMethodField()

def get_is_supplementary(self, obj):
return False
Expand Down
35 changes: 16 additions & 19 deletions backend/api/serializers/model_year_report_supplemental.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,19 @@
from api.serializers.user import MemberSerializer
from api.models.supplemental_report_supplier_information import \
SupplementalReportSupplierInformation
from ..mixins.user_mixin import get_user_data

class ModelYearReportSupplementalBaseSerializer():

def get_update_user(self, obj):
return get_user_data(obj, 'update_user', self.context.get('request'))
def get_create_user(self, obj):
return get_user_data(obj, 'create_user', self.context.get('request'))

class ModelYearReportZevSalesSerializer(ModelSerializer, ModelYearReportSupplementalBaseSerializer):
create_user = SerializerMethodField()
update_user = SerializerMethodField()

class ModelYearReportZevSalesSerializer(ModelSerializer):
class Meta:
model = SupplementalReportSales
fields = '__all__'
Expand All @@ -49,7 +59,9 @@ class Meta:
)


class ModelYearReportSupplementalCommentSerializer(ModelSerializer):
class ModelYearReportSupplementalCommentSerializer(ModelSerializer, ModelYearReportSupplementalBaseSerializer):
create_user = SerializerMethodField()

class Meta:
model = SupplementalReportComment
fields = (
Expand All @@ -60,20 +72,12 @@ class Meta:
)


class SupplementalReportAssessmentCommentSerializer(ModelSerializer):
class SupplementalReportAssessmentCommentSerializer(ModelSerializer, ModelYearReportSupplementalBaseSerializer):
"""
Serializer for supplemental report 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 = SupplementalReportAssessmentComment
fields = (
Expand Down Expand Up @@ -213,7 +217,7 @@ class Meta:
)


class ModelYearReportSupplementalSerializer(ModelSerializer):
class ModelYearReportSupplementalSerializer(ModelSerializer, ModelYearReportSupplementalBaseSerializer):
status = EnumField(ModelYearReportStatuses)
credit_activity = SerializerMethodField()
supplier_information = SerializerMethodField()
Expand Down Expand Up @@ -279,13 +283,6 @@ def get_reassessment(self, obj):
'status': obj.status.value
}

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

def get_actual_status(self, obj):
request = self.context.get('request')
# is this a reassessment report? if so this is the actual status
Expand Down
Loading

0 comments on commit 3d8b89f

Please sign in to comment.