Owner Content

If your project was to become large with lots of users and content, it would be useful for users to see a summary of their own content, such as the blog posts they’ve written and the comments they’ve made.

There’s no need for new APIs for this: the relationships added in the Authorisation section already give every user blogposts and comments lists. The profile template loops over them:

web/templates/profile/profile.htmljinja
<h4>Your blog posts</h4>
<table class="table table-striped">
    <thead><tr><th>Title</th><th>Date</th><th>Published</th></tr></thead>
    <tbody>
    {% for post in context.user.blogposts|sort(attribute='created_date', reverse=True) %}
    <tr>
        <td><a href="/blog/{{ post.id }}">{{ post.title }}</a></td>
        <td>{{ post.created_date.strftime('%d %b %Y') }}</td>
        <td>{{ 'Yes' if post.published else 'No' }}</td>
    </tr>
    {% else %}
    <tr><td colspan="3">You haven't written any blog posts.</td></tr>
    {% endfor %}
    </tbody>
</table>

<h4>Your comments</h4>
<table class="table table-striped">
    <thead><tr><th>Comment</th><th>On</th><th>Date</th><th>Approved</th></tr></thead>
    <tbody>
    {% for comment in context.user.comments|sort(attribute='created_date', reverse=True) %}
    <tr>
        <td>{{ comment.comment }}</td>
        <td><a href="/blog/{{ comment.blogpost_id }}">{{ comment.blogpost.title }}</a></td>
        <td>{{ comment.created_date.strftime('%d %b %Y') }}</td>
        <td>{{ 'Yes' if comment.approved else 'Not yet' }}</td>
    </tr>
    {% else %}
    <tr><td colspan="4">You haven't left any comments.</td></tr>
    {% endfor %}
    </tbody>
</table>

|sort(attribute='created_date', reverse=True) is a Jinja filter that puts the newest first. Each comment links to the blog post it’s on, using the comment’s blogpost relationship for the title. Users can also see which of their comments are still waiting for approval.