-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
-refactors mixin and updates the files that use it
-adds extra check to credit transfers details page
- Loading branch information
Showing
13 changed files
with
121 additions
and
186 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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')) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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')) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.