# 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
def _access_token(request, code):
    access_token_url = 'https://api.weibo.com/oauth2/access_token'
    params = {
        'client_id': settings.WEIBO_OAUTH_APP_KEY,
        'client_secret': settings.WEIBO_OAUTH_APP_SECRET,
        'grant_type': 'authorization_code',
        'code': code,
        'redirect_uri': "http://%s/oauth/" % (request.get_host()),
    }
    req = urllib2.Request(access_token_url, data=urllib.urlencode(params))
    return urllib2.urlopen(req).read()

def signin(request):
    if request.user.is_authenticated():
        return HttpResponseRedirect(reverse('userena_profile_detail', kwargs={'username':request.user.username}))
    return userena_views.signin(request, template_name='accounts/signin.html') 

def profile_edit(request, username):
    user = get_object_or_404(User, username=username)
    profile = get_user_profile(user)

    if request.method == 'POST':
        print request.POST
        form = EditProfileForm(request.POST, request.FILES)
        if form.is_valid():
            mugshot = form.cleaned_data['mugshot']
            if mugshot:
                profile.mugshot = form.cleaned_data['mugshot']
                profile.save()
    else:

# 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

def _access_token(request, code):
    access_token_url = 'https://api.weibo.com/oauth2/access_token'
    params = {
        'client_id': settings.WEIBO_OAUTH_APP_KEY,
        'client_secret': settings.WEIBO_OAUTH_APP_SECRET,
        'grant_type': 'authorization_code',
        'code': code,
        'redirect_uri': "http://%s/oauth/" % (request.get_host()),
    }
    req = urllib2.Request(access_token_url, data=urllib.urlencode(params))
    return urllib2.urlopen(req).read()

def signin(request):
    if request.user.is_authenticated():
        return HttpResponseRedirect(reverse('userena_profile_detail', kwargs={'username':request.user.username}))
    return userena_views.signin(request, template_name='accounts/signin.html') 

def profile_edit(request, username):
    user = get_object_or_404(User, username=username)
    profile = get_user_profile(user)

    if request.method == 'POST':
        print request.POST
        form = EditProfileForm(request.POST, request.FILES)
        if form.is_valid():
            mugshot = form.cleaned_data['mugshot']
            if mugshot:
                profile.mugshot = form.cleaned_data['mugshot']
                profile.save()
    else:

        form = EditProfileForm()