Skip to content

Commit

Permalink
finish chapter 2
Browse files Browse the repository at this point in the history
  • Loading branch information
Guinsly Mondesir committed Feb 28, 2016
1 parent 457a7a6 commit 6d68d25
Show file tree
Hide file tree
Showing 11 changed files with 249 additions and 18 deletions.
22 changes: 22 additions & 0 deletions chap_2_blog_advanced/mysite/blog/migrations/0003_post_tags.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.9.2 on 2016-02-28 04:32
from __future__ import unicode_literals

from django.db import migrations
import taggit.managers


class Migration(migrations.Migration):

dependencies = [
('taggit', '0002_auto_20150616_2121'),
('blog', '0002_comment'),
]

operations = [
migrations.AddField(
model_name='post',
name='tags',
field=taggit.managers.TaggableManager(help_text='A comma-separated list of tags.', through='taggit.TaggedItem', to='taggit.Tag', verbose_name='Tags'),
),
]
5 changes: 5 additions & 0 deletions chap_2_blog_advanced/mysite/blog/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
from django.contrib.auth.models import User
from django.core.urlresolvers import reverse

#taggit
from taggit.managers import TaggableManager


# Create your models here.

Expand All @@ -25,9 +28,11 @@ class Post(models.Model):
created = models.DateTimeField(auto_now_add=True)
updated = models.DateTimeField(auto_now=True)
status = models.CharField(max_length=10, choices=STATUS_CHOICES, default="draft")
tags = TaggableManager()

objects = models.Manager()
published = PublishedManager()
tags = TaggableManager()


class Meta:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@ <h1>{{ post.title }}</h1>
<a href="{% url 'blog:post_share' post.id %}">Share this post</a>
</p>

<h2>Similar posts</h2>
{% for post in similar_posts %}
<p><a href="{{ post.get_absolute_url }}">{{ post.title }}</a></p>
{% empty %}
There are no similar posts yet.
{% endfor %}

{% with comments.count as total_comments %}
<h2>{{ total_comments }} comment{{ total_comments|pluralize }}</h2>
{% endwith %}
Expand Down
50 changes: 50 additions & 0 deletions chap_2_blog_advanced/mysite/blog/templates/blog/post/detail.html~
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
{% extends "blog/base.html" %}

{% block title %}{{ post.title }}{% endblock %}

{% block content %}
<h1>{{ post.title }}</h1>
<p class="date">Published {{ post.publish }} by {{ post.author }}</p>
{{ post.body|linebreaks }}

<p>
<a href="{% url 'blog:post_share' post.id %}">Share this post</a>
</p>

<h2>Similar posts</h2>
{% for post in similar_posts %}
<p><a href="{{ post.get_absolute_url }}">{{ post.title }}</a></p>
{% empty %}
There are no similar posts yet.
{% endfor %}

{% with comments.count as total_comments %}
<h2>{{ total_comments }} comment{{ total_comments|pluralize }}</h2>
{% endwith %}

{% for comment in comments %}
<div class="comment">
<p class="info">Comment {{ forloop.counter }} by {{ comment.name }} {{ comment.created }}</p>
{{ comment.body|linebreaks }}
</div>
{% empty %}
<p>There are no comments yet.</p>
{% endfor %}

{% if new_comment %}
<h2>Your comment has been added.</h2>
{% else %}
<h2>Add a new comment</h2>
<form action="." method="post">
{{ comment_form.as_p }}
{% csrf_token %}
<p><input type="submit" value="Add comment"></p>
</form>
{% endif %}




{% endblock %}


14 changes: 14 additions & 0 deletions chap_2_blog_advanced/mysite/blog/templates/blog/post/list.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,28 @@

<h1>My Blog</h1>

{% if tag %}
<h2>Posts tagged with "{{ tag.name }}"</h2>
{% endif %}

{% for post in posts %}
<h2>
<a href="{{ post.get_absolute_url }}">{{ post.title }}</a>
</h2>
<p class="tags">
Tags:
{% for tag in post.tags.all %}
<a href="{% url "blog:post_list_by_tag" tag.slug %}">{{ tag.name }}</a>
{% if not forloop.last %}, {% endif %}
{% endfor %}
</p>

<p class="date">
Published {{ post.publish }} by {{ post.author }}
</p>
{{ post.body|truncatewords:30|linebreaks }}
<p class="tags">Tags: {{ post.tags.all|join:", " }}</p>

{% endfor %}
{% endblock %}

21 changes: 10 additions & 11 deletions chap_2_blog_advanced/mysite/blog/urls.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
from django.conf.urls import url, include
from django.conf.urls import url
from . import views


urlpatterns = [
#url(r'^$', views.post_list, name='post_list'),
url(r'^$', views.PostListView.as_view(),
name='post_list'),
url(r'^(?P<year>\d{4})/(?P<month>\d{2})/(?P<day>\d{2})/(?P<post>[-\w]+)/$', views.post_detail,
# post views
url(r'^$', views.post_list, name='post_list'),
url(r'^tag/(?P<tag_slug>[-\w]+)/$', views.post_list, name='post_list_by_tag'),
#url(r'^$', views.PostListView.as_view(), name='post_list'),
url(r'^(?P<year>\d{4})/(?P<month>\d{2})/(?P<day>\d{2})/(?P<post>[-\w]+)/$',
views.post_detail,
name='post_detail'),
url(r'^(?P<post_id>\d+)/share/$',
views.post_share,
name='post_share'),


]
url(r'^(?P<post_id>\d+)/share/$', views.post_share, name='post_share'),
]
15 changes: 15 additions & 0 deletions chap_2_blog_advanced/mysite/blog/urls.py~
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from django.conf.urls import url, include
from . import views

