Skip to content
Snippets Groups Projects
Commit 3c5117c2 authored by Emmanuel Viennet's avatar Emmanuel Viennet
Browse files

Edition compte user: améliore message erreur si cas_id dupliqué

parent 16e96c24
Branches
No related tags found
No related merge requests found
...@@ -67,14 +67,6 @@ def is_valid_user_name(user_name: str) -> bool: ...@@ -67,14 +67,6 @@ def is_valid_user_name(user_name: str) -> bool:
) )
def is_new_cas_id(cas_id: str) -> bool:
"Check that cas_id is a valid new id (uniqueness, allow nulls)"
if not cas_id:
return True
nb_with_this_id = db.session.query(User).filter_by(cas_id=cas_id).count()
return nb_with_this_id == 0
class User(UserMixin, ScoDocModel): class User(UserMixin, ScoDocModel):
"""ScoDoc users, handled by Flask / SQLAlchemy""" """ScoDoc users, handled by Flask / SQLAlchemy"""
...@@ -435,11 +427,18 @@ class User(UserMixin, ScoDocModel): ...@@ -435,11 +427,18 @@ class User(UserMixin, ScoDocModel):
if cas_id: if cas_id:
new_cas_id = cas_id new_cas_id = cas_id
if new_cas_id != self.cas_id: if new_cas_id != self.cas_id:
if is_new_cas_id(new_cas_id): existing: User = (
db.session.query(User).filter_by(cas_id=new_cas_id).first()
if new_cas_id is not None
else None
)
if not existing:
self.cas_id = new_cas_id self.cas_id = new_cas_id
else: else:
log(f"User.from_dict: CAS id invalide pour {self.user_name}") msg = f"""CAS id invalide pour {self.user_name
raise ScoValueError(f"CAS id invalide pour {self.user_name}") }, déjà utilisé par {existing.user_name}"""
log(f"User.from_dict: {msg}")
raise ScoValueError(msg)
def get_token(self, expires_in=3600): def get_token(self, expires_in=3600):
"Un jeton pour cet user. Stocké en base, non commité." "Un jeton pour cet user. Stocké en base, non commité."
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment