Skip to content

Commit

Permalink
Fixed a bug where update_user was throwing a 500 error as we can no l…
Browse files Browse the repository at this point in the history
…onger map via id (#197)
  • Loading branch information
amichard authored Jun 2, 2020
1 parent 90069cc commit e66d77e
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 deletions.
14 changes: 12 additions & 2 deletions backend/api/serializers/sales_submission.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from api.models.record_of_sale import RecordOfSale
from api.models.sales_submission import SalesSubmission
from api.models.sales_submission_statuses import SalesSubmissionStatuses
from api.models.user_profile import UserProfile
from api.serializers.user import MemberSerializer
from api.serializers.organization import OrganizationSerializer
from api.serializers.record_of_sale import RecordOfSaleSerializer
Expand All @@ -17,7 +18,7 @@ class SalesSubmissionListSerializer(
):
organization = OrganizationSerializer(read_only=True)
totals = SerializerMethodField()
update_user = MemberSerializer(read_only=True)
update_user = SerializerMethodField()
validation_status = EnumField(SalesSubmissionStatuses, read_only=True)
total_a_credits = SerializerMethodField()
total_b_credits = SerializerMethodField()
Expand Down Expand Up @@ -49,6 +50,15 @@ def get_total_b_credits(self, obj):

return round(total, 2)

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 Meta:
model = SalesSubmission
fields = (
Expand Down Expand Up @@ -98,7 +108,7 @@ def update(self, instance, validated_data):

if validation_status:
instance.validation_status = validation_status
instance.update_user = request.user
instance.update_user = request.user.username
instance.save()

return instance
Expand Down
8 changes: 2 additions & 6 deletions backend/api/serializers/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,14 @@ class MemberSerializer(serializers.ModelSerializer):
Serializer for getting the details of the user WITHOUT getting the
organization of the user so it doesn't do an infinite loop.
"""
groups = SerializerMethodField()
roles = RoleSerializer(read_only=True, many=True)

def get_groups(self, object):
return list_groups_for_username(get_token(), object.username)

class Meta:
model = UserProfile
fields = (
'id', 'first_name', 'last_name', 'email',
'username', 'display_name', 'is_active', 'phone',
'groups', 'roles'
'display_name', 'is_active', 'phone',
'roles',
)


Expand Down

0 comments on commit e66d77e

Please sign in to comment.