Ejemplo n.º 1
0
    def post(self):
        user = self.current_user
        do = is_value(self.get_argument("do", "add"))
        tag = is_value(self.get_argument("tag", None))
        tag_page = is_value(self.get_argument("page", 'tag'))
        group_id = is_value(self.get_argument("group_id", '-1'))
        result = {'kind': -1, 'info': ''}
        if tag is None or len(tag) > 45:
            result['info'] = '分类字数需在15字以内!'
            self.write(json_encode(result))
            return
        if tag == "alltags" or tag == "default":
            result['info'] = '不能操作默认分类!'
            self.write(json_encode(result))
            return
        if tag_page != 'group-tag':
            target = user
        else:
            try:
                #print 'group_id', group_id
                group = BasicGroup(_id=group_id)
                if group.get_member_type(user) != 'Manager':
                    result['info'] = '您无权进行设置!'
                    self.write(json_encode(result))
                    return
            except Exception, e:
                logging.error(traceback.format_exc())
                logging.error('Url Error %s' % self.request.uri)

                result['info'] = '小组不存在!'
                self.write(json_encode(result))
                return
            else:
Ejemplo n.º 2
0
def fun_get_feed_by_id(user=None, obj_id=0, page_cap=20):
    # find by the post id

    #feed_id_list = [] #BlogPool().load_all()
    AFW_Group = BasicGroup(_id=AFWConfig.afewords_group_id)
    feed_id_list = AFW_Group.recommended_list.load_all()
    feed_id_list.reverse()
    #feed_id_list = [str(kk) for kk in feed_id_list]

    #print feed_id_list
    if obj_id == '0' or obj_id == 0:
        index = -1
    else:
        index = index_at_list(feed_id_list, convert_id(obj_id))
    if index is None:
        return [1, '操作出错!']

    load_list_id = feed_id_list[index + 1:index + page_cap + 1]
    if len(load_list_id) < page_cap:
        is_all = 'yes'
    else:
        is_all = 'no'
    #print 'index', index
    #print 'load list', load_list_id
    if load_list_id == []:
        last_id = 0
    else:
        last_id = load_list_id[len(load_list_id) - 1]
    tmp_blog_con = Blog.get_instances('_id', load_list_id)

    tmp_blog_list = []
    for one_blog in tmp_blog_con:
        try:
            tmp_user = User(_id=one_blog.author_id)
        except Exception, e:
            log_error('User is not exist, User ID: ' + one_blog.author_id)
            continue
        else:
            tmp_avatar = tmp_user.avatar
            tmp_blog_list.append({
                'blog_id':
                str(one_blog._id),
                'title':
                one_blog.name,
                'author_id':
                str(tmp_user._id),
                'view_body':
                strip_tags(one_blog.view_body) +
                '<a target="_blank" class="blog_detail" href="/blog/' +
                str(one_blog._id) + '">...</a>',
                'summary':
                one_blog.abstract,
                'author_avatar':
                tmp_avatar.thumb_name,
                'author_name':
                tmp_user.name
            })
Ejemplo n.º 3
0
def fun_group_create(user, group_name='', group_detail='', group_class=''):
    if AFWConfig.afewords_group_open is not True:
        return [1, '创建小组暂时未开放!']
    if group_name == '' or group_detail == '':
        return [1, '小组信息不完整!']

    new_group = BasicGroup()
    new_group.set_propertys(**{'name': group_name, 'alias': group_name})
    new_group.about.body = group_detail
    new_group.avatar.thumb_name = '/static/img/afewords-group.jpg'
    user.follow_group(new_group)
    new_group.set_manager(user)

    return [0, str(new_group._id)]
