Exemplo n.º 1
0
def new(req): #new user
    msg = ''
    if req.method == 'GET':
        avatar_form = new_avatar_form()
        userAddForm = UserCreationForm()        
    elif req.method == 'POST':
        userAddForm = UserCreationForm(req.POST)
        avatar_form = new_avatar_form(req.POST)
        if userAddForm.is_valid():
            _a = avatar()
            _a.desc = req.POST['desc']            
            
            if req.FILES.has_key('avatar'): # 如果有上传图像
                _img = req.FILES['avatar']
                if _img.size < 50*1024: # 图片大小合适
                    userAddForm.save()                    
                    _a.avatar = upload_and_replace('avatar',_img,str(userAddForm.instance.id))
                else: # 图片过大
                    msg = "图片大小要小于50kb!"
                    return rr('user/new.htm',
                        {'userAddForm':userAddForm, 'avatar_form':avatar_form,'msg':msg, },
                        context_instance = RequestContext(req))
            else: #没有上传图像,保存新建用户的请求,保存avatar。
                userAddForm.save()
            
            _a.user = userAddForm.instance
            _a.save()
            return rd('/')
    return rr('user/new.htm',
            {'userAddForm':userAddForm, 'avatar_form':avatar_form,'msg':msg, },
            context_instance = RequestContext(req))
        
Exemplo n.º 2
0
def change_avatar(req):
    msg = ''
    if req.method == 'GET':
        avatar_form = new_avatar_form()
        try:
            avatar_form.instance = req.user.get_profile()
        except:
            pass
    elif req.method == 'POST':
        avatar_form = new_avatar_form(req.POST)
        if avatar.objects.filter(user=req.user):
            _a = avatar.objects.filter(user=req.user)[0]
        else :
            _a = avatar()
            _a.user = req.user
        if req.FILES.has_key('avatar'):
            _img = req.FILES['avatar']
                
            if _img.size < 50*1024:                
                _a.avatar = upload_and_replace('avatar',_img,str(_a.user.id))
            else:
                msg = "图片大小要小于50kb!"
                return rr('user/change_avatar.htm',
                    {'avatar_form':avatar_form,'msg':msg, },
                    context_instance = RequestContext(req))
            
        _a.desc = req.POST['desc']
        _a.save()
        return rd('/')
    return rr('user/change_avatar.htm',
            {'avatar_form':avatar_form,'msg':msg, },
            context_instance = RequestContext(req))