Example #1
0
def user_avatar(request):
    u = request.user
    if request.method == 'GET':
        return render_to_response(
            'account/user-avatar.html',
            {'request': request,
             'title': _('avatar setting'),
             'conf': conf},
            context_instance=RequestContext(request))
    else:
        use_gravatar = request.POST.getlist('gravatar') == ['true']
        request.user.profile.use_gravatar = use_gravatar
        f = request.FILES.get('file', None)
        if f:
            extension = os.path.splitext(f.name)[-1]
            if f.size > 524288:
                return error(request, _('file too big'))
            if (extension not in ['.jpg', '.png', '.gif'] or
                    ('image' not in f.content_type)):
                return error(request, _('file type not permitted'))
            im = Image.open(f)
            im.thumbnail((120, 120))
            name = storage.get_available_name(str(u.id)) + '.png'
            url = storage.url(name)
            request.user.profile.avatar_url = url
            im.save('%s/%s' % (storage.location, name), 'PNG')
        u.profile.save()
        return HttpResponseRedirect(reverse('user_avatar'))
Example #2
0
def reg(request):
    if request.method == 'GET':
        return render_to_response('reg.html', {'conf': conf, 'title':u'注册'},
                                  context_instance=RequestContext(request))
    elif request.method == 'POST':
        username = request.POST['username']
        try:
            alphanumeric(username)
        except:
            return error(request, '用户名只允许英文字母、数字及下划线"_"(QQ登陆用户不受此限制)')
        try:
            User.objects.get(username=username)
        except:
            pass
        else:
            return error(request, '用户已存在')
        password = request.POST['password']
        email = request.POST['email']
        user = User.objects.create_user(username, email, password)
        user = authenticate(username=username, password=password)
        login(request, user)
        p = profile()
        p.user = user
        p.save()
        return HttpResponseRedirect(reverse('index'))
Example #3
0
def user_avatar(request):
    u = request.user
    if request.method == 'GET':
        return render_to_response('user-avatar.html', {
            'request': request,
            'title': u'头像设置',
            'conf': conf
        },
                                  context_instance=RequestContext(request))
    else:
        use_gravatar = request.POST.getlist('gravatar') == ['true']
        request.user.profile.use_gravatar = use_gravatar
        f = request.FILES.get('file', None)
        if f:
            extension = os.path.splitext(f.name)[-1]
            if f.size > 524288:
                return error(request, u'文件太大')
            if (extension not in ['.jpg', '.png', '.gif'
                                  ]) or ('image' not in f.content_type):
                return error(request, u'类型不允许')
            name = storage.save(
                storage.get_available_name(str(request.user.id) + extension),
                f)
            url = storage.url(name)
            request.user.profile.avatar_url = url
        request.user.profile.save()
        return HttpResponseRedirect(reverse('user_avatar'))
Example #4
0
def user_avatar(request):
    u = request.user
    if request.method == "GET":
        return render_to_response(
            "account/user-avatar.html",
            {"request": request, "title": _("avatar setting"), "conf": conf},
            context_instance=RequestContext(request),
        )
    else:
        use_gravatar = request.POST.getlist("gravatar") == ["true"]
        request.user.profile.use_gravatar = use_gravatar
        f = request.FILES.get("file", None)
        if f:
            extension = os.path.splitext(f.name)[-1]
            if f.size > 524288:
                return error(request, _("file too big"))
            if (extension not in [".jpg", ".png", ".gif"]) or ("image" not in f.content_type):
                return error(request, _("file type not permitted"))
            im = Image.open(f)
            im.thumbnail((120, 120))
            name = storage.get_available_name(str(u.id)) + ".png"
            url = storage.url(name)
            request.user.profile.avatar_url = url
            im.save("%s/%s" % (storage.location, name), "PNG")
        u.profile.save()
        return HttpResponseRedirect(reverse("user_avatar"))
Example #5
0
def user_avatar(request):
    u = request.user
    if request.method == 'GET':
        return render_to_response('account/user-avatar.html', {
            'request': request,
            'title': "头像设置",
            'conf': conf
        },
                                  context_instance=RequestContext(request))
    else:
        use_gravatar = request.POST.getlist('gravatar') == ['true']
        request.user.profile.use_gravatar = use_gravatar
        f = request.FILES.get('file', None)
        if f:
            extension = os.path.splitext(f.name)[-1]
            if f.size > 524288:
                return error(request, "文件过大")
            if (extension not in ['.jpg', '.png', '.gif'
                                  ]) or ('image' not in f.content_type):
                return error(request, "文件类型不合法")
            im = Image.open(f)
            im.thumbnail((120, 120))
            name = storage.get_available_name(str(u.id)) + '.png'
            url = storage.url(name)
            request.user.profile.avatar_url = url
            im.save('%s/%s' % (storage.location, name), 'PNG')
        u.profile.save()
        return HttpResponseRedirect(reverse('user_avatar'))
Example #6
0
def change_password(request):
    u = request.user
    if request.method == 'GET':
        return render_to_response('change-password.html',{'request': request, 'title': u'修改密码',
                                                          'conf': conf},
                                    context_instance=RequestContext(request))
    elif request.method == 'POST':
        old = request.POST['old-password']
        new = request.POST['password']
        if authenticate(username=u.username, password=old):
            u.set_password(new)
            u.save()
            return error(request, u'密码修改成功', reverse('index'))
        else:
            return error(request, u'填写错误,可能是原始密码错误或', reverse('change_password'))
Example #7
0
def user_info(request, user_id):
    try:
        u = User.objects.get(id=user_id)

        return render_to_response('user-info.html', {'request': request, 'title': u'用户信息',
                                                     'user': u, 'conf': conf,
                                                     'topics': u.profile.latest_activity()['topic'],
                                                     'post_list_title': u'用户%s的最新主题' % (u.profile.username())})
    except:
        return error(request, '用户没有填写详细信息')
Example #8
0
def user_avatar(request):
    u = request.user
    if request.method == 'GET':
        return render_to_response('user-avatar.html', {'request': request, 'title': u'头像设置',
                                                       'conf': conf},
                                  context_instance=RequestContext(request))
    else:
        use_gravatar = request.POST.getlist('gravatar') == ['true']
        request.user.profile.use_gravatar = use_gravatar
        f = request.FILES.get('file', None)
        if f:
            extension = os.path.splitext(f.name)[-1]
            if f.size > 524288:
                return error(request, u'文件太大')
            if (extension not in ['.jpg', '.png', '.gif']) or ('image' not in f.content_type):
                return error(request, u'类型不允许')
            name = storage.save(storage.get_available_name(str(request.user.id) + extension), f)
            url = storage.url(name)
            request.user.profile.avatar_url = url
        request.user.profile.save()
        return HttpResponseRedirect(reverse('user_avatar'))
Example #9
0
def user_login(request):
    if request.method == 'GET':
        return render_to_response('login.html', {'conf': conf, 'title':u'登陆'},
                                  context_instance=RequestContext(request))
    elif request.method == 'POST':
        username = request.POST['username']
        password = request.POST['password']
        user = authenticate(username=username, password=password)
        if user is None:
            return error(request, '登陆失败,请检查用户名密码是否错误')
        login(request, user)
        return HttpResponseRedirect(reverse('index'))
Example #10
0
 def process_request(self, request):
     if (site_off and
         request.path != reverse('signin') and
         not request.user.is_superuser):
         return error(request, 'down for maintenace')
