From 69db00763659b5a095d9462770fa769332eaaa53 Mon Sep 17 00:00:00 2001 From: Sourcery AI <> Date: Thu, 18 May 2023 21:07:59 +0000 Subject: [PATCH] 'Refactored by Sourcery' --- .../app/auth/flask_login.py | 34 ++++++++--------- .../app/crud/crud_user.py | 8 ++-- {{cookiecutter.project_slug}}/app/encoders.py | 38 +++++++++---------- {{cookiecutter.project_slug}}/app/index.py | 2 +- .../app/schemas/user.py | 12 ++---- {{cookiecutter.project_slug}}/app/utils.py | 3 +- .../app/views/users_create.py | 3 +- .../app/views/users_update.py | 2 +- 8 files changed, 42 insertions(+), 60 deletions(-) diff --git a/{{cookiecutter.project_slug}}/app/auth/flask_login.py b/{{cookiecutter.project_slug}}/app/auth/flask_login.py index f07c041..6fb4169 100644 --- a/{{cookiecutter.project_slug}}/app/auth/flask_login.py +++ b/{{cookiecutter.project_slug}}/app/auth/flask_login.py @@ -15,21 +15,20 @@ def login_route(): error = False if self.is_authorized(): return redirect("/") - else: - if request.method == "POST": - db = SessionLocal() - email = request.form.get("email") - password = request.form.get("password") - rememberMe = request.form.get("rememberMe") is not None - try: - user = CRUDUser.authenticate(db, email=email, password=password) - if user: - login_user(user, remember=rememberMe) - return redirect("/") - finally: - db.close() - error = True - return render_template("login.html", error=error) + if request.method == "POST": + db = SessionLocal() + email = request.form.get("email") + password = request.form.get("password") + rememberMe = request.form.get("rememberMe") is not None + try: + user = CRUDUser.authenticate(db, email=email, password=password) + if user: + login_user(user, remember=rememberMe) + return redirect("/") + finally: + db.close() + error = True + return render_template("login.html", error=error) def is_authorized(self): # Is the user authenticated? @@ -43,10 +42,7 @@ def auth_wrapper(self, f): # Wraps all other views than the dash view that is # added before super method is called in the init method def wrap(*args, **kwargs): - if self.is_authorized(): - return f(*args, **kwargs) - else: - return self.login_request() + return f(*args, **kwargs) if self.is_authorized() else self.login_request() return wrap diff --git a/{{cookiecutter.project_slug}}/app/crud/crud_user.py b/{{cookiecutter.project_slug}}/app/crud/crud_user.py index dfd2639..1473ca1 100644 --- a/{{cookiecutter.project_slug}}/app/crud/crud_user.py +++ b/{{cookiecutter.project_slug}}/app/crud/crud_user.py @@ -64,9 +64,7 @@ def count(cls, db): @classmethod def authenticate(cls, db, *, email: str, password: str): - user = cls.get_by_email(db, email=email) - if not user: - return None - if not verify_password(password, user.hashed_password): + if user := cls.get_by_email(db, email=email): + return None if not verify_password(password, user.hashed_password) else user + else: return None - return user diff --git a/{{cookiecutter.project_slug}}/app/encoders.py b/{{cookiecutter.project_slug}}/app/encoders.py index 6a2a75d..23ee1ec 100644 --- a/{{cookiecutter.project_slug}}/app/encoders.py +++ b/{{cookiecutter.project_slug}}/app/encoders.py @@ -43,7 +43,7 @@ def jsonable_encoder( if isinstance(obj, BaseModel): encoder = getattr(obj.__config__, "json_encoders", {}) if custom_encoder: - encoder.update(custom_encoder) + encoder |= custom_encoder obj_dict = obj.dict( include=include, # type: ignore # in Pydantic exclude=exclude, # type: ignore # in Pydantic @@ -98,30 +98,26 @@ def jsonable_encoder( encoded_dict[encoded_key] = encoded_value return encoded_dict if isinstance(obj, (list, set, frozenset, GeneratorType, tuple)): - encoded_list = [] - for item in obj: - encoded_list.append( - jsonable_encoder( - item, - include=include, - exclude=exclude, - by_alias=by_alias, - exclude_unset=exclude_unset, - exclude_defaults=exclude_defaults, - exclude_none=exclude_none, - custom_encoder=custom_encoder, - sqlalchemy_safe=sqlalchemy_safe, - ) + return [ + jsonable_encoder( + item, + include=include, + exclude=exclude, + by_alias=by_alias, + exclude_unset=exclude_unset, + exclude_defaults=exclude_defaults, + exclude_none=exclude_none, + custom_encoder=custom_encoder, + sqlalchemy_safe=sqlalchemy_safe, ) - return encoded_list - + for item in obj + ] if custom_encoder: if type(obj) in custom_encoder: return custom_encoder[type(obj)](obj) - else: - for encoder_type, encoder in custom_encoder.items(): - if isinstance(obj, encoder_type): - return encoder(obj) + for encoder_type, encoder in custom_encoder.items(): + if isinstance(obj, encoder_type): + return encoder(obj) if type(obj) in ENCODERS_BY_TYPE: return ENCODERS_BY_TYPE[type(obj)](obj) diff --git a/{{cookiecutter.project_slug}}/app/index.py b/{{cookiecutter.project_slug}}/app/index.py index cd677dd..d7c1760 100644 --- a/{{cookiecutter.project_slug}}/app/index.py +++ b/{{cookiecutter.project_slug}}/app/index.py @@ -35,7 +35,7 @@ def route(pathname): elif pathname == "/users/create": return views.users_create.layout(sidebar_context) elif re.match(r"^/users/\d+", pathname): - user_id = re.match(r"^/users/(\d+)", pathname).group(1) + user_id = re.match(r"^/users/(\d+)", pathname)[1] db = SessionLocal() try: user = CRUDUser.get(db, id=user_id) diff --git a/{{cookiecutter.project_slug}}/app/schemas/user.py b/{{cookiecutter.project_slug}}/app/schemas/user.py index 75c5fde..14f11b4 100644 --- a/{{cookiecutter.project_slug}}/app/schemas/user.py +++ b/{{cookiecutter.project_slug}}/app/schemas/user.py @@ -12,9 +12,7 @@ class UserBase(BaseModel): @validator("full_name", pre=True) def passwords_match(cls, v, values, **kwargs): - if v == "": - return None - return v + return None if v == "" else v # Properties to receive via API on creation @@ -37,15 +35,11 @@ class UserUpdate(UserBase): @validator("password", pre=True) def password_pre(cls, v, values, **kwargs): - if v == "": - return None - return v + return None if v == "" else v @validator("password2", pre=True) def password2_pre(cls, v, values, **kwargs): - if v == "": - return None - return v + return None if v == "" else v @validator("password2") def passwords2_match(cls, v, values, **kwargs): diff --git a/{{cookiecutter.project_slug}}/app/utils.py b/{{cookiecutter.project_slug}}/app/utils.py index 57f14b1..76b666e 100644 --- a/{{cookiecutter.project_slug}}/app/utils.py +++ b/{{cookiecutter.project_slug}}/app/utils.py @@ -26,6 +26,5 @@ def get_trigger_id(triggered, key="type"): def get_trigger_index(triggered, key="index"): - trigger_id = triggered[0]["prop_id"].split(".")[0] - if trigger_id: + if trigger_id := triggered[0]["prop_id"].split(".")[0]: return json.loads(trigger_id)[key] diff --git a/{{cookiecutter.project_slug}}/app/views/users_create.py b/{{cookiecutter.project_slug}}/app/views/users_create.py index 91caa88..46ae4a4 100644 --- a/{{cookiecutter.project_slug}}/app/views/users_create.py +++ b/{{cookiecutter.project_slug}}/app/views/users_create.py @@ -224,8 +224,7 @@ def create_user(_, __, full_name, email, password, password2, check_list): db = SessionLocal() try: - user = CRUDUser.get_by_email(db, email=email) - if user: + if user := CRUDUser.get_by_email(db, email=email): return ( "toast", noti_class, diff --git a/{{cookiecutter.project_slug}}/app/views/users_update.py b/{{cookiecutter.project_slug}}/app/views/users_update.py index d5959ee..6d4650a 100644 --- a/{{cookiecutter.project_slug}}/app/views/users_update.py +++ b/{{cookiecutter.project_slug}}/app/views/users_update.py @@ -185,7 +185,7 @@ def update_user( if not current_user.is_superuser: raise PreventUpdate() noti_class = "toast-header bg-primary text-white" - user_id = re.match(r"^/users/(\d+)", pathname).group(1) + user_id = re.match(r"^/users/(\d+)", pathname)[1] if triggered_by_id( dash.callback_context.triggered, "usersUpdateFormNotificationClose" ):