#!/usr/bin/env python # encoding: utf-8 """ @version: ?? @author: liangliangyy @license: MIT Licence @contact: liangliangyy@gmail.com @site: https://www.lylinux.org/ @software: PyCharm @file: blog_signals.py @time: 2017/8/12 上午10:18 """ import django.dispatch from django.dispatch import receiver from django.conf import settings from DjangoBlog.utils import cache, send_email, expire_view_cache, logger from DjangoBlog.spider_notify import SpiderNotify from django.contrib.sites.models import Site comment_save_signal = django.dispatch.Signal(providing_args=["comment_id", "username", "serverport"]) article_save_signal = django.dispatch.Signal(providing_args=['id', 'is_update_views']) user_login_logout_signal = django.dispatch.Signal(providing_args=['id', 'type']) @receiver(article_save_signal) def article_save_callback(sender, **kwargs): id = kwargs['id'] is_update_views = kwargs['is_update_views'] type = sender.__name__ obj = None from blog.models import Article, Category, Tag if type == 'Article': obj = Article.objects.get(id=id) elif type == 'Category': obj = Category.objects.get(id=id) elif type == 'Tag': obj = Tag.objects.get(id=id) if obj is not None: if not settings.TESTING and not is_update_views: try: notify_url = obj.get_full_url() SpiderNotify.baidu_notify([notify_url]) except Exception as ex: logger.error("notify sipder", ex) print(ex) @receiver(comment_save_signal) def comment_save_callback(sender, **kwargs): from comments.models import Comment serverport = kwargs['serverport'] username = kwargs['username'] comment = Comment.objects.get(id=kwargs['comment_id']) site = Site.objects.get_current().domain article = comment.article # if not settings.DEBUG: if True: subject = '感谢您发表的评论' article_url = "https://{site}{path}".format(site=site, path=comment.article.get_absolute_url()) html_content = """
非常感谢您在本站发表评论
您可以访问 %s 来查看您的评论, 再次感谢您!