update password change

This commit is contained in:
long2ice
2020-07-10 15:08:05 +08:00
parent 8f9ee4f8ae
commit 51ebc64649
2 changed files with 8 additions and 3 deletions

View File

@@ -20,8 +20,6 @@ async def handle_m2m_fields_create_or_update(
:return:
"""
copy_body = deepcopy(body)
if model == user_model:
copy_body["password"] = pwd_context.hash(copy_body.pop("password"))
m2m_body = {}
for k, v in body.items():
if k in m2m_fields:
@@ -29,6 +27,11 @@ async def handle_m2m_fields_create_or_update(
if create:
obj = await model.create(**copy_body)
else:
password = copy_body.pop("password", None)
if model == user_model:
user = await user_model.get(pk=pk)
if user.password != password:
copy_body["password"] = pwd_context.hash(password)
await model.filter(pk=pk).update(**copy_body)
obj = await model.get(pk=pk)
for k, v in m2m_body.items():

View File

@@ -5,7 +5,9 @@ from fastapi_admin import enums
class AbstractUser(Model):
username = fields.CharField(max_length=20, unique=True)
password = fields.CharField(max_length=200, description="Will auto hash with raw password")
password = fields.CharField(
max_length=200, description="Will auto hash with raw password when change"
)
class Meta:
abstract = True