Пример #1
0
def insert(request):
    '''执行添加'''
    try:
        # 图片的上传处理
        myfile = request.FILES.get("pic",None)
        if not myfile:
            return HttpResponse("没有上传文件信息")
        filename = str(time.time())+"."+myfile.name.split('.').pop()
        destination = open("./static/goods/"+filename,"wb+")
        for chunk in myfile.chunks():      # 分块写入文件  
            destination.write(chunk)  
        destination.close()

        # 图片的缩放
        im = Image.open("./static/goods/"+filename)
        # 缩放到375*375(缩放后的宽高比例不变):
        im.thumbnail((375, 375)) 
        im.save("./static/goods/"+filename,None)
        
        im = Image.open("./static/goods/"+filename)
        # 缩放到220*220(缩放后的宽高比例不变):
        im.thumbnail((220,220)) 
        im.save("./static/goods/m_"+filename,None)

        im = Image.open("./static/goods/"+filename)
        # 缩放到75*75(缩放后的宽高比例不变):
        im.thumbnail((75, 75)) 
        im.save("./static/goods/s_"+filename,None)

        #保存商品信息
        ob = Goods()
        ob.goods = request.POST['goods']
        ob.typeid = request.POST['typeid']
        ob.company = request.POST['company']
        ob.price = request.POST['price']
        ob.store = request.POST['store']
        ob.content = request.POST['content']
        ob.picname = filename
        ob.state = 1
        ob.addtime = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
        ob.save()
        context={"info":"添加成功!"}
    except Exception as err:
        print(err)
        context={"info":"添加失败"}
    return render(request,"myadmin/info.html",context)
Пример #2
0
def insert(request):
    try:
        myfile = request.FILES.get("pic", None)
        if not myfile:
            return HttpResponse("没有上传信息")

        filename = str(time.time()) + "." + myfile.name.split('.').pop()
        destination = open(os.path.join("./static/goods/", filename), 'wb+')
        for chunk in myfile.chunks():
            destination.write(chunk)
        destination.close()

        im = Image.open("./static/goods/" + filename)
        # rgb_im = im.convert('RGB')
        im.thumbnail((375, 375))
        im.save("./static/goods/" + filename)
        im.thumbnail((220, 220))
        im.save("./static/goods/m_" + filename)
        im.thumbnail((75, 75))
        im.save("./static/goods/s_" + filename)

        ob = Goods()
        ob.goods = request.POST['goods']
        ob.typeid = request.POST['typeid']
        ob.company = request.POST['company']
        ob.price = request.POST['price']
        ob.store = request.POST['store']
        ob.content = request.POST['content']
        ob.picname = filename
        ob.state = 1
        ob.addtime = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
        ob.save()
        context = {'info': '添加成功!'}
    except Exception as err:
        print(err)
        context = {'info': '添加失败!'}

    return render(request, "admin/info.html", context)
Пример #3
0
def insert(request):
    try:
        # 判断并执行图片上传,缩放等处理
        myfile = request.FILES.get('pic', None)
        if not myfile:
            return HttpResponse('没有上传文件信息!')

        # 以时间戳命名一个新图片名称
        filename = str(time.time()) + '.' + myfile.name.split('.').pop()
        destination = open(os.path.join('./static/goods/', filename), 'wb+')
        for chunk in myfile.chunks():
            destination.write(chunk)
        destination.close()

        # 执行图片缩放
        im = Image.open('./static/goods/' + filename)
        # 缩放到375*375
        im.thumbnail((375, 375))
        # 把缩放后的图像用jpeg格式保存
        im.save('./static/goods/' + filename, 'jpeg')
        # 缩放到220*220:
        im.thumbnail((220, 220))
        # 把缩放后的图像用jpeg格式保存:
        im.save("./static/goods/m_" + filename, 'jpeg')
        # 缩放到75*75:
        im.thumbnail((75, 75))
        # 把缩放后的图像用jpeg格式保存:
        im.save("./static/goods/s_" + filename, 'jpeg')

        # 获取商品信息并执行添加
        ob = Goods()
        ob.goods = request.POST['goods']
        ob.typeid = request.POST['typeid']
        ob.company = request.POST['company']
        ob.price = request.POST['price']
        ob.store = request.POST['store']
        ob.content = request.POST['content']
        ob.picname = filename
        ob.state = 1
        ob.addtime = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
        ob.save()
        context = {'info': '添加成功!'}
    except Exception as err:
        print(err)
        context = {'info': '添加失败!'}

    return render(request, "myadmin/info.html", context)
