Example #1
0
 def testUploadImage(self):
     # i don't know how to build a django UploadedFile object, so it haven't 
     # been tested TODO
     try:
         self.assertTrue(img_utils.upload())
     except:
         pass    
Example #2
0
def initPic(request, template=settings.TEMPLATE_SETUP_PICTURE, **kwargs):
    '''处理用户上传头像图片,或用户已有头像初始化显示'''

    if request.method != "POST":
        profile = request.user.get_profile()
        if not profile.tmp_pic:
            profile.tmp_pic = profile.big_pic
            profile.save() 
        return render_to_response(settings.TEMPLATE_SETUP_PICTURE, RequestContext(request,))
    
    # 处理POST请求,文件上传,存储
    file = request.FILES["picture"]
    
    if not file:
        return HttpResponseRedirect(reverse("profiles_setpic"))
    
    if (len(file) > settings.PIC_UPPER_BOUND * 1024 * 1024):
        utils.addMsg(request, messages.ERROR, u"请确保上传的图片大小不超过2M!")
        return HttpResponseRedirect(reverse("profiles_setpic"))
    
    fname, ext = os.path.splitext(str(file))
    if ext not in settings.ALLOWED_IMG_FORMAT:
        utils.addMsg(request, messages.ERROR, u"上传的图片格式不正确!")
        return HttpResponseRedirect(reverse("profiles_setpic"))
    
    filename = img_utils.upload(file, settings.PIC_SIZE_BIG, PROFILE_PIC_ROOT, 100)
    
    if not filename:
        utils.addMsg(request, messages.ERROR, u"图片上传失败!")
        return HttpResponseRedirect(reverse("profiles_setpic"))
    
    profile = request.user.get_profile()
    
    # tmp_pic临时存放上传的图片
    profile.tmp_pic = "profile/%s" % (filename)
    profile.save()
    
    return HttpResponseRedirect(reverse("profiles_setpic"))