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 %}