Ejemplo n.º 4
0
def fun_update_blog(user=None,
                    blog=None,
                    title='',
                    summary='',
                    body='',
                    keys=[],
                    permission='public',
                    classes=[],
                    do='post'):
    if type(keys) != list:
        keys = keys_to_list(keys)
    AF_Object = blog
    temp_tag = AF_Object.tag
    remove_tag_list = list(set(temp_tag) - set(classes))
    add_tag_list = list(set(classes) - set(temp_tag))

    blog.set_propertys(
        **{
            'name': title,
            'abstract': summary,
            'body': body,
            "keywords": keys,
            'privilege': permission
        })

    if not AF_Object.is_posted:
        # not post
        for iii in remove_tag_list:
            if iii != 'default':
                AF_Object.remove_from_tag(iii)
        for iii in add_tag_list:
            AF_Object.add_to_tag(iii)
        if do == "post":
            user.post_blog(AF_Object)
            AFW_Group = BasicGroup(_id=AFWConfig.afewords_group_id)
            AFW_Group.recommended_list.push(AF_Object._id)
        return [0, str(AF_Object._id)]
    else:
        # have post the blog
        for iii in remove_tag_list:
            if iii != 'default':
                user.remove_from_tag(AF_Object, iii)
        for iii in add_tag_list:
            user.add_to_tag(AF_Object, iii)

        return [0, str(AF_Object._id)]
Ejemplo n.º 5
0
def fun_update_group_article(user=None,
                             group_id='-1',
                             article_id='-1',
                             article_type='blog',
                             title='',
                             summary='',
                             body='',
                             permission='public',
                             keys=[],
                             classes=[],
                             father_id='-1',
                             father_type='blog',
                             do="post"):

    try:
        group = BasicGroup(_id=group_id)
        limit = group.authority_verify(user)
        #print 'User limit ', limit
    except Exception, e:
        logging.error(traceback.format_exc())
        logging.error('Group not exist %s' % group_id)
        return [1, '小组不存在!']
Ejemplo n.º 6
0
                    father_type='blog',
                    do="post",
                    ref_comments=''):
 # update comment, all comments
 group = None
 if father_type == 'blog':
     try:
         AF_Object = Blog(_id=father_id)
     except Exception, e:
         return [1, '文章不存在!']
 else:
     # for group
     if father_type == 'group-notice':
         try:
             AF_Object = Bulletin(_id=father_id)
             group = BasicGroup(_id=AF_Object.group_id)
             if group.get_member_type(user) is None:
                 return [1, '您不是该小组成员']
         except Exception, e:
             logging.error(traceback.format_exc())
             logging.error('%s not exist, id %s' % (father_type, father_id))
             return [1, '该公告不存在!']
     elif father_type == "group-doc":
         try:
             AF_Object = Blog(_id=father_id)
             group = BasicGroup(_id=AF_Object.group_id)
             if group.get_member_type(user) is None:
                 return [1, '您不是该小组成员']
         except Exception, e:
             logging.error(traceback.format_exc())
             logging.error('%s not exist, id %s' % (father_type, father_id))
