From db72d9bfa6a8132b30656d140094bc02bc2923ea Mon Sep 17 00:00:00 2001 From: liangliang Date: Fri, 21 Sep 2018 23:16:05 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E9=83=A8=E5=88=86=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=E7=BB=93=E6=9E=84=E8=B0=83=E6=95=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- DjangoBlog/blog_signals.py | 27 ++++++++++++++++++++------- DjangoBlog/spider_notify.py | 7 +++++-- blog/admin.py | 4 +--- oauth/views.py | 7 +++++-- templates/blog/article_index.html | 14 -------------- 5 files changed, 31 insertions(+), 28 deletions(-) diff --git a/DjangoBlog/blog_signals.py b/DjangoBlog/blog_signals.py index f92a237..21dadc2 100644 --- a/DjangoBlog/blog_signals.py +++ b/DjangoBlog/blog_signals.py @@ -16,10 +16,10 @@ import django.dispatch from django.dispatch import receiver from django.conf import settings -from DjangoBlog.utils import cache, send_email, expire_view_cache +from DjangoBlog.utils import cache, send_email, expire_view_cache, get_blog_setting from DjangoBlog.spider_notify import SpiderNotify from django.contrib.sites.models import Site - +from oauth.models import OAuthUser import logging logger = logging.getLogger(__name__) @@ -27,6 +27,18 @@ logger = logging.getLogger(__name__) 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']) +oauth_user_login_signal = django.dispatch.Signal(providing_args=['id']) + + +@receiver(oauth_user_login_signal) +def oauth_user_login_callback(sender, **kwargs): + id = kwargs['id'] + oauthuser = OAuthUser.objects.get(id=id) + setting = get_blog_setting() + if oauthuser.picture and not oauthuser.picture.startswith(setting.resource_path): + from DjangoBlog.utils import save_user_avatar + oauthuser.picture = save_user_avatar(oauthuser.picture) + oauthuser.save() @receiver(article_save_signal) @@ -49,22 +61,22 @@ def article_save_callback(sender, **kwargs): SpiderNotify.baidu_notify([notify_url]) except Exception as ex: logger.error("notify sipder", ex) - print(ex) + + from DjangoBlog.utils import cache + cache.clear() @receiver(comment_save_signal) def comment_save_callback(sender, **kwargs): from comments.models import Comment - if settings.DEBUG: - return 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: + if not settings.DEBUG: + subject = '感谢您发表的评论' article_url = "https://{site}{path}".format(site=site, path=comment.article.get_absolute_url()) html_content = """ @@ -103,4 +115,5 @@ def comment_save_callback(sender, **kwargs): from django.core.cache.utils import make_template_fragment_key key = make_template_fragment_key('sidebar', [username]) + logger.info('delete sidebar key:' + key) cache.delete(key) diff --git a/DjangoBlog/spider_notify.py b/DjangoBlog/spider_notify.py index d00b0c4..45cf1c6 100644 --- a/DjangoBlog/spider_notify.py +++ b/DjangoBlog/spider_notify.py @@ -16,6 +16,9 @@ from django.contrib.sitemaps import ping_google import requests from django.conf import settings +import logging + +logger = logging.getLogger(__name__) class SpiderNotify(): @@ -26,14 +29,14 @@ class SpiderNotify(): result = requests.post(settings.BAIDU_NOTIFY_URL, data=data) print(result.text) except Exception as e: - print(e) + logger.error(e) @staticmethod def __google_notify(): try: ping_google('/sitemap.xml') except Exception as e: - print(e) + logger.error(e) @staticmethod def notify(url): diff --git a/blog/admin.py b/blog/admin.py index c22e213..e0db8fb 100644 --- a/blog/admin.py +++ b/blog/admin.py @@ -58,7 +58,7 @@ open_article_commentstatus.short_description = '打开文章评论' class ArticlelAdmin(admin.ModelAdmin): list_per_page = 20 - search_fields = ('body','title') + search_fields = ('body', 'title') form = ArticleForm list_display = ( 'id', 'title', 'author', 'link_to_category', 'created_time', 'views', 'status', 'type', 'article_order') @@ -83,8 +83,6 @@ class ArticlelAdmin(admin.ModelAdmin): def save_model(self, request, obj, form, change): super(ArticlelAdmin, self).save_model(request, obj, form, change) - from DjangoBlog.utils import cache - cache.clear() def get_view_on_site_url(self, obj=None): if obj: diff --git a/oauth/views.py b/oauth/views.py index 819a0d5..d48f4fc 100644 --- a/oauth/views.py +++ b/oauth/views.py @@ -16,6 +16,8 @@ from django.contrib.sites.models import Site from django.core.exceptions import ObjectDoesNotExist from django.http import HttpResponseForbidden from .oauthmanager import get_manager_by_type +from DjangoBlog.blog_signals import oauth_user_login_signal + import logging logger = logging.getLogger(__name__) @@ -52,8 +54,6 @@ def authorize(request): return HttpResponseRedirect(manager.get_authorization_url(nexturl)) user = manager.get_oauth_userinfo() if user: - if user.picture: - user.picture = save_user_avatar(user.picture) if not user.nikename: import datetime user.nikename = "djangoblog" + datetime.datetime.now().strftime('%y%m%d%I%M%S') @@ -80,7 +80,10 @@ def authorize(request): user.author = author user.save() + + oauth_user_login_signal.send(sender=authorize.__class__, id=user.id) login(request, author) + return HttpResponseRedirect(nexturl) if not email: user.save() diff --git a/templates/blog/article_index.html b/templates/blog/article_index.html index fcb7845..1dfa580 100644 --- a/templates/blog/article_index.html +++ b/templates/blog/article_index.html @@ -31,20 +31,6 @@ {% endfor %} {% if is_paginated %} {% load_pagination_info page_obj page_type tag_name %} - {% comment %} {% endcomment %} {% endif %} From 94186988a15cc3fa269470784a523b8227ce3405 Mon Sep 17 00:00:00 2001 From: liangliang Date: Fri, 21 Sep 2018 23:33:01 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E5=B0=86=E5=8F=91=E9=80=81=E9=82=AE?= =?UTF-8?q?=E4=BB=B6=E4=BF=AE=E6=94=B9=E4=B8=BAsignal=20=E6=96=B9=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- DjangoBlog/blog_signals.py | 31 +++++++++++++++++++++++++++-- DjangoBlog/utils.py | 40 ++++++++++++++++++++------------------ oauth/views.py | 6 +----- 3 files changed, 51 insertions(+), 26 deletions(-) diff --git a/DjangoBlog/blog_signals.py b/DjangoBlog/blog_signals.py index 21dadc2..061d9d9 100644 --- a/DjangoBlog/blog_signals.py +++ b/DjangoBlog/blog_signals.py @@ -20,6 +20,8 @@ from DjangoBlog.utils import cache, send_email, expire_view_cache, get_blog_sett from DjangoBlog.spider_notify import SpiderNotify from django.contrib.sites.models import Site from oauth.models import OAuthUser +from django.core.mail import EmailMultiAlternatives + import logging logger = logging.getLogger(__name__) @@ -28,6 +30,31 @@ comment_save_signal = django.dispatch.Signal(providing_args=["comment_id", "user article_save_signal = django.dispatch.Signal(providing_args=['id', 'is_update_views']) user_login_logout_signal = django.dispatch.Signal(providing_args=['id', 'type']) oauth_user_login_signal = django.dispatch.Signal(providing_args=['id']) +send_email_signal = django.dispatch.Signal(providing_args=['emailto', 'title', 'content']) + + +@receiver(send_email_signal) +def send_email_callback(sender, **kwargs): + emailto = kwargs['emailto'] + title = kwargs['title'] + content = kwargs['content'] + + msg = EmailMultiAlternatives(title, content, from_email=settings.DEFAULT_FROM_EMAIL, to=emailto) + msg.content_subtype = "html" + + from servermanager.models import EmailSendLog + log = EmailSendLog() + log.title = title + log.content = content + log.emailto = ','.join(emailto) + + try: + result = msg.send() + log.send_result = result > 0 + except Exception as e: + logger.error(e) + log.send_result = False + log.save() @receiver(oauth_user_login_signal) @@ -75,8 +102,8 @@ def comment_save_callback(sender, **kwargs): comment = Comment.objects.get(id=kwargs['comment_id']) site = Site.objects.get_current().domain article = comment.article - if not settings.DEBUG: - + # if not settings.DEBUG: + if True: subject = '感谢您发表的评论' article_url = "https://{site}{path}".format(site=site, path=comment.article.get_absolute_url()) html_content = """ diff --git a/DjangoBlog/utils.py b/DjangoBlog/utils.py index 19d2407..8b9167b 100644 --- a/DjangoBlog/utils.py +++ b/DjangoBlog/utils.py @@ -150,25 +150,27 @@ class CommonMarkdown(): def send_email(emailto, title, content): - msg = EmailMultiAlternatives(title, content, from_email=settings.DEFAULT_FROM_EMAIL, to=emailto) - msg.content_subtype = "html" - - def sendmsg_withlog(): - from servermanager.models import EmailSendLog - log = EmailSendLog() - log.title = title - log.content = content - log.emailto = ','.join(emailto) - - try: - result = msg.send() - log.send_result = result > 0 - except Exception as e: - logger.error(e) - log.send_result = False - log.save() - - _thread.start_new_thread(sendmsg_withlog, ()) + from DjangoBlog.blog_signals import send_email_signal + send_email_signal.send(send_email.__class__, emailto=emailto, title=title, content=content) + # msg = EmailMultiAlternatives(title, content, from_email=settings.DEFAULT_FROM_EMAIL, to=emailto) + # msg.content_subtype = "html" + # + # def sendmsg_withlog(): + # from servermanager.models import EmailSendLog + # log = EmailSendLog() + # log.title = title + # log.content = content + # log.emailto = ','.join(emailto) + # + # try: + # result = msg.send() + # log.send_result = result > 0 + # except Exception as e: + # logger.error(e) + # log.send_result = False + # log.save() + # + # _thread.start_new_thread(sendmsg_withlog, ()) def parse_dict_to_url(dict): diff --git a/oauth/views.py b/oauth/views.py index d48f4fc..c399255 100644 --- a/oauth/views.py +++ b/oauth/views.py @@ -111,13 +111,9 @@ def emailconfirm(request, id, sign): if result[1]: author.username = oauthuser.nikename author.save() - """ - if oauthuser.email and author.email: - login(request, author) - return HttpResponseRedirect('/') - """ oauthuser.author = author oauthuser.save() + oauth_user_login_signal.send(sender=emailconfirm.__class__, id=oauthuser.id) login(request, author) site = Site.objects.get_current().domain