示例#1
0
def get_facebook_info(user):
    access_token = SocialToken.objects.filter(
        account__user=user, account__provider='facebook').first()

    graph = facebook.GraphAPI(access_token=access_token, version=2.10)
    fb_info = [
        'name', 'location', 'hometown', 'friends', 'education', 'birthday',
        'religion', 'age_range'
    ]

    fb_user_info = graph.get_object(id='me', fields=', '.join(fb_info))

    user_info = UserInfo()
    for k in fb_user_info:
        if k in fb_info:
            if k == 'education':
                temp = fb_user_info['education']
                user_info.education = temp[-1]['school']['name']
            if k == 'location':
                user_info.location = fb_user_info['location']['name']
            if k == 'hometown':
                user_info.hometown = fb_user_info['hometown']['name']
            if k == 'friends':
                user_info.num_friends = fb_user_info['friends']['summary'][
                    'total_count']
            if k == 'name':
                user_info.name = fb_user_info['name']
            if k == 'age_range':
                user_info.age_range = fb_user_info['age_range']['min']
            if k == 'religion':
                user_info.religion = fb_user_info['religion']
    user_info.save()
示例#2
0
def register(request):
    user = request.user
    if user.is_authenticated:
        logout(request)
        return redirect(to='infomanage')
    if request.method == 'POST':
        form = UserInfoForm(request.POST)
        if form.is_valid():
            username = form.cleaned_data['username']
            user_exist = User.objects.filter(username=username)
            if user_exist:
                messages.add_message(request, messages.ERROR, "用户名已存在,请更换!")
                return redirect(to='register')
            else:
                password = form.cleaned_data['password']
                User.objects.create_user(username=username, password=password)
                try:
                    acc = User.objects.get(username=username)
                    user = UserInfo()
                    user.account = acc
                    user.sex = form.cleaned_data['sex']
                    user.name = form.cleaned_data['username']
                    user.job = form.cleaned_data['job']

                    num = UserInfo.objects.order_by(
                        'job_num').reverse().all()[0].job_num
                    user.job_num = num + 1

                    user.save()
                    str = "注冊成功!"
                    messages.add_message(request, messages.ERROR, str)

                except:
                    print('error')
                    pass
                return redirect(to='login')

    elif request.method == 'GET':
        form = UserInfoForm
        return render(request, 'register.html', {'form': form})