def model_post_save_callback(sender, instance, created, raw, using, update_fields, **kwargs): clearcache = False if isinstance(instance, LogEntry): return if 'get_full_url' in dir(instance): is_update_views = update_fields == {'views'} if not settings.TESTING and not is_update_views: try: notify_url = instance.get_full_url() SpiderNotify.baidu_notify([notify_url]) except Exception as ex: logger.error("notify sipder", ex) if not is_update_views: clearcache = True if isinstance(instance, Comment): path = instance.article.get_absolute_url() site = Site.objects.get_current().domain if site.find(':') > 0: site = site[0:site.find(':')] expire_view_cache(path, servername=site, serverport=80, key_prefix='blogdetail') if cache.get('seo_processor'): cache.delete('seo_processor') comment_cache_key = 'article_comments_{id}'.format(id=instance.article.id) cache.delete(comment_cache_key) delete_view_cache(instance.author.username) _thread.start_new(send_comment_email, (instance,)) if clearcache: cache.clear()
def model_post_save_callback(sender, instance, created, raw, using, update_fields, **kwargs): clearcache = False if isinstance(instance, LogEntry): return if 'get_full_url' in dir(instance): is_update_views = update_fields == {'views'} if not is_update_views: clearcache = True if isinstance(instance, Comment): path = instance.article.get_absolute_url() site = get_current_site().domain if site.find(':') > 0: site = site[0:site.find(':')] expire_view_cache(path, servername=site, serverport=80, key_prefix='blogdetail') if cache.get('seo_processor'): cache.delete('seo_processor') comment_cache_key = 'article_comments_{id}'.format( id=instance.article.id) cache.delete(comment_cache_key) delete_sidebar_cache(instance.author.username) delete_view_cache('article_comments', [str(instance.article.pk)]) _thread.start_new(send_comment_email, (instance, )) if clearcache: cache.clear()
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: subject = '感谢您发表的评论' article_url = "https://{site}{path}".format( site=site, path=comment.article.get_absolute_url()) html_content = """ <p>非常感谢您在本站发表评论</p> 您可以访问 <a href="%s" rel="bookmark">%s</a> 来查看您的评论, 再次感谢您! <br /> 如果上面链接无法打开,请将此链接复制至浏览器。 %s """ % (article_url, comment.article.title, article_url) tomail = comment.author.email send_email([tomail], subject, html_content) if comment.parent_comment: html_content = """ 您在 <a href="%s" rel="bookmark">%s</a> 的评论 <br/> %s <br/> 收到回复啦.快去看看吧 <br/> 如果上面链接无法打开,请将此链接复制至浏览器。 %s """ % (article_url, article.title, comment.parent_comment.body, article_url) tomail = comment.parent_comment.author.email send_email([tomail], subject, html_content) path = article.get_absolute_url() site = Site.objects.get_current().domain if site.find(':') > 0: site = site[0:site.find(':')] expire_view_cache(path, servername=site, serverport=serverport, key_prefix='blogdetail') if cache.get('seo_processor'): cache.delete('seo_processor') comment_cache_key = 'article_comments_{id}'.format(id=article.id) cache.delete(comment_cache_key) from django.core.cache.utils import make_template_fragment_key key = make_template_fragment_key('sidebar', [username]) cache.delete(key)
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: subject = '感谢您发表的评论' article_url = "https://{site}{path}".format(site=site, path=comment.article.get_absolute_url()) html_content = """ <p>非常感谢您在本站发表评论</p> 您可以访问 <a href="%s" rel="bookmark">%s</a> 来查看您的评论, 再次感谢您! <br /> 如果上面链接无法打开,请将此链接复制至浏览器。 %s """ % (article_url, comment.article.title, article_url) tomail = comment.author.email send_email([tomail], subject, html_content) if comment.parent_comment: html_content = """ 您在 <a href="%s" rel="bookmark">%s</a> 的评论 <br/> %s <br/> 收到回复啦.快去看看吧 <br/> 如果上面链接无法打开,请将此链接复制至浏览器。 %s """ % (article_url, article.title, comment.parent_comment.body, article_url) tomail = comment.parent_comment.author.email send_email([tomail], subject, html_content) path = article.get_absolute_url() site = Site.objects.get_current().domain if site.find(':') > 0: site = site[0:site.find(':')] expire_view_cache(path, servername=site, serverport=serverport, key_prefix='blogdetail') if cache.get('seo_processor'): cache.delete('seo_processor') comment_cache_key = 'article_comments_{id}'.format(id=article.id) cache.delete(comment_cache_key) from django.core.cache.utils import make_template_fragment_key key = make_template_fragment_key('sidebar', [username]) cache.delete(key)
def form_valid(self, form): """提交的数据验证合法后的逻辑""" user = self.request.user article_id = self.kwargs['article_id'] article = Article.objects.get(pk=article_id) if not self.request.user.is_authenticated(): email = form.cleaned_data['email'] username = form.cleaned_data['name'] user = get_user_model().objects.get_or_create(username=username, email=email)[0] # auth.login(self.request, user) comment = form.save(False) comment.article = article comment.author = user if form.cleaned_data['parent_comment_id']: parent_comment = Comment.objects.get( pk=form.cleaned_data['parent_comment_id']) comment.parent_comment = parent_comment comment.save(True) from DjangoBlog.utils import expire_view_cache, cache from django.contrib.sites.models import Site path = article.get_absolute_url() site = Site.objects.get_current().domain if site.find(':') > 0: site = site[0:site.find(':')] port = 80 try: # django1.8 没有这个方法... port = self.request.get_port() except: pass expire_view_cache(path, servername=site, serverport=port, key_prefix='blogdetail') if cache.get('seo_processor'): cache.delete('seo_processor') comment_cache_key = 'article_comments_{id}'.format(id=article_id) cache.delete(comment_cache_key) from django.core.cache.utils import make_template_fragment_key username = self.request.user.username if self.request.user else '' key = make_template_fragment_key('sidebar', [username]) cache.delete(key) return HttpResponseRedirect("%s#div-comment-%d" % (article.get_absolute_url(), comment.pk))