# Path: src/accounts/models.py
# class UserSource(models.Model):
#     flag = models.IntegerField()    # 0: 管理员添加, 10: 网站注册，大于等于100: OAuth
#     name = models.CharField(max_length=50)
# 
#     def __unicode__(self):
#         return self.name
# 
# Path: src/accounts/forms.py
# class EditProfileForm(forms.ModelForm):
#     class Meta:
#         model = UserProfile
#         fields = ['mugshot']


from django.http import HttpResponse, HttpResponseRedirect
from django.shortcuts import render
from django.contrib.auth import authenticate, login as auth_login
from django.contrib.auth.models import User
from django.shortcuts import get_object_or_404
from django.http import Http404
from django.core.urlresolvers import reverse
from userena.utils import signin_redirect, get_user_profile
from userena import views as userena_views
from .models import UserSource
from .forms import EditProfileForm
from presenters.models import Presenter
from thirtylol import settings
import json
import urllib, urllib2
# -*- coding: utf-8 -*-

# from django.core.urlresolvers import reverse
# from django.http import Http404

def oauth(request):
    try:
        code = request.GET.get('code', '')
        token_str = _access_token(request, code)
        token_json = json.loads(token_str)
        user = authenticate(token=token_json['access_token'], uid=token_json['uid'], expire_in=(60 * 60 * 24))
        if user:
            try:
                user_str = _show_user(token_json['access_token'], token_json['uid'])
                user_json = json.loads(user_str)
                user.username = user_json['screen_name']
                user.save()
            except:
                pass
            
            auth_login(request, user)
            profile = get_user_profile(user)

# Path: src/accounts/models.py
# class UserSource(models.Model):
#     flag = models.IntegerField()    # 0: 管理员添加, 10: 网站注册，大于等于100: OAuth
#     name = models.CharField(max_length=50)
# 
#     def __unicode__(self):
#         return self.name
# 
# Path: src/accounts/forms.py
# class EditProfileForm(forms.ModelForm):
#     class Meta:
#         model = UserProfile
#         fields = ['mugshot']


# Path: src/accounts/views.py
from django.http import HttpResponse, HttpResponseRedirect
from django.shortcuts import render
from django.contrib.auth import authenticate, login as auth_login
from django.contrib.auth.models import User
from django.shortcuts import get_object_or_404
from django.http import Http404
from django.core.urlresolvers import reverse
from userena.utils import signin_redirect, get_user_profile
from userena import views as userena_views
from .models import UserSource
from .forms import EditProfileForm
from presenters.models import Presenter
from thirtylol import settings
import json
import urllib, urllib2

# -*- coding: utf-8 -*-

# from django.core.urlresolvers import reverse
# from django.http import Http404

def oauth(request):
    try:
        code = request.GET.get('code', '')
        token_str = _access_token(request, code)
        token_json = json.loads(token_str)
        user = authenticate(token=token_json['access_token'], uid=token_json['uid'], expire_in=(60 * 60 * 24))
        if user:
            try:
                user_str = _show_user(token_json['access_token'], token_json['uid'])
                user_json = json.loads(user_str)
                user.username = user_json['screen_name']
                user.save()
            except:
                pass
            
            auth_login(request, user)
            profile = get_user_profile(user)

            profile.source = UserSource.objects.get(flag=100)