5
5
from uuslug import slugify
6
6
from DjangoBlog .spider_notify import sipder_notify
7
7
from django .contrib .sites .models import Site
8
+ from DjangoBlog .utils import cache_decorator
9
+ from django .utils .functional import cached_property
10
+
11
+ class BaseModel (models .Model ):
12
+ def save (self , * args , ** kwargs ):
13
+ super ().save (* args , ** kwargs )
14
+
15
+ if 'update_fields' in kwargs and len (kwargs ['update_fields' ]) == 1 and kwargs ['update_fields' ][0 ] == 'views' :
16
+ return
17
+ try :
18
+ notify = sipder_notify ()
19
+ notify_url = self .get_full_url ()
20
+ notify .baidu_notify (notify_url )
21
+ except Exception as ex :
22
+ print (ex )
23
+
24
+ def get_full_url (self ):
25
+ site = Site .objects .get_current ().domain
26
+ url = "https://{site}{path}" .format (site = site , path = self .get_absolute_url ())
27
+ return url
28
+
29
+ class Meta :
30
+ abstract = True
8
31
9
32
10
- class Article (models . Model ):
33
+ class Article (BaseModel ):
11
34
"""文章"""
12
35
STATUS_CHOICES = (
13
36
('d' , '草稿' ),
@@ -57,6 +80,7 @@ def get_absolute_url(self):
57
80
'slug' : self .slug
58
81
})
59
82
83
+ @cache_decorator (60 * 60 * 10 )
60
84
def get_category_tree (self ):
61
85
names = []
62
86
@@ -73,17 +97,20 @@ def save(self, *args, **kwargs):
73
97
if not self .slug or self .slug == 'no-slug' or not self .id :
74
98
# Only set the slug when the object is created.
75
99
self .slug = slugify (self .title )
100
+ """
76
101
try:
77
102
notify = sipder_notify()
78
103
notify.notify(self.get_full_url())
79
104
except Exception as e:
80
105
print(e)
106
+ """
81
107
super ().save (* args , ** kwargs )
82
108
83
109
def viewed (self ):
84
110
self .views += 1
85
111
self .save (update_fields = ['views' ])
86
112
113
+ @cache_decorator (60 * 60 * 10 )
87
114
def comment_list (self ):
88
115
comments = self .comment_set .all ()
89
116
parent_comments = comments .filter (parent_comment = None )
@@ -92,10 +119,15 @@ def get_admin_url(self):
92
119
info = (self ._meta .app_label , self ._meta .model_name )
93
120
return reverse ('admin:%s_%s_change' % info , args = (self .pk ,))
94
121
95
- def get_full_url (self ):
96
- site = Site .objects .get_current ().domain
97
- article_url = "https://{site}{path}" .format (site = site , path = self .get_absolute_url ())
98
- return article_url
122
+ @cached_property
123
+ def next_article (self ):
124
+ # 下一篇
125
+ return Article .objects .filter (id__gt = self .id , status = 0 ).order_by ('id' ).first ()
126
+
127
+ @cached_property
128
+ def prev_article (self ):
129
+ # 前一篇
130
+ return Article .objects .filter (id__lt = self .id , status = 0 ).first ()
99
131
100
132
101
133
'''
@@ -162,7 +194,7 @@ def get_category_tree(self):
162
194
'''
163
195
164
196
165
- class Category (models . Model ):
197
+ class Category (BaseModel ):
166
198
"""文章分类"""
167
199
name = models .CharField ('分类名' , max_length = 30 )
168
200
created_time = models .DateTimeField ('创建时间' , auto_now_add = True )
@@ -180,16 +212,8 @@ def get_absolute_url(self):
180
212
def __str__ (self ):
181
213
return self .name
182
214
183
- def save (self , * args , ** kwargs ):
184
- try :
185
- notify = sipder_notify ()
186
- notify .notify (self .get_absolute_url ())
187
- except Exception as e :
188
- print (e )
189
- super ().save (* args , ** kwargs )
190
-
191
215
192
- class Tag (models . Model ):
216
+ class Tag (BaseModel ):
193
217
"""文章标签"""
194
218
name = models .CharField ('标签名' , max_length = 30 )
195
219
created_time = models .DateTimeField ('创建时间' , auto_now_add = True )
@@ -201,6 +225,7 @@ def __str__(self):
201
225
def get_absolute_url (self ):
202
226
return reverse ('blog:tag_detail' , kwargs = {'tag_name' : self .name })
203
227
228
+ @cache_decorator (60 * 60 * 10 )
204
229
def get_article_count (self ):
205
230
return Article .objects .filter (tags__name = self .name ).distinct ().count ()
206
231
@@ -209,14 +234,6 @@ class Meta:
209
234
verbose_name = "标签"
210
235
verbose_name_plural = verbose_name
211
236
212
- def save (self , * args , ** kwargs ):
213
- try :
214
- notify = sipder_notify ()
215
- notify .notify (self .get_absolute_url ())
216
- except Exception as e :
217
- print (e )
218
- super ().save (* args , ** kwargs )
219
-
220
237
221
238
class Links (models .Model ):
222
239
"""友情链接"""
0 commit comments