Exemplo n.º 1
0
def qqlogin(request):
    # 构造url,直接打开

    socialsites = SocialSites(SOCIALOAUTH_SITES)

    def _link(site_class):
        _s = socialsites.get_site_object_by_class(site_class)
        return _s.authorize_url

    link = map(_link, socialsites.list_sites_class())  # 获取到了url
    # 获取返回的code
    code = request.GET.get("code")
    if not code:
        return render_to_response("index.html", {"link": link[0]}, RequestContext(request))
    s = socialsites.get_site_object_by_name("qq")
    s.get_access_token(code)
    # s.site_name s.uid s.name s.avatar
    sitename = s.site_name
    uid = s.uid
    name = s.name
    avatar = s.avatar
    # 根据昵称 ID  图标来添加新用户
    # 判断获取的s的数据是否正确,如果正确,则添加新用户
    try:
        user = User.objects.get(username=s.uid)  # 只需要uid,便可以登录
    #        user = User.objects.create_user(username=s.uid)  #只需要uid,便可以登录
    except:
        user = User.objects.create_user(username=s.uid, password="******")  # 只需要uid,便可以登录
        user.save()
        person = Person(user=user, nick_name=s.name, photo=s.avatar)  # 不再本地保存头像,保存url
        person.save()
    person = authenticate(username=s.uid, password="******")
    # login(user.username,user.password)
    # 用code换取token ,重定向到主页
    if person is not None:
        login(request, person)
    #    return HttpResponseRedirect('/')
    return blogall(request, loginflag=True)
Exemplo n.º 2
0
def register(request):
    if request.user.is_authenticated():
        return HttpResponseRedirect("/")
    if request.method == "POST":
        form = PersonForm(request.POST, request.FILES)
        if form.is_valid():
            user = User.objects.create_user(
                username=form.cleaned_data["username"],
                email=form.cleaned_data["email"],
                password=form.cleaned_data["password1"],
            )
            user.save()
            nickname = form.cleaned_data["nickname"]
            photo = request.FILES.get("photo", "")
            person = Person(user=user, nick_name=nickname, photo=photo)
            person.save()
            # use the register login
            person = authenticate(username=form.cleaned_data["username"], password=form.cleaned_data["password1"])
            login(request, person)
            return HttpResponseRedirect("/")
    else:
        form = PersonForm()
    return render_to_response("register.html", {"form": form})