Skip to content
This repository has been archived by the owner on Feb 28, 2021. It is now read-only.

Commit

Permalink
Implemented basic tags, and tests, github issue #7
Browse files Browse the repository at this point in the history
  • Loading branch information
ionescu77 committed Jun 21, 2016
1 parent cd09176 commit dea6a18
Show file tree
Hide file tree
Showing 7 changed files with 167 additions and 16 deletions.
4 changes: 3 additions & 1 deletion src/blogengine/templates/category_list.html
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ <h2><a href="{{ post.get_absolute_url }}">{{ post.title }}</a></h2>

{% endfor %}
{% else %}
<p>No posts found</p>
<div class="col-sm-12">
<p>Category not found, sorry no posts found!</p>
</div><!-- /.col-lg-12-->
{% endif %}

<div class="col-sm-12 text-center">
Expand Down
3 changes: 3 additions & 0 deletions src/blogengine/templates/post_detail.html
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ <h2>{{ object.title }}</h2>
{{ object.pub_date }}
<a href="{{ post.category.get_absolute_url }}">{{ post.category.name }}</a>
</small>
{% for tag in post.tags.all %}
<a href="{{ tag.get_absolute_url }}">{{ tag.name }}</a>
{% endfor %}
</p>
<hr>
<p class="text-justified">{{ object.text|custom_markdown }}</p>
Expand Down
5 changes: 5 additions & 0 deletions src/blogengine/templates/post_list.html
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ <h2><a href="{{ post.get_absolute_url }}">{{ post.title }}</a></h2>
<a href="{{ post.category.get_absolute_url }}">{{ post.category.name }}</a>
<br>
{{ post.text | custom_markdown | truncatechars_html:159 }}

{% for tag in post.tags.all %}
<a href="{{ tag.get_absolute_url }}">{{ tag.name }}</a>
{% endfor %}

</small>
</p>
</div><!-- /.col-lg-12-->
Expand Down
76 changes: 76 additions & 0 deletions src/blogengine/templates/tag_list.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
{% extends "includes/base.html" %}

{% load custom_markdown %}

{% block header %}
<div class="header">
<h1><code>Tags</code></h1>
</div>
{% endblock %}

{% block content %}
{% if object_list %}
{% for post in object_list %}
<div class="post">
<div class="col-sm-10">
<h2><a href="{{ post.get_absolute_url }}">{{ post.title }}</a></h2>
</div><!-- /.col-lg-12-->

<div class="col-sm-2">
<span>
<ul class="pager pull-right">
<li class="previous"><a href="{{ post.get_absolute_url }}"> Read <span aria-hidden="true">&rarr;</span></a></li>
</ul>
</span>
</div><!-- /.col-sm-2-->

<div class="col-sm-12">
<p>
<small>
{{ post.pub_date }}
{% if post.category %}
<a href="{{ post.category.get_absolute_url }}"><span class="label label-primary">{{ post.category.name }}</span></a>
{% endif %}
<br>
{{ post.text | custom_markdown | truncatechars_html:159 }}
{% if post.tags %}
<div class="col-md-12">
{% for tag in post.tags.all %}
<a href="{{ tag.get_absolute_url }}"><span class="label label-success">{{ tag.name }}</span></a>
{% endfor %}
</div>
{% endif %}

</small>
</p>
</div><!-- /.col-lg-12-->

</div>

{% endfor %}
{% else %}
<div class="col-sm-12">
<p>Tag not found, sorry no coresponding posts found</p>
</div><!-- /.col-lg-12-->
{% endif %}

<div class="col-sm-12 text-center">
<span>
<br>
<ul class="pager">
{% if page_obj.has_previous %}
<li><a href="/blog/{{ page_obj.previous_page_number }}/"><span aria-hidden="true">&larr;</span> Prev</a></li>
{% else %}
<li class="disabled"><a href=""><span aria-hidden="true">&larr;</span> Prev</a></li>
{% endif %}
{% if page_obj.has_next %}
<li><a href="/blog/{{ page_obj.next_page_number }}/">Next <span aria-hidden="true">&rarr;</span></a></li>
{% else %}
<li class="disabled"><a href="">Next <span aria-hidden="true">&rarr;</span></a></li>
{% endif %}
</ul>
</span>

</div><!-- /.col-lg-12-->

{% endblock %}
78 changes: 66 additions & 12 deletions src/blogengine/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,13 @@ def test_index(self):
category.name = 'python'
category.description = 'The Python programming language'
category.save()

# Create the tag
tag = Tag()
tag.name = 'pythonsky'
tag.description = 'The Pythonsky programming language'
tag.save()

# Create the post
post = Post()
post.title = 'My first test post for View'
Expand All @@ -438,6 +445,7 @@ def test_index(self):
post.pub_date = timezone.now()
post.category = category
post.save()
post.tags.add(tag)
# Check post saved
all_posts = Post.objects.all()
self.assertEquals(len(all_posts), 1)
Expand All @@ -448,10 +456,13 @@ def test_index(self):
#print "%s" %response
self.assertTrue(post.title in response.content)
# Check post text is in response
# Check the post category is in the response
self.assertTrue(post.category.name in response.content)
#print "%s" %response
self.assertTrue(markdown.markdown(post.text) in response.content.decode('utf-8'))
# Check the post category is in the response
self.assertTrue(post.category.name in response.content)
# Check the post tag is in the response
post_tag = all_posts[0].tags.all()[0]
self.assertTrue(post_tag.name in response.content.decode('utf-8'))
# Check the post date is in the response
self.assertTrue(str(post.pub_date.year) in response.content)
self.assertTrue(post.pub_date.strftime('%b') in response.content)
Expand All @@ -469,6 +480,11 @@ def test_post_page(self):
category.name = 'python'
category.description = 'The Python programming language'
category.save()
# Create the tag
tag = Tag()
tag.name = 'pythonsky'
tag.description = 'The Pythonsky programming language'
tag.save()
# Create the post
post = Post()
post.title = 'My first post'
Expand All @@ -477,6 +493,8 @@ def test_post_page(self):
post.pub_date = timezone.now()
post.category = category
post.save()
post.tags.add(tag)
post.save()

