From 9d41401509f53720130a4730fd7b462c4ba04855 Mon Sep 17 00:00:00 2001 From: liangliangyy Date: Mon, 31 Dec 2018 00:52:31 +0800 Subject: [PATCH] close #196 --- DjangoBlog/blog_signals.py | 2 +- DjangoBlog/feeds.py | 2 +- DjangoBlog/sitemap.py | 2 +- DjangoBlog/spider_notify.py | 2 +- DjangoBlog/tests.py | 2 +- DjangoBlog/utils.py | 2 +- accounts/forms.py | 2 +- accounts/templatetags/__init__.py | 2 +- accounts/urls.py | 2 +- accounts/user_login_backend.py | 2 +- blog/context_processors.py | 2 +- blog/forms.py | 2 +- blog/management/commands/clear_cache.py | 2 +- blog/management/commands/create_testdata.py | 2 +- blog/management/commands/ping_baidu.py | 2 +- blog/middleware.py | 2 +- blog/search_indexes.py | 2 +- blog/templatetags/__init__.py | 2 +- blog/templatetags/blog_tags.py | 9 ++++----- blog/tests.py | 7 +++++-- blog/urls.py | 2 +- blog/views.py | 17 ++++++++++------- comments/forms.py | 2 +- comments/templatetags/__init__.py | 2 +- comments/templatetags/comments_tags.py | 2 +- comments/tests.py | 2 +- comments/urls.py | 2 +- oauth/forms.py | 2 +- oauth/oauthmanager.py | 2 +- oauth/templatetags/__init__.py | 2 +- oauth/templatetags/oauth_tags.py | 2 +- oauth/urls.py | 2 +- servermanager/Api/__init__.py | 2 +- servermanager/Api/blogapi.py | 2 +- servermanager/Api/commonapi.py | 2 +- servermanager/MemcacheStorage.py | 2 +- servermanager/robot.py | 2 +- servermanager/urls.py | 2 +- 38 files changed, 54 insertions(+), 49 deletions(-) diff --git a/DjangoBlog/blog_signals.py b/DjangoBlog/blog_signals.py index f05ee6f..1abe76b 100644 --- a/DjangoBlog/blog_signals.py +++ b/DjangoBlog/blog_signals.py @@ -7,7 +7,7 @@ @author: liangliangyy @license: MIT Licence @contact: liangliangyy@gmail.com -@site: https://www.lylinux.org/ +@site: https://www.lylinux.net/ @software: PyCharm @file: blog_signals.py @time: 2017/8/12 上午10:18 diff --git a/DjangoBlog/feeds.py b/DjangoBlog/feeds.py index 37d64b0..0d198bc 100644 --- a/DjangoBlog/feeds.py +++ b/DjangoBlog/feeds.py @@ -7,7 +7,7 @@ @author: liangliangyy @license: MIT Licence @contact: liangliangyy@gmail.com -@site: https://www.lylinux.org/ +@site: https://www.lylinux.net/ @software: PyCharm @file: feed.py @time: 2016/12/22 下午10:16 diff --git a/DjangoBlog/sitemap.py b/DjangoBlog/sitemap.py index 72fcd04..e4bcfee 100644 --- a/DjangoBlog/sitemap.py +++ b/DjangoBlog/sitemap.py @@ -7,7 +7,7 @@ @author: liangliangyy @license: MIT Licence @contact: liangliangyy@gmail.com -@site: https://www.lylinux.org/ +@site: https://www.lylinux.net/ @software: PyCharm @file: sitemap.py @time: 2016/12/15 下午10:26 diff --git a/DjangoBlog/spider_notify.py b/DjangoBlog/spider_notify.py index 45cf1c6..c13835c 100644 --- a/DjangoBlog/spider_notify.py +++ b/DjangoBlog/spider_notify.py @@ -7,7 +7,7 @@ @author: liangliangyy @license: MIT Licence @contact: liangliangyy@gmail.com -@site: https://www.lylinux.org/ +@site: https://www.lylinux.net/ @software: PyCharm @file: spider_notify.py @time: 2017/1/15 下午1:41 diff --git a/DjangoBlog/tests.py b/DjangoBlog/tests.py index 09fb617..dfbaa20 100644 --- a/DjangoBlog/tests.py +++ b/DjangoBlog/tests.py @@ -36,7 +36,7 @@ class DjangoBlogTest(TestCase): import os ``` - [url](https://www.lylinux.org/) + [url](https://www.lylinux.net/) [ddd](http://www.baidu.com) diff --git a/DjangoBlog/utils.py b/DjangoBlog/utils.py index 6c6939e..fddc0e4 100644 --- a/DjangoBlog/utils.py +++ b/DjangoBlog/utils.py @@ -7,7 +7,7 @@ @author: liangliangyy @license: MIT Licence @contact: liangliangyy@gmail.com -@site: https://www.lylinux.org/ +@site: https://www.lylinux.net/ @software: PyCharm @file: utils.py @time: 2017/1/19 上午2:30 diff --git a/accounts/forms.py b/accounts/forms.py index bde2e3e..a95e353 100644 --- a/accounts/forms.py +++ b/accounts/forms.py @@ -7,7 +7,7 @@ @author: liangliangyy @license: MIT Licence @contact: liangliangyy@gmail.com -@site: https://www.lylinux.org/ +@site: https://www.lylinux.net/ @software: PyCharm @file: forms.py @time: 2016/11/20 下午3:16 diff --git a/accounts/templatetags/__init__.py b/accounts/templatetags/__init__.py index 09cf369..8875802 100644 --- a/accounts/templatetags/__init__.py +++ b/accounts/templatetags/__init__.py @@ -7,7 +7,7 @@ @author: liangliangyy @license: MIT Licence @contact: liangliangyy@gmail.com -@site: https://www.lylinux.org/ +@site: https://www.lylinux.net/ @software: PyCharm @file: __init__.py @time: 2016/11/2 下午9:15 diff --git a/accounts/urls.py b/accounts/urls.py index a648a76..8f812e6 100644 --- a/accounts/urls.py +++ b/accounts/urls.py @@ -7,7 +7,7 @@ @author: liangliangyy @license: MIT Licence @contact: liangliangyy@gmail.com -@site: https://www.lylinux.org/ +@site: https://www.lylinux.net/ @software: PyCharm @file: urls.py @time: 2016/11/20 下午3:52 diff --git a/accounts/user_login_backend.py b/accounts/user_login_backend.py index d2c785e..74b464a 100644 --- a/accounts/user_login_backend.py +++ b/accounts/user_login_backend.py @@ -7,7 +7,7 @@ @author: liangliangyy @license: MIT Licence @contact: liangliangyy@gmail.com -@site: https://www.lylinux.org/ +@site: https://www.lylinux.net/ @software: PyCharm @file: user_login_backend.py @time: 2017/2/17 下午8:45 diff --git a/blog/context_processors.py b/blog/context_processors.py index 8cf7799..845cf20 100644 --- a/blog/context_processors.py +++ b/blog/context_processors.py @@ -7,7 +7,7 @@ @author: liangliangyy @license: MIT Licence @contact: liangliangyy@gmail.com -@site: https://www.lylinux.org/ +@site: https://www.lylinux.net/ @software: PyCharm @file: context_processors.py @time: 2016/11/6 下午4:23 diff --git a/blog/forms.py b/blog/forms.py index 04fa4cc..3c09fc3 100644 --- a/blog/forms.py +++ b/blog/forms.py @@ -7,7 +7,7 @@ @author: liangliangyy @license: MIT Licence @contact: liangliangyy@gmail.com -@site: https://www.lylinux.org/ +@site: https://www.lylinux.net/ @software: PyCharm @file: forms.py @time: 2017/1/7 上午12:36 diff --git a/blog/management/commands/clear_cache.py b/blog/management/commands/clear_cache.py index db0dad2..f941bf4 100644 --- a/blog/management/commands/clear_cache.py +++ b/blog/management/commands/clear_cache.py @@ -7,7 +7,7 @@ @author: liangliangyy @license: MIT Licence @contact: liangliangyy@gmail.com -@site: https://www.lylinux.org/ +@site: https://www.lylinux.net/ @software: PyCharm @file: clear_cache.py @time: 2017/2/17 下午10:30 diff --git a/blog/management/commands/create_testdata.py b/blog/management/commands/create_testdata.py index 7479040..c77ef6a 100644 --- a/blog/management/commands/create_testdata.py +++ b/blog/management/commands/create_testdata.py @@ -7,7 +7,7 @@ @author: liangliangyy @license: MIT Licence @contact: liangliangyy@gmail.com -@site: https://www.lylinux.org/ +@site: https://www.lylinux.net/ @software: PyCharm @file: create_testdata.py @time: 2017/3/11 上午1:58 diff --git a/blog/management/commands/ping_baidu.py b/blog/management/commands/ping_baidu.py index cbd398a..e97bd0a 100644 --- a/blog/management/commands/ping_baidu.py +++ b/blog/management/commands/ping_baidu.py @@ -7,7 +7,7 @@ @author: liangliangyy @license: MIT Licence @contact: liangliangyy@gmail.com -@site: https://www.lylinux.org/ +@site: https://www.lylinux.net/ @software: PyCharm @file: ping_baidu.py @time: 2017/1/17 下午15:29 diff --git a/blog/middleware.py b/blog/middleware.py index 00e0445..3241fc4 100644 --- a/blog/middleware.py +++ b/blog/middleware.py @@ -7,7 +7,7 @@ @author: liangliangyy @license: MIT Licence @contact: liangliangyy@gmail.com -@site: https://www.lylinux.org/ +@site: https://www.lylinux.net/ @software: PyCharm @file: middleware.py @time: 2017/1/19 上午12:36 diff --git a/blog/search_indexes.py b/blog/search_indexes.py index 2cf3a67..9d0268e 100644 --- a/blog/search_indexes.py +++ b/blog/search_indexes.py @@ -7,7 +7,7 @@ @author: liangliangyy @license: MIT Licence @contact: liangliangyy@gmail.com -@site: https://www.lylinux.org/ +@site: https://www.lylinux.net/ @software: PyCharm @file: search_indexes.py @time: 2017/1/7 上午12:44 diff --git a/blog/templatetags/__init__.py b/blog/templatetags/__init__.py index 09cf369..8875802 100644 --- a/blog/templatetags/__init__.py +++ b/blog/templatetags/__init__.py @@ -7,7 +7,7 @@ @author: liangliangyy @license: MIT Licence @contact: liangliangyy@gmail.com -@site: https://www.lylinux.org/ +@site: https://www.lylinux.net/ @software: PyCharm @file: __init__.py @time: 2016/11/2 下午9:15 diff --git a/blog/templatetags/blog_tags.py b/blog/templatetags/blog_tags.py index f0072b0..d29e032 100644 --- a/blog/templatetags/blog_tags.py +++ b/blog/templatetags/blog_tags.py @@ -7,7 +7,7 @@ @author: liangliangyy @license: MIT Licence @contact: liangliangyy@gmail.com -@site: https://www.lylinux.org/ +@site: https://www.lylinux.net/ @software: PyCharm @file: blog_tags.py @time: 2016/11/2 下午11:10 @@ -142,7 +142,6 @@ def load_sidebar(user, linktype): dates = Article.objects.datetimes('created_time', 'month', order='DESC') links = Links.objects.filter(is_enable=True).filter(Q(show_type=str(linktype)) | Q(show_type='a')) commment_list = Comment.objects.filter(is_enable=True).order_by('-id')[:blogsetting.sidebar_comment_count] - # show_adsense = settings.SHOW_GOOGLE_ADSENSE # 标签云 计算字体大小 # 根据总数计算出平均值 大小为 (数目/平均值)*步长 increment = 5 @@ -151,8 +150,10 @@ def load_sidebar(user, linktype): if tags and len(tags) > 0: s = list(map(lambda t: (t, t.get_article_count()), tags)) count = sum(map(lambda t: t[1], s)) - dd = 1 if count == 0 and not len(tags) else count / len(tags) + dd = 1 if (count == 0 and not len(tags)) else count / len(tags) + import random sidebar_tags = list(map(lambda x: (x[0], x[1], (x[1] / dd) * increment + 10), s)) + random.shuffle(sidebar_tags) return { 'recent_articles': recent_articles, @@ -282,8 +283,6 @@ def gravatar_url(email, size=40): return url -# return an image tag with the gravatar -# TEMPLATE USE: {{ email|gravatar:150 }} @register.filter def gravatar(email, size=40): """获得gravatar头像""" diff --git a/blog/tests.py b/blog/tests.py index 0010e37..ac48b31 100644 --- a/blog/tests.py +++ b/blog/tests.py @@ -1,7 +1,7 @@ from django.test import Client, RequestFactory, TestCase from blog.models import Article, Category, Tag, SideBar from django.contrib.auth import get_user_model -from DjangoBlog.utils import get_current_site +from DjangoBlog.utils import get_current_site, get_md5 from blog.forms import BlogSearchForm from django.core.paginator import Paginator from blog.templatetags.blog_tags import load_pagination_info, load_articletags @@ -156,10 +156,13 @@ class ArticleTest(TestCase): imagepath = os.path.join(settings.BASE_DIR, 'python.png') with open(imagepath, 'wb') as file: file.write(rsp.content) + rsp = self.client.post('/upload') + self.assertEqual(rsp.status_code, 403) + sign = get_md5(get_md5(settings.SECRET_KEY)) with open(imagepath, 'rb') as file: imgfile = SimpleUploadedFile('python.png', file.read(), content_type='image/jpg') form_data = {'python.png': imgfile} - rsp = self.client.post('/upload', form_data, follow=True) + rsp = self.client.post('/upload?sign=' + sign, form_data, follow=True) self.assertEqual(rsp.status_code, 200) from DjangoBlog.utils import save_user_avatar, send_email diff --git a/blog/urls.py b/blog/urls.py index 6a26498..5cbefa3 100644 --- a/blog/urls.py +++ b/blog/urls.py @@ -7,7 +7,7 @@ @author: liangliangyy @license: MIT Licence @contact: liangliangyy@gmail.com -@site: https://www.lylinux.org/ +@site: https://www.lylinux.net/ @software: PyCharm @file: urls.py @time: 2016/11/2 下午7:15 diff --git a/blog/views.py b/blog/views.py index ce3b4af..e6a7c90 100644 --- a/blog/views.py +++ b/blog/views.py @@ -7,11 +7,10 @@ from django.views.generic.list import ListView from django.views.generic.detail import DetailView from django.conf import settings from django import forms -from django.http import HttpResponse -from django.http import HttpResponseRedirect +from django.http import HttpResponse, HttpResponseRedirect, HttpResponseForbidden from django.views.decorators.csrf import csrf_exempt from django.contrib.auth.decorators import login_required -from DjangoBlog.utils import cache +from DjangoBlog.utils import cache, get_md5, get_blog_setting from django.shortcuts import get_object_or_404 from blog.models import Article, Category, Tag from comments.forms import CommentForm @@ -233,16 +232,21 @@ class ArchivesView(ArticleListView): @csrf_exempt def fileupload(request): if request.method == 'POST': + sign = request.GET.get('sign', None) + if not sign: + return HttpResponseForbidden() + if not sign == get_md5(get_md5(settings.SECRET_KEY)): + return HttpResponseForbidden() response = [] for filename in request.FILES: timestr = datetime.datetime.now().strftime('%Y/%m/%d') imgextensions = ['jpg', 'png', 'jpeg', 'bmp'] fname = u''.join(str(filename)) - isimage = len([i for i in imgextensions if fname.find(i) >= 0]) > 0 + blogsetting = get_blog_setting() - basepath = r'/var/www/resource/{type}/{timestr}'.format( - type='files' if not isimage else 'image', timestr=timestr) + basepath = r'{basedir}/{type}/{timestr}'.format(basedir=blogsetting.resource_path, + type='files' if not isimage else 'image', timestr=timestr) if settings.TESTING: basepath = settings.BASE_DIR + '/uploads' url = 'https://resource.lylinux.net/{type}/{timestr}/{filename}'.format( @@ -274,7 +278,6 @@ def refresh_memcache(request): cache.clear() return HttpResponse("ok") else: - from django.http import HttpResponseForbidden return HttpResponseForbidden() except Exception as e: logger.error(e) diff --git a/comments/forms.py b/comments/forms.py index 3cf8fda..74751f1 100644 --- a/comments/forms.py +++ b/comments/forms.py @@ -7,7 +7,7 @@ @author: liangliangyy @license: MIT Licence @contact: liangliangyy@gmail.com -@site: https://www.lylinux.org/ +@site: https://www.lylinux.net/ @software: PyCharm @file: forms.py @time: 2016/11/12 下午2:45 diff --git a/comments/templatetags/__init__.py b/comments/templatetags/__init__.py index 09cf369..8875802 100644 --- a/comments/templatetags/__init__.py +++ b/comments/templatetags/__init__.py @@ -7,7 +7,7 @@ @author: liangliangyy @license: MIT Licence @contact: liangliangyy@gmail.com -@site: https://www.lylinux.org/ +@site: https://www.lylinux.net/ @software: PyCharm @file: __init__.py @time: 2016/11/2 下午9:15 diff --git a/comments/templatetags/comments_tags.py b/comments/templatetags/comments_tags.py index a78e8b6..e0fb2c2 100644 --- a/comments/templatetags/comments_tags.py +++ b/comments/templatetags/comments_tags.py @@ -7,7 +7,7 @@ @author: liangliangyy @license: MIT Licence @contact: liangliangyy@gmail.com -@site: https://www.lylinux.org/ +@site: https://www.lylinux.net/ @software: PyCharm @file: comments_tags.py @time: 2016/11/2 下午9:17 diff --git a/comments/tests.py b/comments/tests.py index 36d16d4..baeee53 100644 --- a/comments/tests.py +++ b/comments/tests.py @@ -72,7 +72,7 @@ class CommentsTest(TestCase): import os ``` - [url](https://www.lylinux.org/) + [url](https://www.lylinux.net/) [ddd](http://www.baidu.com) diff --git a/comments/urls.py b/comments/urls.py index 0bcb847..0579ccc 100644 --- a/comments/urls.py +++ b/comments/urls.py @@ -7,7 +7,7 @@ @author: liangliangyy @license: MIT Licence @contact: liangliangyy@gmail.com -@site: https://www.lylinux.org/ +@site: https://www.lylinux.net/ @software: PyCharm @file: urls.py @time: 2016/11/12 下午3:03 diff --git a/oauth/forms.py b/oauth/forms.py index 86851f8..ccf8494 100644 --- a/oauth/forms.py +++ b/oauth/forms.py @@ -7,7 +7,7 @@ @author: liangliangyy @license: MIT Licence @contact: liangliangyy@gmail.com -@site: https://www.lylinux.org/ +@site: https://www.lylinux.net/ @software: PyCharm @file: forms.py @time: 2017/3/7 下午8:58 diff --git a/oauth/oauthmanager.py b/oauth/oauthmanager.py index b3a4791..3d25235 100644 --- a/oauth/oauthmanager.py +++ b/oauth/oauthmanager.py @@ -7,7 +7,7 @@ @author: liangliangyy @license: MIT Licence @contact: liangliangyy@gmail.com -@site: https://www.lylinux.org/ +@site: https://www.lylinux.net/ @software: PyCharm @file: oauthmanager.py @time: 2016/11/26 下午5:09 diff --git a/oauth/templatetags/__init__.py b/oauth/templatetags/__init__.py index a8d9fd4..fd534e9 100644 --- a/oauth/templatetags/__init__.py +++ b/oauth/templatetags/__init__.py @@ -7,7 +7,7 @@ @author: liangliangyy @license: MIT Licence @contact: liangliangyy@gmail.com -@site: https://www.lylinux.org/ +@site: https://www.lylinux.net/ @software: PyCharm @file: __init__.py @time: 2017/3/4 下午3:22 diff --git a/oauth/templatetags/oauth_tags.py b/oauth/templatetags/oauth_tags.py index c336b03..042fee0 100644 --- a/oauth/templatetags/oauth_tags.py +++ b/oauth/templatetags/oauth_tags.py @@ -7,7 +7,7 @@ @author: liangliangyy @license: MIT Licence @contact: liangliangyy@gmail.com -@site: https://www.lylinux.org/ +@site: https://www.lylinux.net/ @software: PyCharm @file: oauth_tags.py @time: 2017/3/4 下午3:22 diff --git a/oauth/urls.py b/oauth/urls.py index 187ed8d..9afa523 100644 --- a/oauth/urls.py +++ b/oauth/urls.py @@ -7,7 +7,7 @@ @author: liangliangyy @license: MIT Licence @contact: liangliangyy@gmail.com -@site: https://www.lylinux.org/ +@site: https://www.lylinux.net/ @software: PyCharm @file: urls.py @time: 2016/11/26 下午5:25 diff --git a/servermanager/Api/__init__.py b/servermanager/Api/__init__.py index afd2725..667e57e 100644 --- a/servermanager/Api/__init__.py +++ b/servermanager/Api/__init__.py @@ -7,7 +7,7 @@ @author: liangliangyy @license: MIT Licence @contact: liangliangyy@gmail.com -@site: https://www.lylinux.org/ +@site: https://www.lylinux.net/ @software: PyCharm @file: __init__.py.py @time: 2017/8/27 上午11:40 diff --git a/servermanager/Api/blogapi.py b/servermanager/Api/blogapi.py index 8f2cc89..b1ba05b 100644 --- a/servermanager/Api/blogapi.py +++ b/servermanager/Api/blogapi.py @@ -7,7 +7,7 @@ @author: liangliangyy @license: MIT Licence @contact: liangliangyy@gmail.com -@site: https://www.lylinux.org/ +@site: https://www.lylinux.net/ @software: PyCharm @file: blogapi.py @time: 2017/8/27 上午11:40 diff --git a/servermanager/Api/commonapi.py b/servermanager/Api/commonapi.py index 56e2fd6..bd55923 100644 --- a/servermanager/Api/commonapi.py +++ b/servermanager/Api/commonapi.py @@ -7,7 +7,7 @@ @author: liangliangyy @license: MIT Licence @contact: liangliangyy@gmail.com -@site: https://www.lylinux.org/ +@site: https://www.lylinux.net/ @software: PyCharm @file: commonapi.py @time: 2017/9/2 上午1:43 diff --git a/servermanager/MemcacheStorage.py b/servermanager/MemcacheStorage.py index 33d4621..e0e233a 100644 --- a/servermanager/MemcacheStorage.py +++ b/servermanager/MemcacheStorage.py @@ -7,7 +7,7 @@ @author: liangliangyy @license: MIT Licence @contact: liangliangyy@gmail.com -@site: https://www.lylinux.org/ +@site: https://www.lylinux.net/ @software: PyCharm @file: MemcacheStorage.py @time: 2017/8/27 上午2:42 diff --git a/servermanager/robot.py b/servermanager/robot.py index e91fbb1..8834a42 100644 --- a/servermanager/robot.py +++ b/servermanager/robot.py @@ -7,7 +7,7 @@ @author: liangliangyy @license: MIT Licence @contact: liangliangyy@gmail.com -@site: https://www.lylinux.org/ +@site: https://www.lylinux.net/ @software: PyCharm @file: robot.py @time: 2017/8/27 上午1:55 diff --git a/servermanager/urls.py b/servermanager/urls.py index d9775ab..a5a075c 100644 --- a/servermanager/urls.py +++ b/servermanager/urls.py @@ -7,7 +7,7 @@ @author: liangliangyy @license: MIT Licence @contact: liangliangyy@gmail.com -@site: https://www.lylinux.org/ +@site: https://www.lylinux.net/ @software: PyCharm @file: urls.py @time: 2017/8/27 上午2:27