Ejemplo n.º 7
0
class UploadImageHandler(BaseHandler):
    ''' all image upload request entrance 
        picture_type = [avatar || article || ...]
        target_type = [blog || comment || avatar || group ]
        target_id = [id || null ]
        article_type == "comment":  need 2 more arguments --- father, name
        article_type == "blog": need 1 more arguments --- name
        article_type == "avatar": need 0 more arguments
        article_type == "group": need 0 more arguments 
        
        Base path:
            in article: http://picture.afewords.com/static/picture/normal/xxx.xxx
                        http://picture.afewords.com/static/picture/small/xxx.xxx
                        subdomain picture1 for test only
            avatar:     http://picture.afewords.com/static/avatar/small/xxx.xxx
                        http://picture.afewords.com/static/avatar/normal/xxx.xxx
            group logo:
                        http://picture.afewords.com/static/logo/small/xxx.xxx
                        http://picture.afewords.com/static/logo/normal/xxx.xxx
    '''
    @authfilter
    def post(self):
        user = self.current_user
        picture_base_domain = "http://" + AFWConfig.afewords_image_domain + ".afewords.com"
        image_path = {
            'avatar': {
                'normal': "/static/avatar/normal/",
                'small': "/static/avatar/small/"
            },
            'logo': {
                'normal': '/static/logo/normal/',
                'small': "/static/logo/small/"
            },
            'article': {
                'normal': "/static/picture/normal/",
                'small': "/static/picture/small/"
            }
        }

        result = {'kind': -1, 'info': ''}
        #print 'request:', self.request
        picture_type = is_value(self.get_argument("picture_type", 'article'))
        if self.request.files == {} or 'picture' not in self.request.files:
            self.write('<script>alert("请选择图片!")</script>')
            return
        # get the file and named send_file
        # send_file { 'body' , 'filename', 'content_type'}
        send_file = self.request.files['picture'][0]
        #print send_file['content_type']
        image_type_list = [
            'image/gif', 'image/jpeg', 'image/pjpeg', 'image/bmp', 'image/png',
            'image/x-png'
        ]
        if send_file['content_type'] not in image_type_list:
            self.write(
                '<script>alert("仅支持jpg,jpeg,bmp,gif,png格式的图片!")</script>')
            return
        #print len(send_file['body'])
        if len(send_file['body']) > 4 * 1024 * 1024:
            restr = '<script>parent.image_upload_handler("' + picture_type + '", 1,"请选择4M以内的图片!")</script>'
            self.write(restr)
            return
        tmp_file = tempfile.NamedTemporaryFile(delete=True)
        #print dir(tmp_file), type(tmp_file)
        tmp_file.write(send_file['body'])
        tmp_file.seek(0)
        #print tmp_file.name, tmp_file.mode, tmp_file.encoding
        image_one = ''
        try:
            image_one = Image.open(tmp_file.name)
        except IOError, error:
            logging.error(error)
            logging.error('+' * 30 + '\n')
            logging.error(self.request.headers)
            tmp_file.close()
            restr = '<script>parent.image_upload_handler("' + picture_type + '", 1,"图片不合法!")</script>'
            self.write(restr)
            return
        #print image_one.size
        #print image_one.format
        if image_one.size[0] < 150 or image_one.size[1] < 150 or \
                image_one.size[0] > 2000 or image_one.size[1] > 2000:
            tmp_file.close()
            restr = '<script>parent.image_upload_handler("' + picture_type + '", 1,"图片长宽在150px~2000px之间!")</script>'
            self.write(restr)
            return
        image_format = send_file['filename'].split('.').pop().lower()
        store_name = "afw_" + random_string(10).lower() + str(int(
            time.time())) + '.' + image_format.lower()
        #print 'format', image_format
        #print 'store name ', store_name
        if picture_type in ['article', 'avatar', 'logo']:
            full_name_normal = AFWConfig.afewords_image_path + image_path[
                picture_type]['normal'] + store_name
            full_name_small = AFWConfig.afewords_image_path + image_path[
                picture_type]['small'] + store_name
            domain_name_normal = picture_base_domain + image_path[
                picture_type]['normal'] + store_name
            domain_name_small = picture_base_domain + image_path[picture_type][
                'small'] + store_name
        else:
            self.write('<script>alert("不支持当前上传!")</script>')
            return
        #image_one.save('./1.jpg')
        #if self.request.files['picture'] and
        #if self.request.files['picture'][0]
        #print '+'*30, self.request.files

        if picture_type == "article":
            # this is for article , [blog, comment, about ]
            #print 'picture_type', picture_type
            title = is_value(self.get_argument("title", None))
            article_id = is_value(self.get_argument("article_id", None))
            article_type = is_value(self.get_argument("article_type", None))
            father_id = is_value(self.get_argument("father_id", None))
            group_id = is_value(self.get_argument("group_id", None))
            if title is None or article_id is None or article_type is None:
                result['info'] = '标题不能为空!'
                restr = '<script>parent.picture_upload_handler(1,"' + result[
                    'info'] + '",0,0,-1,0)</script>'

                self.write(restr)
                return
            image_one.thumbnail((750, 1000), resample=1)
            image_one.save(str(full_name_normal))
            image_one.thumbnail((130, 120), resample=1)
            image_one.save(str(full_name_small))
            result['kind'], result['info'] = fun_new_article_pic(
                user,
                article_id=article_id,
                group_id=group_id,
                article_type=article_type,
                title=title,
                url=domain_name_normal,
                thumb=domain_name_small,
                father_id=father_id)
            if result['kind'] == 1:
                restr = '<script>parent.picture_upload_handler(1,"' + result[
                    'info'] + '",0,0,-1,0)</script>'
                self.write(restr)
                return
            # right
            res = result['info']
            #print res['article'], type(res['article'])
            article_id = res['article']
            restr = []
            restr.append('<script>parent.picture_upload_handler(')
            restr.append(str(result['kind']) + ',"')
            restr.append(domain_name_small + '",')
            restr.append(str(res['alias']) + ',"')
            restr.append(title + '",' + str(res['isnew']) + ',"')
            restr.append(res['article'])
            restr.append('")</script>')
            '''
            restr = ('<script>parent.picture_upload_handler(' 
                        + str(result['kind']) + ',"' 
                        + domain_name_small + '",' 
                        + str(res['alias']) +',"' 
                        + title +'",' + res['isnew']+
                        + ',"' + res['article'] +'")</script>')'''
            self.write(''.join(restr))
            return

        elif picture_type == "avatar":
            # this is for user to set avatar
            image_one.thumbnail((750, 1000), resample=1)
            image_one.save(str(full_name_normal))
            result['kind'], result['info'] = fun_save_thumb(store_name)
            if result['kind'] == 1:
                restr = '<script>parent.image_upload_handler("avatar", 1,"请选择图片")</script>'
                self.write(restr)
                return
            # set to user
            #print domain_name_small
            user_avatar = user.avatar
            user_avatar.set_propertys(**{
                'file_name': domain_name_normal,
                'thumb_name': domain_name_small
            })
            restr = '<script>parent.image_upload_handler("avatar", 0,"' + domain_name_normal + '")</script>'
            self.write(restr)
            return

        elif picture_type == "logo":
            # this is for group to set logo
            group_id = is_value(self.get_argument("group_id", 1))
            try:
                tmp_group = BasicGroup(_id=group_id)
                if tmp_group.get_member_type(user) != 'Manager':
                    restr = '<script>parent.image_upload_handler("logo", 1,"您无权设置!")</script>'
                    self.write(restr)
                    return
            except Exception, e:
                logging.error(e)
                logging.error('Group not exist, ' + group_id)
                restr = '<script>parent.image_upload_handler("logo", 1,"参数出错!")</script>'
                self.write(restr)
                return

            image_one.thumbnail((750, 1000), resample=1)
            image_one.save(str(full_name_normal))
            result['kind'], result['info'] = fun_save_thumb(
                store_name,
                normal_path=AFWConfig.afewords_image_path +
                "/static/logo/normal/",
                small_path=AFWConfig.afewords_image_path +
                "/static/logo/small/")
            if result['kind'] == 1:
                restr = '<script>parent.image_upload_handler("logo", 1,"请选择Logo")</script>'
                self.write(restr)
                return

            group_avatar = tmp_group.avatar
            group_avatar.set_propertys(**{
                'file_name': domain_name_normal,
                'thumb_name': domain_name_small
            })
            restr = '<script>parent.image_upload_handler("logo", 0,"' + domain_name_normal + '", "' + group_id + '")</script>'
            self.write(restr)
            return
