fix admin update user real_name problem

This commit is contained in:
zema1 2017-12-10 09:58:08 +08:00
parent 092bf9a73f
commit 10ecb79152
3 changed files with 16 additions and 2 deletions

View File

@ -69,15 +69,24 @@ class UserSerializer(serializers.ModelSerializer):
class UserProfileSerializer(serializers.ModelSerializer):
user = UserSerializer()
real_name = serializers.SerializerMethodField()
class Meta:
model = UserProfile
fields = "__all__"
def __init__(self, *args, **kwargs):
self.show_real_name = kwargs.pop("show_real_name", False)
super(UserProfileSerializer, self).__init__(*args, **kwargs)
def get_real_name(self, obj):
return obj.real_name if self.show_real_name else None
class EditUserSerializer(serializers.Serializer):
id = serializers.IntegerField()
username = serializers.CharField(max_length=32)
real_name = serializers.CharField(max_length=32, allow_blank=True)
password = serializers.CharField(min_length=6, allow_blank=True, required=False, default=None)
email = serializers.EmailField(max_length=64)
admin_type = serializers.ChoiceField(choices=(AdminType.REGULAR_USER, AdminType.ADMIN, AdminType.SUPER_ADMIN))

View File

@ -95,6 +95,8 @@ class UserAdminAPI(APIView):
user.save()
if pre_username != user.username:
Submission.objects.filter(username=pre_username).update(username=user.username)
UserProfile.objects.filter(user=user).update(real_name=data["real_name"])
return self.success(UserAdminSerializer(user).data)
@super_admin_required

View File

@ -37,15 +37,18 @@ class UserProfileAPI(APIView):
user = request.user
if not user.is_authenticated():
return self.success()
show_real_name = False
username = request.GET.get("username")
try:
if username:
user = User.objects.get(username=username, is_disabled=False)
else:
user = request.user
# api返回的是自己的信息可以返real_name
show_real_name = True
except User.DoesNotExist:
return self.error("User does not exist")
return self.success(UserProfileSerializer(user.userprofile).data)
return self.success(UserProfileSerializer(user.userprofile, show_real_name=show_real_name).data)
@validate_serializer(EditUserProfileSerializer)
@login_required
@ -55,7 +58,7 @@ class UserProfileAPI(APIView):
for k, v in data.items():
setattr(user_profile, k, v)
user_profile.save()
return self.success(UserProfileSerializer(user_profile).data)
return self.success(UserProfileSerializer(user_profile, show_real_name=True).data)
class AvatarUploadAPI(APIView):