# 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
        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)
            profile.save()
            return HttpResponseRedirect(signin_redirect(user=user))
    except:
        return HttpResponse("很抱歉，使用新浪微博认证登录失败，请尝试从网站注册！")

def request(url, method='GET', headers={}, params=None):
    data = None
    if params:
         data = urllib.urlencode(params)

    if method.upper() == 'GET':
        url = url + '?' + data
        data = None

    req = urllib2.Request(url, data=data, headers=headers)
    resp = urllib2.urlopen(req)
    return resp.info(), resp.read()


# 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

        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)
            profile.save()
            return HttpResponseRedirect(signin_redirect(user=user))
    except:
        return HttpResponse("很抱歉，使用新浪微博认证登录失败，请尝试从网站注册！")

def request(url, method='GET', headers={}, params=None):
    data = None
    if params:
         data = urllib.urlencode(params)

    if method.upper() == 'GET':
        url = url + '?' + data
        data = None

    req = urllib2.Request(url, data=data, headers=headers)
    resp = urllib2.urlopen(req)
    return resp.info(), resp.read()


def _show_user(access_token, uid):