Esempio n. 1
0
 def save_image(self):
     from uliweb.utils.image import crop_resize
     from uliweb.contrib.upload import get_filename
     
     x = int(request.POST.get('x'))
     y = int(request.POST.get('y'))
     w = int(request.POST.get('w'))
     h = int(request.POST.get('h'))
     
     of = get_filename(_get_portrait_image_filename(request.user.id))
     f = get_filename(_get_portrait_image_thumbnail(request.user.id, size=50))
     crop_resize(of, f, x, y, w, h, size=(50, 50))
     f = get_filename(_get_portrait_image_thumbnail(request.user.id, size=20))
     crop_resize(of, f, x, y, w, h, size=(20, 20))
     flash(_('Save portrait successful'))
     return redirect(url_for(UserView.view))
Esempio n. 2
0
 def edit_image(self):
     from forms import UploadImageForm
     from uliweb.utils.generic import EditView
     from uliweb.contrib.upload import get_url, get_filename
     import os
     import Image
     
     def pre_save(obj, data):
         if 'image' in data and data['image']:
             data['image'].filename = _get_portrait_image_filename(request.user.id)
             
     image = _get_portrait_image_filename(request.user.id)
     f = get_filename(image)
     if os.path.exists(f):
         url = get_url(image)
         img = Image.open(f)
         template_data = {'image_url':url, 'size':img.size}
     else:
         url = None
         template_data = {'image_url':url, 'size':(0, 0)}
         
     view = EditView('user', condition=request.user.id, ok_url=url_for(UserView.edit_image),
         pre_save=pre_save, fields=['image'], template_data=template_data)
     
     return view.run()
Esempio n. 3
0
 def save_image(self):
     from uliweb.utils.image import crop_resize
     from uliweb.contrib.upload import get_filename
     
     x = int(request.POST.get('x'))
     y = int(request.POST.get('y'))
     w = int(request.POST.get('w'))
     h = int(request.POST.get('h'))
     
     of = get_filename(_get_portrait_image_filename(request.user.id))
     f = get_filename(_get_portrait_image_thumbnail(request.user.id, size=50))
     crop_resize(of, f, x, y, w, h, size=(50, 50))
     f = get_filename(_get_portrait_image_thumbnail(request.user.id, size=20))
     crop_resize(of, f, x, y, w, h, size=(20, 20))
     flash(_('Save portrait successful'))
     return redirect(url_for(UserView.view))
Esempio n. 4
0
    def _clear_files(self, slug, text):
        import re
        import itertools
        from uliweb.contrib.upload import get_filename, get_url
        from uliweb.utils.image import fix_filename
        File = get_model('forumattachment')

        r_links = re.compile(
            r'<a.*?href=\"([^"]+)\"|<img.*?src=\"([^"]+)\"|<embed.*?src=\"([^"]+)\"',
            re.DOTALL)
        files = filter(None, itertools.chain(*re.findall(r_links, text)))
        for row in File.filter(File.c.slug == slug):
            _f = get_filename(row.file_name)
            url = get_url(row.file_name)
            if url in files:
                row.enabled = True
                row.save()
            else:
                if os.path.exists(_f):
                    os.unlink(_f)
                thumbnail = fix_filename(_f, '.thumbnail')
                if os.path.exists(thumbnail):
                    os.unlink(thumbnail)

        File.filter(File.c.slug == slug).filter(
            File.c.enabled == False).remove()
Esempio n. 5
0
 def view(self, id):
     from uliweb.utils.generic import DetailView
     from uliweb import orm
     from uliweb.contrib.upload import get_filename, get_url
     import os
     
     User = orm.get_model('user')
     
     def boolean_convert(b, obj):
         if b:
             return '<div class="ui-icon ui-icon-check"></div>'
         else:
             return '<div class="ui-icon ui-icon-closethick"></div>'
     
     user = User.get(int(id))
     if not user:
         error(_('User is not exists!'))
         
     image = get_filename(_get_portrait_image_thumbnail(user.id))
     if os.path.exists(image):
         image_url = get_url(_get_portrait_image_thumbnail(user.id))
     else:
         image_url = user.get_image_url()
     can_modify = user.id == request.user.id
     template_data = {'image_url':image_url, 'can_modify':can_modify}
     view = DetailView('user', obj=user, template_data=template_data)
     view.types_convert_map = {orm.BooleanProperty:boolean_convert}
     return view.run()
