Пример #1
0
    def post(self, request, page):
        """处理商品上架"""

        # 接收数据
        name = request.POST.get('name')
        price = request.POST.get('price')
        stock = request.POST.get('stock')
        pack = request.POST.get('pack')
        typ = request.POST.get('type')
        img = request.FILES.get('file')

        # 校验基本数据
        if not all([img, name, price, stock, pack, typ]):
            # 数据不完整
            return render(request, 'sj_cpgl.html', {'errmsg': '缺少相关数据'})

        # FDFS上传图片
        rec = FastDFSStorage().save(img.name, img)

        # 业务表添加数据

        try:
            df_shop = Shop.objects.get(user_id=request.user.id)
        except Shop.DoesNotExist:
            return render(request, 'login.html', {'errmsg': '用户登录信息已失效,请重新登录!'})

        df_goods = Goods(name=name, shop_id=df_shop.id)
        df_goods.save()

        # 确定是否为新增类型
        try:
            df_goods_type = GoodsType.objects.get(name=typ, shop_id=df_shop.id)
        except GoodsType.DoesNotExist:
            df_goods_type = GoodsType(name=typ, shop_id=df_shop.id)
            df_goods_type.save()

        df_goods_sku = GoodsSKU(goods=df_goods, type=df_goods_type, name=name, price=price,
                                unite='per', stock=stock, pack=pack)
        df_goods_sku.save()

        df_goods_image = GoodsImage(image=rec, sku_id=df_goods_sku.id)
        df_goods_image.save()

        return redirect(reverse('goodsmanage:sj_cpgl', kwargs={'page': 1}))
Пример #2
0
import hashlib

pwd = os.path.dirname(os.path.realpath(__file__))
sys.path.append(pwd + "../")
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "OnlineMarket.settings")

import django

django.setup()

from apps.goods.models import Goods, GoodsCategory, GoodsImage

from db_tools.data.my_product_data import row_data

for goods_detail in row_data:
    goods = Goods()
    goods.name = goods_detail["name"]
    goods.price = float(
        int(goods_detail["price"].replace("¥", "").replace("元", "")))
    goods.goods_intro = goods_detail["desc"] if goods_detail[
        "desc"] is not None else ""
    goods.goods_front_img = goods_detail["images"][0] if goods_detail[
        "images"] else ""
    goods.goods_sn = hashlib.md5(
        (str(int(time.time())) + goods.name).encode('utf-8')).hexdigest()

    category_name = goods_detail["categorys"][-1]
    category = GoodsCategory.objects.filter(name=category_name)
    if category:
        goods.category = category[0]
    goods.save()
Пример #3
0
import sys
import os

pwd = os.path.dirname(os.path.realpath(__file__))
sys.path.append(os.path.dirname(pwd))
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "CnShop.settings")

import django
django.setup()

from apps.goods.models import Goods, GoodsCategory, GoodsImage

from db_tools.data.product_data import row_data

for goods_detail in row_data:
    goods = Goods()
    goods.name = goods_detail["name"]
    goods.market_price = float(
        int(goods_detail["market_price"].replace("¥", "").replace("元", "")))
    goods.shop_price = float(
        int(goods_detail["sale_price"].replace("¥", "").replace("元", "")))
    goods.goods_brief = goods_detail["desc"] if goods_detail[
        "desc"] is not None else ""
    goods.goods_desc = goods_detail["goods_desc"] if goods_detail[
        "goods_desc"] is not None else ""
    goods.goods_front_image = goods_detail["images"][0] if goods_detail[
        "images"] else ""

    category_name = goods_detail["categorys"][-1]
    category = GoodsCategory.objects.filter(name=category_name)
    if category:
