Ejemplo n.º 1
0
    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 = Site.objects.get_current().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>请点击下面链接绑定您的邮箱</p>

                <a href="{url}" rel="bookmark">{url}</a>

                再次感谢您!
                <br />
                如果上面链接无法打开,请将此链接复制至浏览器。
                {url}
                """.format(url=url)
        send_email(emailto=[email, ], title='绑定您的电子邮箱', content=content)
        url = reverse('oauth:bindsuccess', kwargs={
            'oauthid': oauthid
        })
        url = url + '?type=email'
        return HttpResponseRedirect(url)
Ejemplo n.º 2
0
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)
Ejemplo n.º 3
0
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)
    author = None
    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.username = oauthuser.nikename
            author.save()
    """
    if oauthuser.email and author.email:
        login(request, author)
        return HttpResponseRedirect('/')
    """
    oauthuser.author = author
    oauthuser.save()
    login(request, author)

    site = Site.objects.get_current().domain
    content = '''
     <p>恭喜您,您已经成功绑定您的邮箱,您可以使用{type}来直接免密码登录本网站.欢迎您继续关注本站,地址是</p>

                <a href="{url}" rel="bookmark">{url}</a>

                再次感谢您!
                <br />
                如果上面链接无法打开,请将此链接复制至浏览器。
                {url}
    '''.format(type=oauthuser.type, url='http://' + site)

    send_email(emailto=[oauthuser.email, ], title='恭喜您绑定成功!', content=content)
    url = reverse('oauth:bindsuccess', kwargs={
        'oauthid': id
    })
    url = url + '?type=success'
    return HttpResponseRedirect(url)
Ejemplo n.º 4
0
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)
    author = None
    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.username = oauthuser.nikename
            author.save()
    oauthuser.author = author
    oauthuser.save()
    oauth_user_login_signal.send(sender=emailconfirm.__class__,
                                 id=oauthuser.id)
    login(request, author)

    site = Site.objects.get_current().domain
    content = '''
     <p>恭喜您,您已经成功绑定您的邮箱,您可以使用{type}来直接免密码登录本网站.欢迎您继续关注本站,地址是</p>

                <a href="{url}" rel="bookmark">{url}</a>

                再次感谢您!
                <br />
                如果上面链接无法打开,请将此链接复制至浏览器。
                {url}
    '''.format(type=oauthuser.type, url='http://' + site)

    send_email(emailto=[
        oauthuser.email,
    ], title='恭喜您绑定成功!', content=content)
    url = reverse('oauth:bindsuccess', kwargs={'oauthid': id})
    url = url + '?type=success'
    return HttpResponseRedirect(url)
Ejemplo n.º 5
0
    def form_valid(self, form):
        if form.is_valid():
            user = form.save(False)
            user.is_active = False
            user.source = 'Register'
            user.save(True)
            site = get_current_site().domain
            sign = get_sha256(get_sha256(settings.SECRET_KEY + str(user.id)))

            if settings.DEBUG:
                site = '127.0.0.1:8000'
            path = reverse('account:result')
            url = "http://{site}{path}?type=validation&id={id}&sign={sign}".format(
                site=site, path=path, id=user.id, sign=sign)

            content = """
                            <p>请点击下面链接验证您的邮箱</p>

                            <a href="{url}" rel="bookmark">{url}</a>

                            再次感谢您!
                            <br />
                            如果上面链接无法打开,请将此链接复制至浏览器。
                            {url}
                            """.format(url=url)
            send_email(
                emailto=[
                    user.email,
                ],
                title='验证您的电子邮箱',
                content=content)

            url = reverse('accounts:result') + \
                '?type=register&id=' + str(user.id)
            return HttpResponseRedirect(url)
        else:
            return self.render_to_response({
                'form': form
            })
Ejemplo n.º 6
0
 def test_image(self):
     import requests
     rsp = requests.get(
         'https://www.python.org/static/img/[email protected]')
     imagepath = os.path.join(settings.BASE_DIR, 'python.png')
     with open(imagepath, 'wb') as file:
         file.write(rsp.content)
     rsp = self.client.post('/upload')
     self.assertEqual(rsp.status_code, 403)
     sign = get_sha256(get_sha256(settings.SECRET_KEY))
     with open(imagepath, 'rb') as file:
         imgfile = SimpleUploadedFile(
             'python.png', file.read(), content_type='image/jpg')
         form_data = {'python.png': imgfile}
         rsp = self.client.post(
             '/upload?sign=' + sign, form_data, follow=True)
         self.assertEqual(rsp.status_code, 200)
     os.remove(imagepath)
     from DjangoBlog.utils import save_user_avatar, send_email
     send_email(['*****@*****.**'], 'testTitle', 'testContent')
     save_user_avatar(
         'https://www.python.org/static/img/[email protected]')
Ejemplo n.º 7
0
    def form_valid(self, form):
        if form.is_valid():
            user = form.save(False)
            user.is_active = False
            user.source = 'Register'
            user.save(True)
            site = get_current_site().domain
            sign = get_md5(get_md5(settings.SECRET_KEY + str(user.id)))

            if settings.DEBUG:
                site = '127.0.0.1:8000'
            path = reverse('account:result')
            url = "http://{site}{path}?type=validation&id={id}&sign={sign}".format(
                site=site, path=path, id=user.id, sign=sign)
            content = render_template('confirm_email.j2', vars=locals())
            subject = 'Подтвердите email адрес'
            if content is not None:
                send_email(emailto=[
                    user.email,
                ],
                           title=subject,
                           content=content,
                           images={
                               "logo.png": "image/png",
                               "mail_icon.png": "image/png"
                           })

            url = reverse('accounts:result') + '?type=register&id=' + str(
                user.id)
            messages.success(
                self.request,
                f"Новый аккаунт %s создан. Подтвердите свой почтовый ящик: %s"
                % (user.username, user.email))
            return HttpResponseRedirect(url)
        else:
            messages.error(self.request, form.errors)
            return self.render_to_response({'form': form})
Ejemplo n.º 8
0
    def form_valid(self, form):
        if form.is_valid():
            user = form.save(False)
            user.is_active = False
            user.source = 'Register'
            user.save(True)
            site = get_current_site().domain
            sign = get_md5(get_md5(settings.SECRET_KEY + str(user.id)))

            if settings.DEBUG:
                site = '127.0.0.1:8000'
            path = reverse('account:result')
            url = "http://{site}{path}?type=validation&id={id}&sign={sign}".format(
                site=site, path=path, id=user.id, sign=sign)

            content = """
                            <p>Please click the link below to verify your email</p>

                            <a href="{url}" rel="bookmark">{url}</a>

                            Thank you again!
                            <br />
                            If the link above cannot be opened, please copy this link to your browser.
                            {url}
                            """.format(url=url)
            send_email(emailto=[
                user.email,
            ],
                       title='Verify your email',
                       content=content)

            url = reverse('accounts:result') + \
                '?type=register&id=' + str(user.id)
            return HttpResponseRedirect(url)
        else:
            return self.render_to_response({'form': form})
Ejemplo n.º 9
0
    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 = 'www.cxx1.com:80'
        path = reverse('oauth:email_confirm', kwargs={
            'id': oauthid,
            'sign': sign
        })
        url = "http://{site}{path}".format(site=site, path=path)

        content = """
                <div style="background-color:#ECECEC; padding: 35px;">
    <table cellpadding="0" align="center"
           style="width: 600px; margin: 0px auto; text-align: left; position: relative; border-top-left-radius: 5px; border-top-right-radius: 5px; border-bottom-right-radius: 5px; border-bottom-left-radius: 5px; font-size: 14px; font-family:微软雅黑, 黑体; line-height: 1.5; box-shadow: rgb(153, 153, 153) 0px 0px 5px; border-collapse: collapse; background-position: initial initial; background-repeat: initial initial;background:#fff;">
        <tbody>
        <tr>
            <th valign="middle"
                style="height: 25px; line-height: 25px; padding: 15px 35px; border-bottom-width: 1px; border-bottom-style: solid; border-bottom-color: #42a3d3; background-color: #49bcff; border-top-left-radius: 5px; border-top-right-radius: 5px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px;">
                <font face="微软雅黑" size="5" style="color: rgb(255, 255, 255); ">欢迎来到Cxx1.COM! </font>
            </th>
        </tr>
        <tr>
            <td>
                <div style="padding:25px 35px 40px; background-color:#fff;">
                    <h2 style="margin: 5px 0px; ">
                        <font color="#333333" style="line-height: 20px; ">
                            <font style="line-height: 22px; " size="4">
                                亲爱的用户请绑定邮箱</font>
                        </font>
                    </h2>
                    <p>首先感谢您加入本Cxx1站!请点击下方链接<br>
                        <b> <a href="{url}" rel="bookmark">{url}</a></b><br>
                        <b>如果没法打开请点击: {url} 并且复制到游览器</b>
                    <p align="right"></p>
                    <div style="width:700px;margin:0 auto;">
                        <div style="padding:10px 10px 0;border-top:1px solid #ccc;color:#747474;margin-bottom:20px;line-height:1.3em;font-size:12px;">
                            <p>此为系统邮件,请勿回复<br>
                                请保管好您的邮箱,避免账号被他人盗用
                            </p>
                            <p>©***</p>
                        </div>
                    </div>
                </div>
            </td>
        </tr>
        </tbody>
    </table>
</div>
                """.format(url=url)
        send_email(emailto=[email, ], title='绑定您的电子邮箱', content=content)
        url = reverse('oauth:bindsuccess', kwargs={
            'oauthid': oauthid
        })
        url = url + '?type=email'
        return HttpResponseRedirect(url)