def form_valid(self, form): email = form.cleaned_data['email'] oauthid = form.cleaned_data['oauthid'] oauthuser = get_object_or_404(OAuthUser, pk=oauthid) oauthuser.email = email oauthuser.save() sign = get_md5(settings.SECRET_KEY + str(oauthuser.id) + settings.SECRET_KEY) site = get_current_site().domain if settings.DEBUG: site = '127.0.0.1:8000' path = reverse('oauth:email_confirm', kwargs={ 'id': oauthid, 'sign': sign }) url = "http://{site}{path}".format(site=site, path=path) content = """ <p>Please click the link below to bind your email</p> <a href="{url}" rel="bookmark">{url}</a> Thank you again! <br /> If the above link cannot be opened, please copy this link to your browser. {url} """ .format(url=url) send_email(emailto=[email, ], title='Bind your email', content=content) url = reverse('oauth:bindsuccess', kwargs={ 'oauthid': oauthid }) url = url + '?type=email' return HttpResponseRedirect(url)
def send_comment_email(comment): site = get_current_site().domain 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) try: if comment.parent_comment: html_content = """ 您在 <a href="%s" rel="bookmark">%s</a> 的评论 <br/> %s <br/> 收到回复啦.快去看看吧 <br/> 如果上面链接无法打开,请将此链接复制至浏览器。 %s """ % (article_url, comment.article.title, comment.parent_comment.body, article_url) tomail = comment.parent_comment.author.email send_email([tomail], subject, html_content) except Exception as e: logger.error(e)
def get_redirecturl(request): nexturl = request.GET.get('next_url', None) if not nexturl or nexturl == '/login/' or nexturl == '/login': nexturl = '/' return nexturl p = urlparse(nexturl) if p.netloc: site = get_current_site().domain if not p.netloc.replace('www.', '') == site.replace('www.', ''): logger.info('illegalurl:' + nexturl) return "/" return nexturl
def oauth_user_login_signal_handler(sender, **kwargs): id = kwargs['id'] oauthuser = OAuthUser.objects.get(id=id) site = get_current_site().domain if oauthuser.picture and not oauthuser.picture.find(site) >= 0: from webdev.utils import save_user_avatar oauthuser.picture = save_user_avatar(oauthuser.picture) oauthuser.save() delete_sidebar_cache(oauthuser.author.username) cache.clear()
def emailconfirm(request, id, sign): if not sign: return HttpResponseForbidden() if not get_md5( settings.SECRET_KEY + str(id) + settings.SECRET_KEY).upper() == sign.upper(): return HttpResponseForbidden() oauthuser = get_object_or_404(OAuthUser, pk=id) with transaction.atomic(): if oauthuser.author: author = get_user_model().objects.get(pk=oauthuser.author_id) else: result = get_user_model().objects.get_or_create(email=oauthuser.email) author = result[0] if result[1]: author.source = 'emailconfirm' author.username = oauthuser.nikename.strip() if oauthuser.nikename.strip( ) else "djangoblog" + datetime.datetime.now().strftime('%y%m%d%I%M%S') author.save() oauthuser.author = author oauthuser.save() oauth_user_login_signal.send( sender=emailconfirm.__class__, id=oauthuser.id) login(request, author) site = get_current_site().domain content = ''' p>Congratulations, you have successfully bound your mailbox, you can use {type} to log in to this website directly without a password. Welcome to continue to pay attention to this website, the address is</p> <a href="{url}" rel="bookmark">{url}</a> '''.format(type=oauthuser.type, url='http://' + site) send_email(emailto=[oauthuser.email, ], title='Congratulations on your successful binding!!', content=content) url = reverse('oauth:bindsuccess', kwargs={ 'oauthid': id }) url = url + '?type=success' return HttpResponseRedirect(url)
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 = 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 get_full_url(self): site = get_current_site().domain url = "https://{site}{path}".format(site=site, path=self.get_absolute_url()) return url