Skip to content

Commit a846071

Browse files
author
yangxueguang
committed
实现点赞通知功能
1 parent ae18244 commit a846071

40 files changed

+1256
-9
lines changed

blog/admin.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
# Register your models here.
44
from blog.models import Article, Category, Tag
5+
from notifications.models import Notification
56

67
admin.site.register(Article)
78
admin.site.register(Category)

blog/models.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from django.db import models
2+
from django.db.models import F
23
from django.core.urlresolvers import reverse
34
from django.conf import settings
45

@@ -34,6 +35,10 @@ def save(self, *args, **kwargs):
3435
self.abstract = self.abstract or self.body[:120]
3536
super().save(*args, **kwargs)
3637

38+
def viewed(self):
39+
self.views += 1
40+
self.save(update_fields=['views'])
41+
3742

3843
class Category(models.Model):
3944
name = models.CharField('分类名', max_length=30)

blog/static/blog/css/comment.css

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,9 @@
7373
display: none;
7474
}
7575

76-
76+
input[name='honeypot'] {
77+
display: none;
78+
}
7779

7880
.comment-panel textarea {
7981
height: 32px;

blog/static/blog/css/custom.css

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -238,14 +238,16 @@ figure {
238238

239239
.post-category,
240240
.post-date,
241-
.post-author {
241+
.post-author,
242+
.comments-link {
242243
position: relative;
243244
padding-right: 15px;
244245
}
245246

246247
.post-category::after,
247248
.post-date::after,
248-
.post-author::after {
249+
.post-author::after,
250+
.comments-link::after {
249251
position: absolute;
250252
content: '.';
251253
color: #000;

blog/templates/blog/detail.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ <h1 class="entry-title">{{ article.title }}</h1>
2626
<span class="post-author"><a href="#">{{ article.author.username }}</a></span>
2727
{% get_comment_count for article as num_comments %}
2828
<span class="comments-link"><a href="#">{{ num_comments }} 评论</a></span>
29+
<span class="post-views"><a href="#">{{ article.views }} 阅读</a></span>
2930
</div>
3031
</header>
3132
<div class="entry-content clearfix">
@@ -37,7 +38,7 @@ <h1 class="entry-title">{{ article.title }}</h1>
3738
{% else %}
3839
<div>登录后评论</div>
3940
{% endif %}
40-
{% render_comment_form for article %}
41+
4142
{% render_comment_list for article %}
4243
</main>
4344
{% block aside %}

blog/templates/blog/index.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@ <h1 class="entry-title">
2424
<span class="post-author"><a href="#">{{ article.author.username }}</a></span>
2525
{% load comments_tags %}
2626
{% get_comment_count for article as num_comments %}
27+
2728
<span class="comments-link"><a href="#">{{ num_comments }} 评论</a></span>
29+
<span class="post-views"><a href="#">{{ article.views }} 阅读</a></span>
2830
</div>
2931
</header>
3032
<div class="entry-content clearfix">

blog/views.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@ class ArticleDetailView(DetailView):
2121
template_name = "blog/detail.html"
2222
pk_url_kwarg = 'article_id'
2323

24+
def get_object(self, queryset=None):
25+
obj = super(ArticleDetailView, self).get_object(queryset=None)
26+
obj.viewed()
27+
return obj
28+
2429

2530
class CategoryView(ListView):
2631
template_name = "blog/index.html"

blog_project/settings.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@
3838
'django.contrib.sites',
3939
'markdown_deux',
4040
'django_comments',
41+
'notifications',
42+
'likes',
4143
'blog',
4244
'comments',
4345
'accounts',
@@ -63,7 +65,8 @@
6365
TEMPLATES = [
6466
{
6567
'BACKEND': 'django.template.backends.django.DjangoTemplates',
66-
'DIRS': [os.path.join(BASE_DIR, 'blog/templates'), os.path.join(BASE_DIR, 'comments/templates')]
68+
'DIRS': [os.path.join(BASE_DIR, 'templates'), os.path.join(BASE_DIR, 'blog/templates'),
69+
os.path.join(BASE_DIR, 'comments/templates')]
6770
,
6871
'APP_DIRS': True,
6972
'OPTIONS': {
@@ -142,3 +145,15 @@
142145
}
143146

144147
AUTH_USER_MODEL = 'accounts.BlogUser'
148+
149+
# pinax likes
150+
PINAX_LIKES_LIKABLE_MODELS = {
151+
"blog.Article": {},
152+
"comments.CommentWithParent": {}, # can override default config settings for each model here
153+
}
154+
155+
AUTHENTICATION_BACKENDS = [
156+
'likes.auth_backends.CanLikeBackend',
157+
]
158+
159+
LOGIN_URL = '/login/'

blog_project/urls.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@
2020
urlpatterns = [
2121
url(r'^admin/', admin.site.urls),
2222
url(r'', include('blog.urls', namespace='blog', app_name='blog')),
23+
url(r'', include('comments.urls')),
2324
url(r'', include('django_comments.urls')),
2425
url(r'', include('django.contrib.auth.urls')),
26+
url('^notifications/', include('notifications.urls', namespace='notifications')),
27+
url(r'^likes/', include('likes.urls', namespace='pinax_likes')),
2528
]

comments/forms.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,16 @@
1212
class CommentWithParentForm(CommentForm):
1313
parent = forms.IntegerField(required=False, widget=forms.HiddenInput)
1414

15-
def __init__(self, target_object, parent=None, data=None, initial=None):
15+
def __init__(self, target_object, parent=None, data=None, initial=None, auto_id=False):
1616
self.parent = parent
1717

1818
if initial is None:
1919
initial = {}
2020
initial.update({'parent': self.parent})
21-
super(CommentWithParentForm, self).__init__(target_object, data=data, initial=initial)
21+
super(CommentWithParentForm, self).__init__(target_object, data=data, initial=initial, auto_id=auto_id)
2222
del self.fields['name']
2323
del self.fields['email']
2424
del self.fields['url']
25-
del self.fields['honeypot']
2625

2726
def get_comment_model(self):
2827
return CommentWithParent

0 commit comments

Comments
 (0)