Skip to content

Commit

Permalink
Merge pull request #37 from p3dr0migue1/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
p3dr0migue1 authored Aug 31, 2018
2 parents 42e29e0 + e380e6c commit 12cc80d
Show file tree
Hide file tree
Showing 7 changed files with 253 additions and 167 deletions.
15 changes: 9 additions & 6 deletions blog/templates/blog/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@
{% if posts %}
{% for post in posts %}
<article class="post">
<h2><a href="{{ post.get_absolute_url }}">{{ post.title }}</a></h2>
<h2>
<a href="{{ post.get_absolute_url }}">{{ post.title }}</a>
{% if post.status == 1 %}<span class="draft">Draft</span>{% endif %}
</h2>
<div class="post_footer">
<span class="post-date"> {{ post.date_created|date:'D d M Y' }}</span>

Expand Down Expand Up @@ -39,14 +42,14 @@ <h3>No posts to display yet..</h3>
<!-- blog posts pagination -->
<div class="pagination">
<span class="step-links">
{% if posts.has_previous %}
<a href="?page={{ posts.previous_page_number }}">previous</a>
{% if page_obj.has_previous %}
<a href="?page={{ page_obj.previous_page_number }}">previous</a>
{% endif %}
<span class="current">
Page {{ posts.number }} of {{ posts.paginator.num_pages }}.
Page {{ page_obj.number }} of {{ page_obj.paginator.num_pages }}.
</span>
{% if posts.has_next %}
<a href="?page={{ posts.next_page_number }}">next</a>
{% if page_obj.has_next %}
<a href="?page={{ page_obj.next_page_number }}">next</a>
{% endif %}
</span>
</div>
Expand Down
20 changes: 12 additions & 8 deletions blog/templates/includes/blog_navigation.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,25 @@ <h3>Search</h3>
</div>
{% endblock searchform %}

<nav class="sidebar-menu">
<h3>Menu</h3>
<ul>
<li><a href="{% url 'blog:new-post' %}">Create New Post</a></li>
<li><a href="{% url 'blog:new-tag' %}">Create New Tag</a></li>
</ul>
</nav>
{% if staff_user %}
<nav class="sidebar-menu">
<h3>Menu</h3>
<ul>
<li><a href="{% url 'blog:new-post' %}">Create New Post</a></li>
<li><a href="{% url 'blog:new-tag' %}">Create New Tag</a></li>
</ul>
</nav>
{% endif %}

<!-- tags -->
{% if tags %}
<nav class="tags-menu">
<h3>Tags</h3>
<ul>
{% for t_obj in tags %}
<li class="{% cycle 'odd_tag' 'even_tag' %}"><a class="{{ t_obj.css_class }}" href="{% url 'blog:posts_in_tag' t_obj.tag__slug %}">{{ t_obj.tag__name }}</a></li>
<li class="{% cycle 'odd_tag' 'even_tag' %}">
<a href="{% url 'blog:posts_in_tag' t_obj.tag__slug %}">{{ t_obj.tag__name }}</a>
</li>
{% endfor %}
</ul>
</nav>
Expand Down
9 changes: 8 additions & 1 deletion blog/templatetags/blog_tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,12 @@
@register.filter(name='markdown')
def markdown_format(text):
return mark_safe(
markdown.markdown(text, ["markdown.extensions.extra", "codehilite"])
markdown.markdown(
text,
[
"markdown.extensions.extra",
"markdown.extensions.toc",
"codehilite",
]
)
)
74 changes: 71 additions & 3 deletions blog/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
from django.core.urlresolvers import reverse
from django.test import Client

from ..models import Tag, Posts, PostTags
from ..views import NewPost, NewTag, PostList
from ..models import Posts, PostTags, Tag
from ..views import PostCreate, PostList, TagCreate


class TestTags(TestCase):
Expand All @@ -30,7 +30,7 @@ def setUp(self):
def test_rendering_new_tag_page_as_a_staff_user(self):
request = self.factory.get('new-tag')
request.user = self.staff_user
response = NewTag.as_view()(request)
response = TagCreate.as_view()(request)

self.assertTrue(response.status_code, 200)

Expand Down Expand Up @@ -206,3 +206,71 @@ def test_view_post_as_a_non_staff_user_displays_ok(self):
self.assertEqual(response.status_code, 200)
self.assertEqual(response.request['PATH_INFO'], page_url)
self.assertContains(response, 'The Future of Cryptocurrency')

def test_post_update_view_displays_ok(self):
tag = Tag.objects.create(name='Cryptocurrency')
post = Posts.objects.create(
title='The Future of Cryptocurrency',
content='Lorem ipsum content',
status=2,)
post_tags = PostTags.objects.create(post=post, tag=tag)
page_url = reverse('blog:edit-post', kwargs={'pk': post.pk})