urlpatterns = [
#url(r'^$', views.post_list, name='post_list'),
#url(r'^$', views.PostListView.as_view(), name='post_list'),
url(r'^tag/(?P<tag_slug>[-\w]+)/$', views.post_list, name='post_list_by_tag'),
url(r'^(?P<year>\d{4})/(?P<month>\d{2})/(?P<day>\d{2})/(?P<post>[-\w]+)/$', views.post_detail,
name='post_detail'),
url(r'^(?P<post_id>\d+)/share/$',
views.post_share,
name='post_share'),


]
30 changes: 23 additions & 7 deletions chap_2_blog_advanced/mysite/blog/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,21 @@
from .models import Post, Comment
from .forms import EmailPostForm, CommentForm
from django.core.mail import send_mail
#Taggit
from taggit.models import Tag
from django.db.models import Count


def post_list(request, category=None):
def post_list(request, tag_slug=None):
"""
"""
object_list = Post.published.all()
tag = None

if tag_slug:
tag = get_object_or_404(Tag, slug=tag_slug)
object_list = object_list.filter(tags__in=[tag])

paginator = Paginator(object_list, 3) # 3 posts in each page
page = request.GET.get('page')
try:
Expand All @@ -22,7 +30,8 @@ def post_list(request, category=None):
# If page is out of range deliver last page of results
posts = paginator.page(paginator.num_pages)
return render(request, 'blog/post/list.html', {'page': page,
'posts': posts})
'posts': posts,
'tag':tag})


class PostListView(ListView):
Expand Down Expand Up @@ -59,10 +68,17 @@ def post_detail(request, year, month, day, post):
new_comment.save()
else:
comment_form = CommentForm()
return render(request, 'blog/post/detail.html', {
'post': post,
'comments': comments,
'comment_form': comment_form})

# List of similar posts
post_tags_ids = post.tags.values_list('id', flat=True)
similar_posts = Post.published.filter(tags__in=post_tags_ids).exclude(id=post.id)
similar_posts = similar_posts.annotate(same_tags=Count('tags')).order_by('-same_tags',
'-publish')[:4]
return render(request, 'blog/post/detail.html', {'post': post,
'comments': comments,
'comment_form': comment_form,
'similar_posts': similar_posts})




Expand Down
102 changes: 102 additions & 0 deletions chap_2_blog_advanced/mysite/blog/views.py~
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
from django.shortcuts import render, get_object_or_404
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
from django.views.generic import ListView
from .models import Post, Comment
from .forms import EmailPostForm, CommentForm
from django.core.mail import send_mail
#Taggit
from taggit.models import Tag
from django.db.models import Count

def post_list(request, tag_slug=None):
"""

"""
object_list = Post.published.all()
tag = None

if tag_slug:
tag = get_object_or_404(Tag, slug=tag_slug)
object_list = object_list.filter(tags__in=[tag])

paginator = Paginator(object_list, 3) # 3 posts in each page
page = request.GET.get('page')
try:
posts = paginator.page(page)
except PageNotAnInteger:
# If page is not an integer deliver the first page
posts = paginator.page(1)
except EmptyPage:
# If page is out of range deliver last page of results
posts = paginator.page(paginator.num_pages)
return render(request, 'blog/post/list.html', {'page': page,
'posts': posts,
'tag':tag})


class PostListView(ListView):
"""

"""
queryset = Post.published.all()
context_object_name = 'posts'
paginate_by = 3
template_name = 'blog/post/list.html'


def post_detail(request, year, month, day, post):
"""

"""
post = get_object_or_404(Post, slug=post,
status='published',
publish__year=year,
publish__month=month,
publish__day=day)
#list of active comments for this post
comments = post.comments.filter(active=True)

if request.method == 'POST':
#A comment was posted
comment_form = CommentForm(data=request.POST)
if comment_form.is_valid():
#Created Comment object but don't save to database yet
new_comment = comment_form.save(commit=False)
# Assign the current post to the comment
new_comment.post = post
# Save the comment to the database
new_comment.save()
else:
comment_form = CommentForm()
return render(request, 'blog/post/detail.html', {
'post': post,
'comments': comments,
'comment_form': comment_form})



def post_share(request, post_id):
"""

"""
#Retrieve the post by id
post = get_object_or_404(Post, id=post_id, status='published')
sent = False

if request.method == 'POST':
#Form was submitted
form = EmailPostForm(request.POST)
if form.is_valid():
#Form fields passed validation
cd = form.cleaned_data
# .... send email
post_url = request.build_absolute_uri(post.get_absolute_url())
subject = '{} ({}) recommends you reading "{}"'.format(cd['name'], cd['email'], post.title)
message = 'Read "{}" at {}\n\n{}\'s comments: {}'.format(post.title, post_url, cd['name'], cd['comments'])
#send_mail(subject, message, '[email protected]', [cd['to']])
sent = True
else:
form = EmailPostForm()
return render(request, 'blog/post/share.html', {'post':post, 'form': form, 'sent': sent})


Binary file modified chap_2_blog_advanced/mysite/db.sqlite3
Binary file not shown.
1 change: 1 addition & 0 deletions chap_2_blog_advanced/mysite/mysite/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
'django.contrib.staticfiles',
#Third-Party
'django_extensions',
'taggit',
#Applications
'blog',
)
Expand Down

0 comments on commit 6d68d25

Please sign in to comment.