Ejemplo n.º 8
0
class CropImageHandler(BaseHandler):
    @authfilter
    def post(self):
        result = {'kind': 1, 'info': ''}
        user = self.current_user
        crop_type = is_value(self.get_argument("crop_type", 'avatar'))
        if crop_type not in ['avatar', 'logo', 'article']:
            result['info'] = '不支持当前操作!'
            self.write(json_encode(result))
            return
        pos_x = is_value(self.get_argument("pos-x", None))
        pos_y = is_value(self.get_argument("pos-y", None))
        pos_w = is_value(self.get_argument("pos-w", None))
        pos_time = is_value(self.get_argument("pos-time", 1))
        #print '(x, y, w, pos_time):', pos_x, pos_y, pos_w , pos_time
        try:
            pos_x = int(pos_x)
            pos_y = int(pos_y)
            pos_w = int(pos_w)
            pos_time = float(pos_time)
        except Exception, e:
            result['info'] = '您设置的参数出错,请您确认您的参数!'
            self.write(json_encode(result))
            return
        if pos_w < 150:
            result['info'] = '您设置的参数出错,请您确认您的参数!'
            self.write(json_encode(result))
            return

        if crop_type == "avatar":
            # set user avatar
            user_avatar = (user.avatar).file_name
            #print 'path: ', user_avatar
            if user_avatar == '' or user_avatar is None:
                result['info'] = '您的头像未设置,请您先上传并设置头像!'
                self.write(json_encode(result))
                return
            image_name = urlparse.urlparse(user_avatar).path.split('/').pop()
            result['kind'], result['info'] = fun_save_thumb(image_name,
                                                            pos_x=pos_x,
                                                            pos_y=pos_y,
                                                            pos_w=pos_w,
                                                            pos_time=pos_time)
            self.write(json_encode(result))
            return
        elif crop_type == 'logo':
            # crop group logo
            group_id = is_value(self.get_argument("group_id", 1))
            try:
                tmp_group = BasicGroup(_id=group_id)
                if tmp_group.get_member_type(user) != 'Manager':
                    result['info'] = '您无权进行设置!'
                    self.write(result)
                    return
            except Exception, e:
                result['info'] = '参数错误!'
                self.write(result)
                return
            group_avatar = tmp_group.avatar.file_name
            if group_avatar == '' or group_avatar is None:
                result['info'] = '请您先上传小组的Logo'
                self.write(result)
                return
            image_name = urlparse.urlparse(group_avatar).path.split('/').pop()
            result['kind'], result['info'] = fun_save_thumb(
                image_name,
                pos_x=pos_x,
                pos_y=pos_y,
                pos_w=pos_w,
                pos_time=pos_time,
                normal_path=AFWConfig.afewords_image_path +
                "/static/logo/normal/",
                small_path=AFWConfig.afewords_image_path +
                "/static/logo/small/")
            self.write(json_encode(result))
            return