Пример #4
0
def insert(request):
    try:
        myajax_content = request.POST.get('content')
        # 判断是否有圖,并执行图片上传,缩放等处理
        myfile = request.FILES.get("pic", None)
        if not myfile:
            return HttpResponse("尚未上传雜誌封面圖!")
        # 以时间戳命名一个新图片名称
        filename = str(int(time.time())) + "_.jpg"
        destination = open(os.path.join("./static/goods/", filename), 'wb+')
        for chunk in myfile.chunks():  # 分块写入文件
            destination.write(chunk)
        destination.close()

        # 执行图片缩放
        im = Image.open("./static/goods/" + filename)
        # 缩放到375*375:
        im.thumbnail((375, 375))
        # 把缩放后的图像用jpeg格式保存:
        im.save("./static/goods/" + filename, 'jpeg')
        # 缩放到220*220:
        im.thumbnail((220, 220))
        # 把缩放后的图像用jpeg格式保存:
        im.save("./static/goods/m_" + filename, 'jpeg')
        # 缩放到75*75:
        im.thumbnail((75, 75))
        # 把缩放后的图像用jpeg格式保存:
        im.save("./static/goods/s_" + filename, 'jpeg')

        # 获取商品信息并执行添加
        ob = Goods()
        ob.goods = request.POST['goods']
        ob.typeid = request.POST['typeid']
        ob.publish_date = request.POST['publish_date']
        ob.language = request.POST['language']
        ob.price = request.POST['price']
        ob.store = request.POST['store']
        ob.content = html.unescape(
            myajax_content)  # html.unescape 可以與前端的模板語法safe相呼應,避免亂碼
        ob.picname = filename
        ob.state = 1
        # ob.addtime = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
        ob.addtime = timezone.now()
        ob.save()
        context = {'info': '添加成功!'}
    except Exception as err:
        print(err)
        context = {'info': '添加失败!'}

    return render(request, "myadmin/info.html", context)
Пример #5
0
def insert(request):
    '''执行添加'''
    try:
        #保存商品信息
        ob = Goods()
        if request.POST['goods']=='':
            context={"info":"商品名称不能为空!"}
            request.session['url']='myadmin_goods_add'
            request.session['data']=-1
            return render(request,"myadmin/info.html",context)
        else:
            ob.goods = request.POST['goods']

        if request.POST['price']=='':
            context={"info":"单价不能为空!"}
            request.session['url']='myadmin_goods_add'
            request.session['data']=-1
            return render(request,"myadmin/info.html",context)  
        else:       
            ob.price = request.POST['price']

        if request.POST['store']=='':
            request.session['url']='myadmin_goods_add'
            request.session['data']=-1
            context={"info":"库存量不能为空!"}
            return render(request,"myadmin/info.html",context)  
        else: 
            ob.store = request.POST['store']

        #图片的上传处理
        myfile = request.FILES.get("pic",None)
        if not myfile:
            context={"info":"没有上传文件!"}
            request.session['url']='myadmin_goods_add'
            request.session['data']=-1
            return render(request,"myadmin/info.html",context)
        filename = str(time.time())+"."+myfile.name.split('.').pop()
        destination = open("./static/goods/"+filename,"wb+")
        #分块写入文件 
        for chunk in myfile.chunks():       
            destination.write(chunk)  
        destination.close()

        #图片的缩放
        im = Image.open("./static/goods/"+filename)
        #缩放到375*375(缩放后的宽高比例不变):
        im.thumbnail((375, 375)) 
        im.save("./static/goods/"+filename,None)
        
        im = Image.open("./static/goods/"+filename)
        #缩放到220*220(缩放后的宽高比例不变):
        im.thumbnail((220,220)) 
        im.save("./static/goods/m_"+filename,None)

        im = Image.open("./static/goods/"+filename)
        #缩放到75*75(缩放后的宽高比例不变):
        im.thumbnail((75, 75)) 
        im.save("./static/goods/s_"+filename,None)

        #保存商品信息
        ob.typeid = request.POST['typeid']
        ob.company = request.POST['company']
        ob.content = request.POST['content']
        ob.picname = filename
        ob.state = 0
        ob.addtime = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
        ob.save()
        context={"info":"添加成功!"}
        request.session['url']='myadmin_goods_index'
        request.session['data']=-1
    except Exception as err:
        print(err)
        request.session['url']='myadmin_goods_add'
        request.session['data']=-1
        context={"info":"添加失败!"}
    return render(request,"myadmin/info.html",context)
