# Path: djangoblog/blog_signals.py
# def send_email_signal_handler(sender, **kwargs):
# def oauth_user_login_signal_handler(sender, **kwargs):
# def model_post_save_callback(
#         sender,
#         instance,
#         created,
#         raw,
#         using,
#         update_fields,
#         **kwargs):
# def user_auth_callback(sender, request, user, **kwargs):
# 
# Path: djangoblog/utils.py
# @cache_decorator()
# def get_current_site():
#     site = Site.objects.get_current()
#     return site
# 
# Path: djangoblog/utils.py
# def send_email(emailto, title, content):
#     from djangoblog.blog_signals import send_email_signal
#     send_email_signal.send(
#         send_email.__class__,
#         emailto=emailto,
#         title=title,
#         content=content)
# 
# def get_sha256(str):
#     m = sha256(str.encode('utf-8'))
#     return m.hexdigest()
# 
# Path: oauth/forms.py
# 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"})
# 
# Path: oauth/models.py
# class OAuthUser(models.Model):
#     author = models.ForeignKey(
#         settings.AUTH_USER_MODEL,
#         verbose_name='用户',
#         blank=True,
#         null=True,
#         on_delete=models.CASCADE)
#     openid = models.CharField(max_length=50)
#     nikename = models.CharField(max_length=50, verbose_name='昵称')
#     token = models.CharField(max_length=150, null=True, blank=True)
#     picture = models.CharField(max_length=350, blank=True, null=True)
#     type = models.CharField(blank=False, null=False, max_length=50)
#     email = models.CharField(max_length=50, null=True, blank=True)
#     matedata = models.TextField(null=True, blank=True)
#     created_time = models.DateTimeField('创建时间', default=now)
#     last_mod_time = models.DateTimeField('修改时间', default=now)
# 
#     def __str__(self):
#         return self.nikename
# 
#     class Meta:
#         verbose_name = 'oauth用户'
#         verbose_name_plural = verbose_name
#         ordering = ['-created_time']
# 
# Path: oauth/oauthmanager.py
# def get_manager_by_type(type):
#     applications = get_oauth_apps()
#     if applications:
#         finds = list(
#             filter(
#                 lambda x: x.ICON_NAME.lower() == type.lower(),
#                 applications))
#         if finds:
#             return finds[0]
#     return None
# 
# class OAuthAccessTokenException(Exception):
#     '''
#     oauth授权失败异常
#     '''


import datetime
import logging
            import datetime
from urllib.parse import urlparse
from django.conf import settings
from django.contrib.auth import get_user_model
from django.contrib.auth import login
from django.core.exceptions import ObjectDoesNotExist
from django.db import transaction
from django.http import HttpResponseForbidden
from django.http import HttpResponseRedirect
from django.shortcuts import get_object_or_404
from django.shortcuts import render
from django.urls import reverse
from django.views.generic import FormView
from djangoblog.blog_signals import oauth_user_login_signal
from djangoblog.utils import get_current_site
from djangoblog.utils import send_email, get_sha256
from oauth.forms import RequireEmailForm
from .models import OAuthUser
from .oauthmanager import get_manager_by_type, OAuthAccessTokenException
            url = reverse('oauth:require_email', kwargs={
                'oauthid': user.id
            })

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


def emailconfirm(request, id, sign):
    if not sign:
        return HttpResponseForbidden()
    if not get_sha256(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()

# Path: djangoblog/blog_signals.py
# def send_email_signal_handler(sender, **kwargs):
# def oauth_user_login_signal_handler(sender, **kwargs):
# def model_post_save_callback(
#         sender,
#         instance,
#         created,
#         raw,
#         using,
#         update_fields,
#         **kwargs):
# def user_auth_callback(sender, request, user, **kwargs):
# 
# Path: djangoblog/utils.py
# @cache_decorator()
# def get_current_site():
#     site = Site.objects.get_current()
#     return site
# 
# Path: djangoblog/utils.py
# def send_email(emailto, title, content):
#     from djangoblog.blog_signals import send_email_signal
#     send_email_signal.send(
#         send_email.__class__,
#         emailto=emailto,
#         title=title,
#         content=content)
# 
# def get_sha256(str):
#     m = sha256(str.encode('utf-8'))
#     return m.hexdigest()
# 
# Path: oauth/forms.py
# 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"})
# 
# Path: oauth/models.py
# class OAuthUser(models.Model):
#     author = models.ForeignKey(
#         settings.AUTH_USER_MODEL,
#         verbose_name='用户',
#         blank=True,
#         null=True,
#         on_delete=models.CASCADE)
#     openid = models.CharField(max_length=50)
#     nikename = models.CharField(max_length=50, verbose_name='昵称')
#     token = models.CharField(max_length=150, null=True, blank=True)
#     picture = models.CharField(max_length=350, blank=True, null=True)
#     type = models.CharField(blank=False, null=False, max_length=50)
#     email = models.CharField(max_length=50, null=True, blank=True)
#     matedata = models.TextField(null=True, blank=True)
#     created_time = models.DateTimeField('创建时间', default=now)
#     last_mod_time = models.DateTimeField('修改时间', default=now)
# 
#     def __str__(self):
#         return self.nikename
# 
#     class Meta:
#         verbose_name = 'oauth用户'
#         verbose_name_plural = verbose_name
#         ordering = ['-created_time']
# 
# Path: oauth/oauthmanager.py
# def get_manager_by_type(type):
#     applications = get_oauth_apps()
#     if applications:
#         finds = list(
#             filter(
#                 lambda x: x.ICON_NAME.lower() == type.lower(),
#                 applications))
#         if finds:
#             return finds[0]
#     return None
# 
# class OAuthAccessTokenException(Exception):
#     '''
#     oauth授权失败异常
#     '''


# Path: oauth/views.py
import datetime
import logging
            import datetime
from urllib.parse import urlparse
from django.conf import settings
from django.contrib.auth import get_user_model
from django.contrib.auth import login
from django.core.exceptions import ObjectDoesNotExist
from django.db import transaction
from django.http import HttpResponseForbidden
from django.http import HttpResponseRedirect
from django.shortcuts import get_object_or_404
from django.shortcuts import render
from django.urls import reverse
from django.views.generic import FormView
from djangoblog.blog_signals import oauth_user_login_signal
from djangoblog.utils import get_current_site
from djangoblog.utils import send_email, get_sha256
from oauth.forms import RequireEmailForm
from .models import OAuthUser
from .oauthmanager import get_manager_by_type, OAuthAccessTokenException

            url = reverse('oauth:require_email', kwargs={
                'oauthid': user.id
            })

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


def emailconfirm(request, id, sign):
    if not sign:
        return HttpResponseForbidden()
    if not get_sha256(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(