From dcf7dd31d35b15440abeb17601636ea52bb82d84 Mon Sep 17 00:00:00 2001 From: liangliangyy Date: Tue, 7 Mar 2017 22:32:16 +0800 Subject: [PATCH] =?UTF-8?q?oauth=E9=82=AE=E7=AE=B1=E7=BB=91=E5=AE=9A?= =?UTF-8?q?=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- DjangoBlog/utils.py | 16 +++++++ oauth/forms.py | 26 +++++++++++ oauth/models.py | 2 +- oauth/oauthmanager.py | 4 +- oauth/tests.py | 10 ---- oauth/urls.py | 5 +- oauth/views.py | 75 ++++++++++++++++++++++++++++-- templates/oauth/require_email.html | 46 ++++++++++++++++++ 8 files changed, 167 insertions(+), 17 deletions(-) create mode 100644 oauth/forms.py create mode 100644 templates/oauth/require_email.html diff --git a/DjangoBlog/utils.py b/DjangoBlog/utils.py index cdfcfda..7be4976 100644 --- a/DjangoBlog/utils.py +++ b/DjangoBlog/utils.py @@ -19,6 +19,8 @@ from pygments import highlight from pygments.lexers import get_lexer_by_name from pygments.formatters import html import logging +import _thread +from django.core.mail import EmailMultiAlternatives logger = logging.getLogger('djangoblog') from importlib import import_module @@ -120,3 +122,17 @@ class common_markdown(): mdp = mistune.Markdown(escape=True, renderer=renderer) return mdp(value) + + +def send_email(subject, html_content, tomail): + msg = EmailMultiAlternatives(subject, html_content, from_email='no-reply@lylinux.net', to=tomail) + msg.content_subtype = "html" + + def send_comment_email(msg): + try: + msg.send() + except: + print('send email error') + pass + + _thread.start_new_thread(send_comment_email, (msg,)) diff --git a/oauth/forms.py b/oauth/forms.py new file mode 100644 index 0000000..86851f8 --- /dev/null +++ b/oauth/forms.py @@ -0,0 +1,26 @@ +#!/usr/bin/env python +# encoding: utf-8 + + +""" +@version: ?? +@author: liangliangyy +@license: MIT Licence +@contact: liangliangyy@gmail.com +@site: https://www.lylinux.org/ +@software: PyCharm +@file: forms.py +@time: 2017/3/7 下午8:58 +""" + +from django.contrib.auth.forms import forms +from django.forms import widgets + + +class RequireEmailForm(forms.Form): + email = forms.EmailField(label='电子邮箱', required=True) + oauthid = forms.IntegerField(widget=forms.HiddenInput, required=False) + + def __init__(self, *args, **kwargs): + super(RequireEmailForm, self).__init__(*args, **kwargs) + self.fields['email'].widget = widgets.EmailInput(attrs={'placeholder': "email", "class": "form-control"}) diff --git a/oauth/models.py b/oauth/models.py index 5c0497d..4613a8c 100644 --- a/oauth/models.py +++ b/oauth/models.py @@ -4,7 +4,7 @@ from django.db import models from django.conf import settings -class oauthuser(models.Model): +class OAuthUser(models.Model): author = models.ForeignKey(settings.AUTH_USER_MODEL, verbose_name='用户', blank=True, null=True) openid = models.CharField(max_length=50) nikename = models.CharField(max_length=50, verbose_name='昵称') diff --git a/oauth/oauthmanager.py b/oauth/oauthmanager.py index 477b568..66c0bbd 100644 --- a/oauth/oauthmanager.py +++ b/oauth/oauthmanager.py @@ -14,7 +14,7 @@ """ from abc import ABCMeta, abstractmethod, abstractproperty -from oauth.models import oauthuser +from oauth.models import OAuthUser from django.conf import settings import requests import json @@ -226,7 +226,7 @@ class GitHubOauthManager(BaseOauthManager): print(rsp) try: datas = json.loads(rsp) - user = oauthuser() + user = OAuthUser() user.picture = datas['avatar_url'] user.nikename = datas['name'] user.openid = datas['id'] diff --git a/oauth/tests.py b/oauth/tests.py index d65f3fd..7ce503c 100644 --- a/oauth/tests.py +++ b/oauth/tests.py @@ -1,13 +1,3 @@ from django.test import TestCase # Create your tests here. - -class OAuthTet(TestCase): - def setUp(self): - pass - - -from oauth.oauthmanager import WBOauthManager -from django.conf import settings -settings.OAHUTH['sina'] -manager=WBOauthManager(client_id=settings.OAHUTH['sina']['appkey'],client_secret=settings.OAHUTH['sina']['appsecret'],callback_url=settings.OAHUTH['sina']['callbackurl']) \ No newline at end of file diff --git a/oauth/urls.py b/oauth/urls.py index 6a43a66..b14dbfd 100644 --- a/oauth/urls.py +++ b/oauth/urls.py @@ -17,7 +17,10 @@ from django.conf.urls import url from django.views.decorators.cache import cache_page from . import views -urlpatterns = [url(r'^oauth/authorize$', views.authorize), ] +urlpatterns = [ + url(r'^oauth/authorize$', views.authorize), + url(r'^oauth/requireemail/(?P\d+)', views.RequireEmailView.as_view(), name='require_email'), +] """ urlpatterns = [ diff --git a/oauth/views.py b/oauth/views.py index f9d6f5e..c44fab3 100644 --- a/oauth/views.py +++ b/oauth/views.py @@ -5,8 +5,12 @@ from .oauthmanager import WBOauthManager, GoogleOauthManager, get_manager_by_typ from django.conf import settings from django.http import HttpResponse, HttpResponseRedirect from django.contrib.auth import get_user_model -from .models import oauthuser +from .models import OAuthUser from django.contrib.auth import login +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 def authorize(request): @@ -25,16 +29,81 @@ def authorize(request): 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["name"], email=email) + 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["name"], email=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 + }) + print(url) + return HttpResponseRedirect(url) + + +""" +def require_email(request, oauthid): + oauthuser = get_object_or_404(OAuthUser, pk=oauthid) + if oauthuser.email: + return HttpResponseRedirect('/') + +""" + + +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) + from DjangoBlog.utils import send_email + url = '123' + content = """ +

请点击下面链接绑定您的邮箱

+ + {url} + + 再次感谢您! +
+ 如果上面链接无法打开,请将此链接复制至浏览器。 + {url} + """.format(url=url) + send_email('绑定您的电子邮箱', content, [email, ]) + return HttpResponseRedirect('/') """ diff --git a/templates/oauth/require_email.html b/templates/oauth/require_email.html new file mode 100644 index 0000000..a8ebb95 --- /dev/null +++ b/templates/oauth/require_email.html @@ -0,0 +1,46 @@ +{% extends 'share_layout/base_account.html' %} + +{% load static %} +{% block content %} +
+ + + + + +

+ Sign In +

+ +
+{% endblock %} \ No newline at end of file