코드리뷰[2]

BackEnd_Ash.log·2020년 3월 21일
0
class ProfileUpdate(View):
    @login_requested
    def post(self, request):
        data = json.loads(request.body)
        profile = Account.objects.get(id = request.agent.id)
        try:
            if data.get('username'):
                profile.username = data.get('username')
            if data.get('phone'):
                profile.phone    = data.get('phone')
            if data.get('email'):
                profile.email    = data.get('email')
            profile.save()
            return JsonResponse({'message':'USERINFO_CHANGED'}, status=200)
        except KeyError:
            return JsonResponse({'message':'INVALID_KEY'}, status=400)


    @login_requested
    def get(self, request):
        profile = Account.objects.get(id = request.agent.id)
        try:
            agent_profile = {
                "username"      : profile.username,
                "email"         : profile.email,
                "phone"         : profile.phone
            }
            return JsonResponse({"agent_profile": agent_profile}, status=200)
        except KeyError:
            return JsonResponse({"message":"INVALID_KEY"}, status=400)

에서

class ProfileUpdate(View):
    @login_requested
    def post(self, request):
        data = json.loads(request.body)
        profile = Account.objects.get(id = request.agent.id)

        try:
            profile.username = data.get('username' , None)
            profile.phone    = data.get('phone' , None)
            profile.email    = data.get('email' , None)
            profile.save()
            return JsonResponse(status=200)

        except KeyError:
            return JsonResponse({'message':'INVALID_KEY'}, status=400)

    @login_requested
    def get(self, request):
        try:
            profile = Account.objects.get(id = request.agent.id).values('username' , ' email' , 'phone')

            return JsonResponse({"agent_profile": agent_profile}, status=200)
        except KeyError:
            return JsonResponse({"message":"INVALID_KEY"}, status=400)
        except Account.DoesNotExist:
            return JsonResponse({"message": 'something else'} , status=401)

으로 바꿈.

profile
꾸준함이란 ... ?

0개의 댓글