# login
self.client.login(username='pedro', password='pedro30#')
response = self.client.get(page_url)

self.assertEqual(response.status_code, 200)
self.assertEqual(response.request['PATH_INFO'], page_url)

def test_post_update_valid_form(self):
tag = Tag.objects.create(name='Cryptocurrency')
post = Posts.objects.create(
title='The Future of Cryptocurrency',
content='Lorem ipsum content',
status=2,)
post_tags = PostTags.objects.create(post=post, tag=tag)

page_url = reverse('blog:edit-post', kwargs={'pk': post.pk})
post_update = {
'pk': post.pk,
'title': 'Time out!',
'content': 'This is a post update',
'tags': tag.id,
'status': 2,
}

self.client.login(username='pedro', password='pedro30#')
response = self.client.post(page_url, post_update)
post = Posts.objects.get(pk=post.pk)

self.assertEqual(response.status_code, 302)
self.assertEqual(post.title, post_update['title'])
self.assertEqual(post.content, post_update['content'])

def test_post_update_without_title_returns_invalid_form(self):
tag = Tag.objects.create(name='Cryptocurrency')
post = Posts.objects.create(
title='The Future of Cryptocurrency',
content='Lorem ipsum content',
status=2,)
post_tags = PostTags.objects.create(post=post, tag=tag)

page_url = reverse('blog:edit-post', kwargs={'pk': post.pk})
post_update = {
'pk': post.pk,
'title': '',
'content': 'This is a post update',
'tags': tag.id,
'status': 2,
}

self.client.login(username='pedro', password='pedro30#')
response = self.client.post(page_url, post_update)
form = response.context_data['form']
expected_form_error = {'title': ['This field is required.']}

self.assertEqual(response.status_code, 200)
self.assertFalse(form.is_valid())
self.assertEqual(form.errors, expected_form_error)

26 changes: 13 additions & 13 deletions blog/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@
from django.contrib.auth import views as auth_views

from .views import (
PostCreate,
PostDetail,
PostList,
PostUpdate,
PostsInTag,
TagCreate,
TagCreatePopUp,
custom_login,
EditPost,
NewPost,
NewTag,
NewTagPopUp,
post_search,
posts_in_tag,
PostList,
ViewPost,
)


Expand All @@ -21,11 +21,11 @@
url(r"^login/$", custom_login, name="login"),
url(r"^logout/$", auth_views.logout_then_login, name="logout"),

url(r"^new-post/$", NewPost.as_view(), name="new-post"),
url(r"^new-tag/$", NewTag.as_view(), name="new-tag"),
url(r"^new-tag/popup/$", NewTagPopUp.as_view(), name="new-tag-popup"),
url(r"^post/(?P<slug>[\w\-]+)/$", ViewPost.as_view(), name="view-post"),
url(r"^post/edit/(?P<pk>[0-9]+)/$", EditPost.as_view(), name="edit-post"),
url(r"^new-post/$", PostCreate.as_view(), name="new-post"),
url(r"^new-tag/$", TagCreate.as_view(), name="new-tag"),
url(r"^new-tag/popup/$", TagCreatePopUp.as_view(), name="new-tag-popup"),
url(r"^post/(?P<slug>[\w\-]+)/$", PostDetail.as_view(), name="view-post"),
url(r"^post/edit/(?P<pk>[0-9]+)/$", PostUpdate.as_view(), name="edit-post"),
url(r"^search/$", post_search, name='post_search'),
url(r"^tag/(?P<tag_slug>[\w\-]+)/$", posts_in_tag, name="posts_in_tag"),
url(r"^tag/(?P<tag_slug>[\w\-]+)/$", PostsInTag.as_view(), name="posts_in_tag"),
]
18 changes: 18 additions & 0 deletions blog/utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,24 @@
from django.core.paginator import EmptyPage, PageNotAnInteger, Paginator
from django.http import Http404


def pagination(queryset, paginate_by, page_number):
# show 7 posts per page
paginator = Paginator(queryset, paginate_by)
# page = request.GET.get('page')

try:
page = paginator.page(page_number)
except PageNotAnInteger:
# if page is not an integer, deliver first page.
page = paginator.page(1)
except EmptyPage:
# if page is out of range (e.g. 9999), deliver
# last page of results.
page = paginator.page(paginator.num_pages)
return (paginator, page, page.object_list, page.has_other_pages())


class StaffUserMixin:
"""
Mixin which requires the user to be a staff user.
Expand Down
Loading

0 comments on commit 12cc80d

Please sign in to comment.