Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
This repository was archived by the owner on May 25, 2022. It is now read-only.

Commit 2919821

Browse files
author
Zaytsev Dmitriy
committed
refactor and clean up code with pep8
1 parent 6f67e2f commit 2919821

File tree

1 file changed

+33
-43
lines changed

1 file changed

+33
-43
lines changed

‎projects/Todo_app/app.py

Lines changed: 33 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,77 +1,67 @@
1-
from flask import Flask, render_template,url_for, request, redirect
1+
from flask import Flask, render_template,url_for, request, redirect
22
from flask_sqlalchemy import SQLAlchemy
33
from datetime import datetime
44

55
app = Flask(__name__)
6-
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///test.db'
7-
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
6+
app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:///test.db"
7+
app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False
88
db = SQLAlchemy(app)
99

1010

11-
12-
13-
14-
1511
class Todo(db.Model):
16-
id=db.Column(db.Integer, primary_key=True)
17-
content=db.Column(db.String(200),nullable=False)
18-
completed=db.Column(db.Integer,default=0)
19-
pub_date = db.Column(db.DateTime, nullable=False,
20-
default=datetime.utcnow)
21-
def __repr__(self):
22-
return '<Task %r>'%self.id
23-
24-
25-
12+
id = db.Column(db.Integer, primary_key=True)
13+
content = db.Column(db.String(200), nullable=False)
14+
completed = db.Column(db.Integer, default=0)
15+
pub_date = db.Column(db.DateTime, nullable=False, default=datetime.utcnow)
2616

17+
def __repr__(self):
18+
return "<Task %r>" % self.id
2719

2820

29-
@app.route('/',methods=['POST','GET'])
21+
@app.route("/", methods=["POST", "GET"])
3022
def index():
31-
if request.method=="POST":
32-
task_content= request.form['task']
33-
new_task=Todo(content=task_content)
34-
23+
if request.method == "POST":
24+
task_content = request.form["task"]
25+
new_task = Todo(content=task_content)
3526
try:
36-
db.session.add(new_task)
27+
db.session.add(new_task)
3728
db.session.commit()
38-
return redirect('/')
29+
return redirect("/")
3930
except:
40-
return 'There is an issue'
31+
return "There is an issue"
4132
else:
42-
tasks=Todo.query.order_by(Todo.pub_date).all()
43-
return render_template('index.html',tasks=tasks)
33+
tasks = Todo.query.order_by(Todo.pub_date).all()
34+
return render_template("index.html", tasks=tasks)
35+
4436

45-
@app.route('/delete/<int:id>')
37+
@app.route("/delete/<int:id>")
4638
def delete(id):
47-
task=Todo.query.get_or_404(id)
39+
task=Todo.query.get_or_404(id)
4840
try:
4941
db.session.delete(task)
5042
db.session.commit()
51-
return redirect('/')
43+
return redirect("/")
5244
except:
5345
return "This is an Problem while deleting"
5446

55-
@app.route('/update/<int:id>',methods=['POST','GET'])
47+
48+
@app.route("/update/<int:id>", methods=["POST", "GET"])
5649
def update(id):
57-
task=Todo.query.get_or_404(id)
58-
if request.method=="POST":
59-
task.content= request.form['task']
60-
50+
task = Todo.query.get_or_404(id)
51+
if request.method == "POST":
52+
task.content = request.form["task"]
6153

6254
try:
63-
6455
db.session.commit()
65-
return redirect('/')
56+
return redirect("/")
6657
except:
67-
return 'There is an issue'
58+
return "There is an issue"
6859
else:
69-
tasks=Todo.query.order_by(Todo.pub_date).all()
70-
71-
return render_template('index.html', update_task=task,tasks=tasks)
60+
tasks = Todo.query.order_by(Todo.pub_date).all()
7261

62+
return render_template("index.html", update_task=task, tasks=tasks)
7363

7464

65+
if __name__ == "__main__":
66+
app.run(debug=True)
7567

76-
if __name__=="__main__":
77-
app.run(debug=True)

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /