예제 #1
0
 def set_node(id, name, imgurl, about):
     if id:
         n_obj_str = kv.get('n-' + id)
         ndict = decode_dict(n_obj_str)
         
         ndict['id'] = id
         ndict['name'] = name
         ndict['imgurl'] = imgurl
         ndict['about'] = about
             
         if kv.set('n-' + id, encode_dict(ndict)):
             clear_cache_multi(['newest_add_node'])
             return id #True
         else:
             return False
     else:
         ndict = NODE_DICT.copy()
         #get node id
         cur_num = kv.get('node_auto_increment')
         if cur_num:
             id = str(cur_num)
         else:
             id = '1'
         ndict['id'] = id
         ndict['name'] = name
         ndict['imgurl'] = imgurl
         ndict['about'] = about
         
         if kv.set('n-' + id, encode_dict(ndict)):
             if kv.set('node_auto_increment', int(id)+1):
                 clear_cache_multi(['newest_add_node'])
                 return id #True
         return False
예제 #2
0
파일: view.py 프로젝트: QiaoHaiZhong/rebang
 def get(self): 
     username = self.get_cookie('username','')
     if username:
         clear_cache_multi(['cur_user:'******'/')        
예제 #3
0
파일: model.py 프로젝트: zyfforlinux/rebang
    def set_node(id, name, imgurl, about):
        if id:
            n_obj_str = kv.get('n-' + id)
            ndict = decode_dict(n_obj_str)

            ndict['id'] = id
            ndict['name'] = name
            ndict['imgurl'] = imgurl
            ndict['about'] = about

            if kv.set('n-' + id, encode_dict(ndict)):
                clear_cache_multi(['newest_add_node'])
                return id  #True
            else:
                return False
        else:
            ndict = NODE_DICT.copy()
            #get node id
            cur_num = kv.get('node_auto_increment')
            if cur_num:
                id = str(cur_num)
            else:
                id = '1'
            ndict['id'] = id
            ndict['name'] = name
            ndict['imgurl'] = imgurl
            ndict['about'] = about

            if kv.set('n-' + id, encode_dict(ndict)):
                if kv.set('node_auto_increment', int(id) + 1):
                    clear_cache_multi(['newest_add_node'])
                    return id  #True
            return False
예제 #4
0
    def get(self):
        username = self.get_cookie('username', '')
        if username:
            clear_cache_multi(['cur_user:'******'/')
예제 #5
0
파일: view.py 프로젝트: QiaoHaiZhong/rebang
 def post(self):
     errors = []
     username = str(self.get_cookie('username',''))
     userflag = str(self.get_cookie('userflag',''))
     if not username or not userflag:
         self.write('403')
         return
     myfile = self.request.files.get('myfile',[0])[0]
     if myfile:
         im = Image.open(StringIO(myfile['body']))
         max_w = 73
         max_h = 73
         if im.size[0] > max_w or im.size[1] > max_w:
             ss = max(float(im.size[0])/max_w, float(im.size[1])/max_h)
             im = im.resize((int(im.size[0]/ss), int(im.size[1]/ss)), Image.ANTIALIAS)
         
         output = StringIO()
         im.convert('RGB').save(output, 'JPEG', quality = 95)#95
         img_data = output.getvalue()
         output.close()
         
         #self.set_header('Content-Type','image/jpeg')
         #self.write(img_data)
         #return
         
         if IMG_STORAGE=='sae':
             file_path_name = '%s.jpg'% username
             avatar = put_obj2storage(file_name = file_path_name, data = img_data, domain_name = DOMAIN_NAME_AVATAR)
         elif IMG_STORAGE=='upyun':
             u = UpYun(DOMAIN_NAME_AVATAR, UPYUN_USER, UPYUN_PW)
             file_path_name = '/avatar/%s.jpg'% username
             avatar = u.writeFile( file_path_name, img_data, True)
         
         if avatar:
             if username == 'admin':
                 Member.set_flag(username, '99')
             if userflag == '1':
                 if Member.set_flag(username, '2'):
                     self.set_cookie('userflag', '2', path="/", expires_days = 365 )
                     clear_cache_multi(['cur_user:'******'/setavatar')
                     return
                 else:
                     errors.append('服务器暂时出错,请稍后再试')
             else:
                 self.redirect('/setavatar')
                 return
         else:
             errors.append('保存图片出错,请稍后再试')
     else:
         errors.append('图片没有正确上传')
         
     self.echo('setavatar.html', {
         'title': "设置头像",
         'errors':errors,
         'wb_avatar_large': '',
     }, layout='_layout.html')
예제 #6
0
파일: view.py 프로젝트: QiaoHaiZhong/rebang
 def get(self, nodekey, topicnum):
     nodekey = str(nodekey)
     topicnum = str(topicnum)
     h_obj = Commomkvdb.get_by_key('nodekey-topicnum')
     if h_obj:
         h_obj[nodekey] = topicnum
         Commomkvdb.save('nodekey-topicnum', h_obj)
         clear_cache_multi(['get_hot_node'])
     else:
         Commomkvdb.save('nodekey-topicnum', {nodekey:topicnum})
예제 #7
0
 def get(self, nodekey, topicnum):
     nodekey = str(nodekey)
     topicnum = str(topicnum)
     h_obj = Commomkvdb.get_by_key('nodekey-topicnum')
     if h_obj:
         h_obj[nodekey] = topicnum
         Commomkvdb.save('nodekey-topicnum', h_obj)
         clear_cache_multi(['get_hot_node'])
     else:
         Commomkvdb.save('nodekey-topicnum', {nodekey: topicnum})
예제 #8
0
파일: view.py 프로젝트: QiaoHaiZhong/rebang
 def get(self, t_key):
     t_key = str(t_key)
     m_obj = self.cur_user()
     if m_obj.get('notic', ''):
         klist = m_obj['notic'].split(',')
         if t_key in klist:
             klist.remove(t_key)
             m_obj['notic'] = ','.join(klist)
             if Commomkvdb.save('m-'+str(m_obj['name']), m_obj):
                 clear_cache_multi(['cur_user:'******'name'])])
     self.redirect('/'+t_key)
예제 #9
0
 def add_node_key(node_key):
     keys = kv.get('recent_view_node')
     if keys:
         keys_list = keys.split(',')
         if node_key not in keys_list:
             keys_list.insert(0, node_key)
             kv.set('recent_view_node', ','.join(keys_list[:10]))
             clear_cache_multi(['recent_view_node'])
     else:
         kv.set('recent_view_node', node_key)
         clear_cache_multi(['recent_view_node'])
예제 #10
0
파일: model.py 프로젝트: zyfforlinux/rebang
 def add_node_key(node_key):
     keys = kv.get('recent_view_node')
     if keys:
         keys_list = keys.split(',')
         if node_key not in keys_list:
             keys_list.insert(0, node_key)
             kv.set('recent_view_node', ','.join(keys_list[:10]))
             clear_cache_multi(['recent_view_node'])
     else:
         kv.set('recent_view_node', node_key)
         clear_cache_multi(['recent_view_node'])
예제 #11
0
파일: view.py 프로젝트: QiaoHaiZhong/rebang
 def post(self):
     m_name = str(self.get_argument('name',''))
     flag = self.get_argument('flag','')
     
     if m_name and flag:
         if Member.set_flag(m_name, flag):
             clear_cache_multi(['cur_user:'******'/set-user-flag?name=%s'%m_name)
             return
     
     self.redirect('/set-user-flag?name=%s'%m_name)
예제 #12
0
    def post(self):
        m_name = str(self.get_argument('name', ''))
        flag = self.get_argument('flag', '')

        if m_name and flag:
            if Member.set_flag(m_name, flag):
                clear_cache_multi(['cur_user:'******'/set-user-flag?name=%s' % m_name)
                return

        self.redirect('/set-user-flag?name=%s' % m_name)
예제 #13
0
 def get(self, t_key):
     t_key = str(t_key)
     m_obj = self.cur_user()
     if m_obj.get('notic', ''):
         klist = m_obj['notic'].split(',')
         if t_key in klist:
             klist.remove(t_key)
             m_obj['notic'] = ','.join(klist)
             if Commomkvdb.save('m-' + str(m_obj['name']), m_obj):
                 clear_cache_multi(['cur_user:'******'name'])])
     self.redirect('/' + t_key)
예제 #14
0
 def post(self, t_key):
     t_key = str(t_key)
     member = self.get_argument('member', '')
     if member:
         clearkeys = []
         for m in member.split(','):
             logging.error(m)
             m_obj = Member.get_by_name(str(m))
             if m_obj:
                 if m_obj.get('notic', ''):
                     klist = m_obj['notic'].split(',')
                 else:
                     klist = []
                 if t_key not in klist:
                     klist.insert(0, t_key)
                     m_obj['notic'] = ','.join(klist[:NOTIFY_NUM])
                     try:
                         if Commomkvdb.save('m-' + str(m), m_obj):
                             clearkeys.append('cur_user:'******'test task')
예제 #15
0
파일: view.py 프로젝트: QiaoHaiZhong/rebang
 def post(self, t_key):
     t_key = str(t_key)
     member = self.get_argument('member','')
     if member:
         clearkeys = []
         for m in member.split(','):
             logging.error(m)
             m_obj = Member.get_by_name(str(m))
             if m_obj:
                 if m_obj.get('notic', ''):
                     klist = m_obj['notic'].split(',')
                 else:
                     klist = []
                 if t_key not in klist:
                     klist.insert(0, t_key)
                     m_obj['notic'] = ','.join(klist[:NOTIFY_NUM])
                     try:
                         if Commomkvdb.save('m-'+str(m), m_obj):
                             clearkeys.append('cur_user:'******'test task')
예제 #16
0
    def post(self, nodeid='1'):
        n_obj = Node.get_by_key('n-' + str(nodeid))
        if not n_obj:
            self.set_status(404)
            self.write('404')
            return

        errors = []
        author = str(self.get_cookie('username', ''))
        title = self.get_argument('title', '')
        content = self.get_argument('content', '')

        t_obj = TOPIC_DICT.copy()
        if title and content:
            if len(title) <= TITLE_MAX_S and len(content) <= CONTENT_MAX_S:
                int_time = int(time())
                #check spam
                u_topic_time = kv.get('u_topic_time:' + author)
                if u_topic_time:
                    tlist = u_topic_time.split(',')
                    if len(tlist) == MEMBER_RECENT_TOPIC and (
                            int_time - int(tlist[-1])) < 3600:
                        self.write(
                            u'403:不要发帖太频繁了 <a href="/newpost/%s">请返回</a>' %
                            nodeid)
                        return

                #check repeat
                content = textilize(content)
                #content = safe_encode(content)
                con_md5 = md5(content.encode('utf-8')).hexdigest()
                if mc.get('c_' + con_md5):
                    self.write(u'403:请勿灌水 <a href="/newpost/%s">请返回</a>' %
                               nodeid)
                    return
                else:
                    mc.set('c_' + con_md5, '1', 36000)

                t_obj['title'] = title
                t_obj['nodeid'] = str(nodeid)
                t_obj['nodename'] = n_obj['name']
                t_obj['author'] = author
                t_obj['add'] = int_time
                t_obj['content'] = content

                if n_obj['count']:
                    topic_id = int(n_obj['count']) + 1
                else:
                    topic_id = 1
                if Topic.add(topic_id, t_obj):
                    topic_key = 't-%s-%s' % (str(nodeid), str(topic_id))
                    #node count +1
                    n_obj['count'] = str(topic_id)
                    Commomkvdb.save('n-' + str(nodeid), n_obj)

                    #member recent +key
                    #Member.add_key_rencent_topic(author, topic_key)
                    rt_obj = kv.get('topic-' + author)
                    if rt_obj:
                        olist = rt_obj.split(',')
                        if topic_key not in olist:
                            olist.insert(0, topic_key)
                            rt_obj = ','.join(olist[:MEMBER_RECENT_TOPIC])
                            kv.set('topic-' + author, rt_obj)
                    else:
                        rt_obj = topic_key
                        kv.set('topic-' + author, topic_key)

                    #recent in home +key
                    Commomkvdb.add_key_rencent_topic('recent-topic-home',
                                                     topic_key)
                    #all topic counter +1
                    Count.key_incr('all-topic-num')
                    #hot node
                    tqueue = TaskQueue('default')
                    tqueue.add(
                        Task('/task/hotnode/%s/%s' %
                             ('n-' + str(nodeid), str(topic_id)),
                             delay=5))
                    #notifications
                    mentions = findall_mentions(t_obj['content'], author)
                    if mentions:
                        tqueue.add(
                            Task('/task/mentions/' + topic_key,
                                 'member=' + ','.join(mentions),
                                 delay=8))

                    #set for check spam
                    #u_topic_time = kv.get('u_topic_time:'+author)
                    if u_topic_time:
                        tlist = u_topic_time.split(',')
                        if str(int_time) not in tlist:
                            tlist.insert(0, str(int_time))
                            u_topic_time = ','.join(
                                tlist[:MEMBER_RECENT_TOPIC])
                            kv.set('u_topic_time:' + author, u_topic_time)
                    else:
                        u_topic_time = str(int_time)
                        kv.set('u_topic_time:' + author, u_topic_time)

                    ##set new sr_code
                    cur_user = self.cur_user()
                    code_list = [cur_user['code'], u_topic_time]
                    u_comment_time = kv.get('u_comment_time:' + author)
                    if u_comment_time:
                        code_list.append(u_comment_time)
                    self.set_cookie('usercode',
                                    md5(''.join(code_list)).hexdigest(),
                                    path="/",
                                    expires_days=365)

                    #del cache
                    clear_cache_multi([
                        'get_topic_by_keys:recent-topic-home',
                        'get_topic_by_keys:topic-' + author,
                        'get_comment_topic_by_keys:recent-topic-home',
                        'get_comment_topic_by_keys:recent-comment-topic-home',
                        'cur_user:'******'/' + topic_key)
                    return
                else:
                    errors.append("服务器出现错误,请稍后再试")
            else:
                t_obj['title'] = title
                t_obj['content'] = content
                errors.append(u"注意标题和内容的最大字数:%s %d" %
                              (len(title), len(content)))
        else:
            errors.append("标题和内容必填")
        self.echo('newpost.html', {
            'title': "发新帖子",
            'errors': errors,
            'n_obj': n_obj,
            't_obj': t_obj,
        },
                  layout='_layout.html')
예제 #17
0
    def get(self):
        if USER_MODEL == 2:
            self.redirect('/sigin')
            return

        code = self.get_argument('code', '')
        if code:
            access_token = qqclient.request_access_token(code).get(
                'access_token', '')
            if access_token:
                #获取当前成功登录的 OpenID
                open_id = qqclient.get_open_id(access_token)
                if open_id:
                    #判断是否已存在
                    k = 'qq_' + str(open_id)
                    self.set_cookie('open_id',
                                    str(open_id),
                                    path="/",
                                    expires_days=1)
                    self.set_cookie('access_token',
                                    str(access_token),
                                    path="/",
                                    expires_days=1)
                    kv_member = kv.get(k)

                    if kv_member and len(kv_member) < 20:
                        #直接登录
                        #get member info
                        name = str(kv_member)
                        m_obj = Member.get_by_name(name)
                        if m_obj:
                            if m_obj['code'] != str(access_token):
                                m_obj['code'] = str(access_token)
                                Commomkvdb.save('m-' + str(kv_member), m_obj)
                                clear_cache_multi(['cur_user:'******'code']]
                            u_topic_time = kv.get('u_topic_time:' + name)
                            if u_topic_time:
                                code_list.append(u_topic_time)
                            u_comment_time = kv.get('u_comment_time:' + name)
                            if u_comment_time:
                                code_list.append(u_comment_time)
                            #code_md5 = md5(''.join(code_list)).hexdigest()

                            #
                            self.set_cookie('username',
                                            str(kv_member),
                                            path="/",
                                            expires_days=365)
                            self.set_cookie(
                                'usercode',
                                str(md5(''.join(code_list)).hexdigest()),
                                path="/",
                                expires_days=365)
                            self.set_cookie('userflag',
                                            m_obj['flag'],
                                            path="/",
                                            expires_days=365)
                            if m_obj['flag'] == '1':
                                self.redirect('/setavatar')
                            else:
                                self.redirect('/')
                            return

                        else:
                            #设置用户名
                            kv.set('qq_' + str(open_id), str(access_token))
                            self.redirect('/setname')
                            return

                    else:
                        #注册新用户
                        kv.set('qq_' + str(open_id), str(access_token))
                        self.redirect('/setname')
                        return
                else:
                    self.write('获取open_id 失败,请返回再试')
            else:
                self.write('获取access_token 失败,请返回再试')
        else:
            self.write('获取code 失败,请返回再试')
예제 #18
0
파일: view.py 프로젝트: QiaoHaiZhong/rebang
 def get(self):
     #尝试读取QQ空间头像
     wb_avatar_large = ''
     qq = self.get_argument('qq', '')
     if qq=='1':
         username = str(self.get_cookie('username',''))
         open_id = str(self.get_cookie('open_id',''))
         access_token = str(self.get_cookie('access_token',''))
         userflag = str(self.get_cookie('userflag',''))
         if username and open_id and access_token and userflag:
             qq_user = qqclient.get_user_info(access_token, open_id)
             #qq_user { "ret":0, "msg":"", "nickname":"qq nicke name", "figureurl_2":"http://qzapp.qlogo.cn/qzapp/100254539/5BDCA5E6321B273E44720E532B1379ED/100" }
             #保存有用的信息
             #id = str(open_id)
             #name = en_code(qq_user['nickname'])
             wb_avatar_large = str(qq_user.get('figureurl_2',''))
             
             headers = {
                 'User-Agent':'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.6) Gecko/20091201 Firefox/3.5.6',
                 'Referer':wb_avatar_large,
             }
             req = urllib2.Request(
                 url = wb_avatar_large,
                 headers = headers
             )
             data = urllib2.urlopen(req).read()
             if data:
                 im = Image.open(StringIO(data))
                 if im.size[0] > 73 or im.size[1] > 73:
                     ss = max(float(im.size[0])/73, float(im.size[1])/73)
                     box = (int(im.size[0]/ss), int(im.size[1]/ss))
                     im = im.resize(box, Image.ANTIALIAS)
                     output = StringIO()
                     im.convert('RGB').save(output, 'JPEG', quality = 95)#95
                     img_data = output.getvalue()
                     output.close()
                     
                 else:
                     img_data = data
                 
                 if IMG_STORAGE=='sae':
                     file_path_name = '%s.jpg'% username
                     avatar = put_obj2storage(file_name = file_path_name, data = img_data, domain_name = DOMAIN_NAME_AVATAR)
                 elif IMG_STORAGE=='upyun':
                     u = UpYun(DOMAIN_NAME_AVATAR, UPYUN_USER, UPYUN_PW)
                     file_path_name = '/avatar/%s.jpg'% username
                     avatar = u.writeFile( file_path_name, img_data, True)
                 
                 if avatar:
                     if userflag == '1':
                         if Member.set_flag(username, '2'):
                             self.set_cookie('userflag', '2', path="/", expires_days = 365 )
                             clear_cache_multi(['cur_user:'******'/')
                             #return
     
     self.echo('setavatar.html', {
         'title': "设置头像",
         'errors':[],
         'newest_node': Node.get_newest(),
         'wb_avatar_large': wb_avatar_large,
     }, layout='_layout.html')
예제 #19
0
    def post(self, nodeid, topicid):
        author = str(self.get_cookie('username', ''))
        content = self.get_argument('content', '')

        if author and content and len(content) <= COMMENT_MAX_S:
            int_time = int(time())
            #check spam
            u_comment_time = kv.get('u_comment_time:' + author)
            if u_comment_time:
                tlist = u_comment_time.split(',')
                if len(tlist) == MEMBER_RECENT_TOPIC and (
                        int_time - int(tlist[-1])) < 3600:
                    self.write(u'403:不要回复太频繁了 <a href="/t-%s-%s">请返回</a>' %
                               (nodeid, topicid))
                    return

            #check repeat
            content = textilize(content)
            #content = safe_encode(content)
            con_md5 = md5(content.encode('utf-8')).hexdigest()
            if mc.get('c_' + con_md5):
                self.write(u'403:请勿灌水 <a href="/t-%s-%s">请返回</a>' %
                           (nodeid, topicid))
                return
            else:
                mc.set('c_' + con_md5, '1', 36000)

            ##
            t_key = 't-%s-%s' % (str(nodeid), str(topicid))
            t_obj = Topic.get_by_key(t_key)

            if t_obj['cnum']:
                id_num = int(t_obj['cnum']) + 1
            else:
                id_num = 1

            c_key = 't-%s-%s-%d' % (str(nodeid), str(topicid), id_num)
            c_obj = COMMENT_DICT.copy()
            c_obj['author'] = author
            c_obj['add'] = int_time
            c_obj['content'] = content

            if Commomkvdb.save(c_key, c_obj):
                #topic commont count +1
                t_obj['cnum'] = id_num
                t_obj['reply'] = author
                t_obj['edit'] = int_time
                Commomkvdb.save(t_key, t_obj)

                #member recent +key
                #Member.add_key_rencent_comment_topic(author, t_key)
                rt_obj = kv.get('comment-topic-' + author)
                if rt_obj:
                    olist = rt_obj.split(',')
                    if t_key in olist:
                        olist.remove(t_key)
                    olist.insert(0, t_key)
                    kv.set('comment-topic-' + author,
                           ','.join(olist[:MEMBER_RECENT_TOPIC]))
                else:
                    kv.set('comment-topic-' + author, t_key)

                #recent comment in home +key
                Commomkvdb.add_key_rencent_topic('recent-comment-topic-home',
                                                 t_key)
                #all comment counter +1
                Count.key_incr('all-comment-num')
                #notifications
                if t_obj['author'] != author:
                    mentions = findall_mentions(
                        c_obj['content'] + ' @%s ' % t_obj['author'], author)
                else:
                    mentions = findall_mentions(c_obj['content'], author)
                if mentions:
                    tqueue = TaskQueue('default')
                    tqueue.add(
                        Task('/task/mentions/' + t_key,
                             'member=' + ','.join(mentions),
                             delay=5))

                #set for check spam
                #u_comment_time = kv.get('u_comment_time:'+author)
                if u_comment_time:
                    tlist = u_comment_time.split(',')
                    if str(int_time) not in tlist:
                        tlist.insert(0, str(int_time))
                        u_comment_time = ','.join(tlist[:MEMBER_RECENT_TOPIC])
                        kv.set('u_comment_time:' + author, u_comment_time)
                else:
                    u_comment_time = str(int_time)
                    kv.set('u_comment_time:' + author, u_comment_time)

                ##set new sr_code
                cur_user = self.cur_user()
                code_list = [cur_user['code']]
                u_topic_time = kv.get('u_topic_time:' + author)
                if u_topic_time:
                    code_list.append(u_topic_time)
                if u_comment_time:
                    code_list.append(u_comment_time)
                self.set_cookie('usercode',
                                md5(''.join(code_list)).hexdigest(),
                                path="/",
                                expires_days=365)

                #del cache
                cachekeys = [
                    'get_topic_by_keys:recent-comment-topic-home',
                    'get_topic_by_keys:comment-topic-' + author,
                    'get_comment_topic_by_keys:recent-topic-home',
                    'get_comment_topic_by_keys:recent-comment-topic-home',
                    'cur_user:'******'recent-topic-home')
                if tks and t_key in tks.split(','):
                    cachekeys.append('get_topic_by_keys:recent-topic-home')
                if id_num < EACH_PAGE_COMMENT_NUM:
                    cachekeys.append('get_comments:%s:1' % t_key)
                else:
                    cachekeys.append('get_comments:%s:%d' % (t_key, [
                        i for i in range(1, id_num, EACH_PAGE_COMMENT_NUM)
                    ][-1]))
                clear_cache_multi(cachekeys)

                self.redirect('/' + t_key)
                return
        else:
            self.set_status(403)
            self.write('错误: 403 (请返回填写内容 或 内容太长了)')
예제 #20
0
    def post(self):
        errors = []
        username = str(self.get_cookie('username', ''))
        userflag = str(self.get_cookie('userflag', ''))
        if not username or not userflag:
            self.write('403')
            return
        myfile = self.request.files.get('myfile', [0])[0]
        if myfile:
            im = Image.open(StringIO(myfile['body']))
            max_w = 73
            max_h = 73
            if im.size[0] > max_w or im.size[1] > max_w:
                ss = max(float(im.size[0]) / max_w, float(im.size[1]) / max_h)
                im = im.resize((int(im.size[0] / ss), int(im.size[1] / ss)),
                               Image.ANTIALIAS)

            output = StringIO()
            im.convert('RGB').save(output, 'JPEG', quality=95)  #95
            img_data = output.getvalue()
            output.close()

            #self.set_header('Content-Type','image/jpeg')
            #self.write(img_data)
            #return

            if IMG_STORAGE == 'sae':
                file_path_name = '%s.jpg' % username
                avatar = put_obj2storage(file_name=file_path_name,
                                         data=img_data,
                                         domain_name=DOMAIN_NAME_AVATAR)
            elif IMG_STORAGE == 'upyun':
                u = UpYun(DOMAIN_NAME_AVATAR, UPYUN_USER, UPYUN_PW)
                file_path_name = '/avatar/%s.jpg' % username
                avatar = u.writeFile(file_path_name, img_data, True)

            if avatar:
                if username == 'admin':
                    Member.set_flag(username, '99')
                if userflag == '1':
                    if Member.set_flag(username, '2'):
                        self.set_cookie('userflag',
                                        '2',
                                        path="/",
                                        expires_days=365)
                        clear_cache_multi(['cur_user:'******'/setavatar')
                        return
                    else:
                        errors.append('服务器暂时出错,请稍后再试')
                else:
                    self.redirect('/setavatar')
                    return
            else:
                errors.append('保存图片出错,请稍后再试')
        else:
            errors.append('图片没有正确上传')

        self.echo('setavatar.html', {
            'title': "设置头像",
            'errors': errors,
            'wb_avatar_large': '',
        },
                  layout='_layout.html')
예제 #21
0
    def get(self):
        #尝试读取QQ空间头像
        wb_avatar_large = ''
        qq = self.get_argument('qq', '')
        if qq == '1':
            username = str(self.get_cookie('username', ''))
            open_id = str(self.get_cookie('open_id', ''))
            access_token = str(self.get_cookie('access_token', ''))
            userflag = str(self.get_cookie('userflag', ''))
            if username and open_id and access_token and userflag:
                qq_user = qqclient.get_user_info(access_token, open_id)
                #qq_user { "ret":0, "msg":"", "nickname":"qq nicke name", "figureurl_2":"http://qzapp.qlogo.cn/qzapp/100254539/5BDCA5E6321B273E44720E532B1379ED/100" }
                #保存有用的信息
                #id = str(open_id)
                #name = en_code(qq_user['nickname'])
                wb_avatar_large = str(qq_user.get('figureurl_2', ''))

                headers = {
                    'User-Agent':
                    'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.6) Gecko/20091201 Firefox/3.5.6',
                    'Referer': wb_avatar_large,
                }
                req = urllib2.Request(url=wb_avatar_large, headers=headers)
                data = urllib2.urlopen(req).read()
                if data:
                    im = Image.open(StringIO(data))
                    if im.size[0] > 73 or im.size[1] > 73:
                        ss = max(
                            float(im.size[0]) / 73,
                            float(im.size[1]) / 73)
                        box = (int(im.size[0] / ss), int(im.size[1] / ss))
                        im = im.resize(box, Image.ANTIALIAS)
                        output = StringIO()
                        im.convert('RGB').save(output, 'JPEG', quality=95)  #95
                        img_data = output.getvalue()
                        output.close()

                    else:
                        img_data = data

                    if IMG_STORAGE == 'sae':
                        file_path_name = '%s.jpg' % username
                        avatar = put_obj2storage(
                            file_name=file_path_name,
                            data=img_data,
                            domain_name=DOMAIN_NAME_AVATAR)
                    elif IMG_STORAGE == 'upyun':
                        u = UpYun(DOMAIN_NAME_AVATAR, UPYUN_USER, UPYUN_PW)
                        file_path_name = '/avatar/%s.jpg' % username
                        avatar = u.writeFile(file_path_name, img_data, True)

                    if avatar:
                        if userflag == '1':
                            if Member.set_flag(username, '2'):
                                self.set_cookie('userflag',
                                                '2',
                                                path="/",
                                                expires_days=365)
                                clear_cache_multi(['cur_user:'******'/')
                                #return

        self.echo('setavatar.html', {
            'title': "设置头像",
            'errors': [],
            'newest_node': Node.get_newest(),
            'wb_avatar_large': wb_avatar_large,
        },
                  layout='_layout.html')
예제 #22
0
파일: view.py 프로젝트: QiaoHaiZhong/rebang
 def post(self, nodeid, topicid):
     author = str(self.get_cookie('username',''))
     content = self.get_argument('content','')
     
     if author and content and len(content)<=COMMENT_MAX_S:
         int_time = int(time())
         #check spam
         u_comment_time = kv.get('u_comment_time:'+author)
         if u_comment_time:
             tlist = u_comment_time.split(',')
             if len(tlist)== MEMBER_RECENT_TOPIC and (int_time-int(tlist[-1])) < 3600:
                 self.write(u'403:不要回复太频繁了 <a href="/t-%s-%s">请返回</a>' % (nodeid, topicid))
                 return
         
         #check repeat
         content = textilize(content)
         #content = safe_encode(content)
         con_md5 = md5(content.encode('utf-8')).hexdigest()
         if mc.get('c_'+con_md5):
             self.write(u'403:请勿灌水 <a href="/t-%s-%s">请返回</a>' % (nodeid, topicid))
             return
         else:
             mc.set('c_'+con_md5, '1', 36000)
         
         ##
         t_key = 't-%s-%s' % (str(nodeid), str(topicid))
         t_obj = Topic.get_by_key(t_key)
         
         if t_obj['cnum']:
             id_num = int(t_obj['cnum']) + 1
         else:
             id_num = 1
         
         c_key = 't-%s-%s-%d' % (str(nodeid), str(topicid), id_num)
         c_obj = COMMENT_DICT.copy()
         c_obj['author'] = author
         c_obj['add'] = int_time
         c_obj['content'] = content
         
         if Commomkvdb.save(c_key, c_obj):
             #topic commont count +1
             t_obj['cnum'] = id_num
             t_obj['reply'] = author
             t_obj['edit'] = int_time
             Commomkvdb.save(t_key, t_obj)
             
             #member recent +key
             #Member.add_key_rencent_comment_topic(author, t_key)
             rt_obj = kv.get('comment-topic-'+author)
             if rt_obj:
                 olist = rt_obj.split(',')
                 if t_key in olist:
                     olist.remove(t_key)
                 olist.insert(0, t_key)
                 kv.set('comment-topic-'+author, ','.join(olist[:MEMBER_RECENT_TOPIC]))
             else:
                 kv.set('comment-topic-'+author, t_key)
             
             #recent comment in home +key
             Commomkvdb.add_key_rencent_topic('recent-comment-topic-home', t_key)
             #all comment counter +1
             Count.key_incr('all-comment-num')
             #notifications
             if t_obj['author'] != author:
                 mentions = findall_mentions(c_obj['content']+' @%s '%t_obj['author'], author)
             else:
                 mentions = findall_mentions(c_obj['content'], author)
             if mentions:
                 tqueue = TaskQueue('default')
                 tqueue.add(Task('/task/mentions/'+t_key, 'member='+','.join(mentions), delay=5))
             
             #set for check spam
             #u_comment_time = kv.get('u_comment_time:'+author)
             if u_comment_time:
                 tlist = u_comment_time.split(',')
                 if str(int_time) not in tlist:
                     tlist.insert(0, str(int_time))
                     u_comment_time = ','.join(tlist[:MEMBER_RECENT_TOPIC])
                     kv.set('u_comment_time:'+author, u_comment_time)
             else:
                 u_comment_time = str(int_time)
                 kv.set('u_comment_time:'+author, u_comment_time)
             
             ##set new sr_code
             cur_user = self.cur_user()
             code_list = [cur_user['code']]
             u_topic_time = kv.get('u_topic_time:'+author)
             if u_topic_time:
                 code_list.append(u_topic_time)
             if u_comment_time:
                 code_list.append(u_comment_time)
             self.set_cookie('usercode', md5(''.join(code_list)).hexdigest(), path="/", expires_days = 365 )
             
                 
             #del cache
             cachekeys = ['get_topic_by_keys:recent-comment-topic-home', 'get_topic_by_keys:comment-topic-'+author, 'get_comment_topic_by_keys:recent-topic-home', 'get_comment_topic_by_keys:recent-comment-topic-home','cur_user:'******'recent-topic-home')
             if tks and t_key in tks.split(','):
                 cachekeys.append('get_topic_by_keys:recent-topic-home')
             if id_num<EACH_PAGE_COMMENT_NUM:
                 cachekeys.append('get_comments:%s:1' % t_key)
             else:
                 cachekeys.append('get_comments:%s:%d' % (t_key, [i for i in range(1,id_num,EACH_PAGE_COMMENT_NUM)][-1]))
             clear_cache_multi(cachekeys)
             
             self.redirect('/'+t_key)
             return
     else:
         self.set_status(403)
         self.write('错误: 403 (请返回填写内容 或 内容太长了)')
예제 #23
0
파일: view.py 프로젝트: QiaoHaiZhong/rebang
 def get(self):
     if USER_MODEL==2:
         self.redirect('/sigin')
         return
     
     code = self.get_argument('code', '')
     if code:
         access_token = qqclient.request_access_token(code).get('access_token','')
         if access_token:
             #获取当前成功登录的 OpenID
             open_id = qqclient.get_open_id(access_token)
             if open_id:
                 #判断是否已存在
                 k = 'qq_' +str(open_id)
                 self.set_cookie('open_id', str(open_id), path="/", expires_days = 1 )
                 self.set_cookie('access_token', str(access_token), path="/", expires_days = 1 )
                 kv_member = kv.get(k)
                 
                 if kv_member and len(kv_member)<20:
                     #直接登录
                     #get member info
                     name = str(kv_member)
                     m_obj = Member.get_by_name(name)
                     if m_obj:
                         if m_obj['code'] != str(access_token):
                             m_obj['code'] = str(access_token)
                             Commomkvdb.save('m-'+str(kv_member), m_obj)
                             clear_cache_multi(['cur_user:'******'code']]
                         u_topic_time = kv.get('u_topic_time:'+name)
                         if u_topic_time:
                             code_list.append(u_topic_time)
                         u_comment_time = kv.get('u_comment_time:'+name)
                         if u_comment_time:
                             code_list.append(u_comment_time)
                         #code_md5 = md5(''.join(code_list)).hexdigest()
                         
                         #
                         self.set_cookie('username', str(kv_member), path="/", expires_days = 365 )
                         self.set_cookie('usercode', str(md5(''.join(code_list)).hexdigest()), path="/", expires_days = 365 )
                         self.set_cookie('userflag', m_obj['flag'], path="/", expires_days = 365 )
                         if m_obj['flag'] == '1':
                             self.redirect('/setavatar')
                         else:
                             self.redirect('/')
                         return
                         
                     else:
                         #设置用户名
                         kv.set('qq_' +str(open_id), str(access_token))
                         self.redirect('/setname')
                         return
                     
                 else:
                     #注册新用户
                     kv.set('qq_' +str(open_id), str(access_token))
                     self.redirect('/setname')
                     return
             else:
                 self.write('获取open_id 失败,请返回再试')
         else:
             self.write('获取access_token 失败,请返回再试')
     else:
         self.write('获取code 失败,请返回再试')
예제 #24
0
파일: view.py 프로젝트: QiaoHaiZhong/rebang
 def post(self, nodeid='1'):
     n_obj = Node.get_by_key('n-'+str(nodeid))
     if not n_obj:
         self.set_status(404)
         self.write('404')
         return
     
     errors = []
     author = str(self.get_cookie('username',''))
     title = self.get_argument('title','')
     content = self.get_argument('content','')
     
     t_obj = TOPIC_DICT.copy()
     if title and content:
         if len(title)<=TITLE_MAX_S and len(content)<=CONTENT_MAX_S:
             int_time = int(time())
             #check spam
             u_topic_time = kv.get('u_topic_time:'+author)
             if u_topic_time:
                 tlist = u_topic_time.split(',')
                 if len(tlist)== MEMBER_RECENT_TOPIC and (int_time-int(tlist[-1])) < 3600:
                     self.write(u'403:不要发帖太频繁了 <a href="/newpost/%s">请返回</a>' % nodeid)
                     return
             
             #check repeat
             content = textilize(content)
             #content = safe_encode(content)
             con_md5 = md5(content.encode('utf-8')).hexdigest()
             if mc.get('c_'+con_md5):
                 self.write(u'403:请勿灌水 <a href="/newpost/%s">请返回</a>' % nodeid)
                 return
             else:
                 mc.set('c_'+con_md5, '1', 36000)
             
             t_obj['title'] = title
             t_obj['nodeid'] = str(nodeid)
             t_obj['nodename'] = n_obj['name']
             t_obj['author'] = author
             t_obj['add'] = int_time
             t_obj['content'] = content
             
             if n_obj['count']:
                 topic_id = int(n_obj['count']) + 1
             else:
                 topic_id = 1
             if Topic.add(topic_id, t_obj):
                 topic_key = 't-%s-%s' % (str(nodeid), str(topic_id))
                 #node count +1
                 n_obj['count'] = str(topic_id)
                 Commomkvdb.save('n-'+str(nodeid), n_obj)
                 
                 #member recent +key
                 #Member.add_key_rencent_topic(author, topic_key)
                 rt_obj = kv.get('topic-'+author)
                 if rt_obj:
                     olist = rt_obj.split(',')
                     if topic_key not in olist:
                         olist.insert(0, topic_key)
                         rt_obj = ','.join(olist[:MEMBER_RECENT_TOPIC])
                         kv.set('topic-'+author, rt_obj)
                 else:
                     rt_obj = topic_key
                     kv.set('topic-'+author, topic_key)
                 
                 #recent in home +key
                 Commomkvdb.add_key_rencent_topic('recent-topic-home', topic_key)
                 #all topic counter +1
                 Count.key_incr('all-topic-num')
                 #hot node
                 tqueue = TaskQueue('default')
                 tqueue.add(Task('/task/hotnode/%s/%s' % ('n-'+str(nodeid), str(topic_id)), delay=5))
                 #notifications
                 mentions = findall_mentions(t_obj['content'], author)
                 if mentions:
                     tqueue.add(Task('/task/mentions/'+topic_key, 'member='+','.join(mentions), delay=8))
                 
                 #set for check spam
                 #u_topic_time = kv.get('u_topic_time:'+author)
                 if u_topic_time:
                     tlist = u_topic_time.split(',')
                     if str(int_time) not in tlist:
                         tlist.insert(0, str(int_time))
                         u_topic_time = ','.join(tlist[:MEMBER_RECENT_TOPIC])
                         kv.set('u_topic_time:'+author, u_topic_time)
                 else:
                     u_topic_time = str(int_time)
                     kv.set('u_topic_time:'+author, u_topic_time)
                 
                 
                 ##set new sr_code
                 cur_user = self.cur_user()
                 code_list = [cur_user['code'],u_topic_time]
                 u_comment_time = kv.get('u_comment_time:'+author)
                 if u_comment_time:
                     code_list.append(u_comment_time)
                 self.set_cookie('usercode', md5(''.join(code_list)).hexdigest(), path="/", expires_days = 365 )
                 
                 
                 #del cache
                 clear_cache_multi(['get_topic_by_keys:recent-topic-home','get_topic_by_keys:topic-' + author, 'get_comment_topic_by_keys:recent-topic-home', 'get_comment_topic_by_keys:recent-comment-topic-home','cur_user:'******'/'+topic_key)
                 return
             else:
                 errors.append("服务器出现错误,请稍后再试")
         else:
             t_obj['title'] = title
             t_obj['content'] = content
             errors.append(u"注意标题和内容的最大字数:%s %d" % (len(title), len(content)))
     else:
         errors.append("标题和内容必填")
     self.echo('newpost.html', {
         'title': "发新帖子",
         'errors':errors,
         'n_obj': n_obj,
         't_obj': t_obj,
     }, layout='_layout.html')