From b40d40644d8eab5f4f8eb678810a550565a7d69f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tim=20Gr=C3=B6ger?= Date: Mon, 15 May 2023 23:52:49 +0200 Subject: [PATCH] if birthday is date then take it otherwise parse from string; prettier --- flaschengeist/controller/userController.py | 25 +++++++++++++++++----- flaschengeist/models/user.py | 6 ++++-- 2 files changed, 24 insertions(+), 7 deletions(-) diff --git a/flaschengeist/controller/userController.py b/flaschengeist/controller/userController.py index a94c022..a59bbce 100644 --- a/flaschengeist/controller/userController.py +++ b/flaschengeist/controller/userController.py @@ -8,7 +8,7 @@ from flask import make_response from flask.json import provider from sqlalchemy import exc from sqlalchemy_utils import merge_references -from datetime import datetime, timedelta, timezone +from datetime import datetime, timedelta, timezone, date from flask.helpers import send_file from werkzeug.exceptions import NotFound, BadRequest, Forbidden @@ -19,7 +19,12 @@ from ..models import Notification, User, Role from ..models.user import _PasswordReset from ..utils.hook import Hook from ..utils.datetime import from_iso_format -from ..controller import imageController, messageController, pluginController, sessionController +from ..controller import ( + imageController, + messageController, + pluginController, + sessionController, +) from ..plugins import AuthPlugin @@ -198,7 +203,11 @@ def delete_user(user: User): deleted_user = get_user("__deleted_user__", True) except NotFound: deleted_user = User( - userid="__deleted_user__", firstname="USER", lastname="DELETED", display_name="DELETED USER", deleted=True + userid="__deleted_user__", + firstname="USER", + lastname="DELETED", + display_name="DELETED USER", + deleted=True, ) db.session.add(user) db.session.flush() @@ -209,7 +218,10 @@ def delete_user(user: User): db.session.delete(user) db.session.commit() except exc.IntegrityError: - logger.error("Delete of user failed, there might be ForeignKey contraits from disabled plugins", exec_info=True) + logger.error( + "Delete of user failed, there might be ForeignKey contraits from disabled plugins", + exec_info=True, + ) # Remove at least all personal data user.userid = f"__deleted_user__{user.id_}" user.display_name = "DELETED USER" @@ -231,7 +243,10 @@ def register(data, passwd=None): values = {key: value for key, value in data.items() if key in allowed_keys} roles = values.pop("roles", []) if "birthday" in data: - values["birthday"] = from_iso_format(data["birthday"]).date() + if isinstance(data["birthday"], date): + values["birthday"] = data["birthday"] + else: + values["birthday"] = from_iso_format(data["birthday"]).date() if "mail" in data and not re.match(r"[^@]+@[^@]+\.[^@]+", data["mail"]): raise BadRequest("Invalid mail given") user = User(**values) diff --git a/flaschengeist/models/user.py b/flaschengeist/models/user.py index 74f7950..21e9604 100644 --- a/flaschengeist/models/user.py +++ b/flaschengeist/models/user.py @@ -1,4 +1,6 @@ -from __future__ import annotations # TODO: Remove if python requirement is >= 3.12 (? PEP 563 is defered) +from __future__ import ( + annotations, +) # TODO: Remove if python requirement is >= 3.12 (? PEP 563 is defered) from typing import Optional, Union, List from datetime import date, datetime @@ -45,7 +47,7 @@ class User(db.Model, ModelSerializeMixin): Attributes: id: Id in Database as Primary Key. - uid: User ID used by authentication provider + userid: User ID used by authentication provider display_name: Name to show firstname: Firstname of the User lastname: Lastname of the User