Esempio n. 6
0
    def _upload_file(self, image=False):
        import os
        import Image
        from uliweb.contrib.upload import get_url, save_file, get_filename

        if image:
            from forms import ImageUploadForm as Form
        else:
            from forms import FileUploadForm as Form
        from uliweb.utils.image import thumbnail_image, fix_filename

        File = get_model("forumattachment")

        forum_id = request.GET.get("forum_id")
        slug = request.GET.get("slug")
        form = Form()
        suffix = date.now().strftime("_%Y_%m_%d")
        if request.method == "GET":
            form.bind({"is_thumbnail": True})
            return {"form": form}
        else:
            flag = form.validate(request.values, request.files)
            if flag:
                f = form.data["filedata"]
                _f = os.path.basename(f["filename"])
                # 文件格式为forum/<forum_id>/<filename_yyyy_mm_dd>
                filename = fix_filename("forum/%s/%s" % (forum_id, _f), suffix)
                if image:
                    filename = save_file(filename, f["file"])
                    if form.data["is_thumbnail"]:
                        # process thumbnail
                        rfilename, thumbnail = thumbnail_image(
                            get_filename(filename, filesystem=True),
                            filename,
                            settings.get_var("PARA/FORUM_THUMBNAIL_SIZE"),
                        )
                        _file = get_url(thumbnail)
                    else:
                        _file = get_url(filename)
                    name = get_url(filename)
                else:
                    filename = save_file(filename, f["file"])
                    _file = get_url(filename)
                    name = form.data["title"]
                    if not name:
                        name = _f
                ff = File(slug=slug, file_name=filename, name=name)
                ff.save()
                return """<script type="text/javascript">
var url='%s||%s';
setTimeout(function(){callback(url);},100);
</script>
""" % (
                    _file,
                    name,
                )
            else:
                return {"form": form}
Esempio n. 7
0
    def _upload_file(self, image=False, show_filename=True):
        import os
        import Image
        from uliweb.contrib.upload import get_url, save_file, get_filename
        if image:
            from forms import ImageUploadForm as Form
        else:
            from forms import FileUploadForm as Form
        from uliweb.utils.image import thumbnail_image, fix_filename

        File = get_model('forumattachment')

        forum_id = request.GET.get('forum_id')
        slug = request.GET.get('slug')
        form = Form()
        suffix = date.now().strftime('_%Y_%m_%d')
        if request.method == 'GET':
            form.bind({'is_thumbnail': True})
            return {'form': form}
        else:
            flag = form.validate(request.values, request.files)
            if flag:
                f = form.data['filedata']
                _f = os.path.basename(f['filename'])
                #文件格式为forum/<forum_id>/<filename_yyyy_mm_dd>
                filename = fix_filename('forum/%s/%s' % (forum_id, _f), suffix)
                if image:
                    filename = save_file(filename, f['file'])
                    if form.data['is_thumbnail']:
                        #process thumbnail
                        rfilename, thumbnail = thumbnail_image(
                            get_filename(filename, filesystem=True), filename,
                            settings.get_var('PARA/FORUM_THUMBNAIL_SIZE'))
                        _file = get_url(thumbnail)
                    else:
                        _file = get_url(filename)
                    name = get_url(filename)
                else:
                    filename = save_file(filename, f['file'])
                    _file = get_url(filename)
                    name = form.data['title']
                    if not name:
                        name = _f
                ff = File(slug=slug, file_name=filename, name=name)
                ff.save()
                if show_filename:
                    fargs = '||%s' % name
                else:
                    fargs = ''
                return '''<script type="text/javascript">
var url='%s%s';
setTimeout(function(){callback(url);},100);
</script>
''' % (_file, fargs)
            else:
                return {'form': form}
Esempio n. 8
0
def get_user_image(user, size=50):
    from uliweb.contrib.upload import get_filename, get_url
    import os
    
    image = get_filename(_get_portrait_image_thumbnail(user.id, size))
    if os.path.exists(image):
        image_url = get_url(_get_portrait_image_thumbnail(user.id, size))
    else:
        image_url = user.get_default_image_url(size)
    return image_url
Esempio n. 9
0
def get_user_image(user, size=50):
    from uliweb.contrib.upload import get_filename, get_url
    import os

    image = get_filename(_get_portrait_image_thumbnail(user.id, size))
    if os.path.exists(image):
        image_url = get_url(_get_portrait_image_thumbnail(user.id, size))
    else:
        image_url = user.get_default_image_url(size)
    return image_url
Esempio n. 10
0
    def _upload_file(self, image=False, show_filename=True):
        import os
        import Image
        from uliweb.contrib.upload import get_url, save_file, get_filename
        if image:
            from forms import ImageUploadForm as Form
        else:
            from forms import FileUploadForm as Form
        from uliweb.utils.image import thumbnail_image, fix_filename
        
        File = get_model('forumattachment')
        
        forum_id = request.GET.get('forum_id')
        slug = request.GET.get('slug')
        form = Form()
        suffix = date.now().strftime('_%Y_%m_%d')
        if request.method == 'GET':
            form.bind({'is_thumbnail':True})
            return {'form':form}
        else:
            flag = form.validate(request.values, request.files)
            if flag:
                f = form.data['filedata']
                _f = os.path.basename(f['filename'])
                #文件格式为forum/<forum_id>/<filename_yyyy_mm_dd>
                filename = fix_filename('forum/%s/%s' % (forum_id, _f), suffix)
                if image:
                    filename = save_file(filename, f['file'])
                    if form.data['is_thumbnail']:
                        #process thumbnail
                        rfilename, thumbnail = thumbnail_image(get_filename(filename, filesystem=True), filename, settings.get_var('PARA/FORUM_THUMBNAIL_SIZE'))
                        _file = get_url(thumbnail)
                    else:
                        _file = get_url(filename)
                    name = get_url(filename)
                else:
                    filename = save_file(filename, f['file'])
                    _file = get_url(filename)
                    name = form.data['title']
                    if not name:
                        name = _f
                ff = File(slug=slug, file_name=filename, name=name)
                ff.save()
                if show_filename:
                    fargs = '||%s' % name
                else:
                    fargs = ''
                return '''<script type="text/javascript">
var url='%s%s';
setTimeout(function(){callback(url);},100);
</script>
''' % (_file, fargs)
            else:
                return {'form':form}
