def validate_comment(sender, comment, request, *args, **kwargs): akismet_enable=OptionSet.get('akismet_enable', 0) domain="http://%s"%(Site.objects.get_current().domain) if int(akismet_enable) == 0: return akismet_key=OptionSet.get('akismet_key', '') ak = Akismet( #key = 'cda0f27f8e2f', key=akismet_key, blog_url=domain ) try: if ak.verify_key(): data = { 'user_ip': request.META.get('REMOTE_ADDR', '127.0.0.1'), 'user_agent': request.META.get('HTTP_USER_AGENT', ''), 'referrer': request.META.get('HTTP_REFERER', ''), 'comment_type': 'comment', 'comment_author': comment.author.encode('utf-8'), } if ak.comment_check(comment.content.encode('utf-8'), data=data, build_data=True): comment.is_public = False comment.save() ak.submit_spam(comment.content.encode('utf-8'), data=data, build_data=True) except AkismetError: pass
def safecode(request): fontSize=15 image = Image.new('RGB', (80, 20), (255, 255, 255)) font = ImageFont.truetype(CAPTCHA_FONT, fontSize) draw = ImageDraw.Draw(image) tp=OptionSet.get('safecode_type', 1); if tp == str(1): code=[] gap = 5 start = 0 for i in range(0, 4): x = start + fontSize * i + random.randint(0, gap) + gap * i txt=str(random.randint(0,9)) code.append(txt) draw.text((x,5), txt,font=font,fill=(100,211, 90)) del draw request.session['safecode']=''.join(code) else: first=random.randint(0,9) second=random.randint(0,9) draw.text((0,5), str(first)+'+'+str(second),font=font,fill=(100,211, 90)) del draw request.session['safecode']=str(first+second) buf = cStringIO.StringIO() image.save(buf, 'gif') return HttpResponse(buf.getvalue(),'image/gif')
def render_to_theme(request,theme_file,template_ctx): global theme if not theme: from blog.models import OptionSet theme = OptionSet.get('blog_theme','classic') tpl_file='themes/%s/%s' % (theme , theme_file) return render(request,tpl_file,template_ctx)
def on_post_was_submit(sender,post,*args,**kwargs): try: from blog.models import OptionSet bind = OptionSet.get('bind_weibo','')=='True' if bind: access_token_key = OptionSet.get('weibo_access_token_key','') access_token_secret = OptionSet.get('weibo_access_token_secret','') weibo_client.setToken(access_token_key,access_token_secret) from weibopy.api import API from django.template.defaultfilters import removetags api = API(weibo_client) api.update_status(status='[%s] %s ... %s'\ %(post.title,removetags(post.content[:60],'a p span div img br'),\ settings.BLOG_DOMAIN+post.get_absolute_url())) except: pass
def on_post_was_submit(sender, post, *args, **kwargs): try: from blog.models import OptionSet bind = OptionSet.get('bind_weibo', '') == 'True' if bind: access_token_key = OptionSet.get('weibo_access_token_key', '') access_token_secret = OptionSet.get('weibo_access_token_secret', '') weibo_client.setToken(access_token_key, access_token_secret) from weibopy.api import API from django.template.defaultfilters import removetags api = API(weibo_client) api.update_status(status='[%s] %s ... %s'\ %(post.title,removetags(post.content[:60],'a p span div img br'),\ settings.BLOG_DOMAIN+post.get_absolute_url())) except: pass
def render_to_theme(request, theme_file, template_ctx): global theme if not theme: from blog.models import OptionSet theme = OptionSet.get("blog_theme", "classic") tpl_file = "themes/%s/%s" % (theme, theme_file) return render(request, tpl_file, template_ctx)
def setting_comment(request): gavatar=OptionSet.get('gavatar','gravatar_default') comments_per_page=OptionSet.get('comments_per_page',10) comments_notify=OptionSet.get('comments_notify',1) enable_akismet=int(OptionSet.get('akismet_enable',0)) akismet_key=OptionSet.get('akismet_key') safecode_type=OptionSet.get('safecode_type',1) return render_response(request,'admin/comment_setting.html',locals())
def on_comment_was_submit(sender,comment,*args,**kwargs): blog=Blog.get() domain="http://%s"%(Site.objects.get_current().domain) if comment.parent_id != '0': old_c=comment.parent emailtitle=u'您在 '+blog.title+u' 上的评论有了新的回复' if old_c.mail_notify: sendmail('email/reply_comment.txt',{'old':old_c,"comment":comment, 'blog':blog,'domain':domain}, emailtitle,old_c.email) else: comments_notify=OptionSet.get('comments_notify',1) if int(comments_notify) == 1 and comment.is_public==True: emailtitle=u'文章'+comment.object.title+u'有了新的评论' sendmail('email/new_comment.txt',{'comment':comment,'domain':domain},emailtitle,blog.email)
def on_comment_was_submit(sender,comment,*args,**kwargs): from blog.models import OptionSet if comment.parent: parent = comment.parent if parent.email and parent.is_public == True: blogtitle = OptionSet.get('blogtitle', 'LogPress') subject=u'您在 [ %s ] 上的评论有了新的回复!!!'%(blogtitle) sendmail('email/reply_comment.txt',{'comment':comment,\ 'settings':settings,\ 'blogtitle':blogtitle}\ ,subject,parent.email) else: if comment.email and comment.is_public==True: subject=u'文章[%s]有了新的评论了!!!'%(comment.object) sendmail('email/new_comment.txt',{'comment':comment,'settings':settings},subject,comment.email)
def save_permalink(request): linkformat=request.POST.get('permalink_format','archive/%(id)s.html') permalink_structure=request.POST.get('permalink_structure','%(year)s/%(month)s/%(day)s/%(postname)s') OptionSet.set('permalink_structure', permalink_structure) if linkformat== 'custom': OptionSet.set('permalink_format', 'custom') else: OptionSet.set('permalink_format', linkformat) messages.add_message(request, messages.INFO, 'save ok!') return HttpResponseRedirect('/admin/permalink')
def on_comment_was_submit(sender, comment, *args, **kwargs): from blog.models import OptionSet if comment.parent: parent = comment.parent if parent.email and parent.is_public == True: blogtitle = OptionSet.get('blogtitle', 'LogPress') subject = u'您在 [ %s ] 上的评论有了新的回复!!!' % (blogtitle) sendmail('email/reply_comment.txt',{'comment':comment,\ 'settings':settings,\ 'blogtitle':blogtitle}\ ,subject,parent.email) else: if comment.email and comment.is_public == True: subject = u'文章[%s]有了新的评论了!!!' % (comment.object) sendmail('email/new_comment.txt', { 'comment': comment, 'settings': settings }, subject, comment.email)
def __init__(self,email): self.email = email self.gavatar=OptionSet.get('gavatar')
def permalink(request): domain = 'http://%s'%Site.objects.get_current().domain permalink_format=OptionSet.get('permalink_format', 'archive/%(id)s.html') permalink_structure=OptionSet.get('permalink_structure','%(year)s/%(month)s/%(day)s/%(postname)s.html') return render_response(request,'admin/permalink.html',locals())
def save_commentOption(request): data=request.POST.copy() gavatar=data['gavatar'] comments_per_page=data['comments_per_page'] comments_notify=request.POST.get('comments_notify',0) akismet_enable=request.POST.get('akismet_enable') akismet_key=request.POST.get('akismet_key') safecode=request.POST.get('safecode_type') if akismet_enable: OptionSet.set('akismet_enable',1) else: OptionSet.set('akismet_enable',0) OptionSet.set('akismet_key',akismet_key) OptionSet.set('gavatar', gavatar) OptionSet.set('comments_per_page',comments_per_page) OptionSet.set('comments_notify',comments_notify) OptionSet.set('safecode_type',safecode) return HttpResponseRedirect('/admin/comment_setting')
def create_validate_code(size=(80, 20), chars=init_chars, img_type="GIF", mode="RGB", bg_color=(255, 255, 255), fg_color=(0, 0, 255), font_size=18, font_type=CAPTCHA_FONT, length=4, draw_lines=True, n_line=(1, 2), draw_points=True, point_chance = 2): width, height = size # 宽, 高 img = Image.new(mode, size, bg_color) # 创建图形 draw = ImageDraw.Draw(img) # 创建画笔 def get_chars(): '''生成给定长度的字符串,返回列表格式''' return random.sample(chars, length) def create_lines(): '''绘制干扰线''' line_num = random.randint(*n_line) # 干扰线条数 for i in range(line_num): # 起始点 begin = (random.randint(0, size[0]), random.randint(0, size[1])) #结束点 end = (random.randint(0, size[0]), random.randint(0, size[1])) draw.line([begin, end], fill=(0, 0, 0)) def create_points(): '''绘制干扰点''' chance = min(100, max(0, int(point_chance))) # 大小限制在[0, 100] for w in xrange(width): for h in xrange(height): tmp = random.randint(0, 100) if tmp > 100 - chance: draw.point((w, h), fill=(0, 0, 0)) def create_strs(): '''绘制验证码字符''' c_chars = get_chars() strs = ' %s ' % ' '.join(c_chars) # 每个字符前后以空格隔开 font = ImageFont.truetype(font_type, font_size) draw.text((0,0),strs, font=font, fill=fg_color) return ''.join(c_chars) def draw_arithmetic(): font = ImageFont.truetype(font_type, font_size) first=random.randint(1,10) second=random.randint(2,12) draw.text((0,0), str(first)+'+'+str(second),font=font,fill=(100,211, 90)) return first+second if draw_lines: create_lines() if draw_points: create_points() tp=OptionSet.get('safecode_type', 1); if tp == str(1): strs = create_strs() else: strs = draw_arithmetic() img = img.filter(ImageFilter.EDGE_ENHANCE_MORE) # 滤镜,边界加强(阈值更大) return img, strs