-
Notifications
You must be signed in to change notification settings - Fork 0
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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() | ||
Comment on lines
-46
to
+45
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
||
return wrap | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
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) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
db = SessionLocal() | ||
try: | ||
user = CRUDUser.get(db, id=user_id) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
||
|
||
# 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 | ||
Comment on lines
-40
to
+38
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
||
@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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
||
@validator("password2") | ||
def passwords2_match(cls, v, values, **kwargs): | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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]: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
return json.loads(trigger_id)[key] |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
return ( | ||
"toast", | ||
noti_class, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
if triggered_by_id( | ||
dash.callback_context.triggered, "usersUpdateFormNotificationClose" | ||
): | ||
|
There was a problem hiding this comment.
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:remove-unnecessary-else
)