diff --git a/app/__init__.py b/app/__init__.py index ad51b47..a357054 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -62,12 +62,11 @@ def create_app(config_class=Config): if not app.debug and not app.testing: if app.config["MAIL_SERVER"]: - 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 mail_handler = SMTPHandler( mailhost=(app.config["MAIL_SERVER"], app.config["MAIL_PORT"]), fromaddr="no-reply@" + app.config["MAIL_SERVER"], diff --git a/app/auth/routes.py b/app/auth/routes.py index 01c18a2..7ca9aa7 100644 --- a/app/auth/routes.py +++ b/app/auth/routes.py @@ -60,8 +60,7 @@ def reset_password_request(): return redirect(url_for("main.index")) form = ResetPasswordRequestForm() 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(): send_password_reset_email(user) flash(_("Check your email for the instructions to reset your password")) return redirect(url_for("auth.login")) diff --git a/app/cli.py b/app/cli.py index ef143d7..7790597 100644 --- a/app/cli.py +++ b/app/cli.py @@ -15,7 +15,9 @@ def init(lang): """Initialize a new language.""" if os.system("pybabel extract -F babel.cfg -k _l -o messages.pot ."): raise RuntimeError("extract command failed") - 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}" + ): raise RuntimeError("init command failed") os.remove("messages.pot") diff --git a/app/main/routes.py b/app/main/routes.py index 4f1fc97..3ee47d7 100644 --- a/app/main/routes.py +++ b/app/main/routes.py @@ -128,40 +128,38 @@ def edit_profile(): @login_required def follow(username): form = EmptyForm() - 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)) @bp.route("/unfollow/", methods=["POST"]) @login_required def unfollow(username): form = EmptyForm() - 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)) @bp.route("/translate", methods=["POST"]) diff --git a/app/models.py b/app/models.py index fde5f41..79a023b 100644 --- a/app/models.py +++ b/app/models.py @@ -17,9 +17,7 @@ def search(cls, expression, page, per_page): ids, total = query_index(cls.__tablename__, expression, page, per_page) if total == 0: return cls.query.filter_by(id=0), 0 - when = [] - for i in range(len(ids)): - when.append((ids[i], i)) + when = [(ids[i], i) for i in range(len(ids))] return ( cls.query.filter(cls.id.in_(ids)).order_by(db.case(*when, value=cls.id)), total, @@ -77,7 +75,7 @@ class User(UserMixin, db.Model): ) def __repr__(self): - return "".format(self.username) + return f"" def set_password(self, password): self.password_hash = generate_password_hash(password) @@ -87,9 +85,7 @@ def check_password(self, password): def avatar(self, size): digest = md5(self.email.lower().encode("utf-8")).hexdigest() - return "https://www.gravatar.com/avatar/{}?d=identicon&s={}".format( - digest, size - ) + return f"https://www.gravatar.com/avatar/{digest}?d=identicon&s={size}" def follow(self, user): if not self.is_following(user): @@ -142,7 +138,7 @@ class Post(SearchableMixin, db.Model): language = db.Column(db.String(5)) def __repr__(self): - return "".format(self.body) + return f"" db.event.listen(db.session, "before_commit", SearchableMixin.before_commit) diff --git a/app/search.py b/app/search.py index 3365fd4..affef6f 100644 --- a/app/search.py +++ b/app/search.py @@ -4,9 +4,7 @@ def add_to_index(index, model): if not current_app.elasticsearch: return - payload = {} - for field in model.__searchable__: - payload[field] = getattr(model, field) + payload = {field: getattr(model, field) for field in model.__searchable__} current_app.elasticsearch.index(index=index, id=model.id, body=payload) diff --git a/app/translate.py b/app/translate.py index 02e264b..afd7967 100644 --- a/app/translate.py +++ b/app/translate.py @@ -14,10 +14,7 @@ def translate(text, source_language, dest_language): "Ocp-Apim-Subscription-Region": "westus2", } r = requests.post( - "https://api.cognitive.microsofttranslator.com" - "/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}", headers=auth, json=[{"Text": text}], )