Ejemplo n.º 9
0
         result['info'] = '该用户不存在!'
         self.write(json_encode(result))
         return
     else:
         if do == "follow":
             user.follow_user(follower)
             result['kind'] = 0
         elif do == "unfollow":
             user.unfollow_user(follower)
             result['kind'] = 0
         self.write(json_encode(result))
         return
 else:
     # for group follow
     try:
         group = BasicGroup(_id=follow_id)
     except Exception, e:
         logging.error(traceback.format_exc())
         logging.error('Url Error %s' % self.request.uri)
         result['info'] = '无此小组!'
         self.write(json_encode(result))
         return
     else:
         if do == "follow":
             user.follow_group(group)
             result['kind'] = 0
         else:
             if group.get_member_type(user) == 'Manager':
                 result['info'] = '管理员无法退出!'
             else:
                 if str(group._id) != AFWConfig.afewords_group_id:
Ejemplo n.º 10
0
        except Exception, e:
            logging.error(traceback.format_exc())
            logging.error('%s not exist, id %s' % (article_type, article_id))
            AF_Object = Blog()
            AF_Object.author = user
            AF_Object.env = user
            article_id = str(AF_Object._id)
            isnew = True
            (user.drafts_lib).add_obj(AF_Object)
    elif article_type == "comment":
        return [1, '完善中']
    elif article_type in Group_Article:
        # for group
        isnew = False
        try:
            group = BasicGroup(_id=group_id)
            limit = group.authority_verify(user)
        except Exception, e:
            logging.error(traceback.format_exc())
            logging.error('Group not exist, id %s' % group_id)
            return [1, '参数错误,小组不存在!']

        class_map = {
            'group-info': About,
            'group-doc': Blog,
            'group-feedback': Feedback,
            'group-notice': Bulletin,
            'group-topic': Topic
        }

        user_role = group.get_member_type(user)