Skip to content

Commit

Permalink
Merge pull request #105 from pluralsight/group_guides_on_profile
Browse files Browse the repository at this point in the history
Group guides by their publish status on profile
  • Loading branch information
durden committed Jul 7, 2016
2 parents 37c4cce + 64210b7 commit 7077871
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 13 deletions.
1 change: 1 addition & 0 deletions pskb_website/models/__init__.py
Expand Up @@ -11,6 +11,7 @@
from .article import branch_or_save_article
from .article import get_articles_for_author
from .article import get_public_articles_for_author
from .article import group_articles_by_status
from .article import find_article_by_title
from .article import change_article_stack
from .article import author_stats
Expand Down
26 changes: 26 additions & 0 deletions pskb_website/models/article.py
Expand Up @@ -225,6 +225,32 @@ def get_public_articles_for_author(author_name):
yield article


def group_articles_by_status(articles):
"""
Group articles by publish status
:param articles: Iterable of Article objects
:returns: Iterable like itertools.groupby with a key as the publish_status
and a list of articles for that status
"""

def status_key(a):
if a.publish_status == PUBLISHED:
cnt = 1
elif a.publish_status == IN_REVIEW:
cnt = 2
elif a.publish_status == DRAFT:
cnt = 3
else:
cnt = 4

return cnt

sorted_by_status = sorted(articles, key=status_key)

return itertools.groupby(sorted_by_status, key=lambda a: a.publish_status)


def author_stats(statuses=None):
"""
Get number of articles for each author
Expand Down
33 changes: 21 additions & 12 deletions pskb_website/templates/profile.html
Expand Up @@ -12,26 +12,35 @@

<div class="row">
<div class="col-md-8">
<h2>{{user.name}}</h2>
<h1>{{user.name}}</h1>
<div class="user-profile">
{% include 'user_info.html' with context %}
</div>

<h2>Guides</h2>
{% if published %}
<h2>Published Guides</h2>
{% with articles=published %}
{% include 'simple_article_list.html' %}
{% endwith %}
{% endif %}

{% for article in articles %}
{% if loop.index0 == 0 %}
<ul>
{% endif %}
{% if in_review %}
<h3>In-Review Guides</h3>
{% with articles=in_review %}
{% include 'simple_article_list.html' %}
{% endwith %}
{% endif %}

<li><a href="{{article|url_for_article}}">{{article.title}}</a></li>
{% if draft %}
<h4>Draft Guides</h4>
{% with articles=draft %}
{% include 'simple_article_list.html' %}
{% endwith %}
{% endif %}

{% if loop.last %}
</ul>
{% endif %}
{% else %}
{% if not published and not in_review and not draft %}
<p>No guides yet!</p>
{% endfor %}
{% endif %}

</div>

Expand Down
11 changes: 11 additions & 0 deletions pskb_website/templates/simple_article_list.html
@@ -0,0 +1,11 @@
{% for article in articles %}
{% if loop.index0 == 0 %}
<ul>
{% endif %}

<li><a href="{{article|url_for_article}}">{{article.title}}</a></li>

{% if loop.last %}
</ul>
{% endif %}
{% endfor %}
15 changes: 14 additions & 1 deletion pskb_website/views.py
Expand Up @@ -205,7 +205,20 @@ def user_profile(author_name):
else:
articles = models.get_public_articles_for_author(user.login)

return render_template('profile.html', user=user, articles=articles)
published = []
in_review = []
draft = []

for status, group in models.group_articles_by_status(articles):
if status == PUBLISHED:
published = list(group)
elif status == IN_REVIEW:
in_review = list(group)
elif status == DRAFT:
draft = list(group)

return render_template('profile.html', user=user, published=published,
in_review=in_review, draft=draft)


@app.route('/my-drafts/')
Expand Down

0 comments on commit 7077871

Please sign in to comment.