forked from liangliangyy/DjangoBlog
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
266 lines (223 loc) · 7.87 KB
/
utils.py
File metadata and controls
266 lines (223 loc) · 7.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
#!/usr/bin/env python
# encoding: utf-8
"""
@version: ??
@author: liangliangyy
@license: MIT Licence
@contact: liangliangyy@gmail.com
@site: https://www.lylinux.net/
@software: PyCharm
@file: utils.py
@time: 2017/1/19 上午2:30
"""
from django.core.cache import cache
from django.contrib.sites.models import Site
from hashlib import md5
import mistune
from mistune import escape, escape_link
from pygments import highlight
from pygments.lexers import get_lexer_by_name
from pygments.formatters import html
import logging
import requests
import uuid
import os
logger = logging.getLogger(__name__)
def get_max_articleid_commentid():
from blog.models import Article
from comments.models import Comment
return (Article.objects.latest().pk, Comment.objects.latest().pk)
def get_md5(str):
m = md5(str.encode('utf-8'))
return m.hexdigest()
def cache_decorator(expiration=3 * 60):
def wrapper(func):
def news(*args, **kwargs):
try:
view = args[0]
key = view.get_cache_key()
except:
key = None
pass
if not key:
unique_str = repr((func, args, kwargs))
m = md5(unique_str.encode('utf-8'))
key = m.hexdigest()
value = cache.get(key)
if value:
# logger.info('cache_decorator get cache:%s key:%s' % (func.__name__, key))
if repr(value) == 'default':
return None
else:
return value
else:
logger.info('cache_decorator set cache:%s key:%s' % (func.__name__, key))
value = func(*args, **kwargs)
if not value:
cache.set(key, 'default', expiration)
else:
cache.set(key, value, expiration)
return value
return news
return wrapper
def expire_view_cache(path, servername, serverport, key_prefix=None):
'''
刷新视图缓存
:param path:url路径
:param servername:host
:param serverport:端口
:param key_prefix:前缀
:return:是否成功
'''
from django.http import HttpRequest
from django.utils.cache import get_cache_key
request = HttpRequest()
request.META = {'SERVER_NAME': servername, 'SERVER_PORT': serverport}
request.path = path
key = get_cache_key(request, key_prefix=key_prefix, cache=cache)
if key:
logger.info('expire_view_cache:get key:{path}'.format(path=path))
if cache.get(key):
cache.delete(key)
return True
return False
def block_code(text, lang, inlinestyles=False, linenos=False):
'''
markdown代码高亮
:param text:
:param lang:
:param inlinestyles:
:param linenos:
:return:
'''
if not lang:
text = text.strip()
return u'<pre><code>%s</code></pre>\n' % mistune.escape(text)
try:
lexer = get_lexer_by_name(lang, stripall=True)
formatter = html.HtmlFormatter(
noclasses=inlinestyles, linenos=linenos
)
code = highlight(text, lexer, formatter)
if linenos:
return '<div class="highlight">%s</div>\n' % code
return code
except:
return '<pre class="%s"><code>%s</code></pre>\n' % (
lang, mistune.escape(text)
)
@cache_decorator()
def get_current_site():
site = Site.objects.get_current()
return site
class BlogMarkDownRenderer(mistune.Renderer):
'''
markdown渲染
'''
def block_code(self, text, lang=None):
# renderer has an options
inlinestyles = self.options.get('inlinestyles')
linenos = self.options.get('linenos')
return block_code(text, lang, inlinestyles, linenos)
def autolink(self, link, is_email=False):
text = link = escape(link)
if is_email:
link = 'mailto:%s' % link
if not link:
link = "#"
site = get_current_site()
nofollow = "" if link.find(site.domain) > 0 else "rel='nofollow'"
return '<a href="%s" %s>%s</a>' % (link, nofollow, text)
def link(self, link, title, text):
link = escape_link(link)
site = get_current_site()
nofollow = "" if link.find(site.domain) > 0 else "rel='nofollow'"
if not link:
link = "#"
if not title:
return '<a href="%s" %s>%s</a>' % (link, nofollow, text)
title = escape(title, quote=True)
return '<a href="%s" title="%s" %s>%s</a>' % (link, title, nofollow, text)
class CommonMarkdown():
@staticmethod
def get_markdown(value):
renderer = BlogMarkDownRenderer(inlinestyles=False)
mdp = mistune.Markdown(escape=True, renderer=renderer)
return mdp(value)
def send_email(emailto, title, content):
from DjangoBlog.blog_signals import send_email_signal
send_email_signal.send(send_email.__class__, emailto=emailto, title=title, content=content)
def parse_dict_to_url(dict):
from urllib.parse import quote
url = '&'.join(['{}={}'.format(quote(k, safe='/'), quote(v, safe='/'))
for k, v in dict.items()])
return url
def get_blog_setting():
value = cache.get('get_blog_setting')
if value:
return value
else:
from blog.models import BlogSettings
if not BlogSettings.objects.count():
setting = BlogSettings()
setting.sitename = 'DjangoBlog'
setting.site_description = '基于Django的博客系统'
setting.site_seo_description = '基于Django的博客系统'
setting.site_keywords = 'Django,Python'
setting.article_sub_length = 300
setting.sidebar_article_count = 10
setting.sidebar_comment_count = 5
setting.show_google_adsense = False
setting.open_site_comment = True
setting.analyticscode = ''
setting.beiancode = ''
setting.show_gongan_code = False
setting.save()
value = BlogSettings.objects.first()
logger.info('set cache get_blog_setting')
cache.set('get_blog_setting', value)
return value
def save_user_avatar(url):
'''
保存用户头像
:param url:头像url
:return: 本地路径
'''
setting = get_blog_setting()
logger.info(url)
try:
imgname = url.split('/')[-1]
if imgname:
path = r'{basedir}/avatar/{img}'.format(basedir=setting.resource_path, img=imgname)
if os.path.exists(path):
os.remove(path)
except:
pass
try:
rsp = requests.get(url, timeout=2)
if rsp.status_code == 200:
basepath = r'{basedir}/avatar/'.format(basedir=setting.resource_path)
if not os.path.exists(basepath):
os.makedirs(basepath)
imgextensions = ['.jpg', '.png', 'jpeg', '.gif']
isimage = len([i for i in imgextensions if url.endswith(i)]) > 0
ext = os.path.splitext(url)[1] if isimage else '.jpg'
savefilename = str(uuid.uuid4().hex) + ext
logger.info('保存用户头像:' + basepath + savefilename)
with open(basepath + savefilename, 'wb+') as file:
file.write(rsp.content)
return 'https://resource.lylinux.net/avatar/' + savefilename
except Exception as e:
logger.error(e)
return url
def delete_sidebar_cache(username):
from django.core.cache.utils import make_template_fragment_key
from blog.models import LINK_SHOW_TYPE
keys = (make_template_fragment_key('sidebar', [username + x[0]]) for x in LINK_SHOW_TYPE)
for k in keys:
logger.info('delete sidebar key:' + k)
cache.delete(k)
def delete_view_cache(prefix, keys):
from django.core.cache.utils import make_template_fragment_key
key = make_template_fragment_key(prefix, keys)
cache.delete(key)