stages/user_write: don't use create_user, create empty object and set attributes

This commit is contained in:
Jens Langhammer 2020-05-28 23:22:15 +02:00
parent b1b63fbb7f
commit 80a36a3441
2 changed files with 31 additions and 24 deletions

View File

@ -25,33 +25,30 @@ class UserWriteStageView(StageView):
LOGGER.debug(message) LOGGER.debug(message)
return self.executor.stage_invalid() return self.executor.stage_invalid()
data = self.executor.plan.context[PLAN_CONTEXT_PROMPT] data = self.executor.plan.context[PLAN_CONTEXT_PROMPT]
if PLAN_CONTEXT_PENDING_USER in self.executor.plan.context: if PLAN_CONTEXT_PENDING_USER not in self.executor.plan.context:
user = self.executor.plan.context[PLAN_CONTEXT_PENDING_USER] self.executor.plan.context[PLAN_CONTEXT_PENDING_USER] = User()
for key, value in data.items():
setter_name = f"set_{key}"
# Check if user has a setter for this key, like set_password
if hasattr(user, setter_name):
setter = getattr(user, setter_name)
if callable(setter):
setter(value)
# User has this key already
elif hasattr(user, key):
setattr(user, key, value)
# Otherwise we just save it as custom attribute
else:
user.attributes[key] = value
user.save()
LOGGER.debug(
"Updated existing user", user=user, flow_slug=self.executor.flow.slug,
)
else:
user = User.objects.create_user(**data)
# Set created user as pending_user, so this can be chained with user_login
self.executor.plan.context[PLAN_CONTEXT_PENDING_USER] = user
self.executor.plan.context[ self.executor.plan.context[
PLAN_CONTEXT_AUTHENTICATION_BACKEND PLAN_CONTEXT_AUTHENTICATION_BACKEND
] = class_to_path(ModelBackend) ] = class_to_path(ModelBackend)
LOGGER.debug( LOGGER.debug(
"Created new user", user=user, flow_slug=self.executor.flow.slug, "Created new user", flow_slug=self.executor.flow.slug,
) )
user = self.executor.plan.context[PLAN_CONTEXT_PENDING_USER]
for key, value in data.items():
setter_name = f"set_{key}"
# Check if user has a setter for this key, like set_password
if hasattr(user, setter_name):
setter = getattr(user, setter_name)
if callable(setter):
setter(value)
# User has this key already
elif hasattr(user, key):
setattr(user, key, value)
# Otherwise we just save it as custom attribute
else:
user.attributes[key] = value
user.save()
LOGGER.debug(
"Updated existing user", user=user, flow_slug=self.executor.flow.slug,
)
return self.executor.stage_ok() return self.executor.stage_ok()

View File

@ -6028,7 +6028,12 @@ definitions:
- e-mail - e-mail
- password - password
- number - number
- checkbox
- data
- data-time
- separator
- hidden - hidden
- static
required: required:
title: Required title: Required
type: boolean type: boolean
@ -6036,6 +6041,11 @@ definitions:
title: Placeholder title: Placeholder
type: string type: string
minLength: 1 minLength: 1
order:
title: Order
type: integer
maximum: 2147483647
minimum: -2147483648
PromptStage: PromptStage:
required: required:
- name - name