Пример #4
0
def goods_upload(request):
    r = json.loads(request.body)
    title = r['title']
    detail = r['detail']
    category = r['category']
    # {url: url1, hash: hash1}
    # url通过 | 分割多个图片链接存在一个字段,hash同理
    url_array = r['url']
    seller_id = r['sellerId']
    price = r['price']
    express = r['express']
    area = r['area']
    url = ''
    for item in url_array:
        url = url + str(item['url']) + '|'
    img_hash = ''
    for item in url_array:
        img_hash = img_hash + str(item['hash']) + '|'
    goods = Goods()
    goods.status = 2
    goods.title = title
    goods.detail = detail
    goods.category = category
    # 去除最后多余的|
    goods.url = url[:-1]
    goods.img_hash = img_hash[:-1]
    goods.seller_id = seller_id
    goods.price = price
    goods.express = express
    # 去除最后多余的,
    goods.area = area[:-1]
    goods.save()
    return JsonResponse({'success': True})
Пример #5
0
 def post(self, request):
     # 获取数据
     user = User.objects.filter(id=request.user.id)[0]
     title = request.POST.get('title')
     province = request.POST.get('province')
     city = request.POST.get('city')
     district = request.POST.get('district')
     gtype = request.POST.get('gtype')
     detail = request.POST.get('gcontent')
     price = request.POST.get('price')
     con = request.POST.get('condition')
     files = request.FILES.getlist('files')
     fineness = []
     for fin in Goods.comm_quality:
         fineness.append({'id': fin[0], 'value': fin[1]})
     context = {
         'fin': fineness,
         'gtype': GoodsType.objects.values('id', 'name'),
         'msg': ''
     }
     # 数据检查
     if not all([
             gtype, title, province, city, district, detail, price, con,
             files
     ]):
         context['msg'] = "数据填写不完整,每个都为必填项!"
         return render(request, 'good.html', context)
     if len(title) > 256:
         context['msg'] = "标题过长!不能超过256个字符"
         return render(request, 'good.html', context)
     if float(price) > 99999.00 or float(price) < 0.00:
         context['msg'] = "价格输入有误(0~99999)!"
         return render(request, 'good.html', context)
     gtype = int(gtype)
     price = float(price)
     con = int(con)
     title.strip()
     city.strip()
     province.strip()
     district.strip()
     addr = ' '.join([province, city, district])
     up_date = str(datetime.datetime.now(tz=timezone.utc))
     # 保存商品信息
     try:
         gtype = GoodsType.objects.get(id=gtype, is_delete=0)
     except Exception:
         context['msg'] = "类型选择错误!"
         return render(request, 'good.html', context)
     good = Goods(type=gtype,
                  user=user,
                  title=title,
                  price=price,
                  detail=detail,
                  addr=addr,
                  fineness=con,
                  update_time=up_date)
     good.save()
     # 上传文件
     fdfs = FDFSStorage()
     for file in files:
         try:
             url = fdfs.save(name=file.name, content=file)
             GoodsImage(goods=good, image=url).save()
         except Exception:
             GoodsImage(goods=good).save()
     return redirect(reverse('goods:good_list'))
Пример #6
0
import os
import sys

pwd = os.path.dirname(os.path.realpath(__file__))
sys.path.append(pwd + '../')
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'aozhou.settings')

import django
django.setup()

from apps.goods.models import Goods, GoodsCategory, GoodsImage

from db_tools.data.goods import row_data

for goods_detail in row_data:
    goods = Goods()
    goods.name = goods_detail['goods_name']
    goods.goods_desc = goods_detail['goods_desc']
    goods.shop_price = float(
        int(goods_detail['shop_price'].replace("¥", "").replace("元", "")))
    # goods.goods_brief = goods_detail['desc'] if goods_detail['desc'] else ""
    goods.market_price = float(
        int(goods_detail['market_price'].replace("¥", "").replace("元", "")))
    goods.goods_thumbnail = goods_detail['goods_image'][0] if goods_detail[
        'goods_image'] else ""

    category_name = goods_detail['goods_category']
    category = GoodsCategory.objects.filter(name=category_name)
    goods.category = category[0]
    goods.save()