# Check new post saved
all_posts = Post.objects.all()
Expand All @@ -499,6 +517,10 @@ def test_post_page(self):
# Check the post category is in the response
self.assertTrue(post.category.name in response.content)

# Check the post category is in the response
post_tag = all_posts[0].tags.all()[0]
self.assertTrue(post_tag.name in response.content.decode('utf-8'))

# Check post text is in response, will fail with markdown unless UTF8 decode
#self.assertTrue(post.text in response.content)
self.assertTrue(markdown.markdown(post.text) in response.content.decode('utf-8'))
Expand Down Expand Up @@ -531,38 +553,70 @@ def test_category_page(self):
self.assertEqual(len(all_posts), 1)
only_post = all_posts[0]
self.assertEqual(only_post, post)

# Get the category URL
category_url = post.category.get_absolute_url()

# Fetch the non-existing category to test view exception
response = self.client.get("/blog/category/non-existing")
self.assertEqual(response.status_code, 200)

# Check empty querySet objects.none() - returns "No posts found"
self.assertTrue('No posts found' in response.content.decode('utf-8'))

self.assertTrue('no posts found' in response.content.decode('utf-8'))
# Fetch the category
response = self.client.get(category_url)
self.assertEqual(response.status_code, 200)

# Check the category name is in the response
self.assertTrue(post.category.name in response.content.decode('utf-8'))

# Check the post text is in the response
self.assertTrue(markdown.markdown(post.text) in response.content.decode('utf-8'))

# Check the post date is in the response
self.assertTrue(str(post.pub_date.year) in response.content.decode('utf-8'))
self.assertTrue(post.pub_date.strftime('%b') in response.content.decode('utf-8'))
self.assertTrue(str(post.pub_date.day) in response.content.decode('utf-8'))

# Check the link is marked up properly
self.assertTrue('<a href="http://127.0.0.1:8000/">my first blog post</a>' in response.content.decode('utf-8'))

# Check the correct template was used
self.assertTemplateUsed(response, 'category_list.html')

def test_tag_page(self):
# Create the tag
tag = Tag()
tag.name = 'pythonsky'
tag.description = 'The Pythonsky programming language'
tag.save()
# Create the post
post = Post()
post.title = 'My tagged post'
post.text = 'This is [my tagged blog post](http://127.0.0.1:8000/)'
post.slug = 'my-tagged-post'
post.pub_date = timezone.now()
post.save()
post.tags.add(tag)
# Check new post saved
all_posts = Post.objects.all()
self.assertEquals(len(all_posts), 1)
only_post = all_posts[0]
self.assertEquals(only_post, post)
# Get the tag URL
tag_url = post.tags.all()[0].get_absolute_url()
# Fetch the non-existing tag to test view exception
response = self.client.get("/blog/tag/non-existing")
self.assertEqual(response.status_code, 200)
# Check empty querySet objects.none() - returns "No posts found"
self.assertTrue('no coresponding posts found' in response.content.decode('utf-8'))

# Fetch the tag
response = self.client.get(tag_url)
self.assertEquals(response.status_code, 200)
# Check the tag name is in the response
self.assertTrue(post.tags.all()[0].name in response.content.decode('utf-8'))
# Check the post text is in the response
self.assertTrue(markdown.markdown(post.text) in response.content.decode('utf-8'))
# Check the post date is in the response
self.assertTrue(str(post.pub_date.year) in response.content)
self.assertTrue(post.pub_date.strftime('%b') in response.content)
self.assertTrue(str(post.pub_date.day) in response.content)
# Check the link is marked up properly
self.assertTrue('<a href="http://127.0.0.1:8000/">my tagged blog post</a>' in response.content)

# TEST for FLATPAGES Section
class FlatPageViewTest(BaseAcceptanceTest):
def test_create_flatpage(self):
Expand Down
4 changes: 2 additions & 2 deletions src/blogengine/urls.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from django.conf.urls import patterns, url
from blogengine.models import Post, Category, Tag
from blogengine.views import PostListView, DetailView, CategoryListView
from blogengine.views import PostListView, DetailView, CategoryListView, TagListView

urlpatterns = [
# Index Blog
Expand All @@ -22,7 +22,7 @@

# Tags
url(r'^tag/(?P<slug>[a-zA-Z0-9-]+)/?$',
CategoryListView.as_view(
TagListView.as_view(
paginate_by=5,
model=Tag,
)),
Expand Down
13 changes: 12 additions & 1 deletion src/blogengine/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#from django.utils.safestring import mark_safe
#import markdown2

from blogengine.models import Post, Category
from blogengine.models import Post, Category, Tag

# Create your views here.
class CategoryListView(ListView):
Expand All @@ -19,6 +19,17 @@ def get_queryset(self):
except Category.DoesNotExist:
return Post.objects.none() # returns "No posts found"

class TagListView(ListView):
template_name = 'tag_list.html'

def get_queryset(self):
slug = self.kwargs['slug']
try:
tag = Tag.objects.get(slug=slug)
return tag.post_set.all()
except Tag.DoesNotExist:
return Post.objects.none()

class PostListView(ListView):
model = Post
template_name = 'post_list.html'
Expand Down

0 comments on commit dea6a18

Please sign in to comment.