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 2313: Endpoint Testing #2335

Closed
wants to merge 9 commits into from
Closed
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
129 changes: 0 additions & 129 deletions backend/.s2i/bin/assemble

This file was deleted.

1 change: 0 additions & 1 deletion backend/.s2i/environment

This file was deleted.

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
Loading