Example #1
0
    async def patch(self, *args, **kwargs):
        user = self.current_user
        res = {}
        data = self.request.body.decode("utf-8")
        data = json.loads(data)
        form = UserForm.from_json(data)
        if form.validate():

            username = form.username.data
            #判断用户名是否重复
            query = UserProfile.select().where(
                UserProfile.username == username)
            count = await self.application.objects.count(query)
            if username == user.username:
                count = 0

            if count:
                res = {"username": "******"}
                self.set_status(400)

            else:
                user.gender = form.gender.data
                user.bio = form.bio.data
                user.birthday = form.birthday.data
                user.address = form.address.data
                user.username = username
                await self.application.objects.update(user)
                res = {
                    "id": user.id,
                    "username": user.username,
                    "gender": user.gender,
                    "bio": user.bio,
                    "birthday": user.birthday,
                    "address": user.address,
                    "icon": user.icon
                }

        else:
            self.set_status(400)
            for field in form.errors:
                res[field] = form.errors[field][0]

        self.finish(json.dumps(res, default=utils.json_serial))