Example #11
0
def qq_oauth(request):
    if request.method == 'GET':
        if (not request.GET['code']) or (request.GET['state'] != 'fairybbs'):
            return error(request, '请求错误')
        code = request.GET['code']
        url = 'https://graph.qq.com/oauth2.0/token'
        data = {
            'grant_type': 'authorization_code',
            'client_id': '',
            'client_secret': '',
            'code': code,
            'redirect_uri': ''
        }
        req = urllib2.Request(url, urllib.urlencode(data))
        res = urllib2.urlopen(req)
        try:
            access_token = re.findall(r'access_token=(.*?)&', res.read())[0]
        except:
            return error(request, u'抱歉,未从腾讯获取到有效的授权信息,可能是和腾讯通信失败,请重试\n')
        url_openid = 'https://graph.qq.com/oauth2.0/me'
        data_openid = {'access_token': access_token}
        req_openid = urllib2.Request(url_openid, urllib.urlencode(data_openid))
        res_openid = urllib2.urlopen(req_openid)
        try:
            JSON_openid = json.loads(res_openid.read()[10:-3])
        except:
            return error(request, u'抱歉,未从腾讯获取到有效的授权信息,可能是和腾讯通信失败,请重试')
        openid = JSON_openid['openid']
        try:
            u = social.objects.get(openid=openid).user
        except:
            url_info = 'https://graph.qq.com/user/get_user_info'
            data_info = {
                'oauth_consumer_key': '',
                'access_token': access_token,
                'openid': openid
            }
            req_info = urllib2.Request(url_info, urllib.urlencode(data_info))
            res_info = urllib2.urlopen(req_info)
            JSON_info = json.loads(res_info.read())
            username = JSON_info['nickname']
            nickname = username
            if JSON_info['figureurl_qq_2']:
                avatar = JSON_info['figureurl_qq_2']
            else:
                avatar = JSON_info['figureurl_2']
            password = User.objects.make_random_password()
            try:
                u = User.objects.get(username=username)
            except:
                pass
            else:
                username = GenerateUsername(nickname)
            u = User(username=username)
            u.set_password(password)
            u.save()
            p = profile(
                user=u,
                #avatar=avatar,
                nickname=nickname,
                avatar_url=avatar,
                use_gravatar=False)
            p.save()
            s = social(
                user=u,
                access_token=access_token,
                openid=openid,
                avatar=avatar,
            )
            s.save()
            user = auth.authenticate(username=username, password=password)
            if user is not None and user.is_active:
                auth.login(request, user)
                return HttpResponseRedirect(reverse('index'))  #login succeed
        else:
            u.social.access_token = access_token
            u.social.save()
            u.backend = 'django.contrib.auth.backends.ModelBackend'
            if u is not None and u.is_active:
                auth.login(request, u)
                return HttpResponseRedirect(reverse('index'))  #login succeed
            else:
                return error(request, u'授权失败,请重试')
Example #12
0
def qq_oauth(request):
    if request.method == "GET":
        if (not request.GET["code"]) or (request.GET["state"] != "fairybbs"):
            return error(request, "请求错误")
        code = request.GET["code"]
        url = "https://graph.qq.com/oauth2.0/token"
        data = {
            "grant_type": "authorization_code",
            "client_id": "",
            "client_secret": "",
            "code": code,
            "redirect_uri": "",
        }
        req = urllib2.Request(url, urllib.urlencode(data))
        res = urllib2.urlopen(req)
        try:
            access_token = re.findall(r"access_token=(.*?)&", res.read())[0]
        except:
            return error(request, u"抱歉,未从腾讯获取到有效的授权信息,可能是和腾讯通信失败,请重试\n")
        url_openid = "https://graph.qq.com/oauth2.0/me"
        data_openid = {"access_token": access_token}
        req_openid = urllib2.Request(url_openid, urllib.urlencode(data_openid))
        res_openid = urllib2.urlopen(req_openid)
        try:
            JSON_openid = json.loads(res_openid.read()[10:-3])
        except:
            return error(request, u"抱歉,未从腾讯获取到有效的授权信息,可能是和腾讯通信失败,请重试")
        openid = JSON_openid["openid"]
        try:
            u = social.objects.get(openid=openid).user
        except:
            url_info = "https://graph.qq.com/user/get_user_info"
            data_info = {"oauth_consumer_key": "", "access_token": access_token, "openid": openid}
            req_info = urllib2.Request(url_info, urllib.urlencode(data_info))
            res_info = urllib2.urlopen(req_info)
            JSON_info = json.loads(res_info.read())
            username = JSON_info["nickname"]
            nickname = username
            if JSON_info["figureurl_qq_2"]:
                avatar = JSON_info["figureurl_qq_2"]
            else:
                avatar = JSON_info["figureurl_2"]
            password = User.objects.make_random_password()
            try:
                u = User.objects.get(username=username)
            except:
                pass
            else:
                username = GenerateUsername(nickname)
            u = User(username=username)
            u.set_password(password)
            u.save()
            p = profile(
                user=u,
                # avatar=avatar,
                nickname=nickname,
                avatar_url=avatar,
                use_gravatar=False,
            )
            p.save()
            s = social(user=u, access_token=access_token, openid=openid, avatar=avatar)
            s.save()
            user = auth.authenticate(username=username, password=password)
            if user is not None and user.is_active:
                auth.login(request, user)
                return HttpResponseRedirect(reverse("index"))  # login succeed
        else:
            u.social.access_token = access_token
            u.social.save()
            u.backend = "django.contrib.auth.backends.ModelBackend"
            if u is not None and u.is_active:
                auth.login(request, u)
                return HttpResponseRedirect(reverse("index"))  # login succeed
            else:
                return error(request, u"授权失败,请重试")
