<NME> views.py
<BEF> from django.shortcuts import render

# Create your views here.
from .oauthmanager import WBOauthManager, GoogleOauthManager, get_manager_by_type
from django.conf import settings
from django.http import HttpResponse, HttpResponseRedirect
from django.contrib.auth import get_user_model
from django.core.exceptions import ObjectDoesNotExist
from django.db import transaction
from django.http import HttpResponseForbidden
from django.views.generic import FormView, RedirectView
from oauth.forms import RequireEmailForm
from django.core.urlresolvers import reverse


def authorize(request):
from .models import OAuthUser
from .oauthmanager import get_manager_by_type, OAuthAccessTokenException

logger = logging.getLogger(__name__)


def get_redirecturl(request):
    nexturl = request.GET.get('next_url', None)
    if not nexturl or nexturl == '/login/' or nexturl == '/login':
    if not rsp:
        return HttpResponseRedirect(manager.get_authorization_url())
    user = manager.get_oauth_userinfo()
    author = None
    if user:
        email = user.email
        email = None
        if email:
            author = get_user_model().objects.get(email=email)
            if not author:
                author = get_user_model().objects.create_user(username=user.nikename, email=email)
            user.author = author
            user.save()
            login(request, author)
            return HttpResponseRedirect('/')
        if not email:
            author = get_user_model().objects.create_user(username=user.nikename)
            user.author = author
            user.save()
            url = reverse('oauth:require_email', kwargs=
            {
                'oauthid': user.id
    except OAuthAccessTokenException as e:
        logger.warning("OAuthAccessTokenException:" + str(e))
            return HttpResponseRedirect(url)


"""
def require_email(request, oauthid):
    oauthuser = get_object_or_404(OAuthUser, pk=oauthid)
    if oauthuser.email:
        return HttpResponseRedirect('/')

"""


class RequireEmailView(FormView):
                            get_user_model().objects.get(username=user.nikename)
                        except ObjectDoesNotExist:
                            author.username = user.nikename
                        else:
                            author.username = "djangoblog" + timezone.now().strftime('%y%m%d%I%M%S')
                        author.source = 'authorize'
                        author.save()

                user.author = author
                user.save()

                oauth_user_login_signal.send(
                    sender=authorize.__class__, id=user.id)
                login(request, author)
                return HttpResponseRedirect(nexturl)
        else:
            user.save()
            url = reverse('oauth:require_email', kwargs={
                'oauthid': user.id
            })

            return HttpResponseRedirect(url)
    else:
        return HttpResponseRedirect(nexturl)


def emailconfirm(request, id, sign):
        email = form.cleaned_data['email']
        oauthid = form.cleaned_data['oauthid']
        oauthuser = get_object_or_404(OAuthUser, pk=oauthid)
        from DjangoBlog.utils import send_email
        url = '123'
        content = """
                <p>请点击下面链接绑定您的邮箱</p>

        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>恭喜您，您已经成功绑定您的邮箱，您可以使用{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)


class RequireEmailView(FormView):
    form_class = RequireEmailForm
    template_name = 'oauth/require_email.html'

    def get(self, request, *args, **kwargs):
        oauthid = self.kwargs['oauthid']
        oauthuser = get_object_or_404(OAuthUser, pk=oauthid)
        if oauthuser.email:
            pass
            # return HttpResponseRedirect('/')

        return super(RequireEmailView, self).get(request, *args, **kwargs)

    def get_initial(self):
        oauthid = self.kwargs['oauthid']
        return {
            'email': '',
            'oauthid': oauthid
        }

    def get_context_data(self, **kwargs):
        oauthid = self.kwargs['oauthid']
        oauthuser = get_object_or_404(OAuthUser, pk=oauthid)
        if oauthuser.picture:
            kwargs['picture'] = oauthuser.picture
        return super(RequireEmailView, self).get_context_data(**kwargs)

    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_sha256(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>请点击下面链接绑定您的邮箱</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)


def bindsuccess(request, oauthid):
    type = request.GET.get('type', None)
    oauthuser = get_object_or_404(OAuthUser, pk=oauthid)
    if type == 'email':
        title = '绑定成功'
        content = "恭喜您，还差一步就绑定成功了，请登录您的邮箱查看邮件完成绑定，谢谢。"
    else:
        title = '绑定成功'
        content = "恭喜您绑定成功，您以后可以使用{type}来直接免密码登录本站啦，感谢您对本站对关注。".format(
            type=oauthuser.type)
    return render(request, 'oauth/bindsuccess.html', {
        'title': title,
        'content': content
    })

<MSG> 完善oauth功能，删除注释掉的无用代码

<DFF> @@ -1,7 +1,7 @@
 from django.shortcuts import render
 
 # Create your views here.
-from .oauthmanager import WBOauthManager, GoogleOauthManager, get_manager_by_type
+
 from django.conf import settings
 from django.http import HttpResponse, HttpResponseRedirect
 from django.contrib.auth import get_user_model
@@ -11,6 +11,11 @@ from django.shortcuts import get_object_or_404
 from django.views.generic import FormView, RedirectView
 from oauth.forms import RequireEmailForm
 from django.core.urlresolvers import reverse
+from DjangoBlog.utils import send_email, get_md5
+from django.contrib.sites.models import Site
+from django.core.exceptions import ObjectDoesNotExist
+from django.http import HttpResponseForbidden
+from .oauthmanager import WBOauthManager, GoogleOauthManager, get_manager_by_type
 
 
 def authorize(request):
@@ -26,22 +31,33 @@ def authorize(request):
     if not rsp:
         return HttpResponseRedirect(manager.get_authorization_url())
     user = manager.get_oauth_userinfo()
-    author = None
+
     if user:
+        try:
+            user = OAuthUser.objects.get(type=type, openid=user.openid)
+        except ObjectDoesNotExist:
+            pass
         email = user.email
-        email = None
         if email:
-            author = get_user_model().objects.get(email=email)
+            author = None
+            try:
+                author = get_user_model().objects.get(email=email)
+            except ObjectDoesNotExist:
+                pass
             if not author:
-                author = get_user_model().objects.create_user(username=user.nikename, email=email)
+                author = get_user_model(). \
+                    objects.create_user(username=user.nikename + '_' + str(user.openid), email=email)
             user.author = author
             user.save()
             login(request, author)
             return HttpResponseRedirect('/')
         if not email:
-            author = get_user_model().objects.create_user(username=user.nikename)
+            # todo
+            # 未避免用户名重复，暂时使用oauth用户名+openid这种方式来创建用户
+            author = get_user_model().objects.get_or_create(username=user.nikename + '_' + str(user.openid))[0]
             user.author = author
             user.save()
+
             url = reverse('oauth:require_email', kwargs=
             {
                 'oauthid': user.id
@@ -50,13 +66,34 @@ def authorize(request):
             return HttpResponseRedirect(url)
 
 
-"""
-def require_email(request, oauthid):
-    oauthuser = get_object_or_404(OAuthUser, pk=oauthid)
-    if oauthuser.email:
+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 = get_user_model().objects.get(pk=oauthuser.author_id)
+    if oauthuser.email and author.email:
+        login(request, author)
         return HttpResponseRedirect('/')
+    author.set_password('$%^Q1W2E3R4T5Y6,./')
+    author.email = oauthuser.email
+    author.save()
+    login(request, author)
 
-"""
+    site = Site.objects.get_current().domain
+    send_email('恭喜您绑定成功!', '''
+     <p>恭喜您，您已经成功绑定您的邮箱，您可以使用{type}来直接免密码登录本网站.欢迎您继续关注本站，地址是</p>
+
+                <a href="{url}" rel="bookmark">{url}</a>
+
+                再次感谢您！
+                <br />
+                如果上面链接无法打开，请将此链接复制至浏览器。
+                {url}
+    '''.format(type=oauthuser.type, url='http://' + site), [oauthuser.email, ])
+
+    return HttpResponseRedirect('/')
 
 
 class RequireEmailView(FormView):
@@ -90,8 +127,18 @@ class RequireEmailView(FormView):
         email = form.cleaned_data['email']
         oauthid = form.cleaned_data['oauthid']
         oauthuser = get_object_or_404(OAuthUser, pk=oauthid)
-        from DjangoBlog.utils import send_email
-        url = '123'
+        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)
+        print(url)
         content = """
                 <p>请点击下面链接绑定您的邮箱</p>
 

{'addition_count': 60, 'commit_subject': '完善oauth功能，删除注释掉的无用代码', 'deletion_count': 13, 'file_extension': '.py', 'lang': 'py', 'license': 'mit', 'repo_name': 'liangliangyy/DjangoBlog'}