fix bugs
This commit is contained in:
1 parent
3826c9a503
commit
1b56810cc7
5 files changed
+101
-80
No files matched your search
@@ -397,16 +397,24 @@ def list_users():
|
||||
@require_admin
|
||||
def create_user():
|
||||
data = request.get_json() or {}
|
||||
if not data.get('username'):
|
||||
raise AppError(code='USER.USERNAME_REQUIRED', message='Username is required', http_status=400, details={'field': 'username'})
|
||||
required_field_errors = (
|
||||
('username', 'USER.USERNAME_REQUIRED', 'Username is required'),
|
||||
('password', 'USER.PASSWORD_REQUIRED', 'Password is required'),
|
||||
('email', 'USER.EMAIL_REQUIRED', 'Email is required'),
|
||||
('real_name', 'USER.REAL_NAME_REQUIRED', 'Real name is required'),
|
||||
('employee_id', 'USER.EMPLOYEE_ID_REQUIRED', 'Employee ID is required'),
|
||||
('phone_number', 'USER.PHONE_REQUIRED', 'Phone number is required'),
|
||||
)
|
||||
for field, code, message in required_field_errors:
|
||||
if _empty_to_none(data.get(field)) is None:
|
||||
raise AppError(code=code, message=message, http_status=400, details={'field': field})
|
||||
if db.session.query(User).filter_by(username=data['username']).first():
|
||||
raise ConflictError('Username already exists', code='USER.USERNAME_EXISTS', details={'field': 'username'})
|
||||
email = _empty_to_none(data.get('email'))
|
||||
if email is not None and (not _is_valid_email(email)):
|
||||
if not _is_valid_email(email):
|
||||
raise AppError(code='USER.EMAIL_INVALID', message='Invalid email format', http_status=400, details={'field': 'email'})
|
||||
user = User(username=data['username'], email=email, department=_empty_to_none(data.get('department')), role=data.get('role', 'user'), status=data.get('status', 'active'), real_name=_empty_to_none(data.get('real_name')), employee_id=_empty_to_none(data.get('employee_id')), phone_number=_empty_to_none(data.get('phone_number')))
|
||||
if data.get('password'):
|
||||
user.set_password(data['password'])
|
||||
user.set_password(data['password'])
|
||||
if data.get('agent_ids'):
|
||||
agents = db.session.query(Agent).filter(Agent.id.in_(data['agent_ids'])).all()
|
||||
user.agents = agents
|
||||
|
||||
Reference in new issue
Block a user