Example #13
0
def qq_oauth(request):
    if request.method == 'GET':
        if (not request.GET['code']) or (request.GET['state'] != 'fairybbs'):
            return error(request, '请求错误')
        code = request.GET['code']
        url = 'https://graph.qq.com/oauth2.0/token'
        data = {'grant_type': 'authorization_code',
                'client_id': '',
                'client_secret': '',
                'code': code,
                'redirect_uri': ''}
        req = urllib2.Request(url, urllib.urlencode(data))
        res = urllib2.urlopen(req)
        try:
            access_token = re.findall(r'access_token=(.*?)&', res.read())[0]
        except:
            return error(request, u'抱歉,未从腾讯获取到有效的授权信息,可能是和腾讯通信失败,请重试\n')
        url_openid = 'https://graph.qq.com/oauth2.0/me'
        data_openid = {'access_token': access_token}
        req_openid = urllib2.Request(url_openid, urllib.urlencode(data_openid))
        res_openid = urllib2.urlopen(req_openid)
        try:
            JSON_openid = json.loads(res_openid.read()[10:-3])
        except:
            return error(request, u'抱歉,未从腾讯获取到有效的授权信息,可能是和腾讯通信失败,请重试')
        openid = JSON_openid['openid']
        try:
            u = social.objects.get(openid=openid).user
        except:
            url_info = 'https://graph.qq.com/user/get_user_info'
            data_info = {'oauth_consumer_key': '',
                         'access_token': access_token,
                         'openid': openid}
            req_info = urllib2.Request(url_info, urllib.urlencode(data_info))
            res_info = urllib2.urlopen(req_info)
            JSON_info = json.loads(res_info.read())
            username = JSON_info['nickname']
            nickname = username
            if JSON_info['figureurl_qq_2']:
                avatar = JSON_info['figureurl_qq_2']
            else:
                avatar = JSON_info['figureurl_2']
            password = User.objects.make_random_password()
            try:
                u = User.objects.get(username=username)
            except:
                pass
            else:
                username = GenerateUsername(nickname)
            u = User(username=username)
            u.set_password(password)
            u.save()
            p = profile(user=u,
                        #avatar=avatar,
                        nickname=nickname,
                        avatar_url=avatar,
                        use_gravatar=False)
            p.save()
            s = social(user=u,
                       access_token=access_token,
                       openid=openid,
                       avatar=avatar, )
            s.save()
            user = auth.authenticate(username=username, password=password)
            if user is not None and user.is_active:
                auth.login(request, user)
                return HttpResponseRedirect(reverse('index')) #login succeed
        else:
            u.social.access_token = access_token
            u.social.save()
            u.backend = 'django.contrib.auth.backends.ModelBackend'
            if u is not None and u.is_active:
                auth.login(request, u)
                return HttpResponseRedirect(reverse('index')) #login succeed
            else:
                return error(request, u'授权失败,请重试')
Example #14
0
 def process_request(self, request):
     if (site_off) and (request.path != reverse('signin')) and (not request.user.is_superuser):
         return error(request, 'down for maintenace')