-
Notifications
You must be signed in to change notification settings - Fork 1
Sourcery Starbot ⭐ refactored voightp/flask-mega-tutorial #1
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
base: master
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 |
|---|---|---|
|
|
@@ -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(): | ||
|
Author
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
|
||
| send_password_reset_email(user) | ||
| flash(_("Check your email for the instructions to reset your password")) | ||
| return redirect(url_for("auth.login")) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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
Author
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
|
||
| raise RuntimeError("init command failed") | ||
| os.remove("messages.pot") | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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
Author
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
|
||
|
|
||
|
|
||
| @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
Author
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
|
||
|
|
||
|
|
||
| @bp.route("/translate", methods=["POST"]) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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))] | ||
|
Author
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 ( | ||
| 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 "<User {}>".format(self.username) | ||
| return f"<User {self.username}>" | ||
|
Comment on lines
-80
to
+78
Author
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
|
||
|
|
||
| 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}" | ||
|
Comment on lines
-90
to
+88
Author
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
|
||
|
|
||
| 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 "<Post {}>".format(self.body) | ||
| return f"<Post {self.body}>" | ||
|
Comment on lines
-145
to
+141
Author
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.event.listen(db.session, "before_commit", SearchableMixin.before_commit) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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__} | ||
|
Author
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
|
||
| current_app.elasticsearch.index(index=index, id=model.id, body=payload) | ||
|
|
||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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}", | ||
|
Author
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
|
||
| headers=auth, | ||
| json=[{"Text": text}], | ||
| ) | ||
|
|
||
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
create_apprefactored with the following changes:elsebranch [×2] (introduce-default-else)split-or-ifs)merge-duplicate-blocks)remove-redundant-if)assign-if-exp)