Skip to content

Commit db72d9b

Browse files
committed
部分代码结构调整
1 parent 08abf91 commit db72d9b

File tree

5 files changed

+31
-28
lines changed

5 files changed

+31
-28
lines changed

DjangoBlog/blog_signals.py

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,29 @@
1616
import django.dispatch
1717
from django.dispatch import receiver
1818
from django.conf import settings
19-
from DjangoBlog.utils import cache, send_email, expire_view_cache
19+
from DjangoBlog.utils import cache, send_email, expire_view_cache, get_blog_setting
2020
from DjangoBlog.spider_notify import SpiderNotify
2121
from django.contrib.sites.models import Site
22-
22+
from oauth.models import OAuthUser
2323
import logging
2424

2525
logger = logging.getLogger(__name__)
2626

2727
comment_save_signal = django.dispatch.Signal(providing_args=["comment_id", "username", "serverport"])
2828
article_save_signal = django.dispatch.Signal(providing_args=['id', 'is_update_views'])
2929
user_login_logout_signal = django.dispatch.Signal(providing_args=['id', 'type'])
30+
oauth_user_login_signal = django.dispatch.Signal(providing_args=['id'])
31+
32+
33+
@receiver(oauth_user_login_signal)
34+
def oauth_user_login_callback(sender, **kwargs):
35+
id = kwargs['id']
36+
oauthuser = OAuthUser.objects.get(id=id)
37+
setting = get_blog_setting()
38+
if oauthuser.picture and not oauthuser.picture.startswith(setting.resource_path):
39+
from DjangoBlog.utils import save_user_avatar
40+
oauthuser.picture = save_user_avatar(oauthuser.picture)
41+
oauthuser.save()
3042

3143

3244
@receiver(article_save_signal)
@@ -49,22 +61,22 @@ def article_save_callback(sender, **kwargs):
4961
SpiderNotify.baidu_notify([notify_url])
5062
except Exception as ex:
5163
logger.error("notify sipder", ex)
52-
print(ex)
64+
65+
from DjangoBlog.utils import cache
66+
cache.clear()
5367

5468

5569
@receiver(comment_save_signal)
5670
def comment_save_callback(sender, **kwargs):
5771
from comments.models import Comment
58-
if settings.DEBUG:
59-
return
6072

6173
serverport = kwargs['serverport']
6274
username = kwargs['username']
6375
comment = Comment.objects.get(id=kwargs['comment_id'])
6476
site = Site.objects.get_current().domain
6577
article = comment.article
66-
# if not settings.DEBUG:
67-
if True:
78+
if not settings.DEBUG:
79+
6880
subject = '感谢您发表的评论'
6981
article_url = "https://{site}{path}".format(site=site, path=comment.article.get_absolute_url())
7082
html_content = """
@@ -103,4 +115,5 @@ def comment_save_callback(sender, **kwargs):
103115
from django.core.cache.utils import make_template_fragment_key
104116

105117
key = make_template_fragment_key('sidebar', [username])
118+
logger.info('delete sidebar key:' + key)
106119
cache.delete(key)

DjangoBlog/spider_notify.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616
from django.contrib.sitemaps import ping_google
1717
import requests
1818
from django.conf import settings
19+
import logging
20+
21+
logger = logging.getLogger(__name__)
1922

2023

2124
class SpiderNotify():
@@ -26,14 +29,14 @@ def baidu_notify(urls):
2629
result = requests.post(settings.BAIDU_NOTIFY_URL, data=data)
2730
print(result.text)
2831
except Exception as e:
29-
print(e)
32+
logger.error(e)
3033

3134
@staticmethod
3235
def __google_notify():
3336
try:
3437
ping_google('/sitemap.xml')
3538
except Exception as e:
36-
print(e)
39+
logger.error(e)
3740

3841
@staticmethod
3942
def notify(url):

blog/admin.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ def open_article_commentstatus(modeladmin, request, queryset):
5858

5959
class ArticlelAdmin(admin.ModelAdmin):
6060
list_per_page = 20
61-
search_fields = ('body','title')
61+
search_fields = ('body', 'title')
6262
form = ArticleForm
6363
list_display = (
6464
'id', 'title', 'author', 'link_to_category', 'created_time', 'views', 'status', 'type', 'article_order')
@@ -83,8 +83,6 @@ def get_form(self, request, obj=None, **kwargs):
8383

8484
def save_model(self, request, obj, form, change):
8585
super(ArticlelAdmin, self).save_model(request, obj, form, change)
86-
from DjangoBlog.utils import cache
87-
cache.clear()
8886

8987
def get_view_on_site_url(self, obj=None):
9088
if obj:

oauth/views.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
from django.core.exceptions import ObjectDoesNotExist
1717
from django.http import HttpResponseForbidden
1818
from .oauthmanager import get_manager_by_type
19+
from DjangoBlog.blog_signals import oauth_user_login_signal
20+
1921
import logging
2022

2123
logger = logging.getLogger(__name__)
@@ -52,8 +54,6 @@ def authorize(request):
5254
return HttpResponseRedirect(manager.get_authorization_url(nexturl))
5355
user = manager.get_oauth_userinfo()
5456
if user:
55-
if user.picture:
56-
user.picture = save_user_avatar(user.picture)
5757
if not user.nikename:
5858
import datetime
5959
user.nikename = "djangoblog" + datetime.datetime.now().strftime('%y%m%d%I%M%S')
@@ -80,7 +80,10 @@ def authorize(request):
8080

8181
user.author = author
8282
user.save()
83+
84+
oauth_user_login_signal.send(sender=authorize.__class__, id=user.id)
8385
login(request, author)
86+
8487
return HttpResponseRedirect(nexturl)
8588
if not email:
8689
user.save()

templates/blog/article_index.html

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -31,20 +31,6 @@
3131
{% endfor %}
3232
{% if is_paginated %}
3333
{% load_pagination_info page_obj page_type tag_name %}
34-
{% comment %} <nav id="nav-below" class="navigation" role="navigation">
35-
<h3 class="assistive-text">文章导航</h3>
36-
{% if page_obj.has_next %}
37-
<div class="nav-previous"><a
38-
href="{{ SITE_BASE_URL }}page/{{ page_obj.next_page_number }}"><span
39-
class="meta-nav">&larr;</span> 早期文章</a></div>
40-
{% endif %}
41-
{% if page_obj.has_previous %}
42-
<div class="nav-next"><a href="{{ SITE_BASE_URL }}page/{{ page_obj.previous_page_number }}">较新文章
43-
<span
44-
class="meta-nav"></span></a>
45-
</div>
46-
{% endif %}
47-
</nav><!-- .navigation -->{% endcomment %}
4834

4935
{% endif %}
5036
</div><!-- #content -->

0 commit comments

Comments
 (0)