diff --git a/blog/views.py b/blog/views.py index fc5e346..d5dc7ec 100644 --- a/blog/views.py +++ b/blog/views.py @@ -114,12 +114,6 @@ class ArticleDetailView(DetailView): pk_url_kwarg = 'article_id' context_object_name = "article" - def get_object(self, queryset=None): - obj = super(ArticleDetailView, self).get_object() - obj.viewed() - self.object = obj - return obj - def get_context_data(self, **kwargs): comment_form = CommentForm() @@ -160,8 +154,9 @@ class ArticleDetailView(DetailView): article = self.object # Action Hook, 通知插件"文章详情已获取" hooks.run_action('after_article_body_get', article=article, request=self.request) - # Filter Hook, 允许插件修改文章正文 - article.body = hooks.apply_filters(ARTICLE_CONTENT_HOOK_NAME, article.body, article=article, request=self.request) + # # Filter Hook, 允许插件修改文章正文 + article.body = hooks.apply_filters(ARTICLE_CONTENT_HOOK_NAME, article.body, article=article, + request=self.request) return context diff --git a/djangoblog/settings.py b/djangoblog/settings.py index 9b527da..e48a018 100644 --- a/djangoblog/settings.py +++ b/djangoblog/settings.py @@ -337,6 +337,7 @@ ACTIVE_PLUGINS = [ 'article_copyright', 'reading_time', 'external_links', + 'view_count' ] # 加载插件 diff --git a/plugins/view_count/__init__.py b/plugins/view_count/__init__.py new file mode 100644 index 0000000..8804fdf --- /dev/null +++ b/plugins/view_count/__init__.py @@ -0,0 +1 @@ +# This file makes this a Python package \ No newline at end of file diff --git a/plugins/view_count/plugin.py b/plugins/view_count/plugin.py new file mode 100644 index 0000000..15e9d94 --- /dev/null +++ b/plugins/view_count/plugin.py @@ -0,0 +1,18 @@ +from djangoblog.plugin_manage.base_plugin import BasePlugin +from djangoblog.plugin_manage import hooks + + +class ViewCountPlugin(BasePlugin): + PLUGIN_NAME = '文章浏览次数统计' + PLUGIN_DESCRIPTION = '统计文章的浏览次数' + PLUGIN_VERSION = '0.1.0' + PLUGIN_AUTHOR = 'liangliangyy' + + def register_hooks(self): + hooks.register('after_article_body_get', self.record_view) + + def record_view(self, article, *args, **kwargs): + article.viewed() + + +plugin = ViewCountPlugin() \ No newline at end of file