Skip to content
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
7 changes: 3 additions & 4 deletions app/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Comment on lines -65 to +69
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_app refactored with the following changes:

mail_handler = SMTPHandler(
mailhost=(app.config["MAIL_SERVER"], app.config["MAIL_PORT"]),
fromaddr="no-reply@" + app.config["MAIL_SERVER"],
Expand Down
3 changes: 1 addition & 2 deletions app/auth/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Copy link
Author

Choose a reason for hiding this comment

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

Function reset_password_request refactored with the following changes:

send_password_reset_email(user)
flash(_("Check your email for the instructions to reset your password"))
return redirect(url_for("auth.login"))
Expand Down
4 changes: 3 additions & 1 deletion app/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
):
Comment on lines -18 to +20
Copy link
Author

Choose a reason for hiding this comment

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

Function register refactored with the following changes:

raise RuntimeError("init command failed")
os.remove("messages.pot")

Expand Down
50 changes: 24 additions & 26 deletions app/main/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Comment on lines -131 to +143
Copy link
Author

Choose a reason for hiding this comment

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

Function follow refactored with the following changes:



@bp.route("/unfollow/<username>", 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))
Comment on lines -151 to +162
Copy link
Author

Choose a reason for hiding this comment

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

Function unfollow refactored with the following changes:



@bp.route("/translate", methods=["POST"])
Expand Down
12 changes: 4 additions & 8 deletions app/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))]
Copy link
Author

Choose a reason for hiding this comment

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

Function SearchableMixin.search refactored with the following changes:

return (
cls.query.filter(cls.id.in_(ids)).order_by(db.case(*when, value=cls.id)),
total,
Expand Down Expand Up @@ -77,7 +75,7 @@ class User(UserMixin, db.Model):
)

def __repr__(self):
return "<User {}>".format(self.username)
return f"<User {self.username}>"
Comment on lines -80 to +78
Copy link
Author

Choose a reason for hiding this comment

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

Function User.__repr__ refactored with the following changes:


def set_password(self, password):
self.password_hash = generate_password_hash(password)
Expand All @@ -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}"
Comment on lines -90 to +88
Copy link
Author

Choose a reason for hiding this comment

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

Function User.avatar refactored with the following changes:


def follow(self, user):
if not self.is_following(user):
Expand Down Expand Up @@ -142,7 +138,7 @@ class Post(SearchableMixin, db.Model):
language = db.Column(db.String(5))

def __repr__(self):
return "<Post {}>".format(self.body)
return f"<Post {self.body}>"
Comment on lines -145 to +141
Copy link
Author

Choose a reason for hiding this comment

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

Function Post.__repr__ refactored with the following changes:



db.event.listen(db.session, "before_commit", SearchableMixin.before_commit)
Expand Down
4 changes: 1 addition & 3 deletions app/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__}
Copy link
Author

Choose a reason for hiding this comment

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

Function add_to_index refactored with the following changes:

current_app.elasticsearch.index(index=index, id=model.id, body=payload)


Expand Down
5 changes: 1 addition & 4 deletions app/translate.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}",
Copy link
Author

Choose a reason for hiding this comment

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

Function translate refactored with the following changes:

headers=auth,
json=[{"Text": text}],
)
Expand Down