Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sourcery refactored main branch #1

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 15 additions & 19 deletions {{cookiecutter.project_slug}}/app/auth/flask_login.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Comment on lines -18 to +31
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function FlaskLogin.__init__ refactored with the following changes:


def is_authorized(self):
# Is the user authenticated?
Expand All @@ -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()
Comment on lines -46 to +45
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function FlaskLogin.auth_wrapper refactored with the following changes:


return wrap

Expand Down
8 changes: 3 additions & 5 deletions {{cookiecutter.project_slug}}/app/crud/crud_user.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Comment on lines -67 to -72
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function CRUDUser.authenticate refactored with the following changes:

38 changes: 17 additions & 21 deletions {{cookiecutter.project_slug}}/app/encoders.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function jsonable_encoder refactored with the following changes:

obj_dict = obj.dict(
include=include, # type: ignore # in Pydantic
exclude=exclude, # type: ignore # in Pydantic
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion {{cookiecutter.project_slug}}/app/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function route refactored with the following changes:

db = SessionLocal()
try:
user = CRUDUser.get(db, id=user_id)
Expand Down
12 changes: 3 additions & 9 deletions {{cookiecutter.project_slug}}/app/schemas/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function UserBase.passwords_match refactored with the following changes:



# Properties to receive via API on creation
Expand All @@ -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
Comment on lines -40 to +38
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function UserUpdate.password_pre refactored with the following changes:


@validator("password2", pre=True)
def password2_pre(cls, v, values, **kwargs):
if v == "":
return None
return v
return None if v == "" else v
Comment on lines -46 to +42
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function UserUpdate.password2_pre refactored with the following changes:


@validator("password2")
def passwords2_match(cls, v, values, **kwargs):
Expand Down
3 changes: 1 addition & 2 deletions {{cookiecutter.project_slug}}/app/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]:
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function get_trigger_index refactored with the following changes:

return json.loads(trigger_id)[key]
3 changes: 1 addition & 2 deletions {{cookiecutter.project_slug}}/app/views/users_create.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function create_user refactored with the following changes:

return (
"toast",
noti_class,
Expand Down
2 changes: 1 addition & 1 deletion {{cookiecutter.project_slug}}/app/views/users_update.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function update_user refactored with the following changes:

if triggered_by_id(
dash.callback_context.triggered, "usersUpdateFormNotificationClose"
):
Expand Down