-
Notifications
You must be signed in to change notification settings - Fork 258
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Guinsly Mondesir
committed
Feb 28, 2016
1 parent
457a7a6
commit 6d68d25
Showing
11 changed files
with
249 additions
and
18 deletions.
There are no files selected for viewing
22 changes: 22 additions & 0 deletions
22
chap_2_blog_advanced/mysite/blog/migrations/0003_post_tags.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'), | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
chap_2_blog_advanced/mysite/blog/templates/blog/post/detail.html~
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 %} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'), | ||
|
||
|
||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -36,6 +36,7 @@ | |
'django.contrib.staticfiles', | ||
#Third-Party | ||
'django_extensions', | ||
'taggit', | ||
#Applications | ||
'blog', | ||
) | ||
|