Пример #6
0
def insert(request):
    '''执行添加操作'''
    goods = Goods()
    goods.goods = request.POST['goods']  #商品名称
    goods.typeid = request.POST['typeid']
    goods.company = request.POST['company']  #生产厂家
    goods.price = request.POST['price']  #价格
    goods.store = request.POST['store']  #库存
    goods.state = 1  #状态
    goods.content = request.POST['content']  #商品简介
    goods.addtime = datetime.now().strftime("%Y-%m-%d %H:%H:%S")

    photofile = request.FILES.get('file', None)
    if photofile == None:
        context = {'info': '未获取到图片信息!请重新添加。'}
        return render(request, './myadmin/goods/info.html', context)
    goods.picname = str(time.time()) + '.' + photofile.name.split('.').pop()
    openphoto = open('./static/myadmin/photo/' + goods.picname, 'wb+')
    for chunk in photofile.chunks():
        openphoto.write(chunk)
    openphoto.close()

    # 执行图片缩放
    im = Image.open("./static/myadmin/photo/" + goods.picname)
    # 缩放到75*75(缩放后的宽高比例不变):
    im.thumbnail((100, 100))
    im.save("./static/myadmin/photo/s_" + goods.picname, None)

    #执行图片删除
    os.remove("./static/myadmin/photo/" + goods.picname)
    goods.save()
    context = {'info': '商品信息添加成功!'}
    return render(request, './myadmin/goods/info.html', context)
Пример #7
0
def insert(request):
    try:
        upfile = request.FILES.get('picname', None)
        if not upfile:
            return HttpResponse("没有上传的文件")
        # 随机生成一个文件名
        filename = str(time.time()) + "." + upfile.name.split('.').pop()
        ob = open("./static/goods/" + filename, "wb+")
        for chunk in upfile.chunks():  # 分块写入文件
            ob.write(chunk)
        ob.close()

        im = Image.open("./static/goods/" + filename)
        # 缩放到375*375:
        im.thumbnail((375, 375))
        # 把缩放后的图像用jpeg格式保存:
        im.save("./static/goods/" + filename, 'jpeg')
        # 缩放到220*220:
        im.thumbnail((220, 220))
        # 把缩放后的图像用jpeg格式保存:
        im.save("./static/goods/m_" + filename, 'jpeg')
        # 缩放到75*75:
        im.thumbnail((75, 75))
        # 把缩放后的图像用jpeg格式保存:
        im.save("./static/goods/s_" + filename, 'jpeg')

        # 执行信息添加
        ob = Goods()
        ob.goods = request.POST['goods']
        ob.typeid = request.POST['typeid']
        ob.company = request.POST['company']
        ob.price = request.POST['price']
        ob.store = request.POST['store']
        ob.content = request.POST['content']
        ob.picname = filename
        ob.save()
        context = {'info': "添加商品信息成功"}
    except Exception as err:
        context = {'info': err}

    return render(request, "myadmin/info.html", context)
