Sourcery Starbot ⭐ refactored voightp/flask-mega-tutorial#1
Sourcery Starbot ⭐ refactored voightp/flask-mega-tutorial#1SourceryAI wants to merge 1 commit intovoightp:masterfrom
Conversation
| auth = None | ||
| if app.config["MAIL_USERNAME"] or app.config["MAIL_PASSWORD"]: | ||
| auth = (app.config["MAIL_USERNAME"], app.config["MAIL_PASSWORD"]) | ||
| secure = None | ||
| if app.config["MAIL_USE_TLS"]: | ||
| secure = () | ||
| else: | ||
| auth = None | ||
| secure = () if app.config["MAIL_USE_TLS"] else None |
There was a problem hiding this comment.
Function create_app refactored with the following changes:
- Move setting of default value for variable into
elsebranch [×2] (introduce-default-else) - Split conditional into multiple branches [×2] (
split-or-ifs) - Merge duplicate blocks in conditional [×2] (
merge-duplicate-blocks) - Remove redundant conditional [×2] (
remove-redundant-if) - Replace if statement with if expression (
assign-if-exp)
| if os.system("pybabel init -i messages.pot -d app/translations -l " + lang): | ||
| if os.system( | ||
| f"pybabel init -i messages.pot -d app/translations -l {lang}" | ||
| ): |
There was a problem hiding this comment.
Function register refactored with the following changes:
- Use f-string instead of string concatenation (
use-fstring-for-concatenation)
| when = [] | ||
| for i in range(len(ids)): | ||
| when.append((ids[i], i)) | ||
| when = [(ids[i], i) for i in range(len(ids))] |
There was a problem hiding this comment.
Function SearchableMixin.search refactored with the following changes:
- Convert for loop into list comprehension (
list-comprehension)
| return "<User {}>".format(self.username) | ||
| return f"<User {self.username}>" |
There was a problem hiding this comment.
Function User.__repr__ refactored with the following changes:
- Replace call to format with f-string (
use-fstring-for-formatting)
| return "https://www.gravatar.com/avatar/{}?d=identicon&s={}".format( | ||
| digest, size | ||
| ) | ||
| return f"https://www.gravatar.com/avatar/{digest}?d=identicon&s={size}" |
There was a problem hiding this comment.
Function User.avatar refactored with the following changes:
- Replace call to format with f-string (
use-fstring-for-formatting)
| payload = {} | ||
| for field in model.__searchable__: | ||
| payload[field] = getattr(model, field) | ||
| payload = {field: getattr(model, field) for field in model.__searchable__} |
There was a problem hiding this comment.
Function add_to_index refactored with the following changes:
- Convert for loop into dictionary comprehension (
dict-comprehension)
| "/translate?api-version=3.0&from={}&to={}".format( | ||
| source_language, dest_language | ||
| ), | ||
| f"https://api.cognitive.microsofttranslator.com/translate?api-version=3.0&from={source_language}&to={dest_language}", |
There was a problem hiding this comment.
Function translate refactored with the following changes:
- Replace call to format with f-string (
use-fstring-for-formatting)
| if form.validate_on_submit(): | ||
| user = User.query.filter_by(email=form.email.data).first() | ||
| if user: | ||
| if user := User.query.filter_by(email=form.email.data).first(): |
There was a problem hiding this comment.
Function reset_password_request refactored with the following changes:
- Use named expression to simplify assignment and conditional (
use-named-expression)
| if form.validate_on_submit(): | ||
| user = User.query.filter_by(username=username).first() | ||
| if user is None: | ||
| flash(_("User %(username)s not found.", username=username)) | ||
| return redirect(url_for("main.index")) | ||
| if user == current_user: | ||
| flash(_("You cannot follow yourself!")) | ||
| return redirect(url_for("main.user", username=username)) | ||
| current_user.follow(user) | ||
| db.session.commit() | ||
| flash(_("You are following %(username)s!", username=username)) | ||
| return redirect(url_for("main.user", username=username)) | ||
| else: | ||
| if not form.validate_on_submit(): | ||
| return redirect(url_for("main.index")) | ||
| user = User.query.filter_by(username=username).first() | ||
| if user is None: | ||
| flash(_("User %(username)s not found.", username=username)) | ||
| return redirect(url_for("main.index")) | ||
| if user == current_user: | ||
| flash(_("You cannot follow yourself!")) | ||
| return redirect(url_for("main.user", username=username)) | ||
| current_user.follow(user) | ||
| db.session.commit() | ||
| flash(_("You are following %(username)s!", username=username)) | ||
| return redirect(url_for("main.user", username=username)) |
There was a problem hiding this comment.
Function follow refactored with the following changes:
- Swap if/else branches (
swap-if-else-branches) - Remove unnecessary else after guard condition (
remove-unnecessary-else)
| if form.validate_on_submit(): | ||
| user = User.query.filter_by(username=username).first() | ||
| if user is None: | ||
| flash(_("User %(username)s not found.", username=username)) | ||
| return redirect(url_for("main.index")) | ||
| if user == current_user: | ||
| flash(_("You cannot unfollow yourself!")) | ||
| return redirect(url_for("main.user", username=username)) | ||
| current_user.unfollow(user) | ||
| db.session.commit() | ||
| flash(_("You are not following %(username)s.", username=username)) | ||
| return redirect(url_for("main.user", username=username)) | ||
| else: | ||
| if not form.validate_on_submit(): | ||
| return redirect(url_for("main.index")) | ||
| user = User.query.filter_by(username=username).first() | ||
| if user is None: | ||
| flash(_("User %(username)s not found.", username=username)) | ||
| return redirect(url_for("main.index")) | ||
| if user == current_user: | ||
| flash(_("You cannot unfollow yourself!")) | ||
| return redirect(url_for("main.user", username=username)) | ||
| current_user.unfollow(user) | ||
| db.session.commit() | ||
| flash(_("You are not following %(username)s.", username=username)) | ||
| return redirect(url_for("main.user", username=username)) |
There was a problem hiding this comment.
Function unfollow refactored with the following changes:
- Swap if/else branches (
swap-if-else-branches) - Remove unnecessary else after guard condition (
remove-unnecessary-else)
Thanks for starring sourcery-ai/sourcery ✨ 🌟 ✨
Here's your pull request refactoring your most popular Python repo.
If you want Sourcery to refactor all your Python repos and incoming pull requests install our bot.
Review changes via command line
To manually merge these changes, make sure you're on the
masterbranch, then run: