← flask-blog

example login added pages and a bit of documentation

6923a3039173be1945c7b21b74315c2172628d93 · ovidiucs · May 18, 2015 21:55

+55 −1 · 4 files changed

  • flask-blog.py +31 −1
  • templates/login.html +16 −0
  • templates/logout.html +0 −0
  • templates/protected.html +8 −0
diff --git a/flask-blog.py b/flask-blog.py
index b2532e8..88b764c 100644
--- a/flask-blog.py
+++ b/flask-blog.py
@@ -55,7 +55,10 @@ class UploadForm(Form):
 # simple, thanks to the SQLAlchemy query syntax
 
 class User(UserMixin, db.Model):
-
+    """
+    Inherit from a user mixin class.
+    Methods > or implement your own methods
+    """
     __tablename__ = 'users'
     id = db.Column(db.Integer, primary_key=True)
     name = db.Column(db.String(16), index=True, unique=True)
@@ -240,6 +243,33 @@ def load_user(id):
 @app.route('/login', methods=['GET', 'POST'])
 def login():
     form = LoginForm()
+    """
+    POST request > wtf run all the validators and if
+    they validate they get in the if body
+    ser.query.filter_by(username=form.username.data)
+    --if user is None
+    1 or None (user is valid, user invalid return None
+    Never get more than one. Username is unique
+
+    --user.verify_password(form.password.data):
+        we need to let the user try again, redirect to login page
+        a redirect to itself
+    --login_user(user, form.remember_me.data)
+        function that comes with flask.login,
+        login the user pass the user object and tell it if
+        we want it that it should be a user to be remembered
+    --return redirect(request.args.get('next') or url_for('index'))
+        redirect to home page or protected page while being logged out
+        intercepted and sent to the login
+    --request.args
+        python dictionary will have all the elements
+        next is used in arguments if user tried to access restricted page
+        if user will login ok then it will use the next parameter, which in
+        turn it will go to protected page
+    --args.get('next'
+        none > redirect to index page or protected page
+    --
+    """
     if form.validate_on_submit():
         user = User.query.filter_by(username=form.username.data).first()
         if user is None or not user.verify_password(form.password.data):
diff --git a/templates/login.html b/templates/login.html
new file mode 100644
index 0000000..5403f01
--- /dev/null
+++ b/templates/login.html
@@ -0,0 +1,16 @@
+{% extends "base.html" %}
+{% import "bootstrap/wtf.html" as wtf %}
+
+{% block title %}Login Example{% endblock %}
+
+{% block page_content %}
+    <h1>Login</h1>
+    <div class="row">
+        <div class="col-md-3">
+            {{ wtf.quick_form(form) }}
+        </div>
+    </div>
+    <p>This page can be viewed by all users.</p>
+    <p>You are {% if not current_user.is_authenticated() %} not {% endif %}
+        logged in.</p>
+{% endblock %}
\ No newline at end of file
diff --git a/templates/logout.html b/templates/logout.html
new file mode 100644
index 0000000..e69de29
diff --git a/templates/protected.html b/templates/protected.html
new file mode 100644
index 0000000..76a5f95
--- /dev/null
+++ b/templates/protected.html
@@ -0,0 +1,8 @@
+{% extends "base.html" %}
+{% block title %}Login Example{% endblock %}
+
+{% block page_content %}
+    <h1>Protected page</h1>
+    <p>This page can only be viewerd by logged in users</p>
+    <p>You are logged in as <b>{{ current_user.username }}</b></p>
+{% endblock %}
\ No newline at end of file