Пример #8
0
def insert(request):
    try:
        #新增会员信息
        mypic = request.FILES.get('txt_photo', None)
        if not mypic:
            info = "照片不能为空!"
            context = {"info": info}
            return render(request, "mymanage/info.html", context)
        if mypic.name.split(".").pop() == 'png':
            img = Image.open(mypic)
            filename = str(time.strftime("%Y%m%d%H%M%S")) + '-' + str(
                random.choice(range(10000))) + '.jpg'
            if len(img.split()) == 4:
                #将图片png格式统一成jpg
                r, g, b, a = img.split()
                img = Image.merge("RGB", (r, g, b))
                img.save("./static/mymanage/goods/" + filename)
                # 执行图片缩放
                im = Image.open("./static/mymanage/goods/" + filename)
                # 缩放到375*375:
                im.thumbnail((375, 375))
                # 把缩放后的图像用jpeg格式保存:
                im.save("./static/mymanage/goods/" + filename, 'jpeg')
                # 缩放到220*220:
                im.thumbnail((220, 220))
                # 把缩放后的图像用jpeg格式保存:
                im.save("./static/mymanage/goods/m_" + filename, 'jpeg')
                # 缩放到75*75:
                im.thumbnail((75, 75))
                # 把缩放后的图像用jpeg格式保存:
                im.save("./static/mymanage/goods/s_" + filename, 'jpeg')
        else:
            if mypic.name.split(".").pop() == 'gif':
                filename = str(time.strftime("%Y%m%d%H%M%S")) + '-' + str(
                    random.choice(range(10000))) + '.' + mypic.name.split(
                        ".").pop()  #time.time()
                destination = open("./static/mymanage/goods/" + filename,
                                   "wb+")  #图片多用wb+代表二进制操作,其他的可以使用W或者w+
                for chunk in mypic.chunks():  # 分块写入文件
                    destination.write(chunk)
                destination.close()

                destination = open("./static/mymanage/goods/m_" + filename,
                                   "wb+")  #图片多用wb+代表二进制操作,其他的可以使用W或者w+
                for chunk in mypic.chunks():  # 分块写入文件
                    destination.write(chunk)
                destination.close()

                destination = open("./static/mymanage/goods/s_" + filename,
                                   "wb+")  #图片多用wb+代表二进制操作,其他的可以使用W或者w+
                for chunk in mypic.chunks():  # 分块写入文件
                    destination.write(chunk)
                destination.close()
            else:
                filename = str(time.strftime("%Y%m%d%H%M%S")) + '-' + str(
                    random.choice(range(10000))) + '.' + mypic.name.split(
                        ".").pop()  #time.time()
                destination = open("./static/mymanage/goods/" + filename,
                                   "wb+")  #图片多用wb+代表二进制操作,其他的可以使用W或者w+
                for chunk in mypic.chunks():  # 分块写入文件
                    destination.write(chunk)
                destination.close()

                # 执行图片缩放
                im = Image.open("./static/mymanage/goods/" + filename)
                # 缩放到375*375:
                im.thumbnail((375, 375))
                # 把缩放后的图像用jpeg格式保存:
                im.save("./static/mymanage/goods/" + filename, 'jpeg')
                # 缩放到220*220:
                im.thumbnail((220, 220))
                # 把缩放后的图像用jpeg格式保存:
                im.save("./static/mymanage/goods/m_" + filename, 'jpeg')
                # 缩放到75*75:
                im.thumbnail((75, 75))
                # 把缩放后的图像用jpeg格式保存:
                im.save("./static/mymanage/goods/s_" + filename, 'jpeg')

        list = Goods()
        list.goods = request.POST['txt_name']
        list.typeid = request.POST['txt_type']
        list.company = request.POST['txt_pro']
        list.state = request.POST['ra_rob']
        list.content = request.POST['txt_text']
        list.price = request.POST['txt_price']
        list.picname = filename
        list.save()
        info = "保存成功!"
        context = {"info": info}
        return render(request, "mymanage/info.html", context)
    except Exception as info:
        context = {"info": info}
        return render(request, "mymanage/info.html", context)
Пример #9
0
def insert(httpRequest):
    '''执行添加'''
    try:
        mod = Goods()
        mod.goods = httpRequest.POST.get('goods', None)
        mod.company = httpRequest.POST.get('company', None)
        mod.typeid = httpRequest.POST.get('typeid', None)
        mod.price = httpRequest.POST.get('price', None)
        mod.store = httpRequest.POST.get('store', None)
        mod.state = 1
        # 存图片名字,存图片
        myfile = httpRequest.FILES.get('picname', None)
        if not myfile:
            return HttpResponse("没有上传文件信息")
        filename = str(time.time()) + "." + myfile.name.split('.').pop()
        destination = open("./static/pics/" + filename, "wb+")
        for chunk in myfile.chunks():  # 分块写入文件
            destination.write(chunk)
            destination.close()

            # 执行图片缩放
        im = Image.open("./static/pics/" + filename)
        # 缩放到75*75(缩放后的宽高比例不变):
        im.thumbnail((75, 75))
        # 把缩放后的图像用jpeg格式保存:
        im.save("./static/pics/s75_" + filename, None)

        im = Image.open("./static/pics/" + filename)
        # 缩放到75*75(缩放后的宽高比例不变):
        im.thumbnail((225, 225))
        # 把缩放后的图像用jpeg格式保存:
        im.save("./static/pics/s225_" + filename, None)

        #执行图片删除
        #os.remove("./static/pics/"+filename)
        mod.picname = filename
        mod.content = httpRequest.POST.get('content', None)
        mod.clicknum = 0
        mod.save()
        data = {"info": '添加成功'}

    except Exception as e:
        print(e)
        data = {"info": '添加失败'}
        return render(httpRequest, 'myadmin/info.html', data)

    return render(httpRequest, 'myadmin/info.html', data)