Esempio n. 11
0
    def remove_topic(self, forum_id, topic_id):
        from uliweb.contrib.upload import get_filename
        from sqlalchemy.sql import select

        Forum = get_model('forum')
        forum = Forum.get(int(forum_id))
        Topic = get_model('forumtopic')
        topic = Topic.get(int(topic_id))
        Post = get_model('forumpost')
        post = Post.get((Post.c.topic == int(topic_id)) & (Post.c.floor == 1))
        FA = get_model('forumattachment')

        if not topic:
            error("主题已经不存在的")

        is_manager = post.topic.forum.managers.has(request.user)
        if is_manager:
            query = FA.filter(FA.c.slug == Post.c.slug).filter(
                Post.c.topic == int(topic_id))
            #删除相应附件
            for a in query:
                f = get_filename(a.file_name)
                if os.path.exists(f):
                    os.unlink(f)
            #删除FA记录
            FA.filter(
                FA.c.slug.in_(
                    select([Post.c.slug],
                           Post.c.topic == int(topic_id)))).remove()
            #删除所有POST
            post_query = Post.filter(Post.c.topic == int(topic_id))
            post_count = post_query.count()
            post_query.remove()
            Topic.get(int(topic_id)).delete()
            Forum.filter(Forum.c.id == int(forum_id)).update(
                num_posts=Forum.c.num_posts - post_count,
                num_topics=Forum.c.num_topics - 1)
            flash('删除成功!')
            return redirect(url_for(ForumView.forum_index, id=forum_id))
        else:
            flash('你无权限删除主题!')
            return redirect(
                url_for(ForumView.topic_view,
                        forum_id=forum_id,
                        topic_id=topic_id))
Esempio n. 12
0
    def remove_topic(self, forum_id, topic_id):
        from uliweb.contrib.upload import get_filename
        from sqlalchemy.sql import select

        Forum = get_model("forum")
        forum = Forum.get(int(forum_id))
        Topic = get_model("forumtopic")
        topic = Topic.get(int(topic_id))
        Post = get_model("forumpost")
        post = Post.get((Post.c.topic == int(topic_id)) & (Post.c.floor == 1))
        FA = get_model("forumattachment")

        if not topic:
            error("主题已经不存在的")

        is_manager = post.topic.forum.managers.has(request.user)
        if is_manager:
            query = FA.filter(FA.c.slug == Post.c.slug).filter(Post.c.topic == int(topic_id))
            # 删除相应附件
            for a in query:
                f = get_filename(a.file_name)
                if os.path.exists(f):
                    os.unlink(f)
            # 删除FA记录
            FA.filter(FA.c.slug.in_(select([Post.c.slug], Post.c.topic == int(topic_id)))).remove()
            # 删除所有POST
            post_query = Post.filter(Post.c.topic == int(topic_id))
            post_count = post_query.count()
            post_query.remove()
            Topic.get(int(topic_id)).delete()
            Forum.filter(Forum.c.id == int(forum_id)).update(
                num_posts=Forum.c.num_posts - post_count, num_topics=Forum.c.num_topics - 1
            )
            flash("删除成功!")
            return redirect(url_for(ForumView.forum_index, id=forum_id))
        else:
            flash("你无权限删除主题!")
            return redirect(url_for(ForumView.topic_view, forum_id=forum_id, topic_id=topic_id))
Esempio n. 13
0
 def _clear_files(self, slug, text):
     import re
     import itertools
     from uliweb.contrib.upload import get_filename, get_url
     from uliweb.utils.image import fix_filename
     File = get_model('forumattachment')
     
     r_links = re.compile(r'<a.*?href=\"([^"]+)\"|<img.*?src=\"([^"]+)\"|<embed.*?src=\"([^"]+)\"', re.DOTALL)
     files = filter(None, itertools.chain(*re.findall(r_links, text)))
     for row in File.filter(File.c.slug==slug):
         _f = get_filename(row.file_name)
         url = get_url(row.file_name)
         if url in files:
             row.enabled = True
             row.save()
         else:
             if os.path.exists(_f):
                 os.unlink(_f)
             thumbnail = fix_filename(_f, '.thumbnail')
             if os.path.exists(thumbnail):
                 os.unlink(thumbnail)
             
     File.filter(File.c.slug==slug).filter(File.c.enabled==False).remove()