Links for Authenticated Users

[layout.html]

...
<div class="grid-33">
  <!-- Say Hi -->
  <h1>Hello {% if current_user.is_authenticated %}{{ current_user.username }}{% endif %}!</h1>
</div>
<div class="grid-33">
  <!-- Log in/Log out -->
  {% if current_user.is_authenticated %}
  <a href="{{ url_for('logout') }}">Log out</a>
  {% else %}
  <a href="{{ url_for('login') }}">Log in</a>
  <a href="{{ url_for('register') }}">Register</a>
  {% endif %}
</div>
...
  <!-- Flash messages -->
  {% with messages = get_flashed_messages( with_categories=True ) %}
    {% if messages %}
      {% for category, message in messages %}
      <div class="notification {{category}}">{{ message }}</div>
      {% endfor %}
    {% endif %}
  {% endwith %}
...


current_user.is_authenticated

Returns True if current user is authenticated.

current_user.username
current_user represents the fetched record of the logged in user.

get_flashed_messages( with_category=True )
By adding the with_category argument, this function returns a list of category, message tuples.

[login.html] / [register.html]

{% extends "layout.html" %}
{% from 'macros.html' import render_field %}

{% block content %}
  <form method="POST" action="" class="form">
  {{ form.hidden_tag() }}
  {% for field in form %}
    {{ render_field(field) }}
  {% endfor %}
  <button type="submit" id="submit">Submit!</button>
</form>
{% endblock %}

Nothing out of the ordinary here; we just extended layout.html and wrapped our code with content block.


Post Model

[models.py]

...
class User(UserMixin, Model):
    ...     
 
    class Meta:
        database = DATABASE
        order_by = ('-joined_at',)

    def get_posts(self):
        return Post.select().where( Post.user == self)

    def get_stream(self):
        return Post.select().where( 
            (Post.user == self)
            )

    @classmethod
    def create_user(cls, username, email, password, admin=False):
    ... 
 
 
class Post(Model):
    timestamp = DateTimeField(default=datetime.datetime.now)
    user = ForeignKeyField( rel_model=User , related_name='posts' )
    content = TextField()

    class Meta:
        database = DATABASE
        order_by = ('-timestamp', )


class Post(Model)

A post will have 3 attributes; time of posting, user who posted it, and the content of the post.

ForeignKeyField( rel_model=User , related_name=’posts’ )
A field represents a Foreign Key that points to the User model (i.e. the related model). The related_name is what the related model (i.e. User) calls this model; the reverse foreign key.

order_by = (‘-timestamp’, )
Automatically orders posts by timestamp. The value of order_by has to be a tuple that can have more than one string to be ordered by in case of conflicts. For example, two posts were posted at the exact same timestamp, next they’ll be ordered by something else.

def get_posts(self):
  return Post.select().where( Post.user == self )
Will enables us to get the posts of the current user.

def get_stream(self):
  return Post.select().where( (Post.user == self) )
Will enables us to get the stream of posts for the current user.