Exemplo n.º 1
0
def create_user_from_weibo(request,
                           template_name='register/create_user_from_weibo.html'
                           ):

    oauth_access_token = request.session.get('oauth_access_token', None)

    if request.user.is_authenticated() or oauth_access_token is None:
        return HttpResponseRedirect(reverse('home.views.index'))

    client = APIClient(app_key=APP_KEY,
                       app_secret=APP_SECRET,
                       redirect_uri=_get_weibo_callback_url(request))
    client.set_access_token(oauth_access_token['access_token'],
                            oauth_access_token['expires_in'])

    weibo_user = client.get.users__show(uid=oauth_access_token['uid'])
    weibo_username = weibo_user.screen_name

    template_var = {}
    form = RegistrationForm(initial={'username': weibo_username})
    if request.method == 'POST':
        form = RegistrationForm(request.POST.copy())
        if request.method == 'POST':
            if form.is_valid():
                username = form.cleaned_data['username']
                email = form.cleaned_data['email']
                password = form.cleaned_data['password']
                user = User.objects.create_user(username, email, password)
                user.is_active = True
                user.save()

                profile = UserProfile()
                profile.user = user
                profile.song_ord_filed = 'post_datetime'
                profile.save()

                #weibo信息记录
                w_user = WeiboUser()
                w_user.user = user

                w_user.weibo_user_id = oauth_access_token['uid']
                w_user.weibo_username = weibo_username
                w_user.oauth_access_token = oauth_access_token['access_token']
                w_user.save()

                #发微博提示
                if request.POST.get('update_msg'):
                    msg = request.POST.get('bind_msg')[0:140]
                    client.post.statuses__update(status=msg)

                user = authenticate(username=username, password=password)
                auth_login(request, user)

                return HttpResponseRedirect(reverse('songs.views.my_home'))

    template_var['form'] = form
    template_var['weibo_username'] = weibo_username
    return render_to_response(template_name,
                              template_var,
                              context_instance=RequestContext(request))
Exemplo n.º 2
0
    def save(self, request):
        if self.cleaned_data['city'] or self.cleaned_data[
                'website'] or self.cleaned_data['intro'] or self.cleaned_data[
                    'qq'] or self.cleaned_data['msn'] or self.cleaned_data[
                        'avatar']:

            current_user = request.user

            try:
                profile = current_user.get_profile()
            except UserProfile.DoesNotExist:
                profile = None

            if not profile:
                profile = UserProfile()
                profile.user = current_user
                profile.song_ord_filed = 'post_datetime'

            profile.city = self.cleaned_data['city']
            profile.website = self.cleaned_data['website']
            profile.intro = self.cleaned_data['intro']
            profile.qq = self.cleaned_data['qq']
            profile.msn = self.cleaned_data['msn']
            if 'avatar' in request.FILES:

                #删除掉原头像
                import os
                if profile and profile.avatar and os.path.exists(
                        profile.avatar.path):
                    os.remove(profile.avatar.path)

                profile.avatar = self.cleaned_data[
                    'avatar']  #request.FILES["avatar"]
            profile.save()

            if self.cleaned_data['username'] != current_user.username:
                current_user.username = self.cleaned_data['username']
                current_user.save()