Exemplo n.º 1
0
def alterSkuImg(request):
    logger = logging.getLogger('Pack.app')
    logger.info(request.REQUEST)
    response = {}
    response['data'] = {}
    response['errorMsg'] = ""
    response_data = {}
    _shopId = request.session.get('shopId')
    _shopId = str(_shopId)
    if not _shopId:
        response['code'] = 1
        response['errorMsg'] = '请先登录'
        return HttpResponse(json.dumps(response),content_type="application/json")
    ##################JUDGE############
    _lastLoginTime = request.session.get('lastLoginTime')
    if not _lastLoginTime:
        response['code'] = 1
        response['errorMsg'] = '请先登录'
        return HttpResponse(json.dumps(response),content_type="application/json")
    try:
        shop = Shop.objects.get(id = _shopId)
    except ObjectDoesNotExist:
        response['code'] = 1
        response['errorMsg'] = '请先登录'
        return HttpResponse(json.dumps(response,ensure_ascii=False),content_type="application/json")
    if _lastLoginTime != shop.lastLoginTime:
        response['code'] = 1
        response['errorMsg'] = '上次登录失效,请重新登录'
        return HttpResponse(json.dumps(response),content_type="application/json")
    ####################END#################

    _skuId = request.REQUEST.get('skuId')
    _skuImg = request.REQUEST.get('skuImg')

    if _skuId == None or _skuId == '':
        response['code'] = -1
        response['errorMsg'] = '商品id为空'
        return HttpResponse(json.dumps(response),content_type="application/json")
    if _skuImg == None or _skuImg == '':
        response['code'] = -1
        response['errorMsg'] = '图片url为空'
        return HttpResponse(json.dumps(response),content_type="application/json")
    _skuId = str(_skuId)
    _skuImg = str(_skuImg)

    try:
        sku = Sku.select_related().objects.get(id = _skuId)
    except ObjectDoesNotExist:
        response['code'] = -1
        response['errorMsg'] = '商品查询失败'
        return HttpResponse(json.dumps(response),content_type="application/json")

    sku.img = _skuImg
    sku.save()
    response['code'] = 0
    response['data'] = sku.img
    return HttpResponse(json.dumps(response),content_type="application/json")
Exemplo n.º 2
0
def skuInfo(request):
    response = {}
    response['data'] = {}
    response['errorMsg'] = ""
    _shopId = request.session.get('shopId')
    if not _shopId:
        response['code'] = 1
        response['errorMsg'] = '请先登录'
        return HttpResponse(json.dumps(response),content_type="application/json")
    ##################JUDGE############
    _lastLoginTime = request.session.get('lastLoginTime')
    if not _lastLoginTime:
        response['code'] = 1
        response['errorMsg'] = '请先登录'
        return HttpResponse(json.dumps(response),content_type="application/json")
    try:
        shop = Shop.objects.get(id = _shopId)
    except ObjectDoesNotExist:
        response['code'] = 1
        response['errorMsg'] = '请先登录'
        return HttpResponse(json.dumps(response,ensure_ascii=False),content_type="application/json")
    if _lastLoginTime != shop.lastLoginTime:
        response['code'] = 1
        response['errorMsg'] = '上次登录失效,请重新登录'
        return HttpResponse(json.dumps(response),content_type="application/json")
    ####################END#################

    _method = request.REQUEST.get('method')
    if _method == None or _method == '':
        response['code'] = -1
        response['errorMsg'] = '获取method失败'
        return HttpResponse(json.dumps(response,ensure_ascii=False),content_type="application/json")
    _method = str(_method)

    #if method = 0 , add; method = 1, update; method = 2 , queryOne; method = 3, delete
    if _method == '0':
        _name = request.REQUEST.get('skuName')
        _desc = request.REQUEST.get('skuDesc')
        _categoryId = request.REQUEST.get('categoryId')
        _img = request.REQUEST.get('skuImg')
        _skuSizeList = request.get('skuSizeList')
        __skuSizeList = json.loads(_skuSizeList)

        if _name == None or _name == '':
            response['code'] = -1
            response['errorMsg'] = '请输入名字'
            return HttpResponse(json.dumps(response),content_type="application/json")
        if _desc == None:
            _desc = ''
        if _categoryId == None or _categoryId == '':
            response['code'] = -1
            response['errorMsg'] = '请选择品类'
            return HttpResponse(json.dumps(response),content_type="application/json")
        if _img == None or _img == '':
            response['code'] = -1
            response['errorMsg'] = '请上传图片'
            return HttpResponse(json.dumps(response),content_type="application/json")


        try:
            category = Category.objects.get(id = str(_categoryId))
        except ObjectDoesNotExist:
            response['code'] = -1
            response['errorMsg'] = '获取品类失败'
            return HttpResponse(json.dumps(response,ensure_ascii=False),content_type="application/json")

        if category.shop.id != _shopId:
            response['code'] = -1
            response['errorMsg'] = '获取品类失败'
            return HttpResponse(json.dumps(response,ensure_ascii=False),content_type="application/json")
        sku = Sku(name = _name, desc = _desc, img = _img, category = category)
        sku.save()
        response_data = {}
        response_data['skuId'] = sku.id
        response_data['skuName'] = sku.name.encode('utf-8')
        response_data['skuDesc'] = sku.desc.encode('utf-8')
        response_data['skuImg'] = sku.img

        response_sku_size_list = []
        for _skuSize in __skuSizeList:
            response_sku_size = {}
            _sizeName = _skuSize['skuSizeName']
            _price = _skuSize['skuPrice']
            skuSize = SkuSize(sku = sku, name = _sizeName, price = _price)
            skuSize.save()
            response_sku_size['skuSizeId'] = skuSize.id
            response_sku_size['skuSizeName'] = skuSize.name.encode('utf-8')
            response_sku_size['skuPrice'] = str(skuSize.price)
            response_sku_size_list.append(response_sku_size)
        response_data['skuSizeList'] = response_sku_size_list
        response['code'] = 0
        response['data'] = response_data
        return HttpResponse(json.dumps(response),content_type="application/json")

    elif _method == '1':
        _skuId = request.REQUEST.get('skuId')
        _name = request.REQUEST.get('skuName')
        _desc = request.REQUEST.get('skuDesc')
        _img = request.REQUEST.get('skuImg')
        _skuSizeList = request.get('skuSizeList')
        __skuSizeList = json.loads(_skuSizeList)

        if _name == None or _name == '':
            response['code'] = -1
            response['errorMsg'] = '请输入名字'
            return HttpResponse(json.dumps(response),content_type="application/json")
        if _desc == None:
            _desc = ''
        if _img == None or _img == '':
            response['code'] = -1
            response['errorMsg'] = '请上传图片'
            return HttpResponse(json.dumps(response),content_type="application/json")
        if _skuId == None or _skuId == '':
            response['code'] = -1
            response['errorMsg'] = '商品id为空'
            return HttpResponse(json.dumps(response),content_type="application/json")

        try:
            sku = Sku.select_related().objects.get(id = _skuId)
        except ObjectDoesNotExist:
            response['code'] = -1
            response['errorMsg'] = '商品查询失败'
            return HttpResponse(json.dumps(response),content_type="application/json")
        
        sku.name = _name
        sku.desc = _desc
        sku.img = _img
        sku.save()

        response['code'] = 0
        response_data = {}
        response_data['skuId'] = sku.id
        response_data['skuName'] = sku.name.encode('utf-8')
        response_data['skuDesc'] = sku.description.encode('utf-8')
        _img = sku.img.split(',')
        response_data['skuImg'] = _img[0]

        _skuSizeList = sku.skuSize_set.all()
        response_sku_size_list = []
        for skuSize in _skuSizeList:
            response_sku_size = {}
            response_sku_size['skuSizeid'] = skuSize.id
            response_sku_size['skuSizeName'] = skuSize.name.encode('utf-8')
            response_sku_size['skuPrice'] = str(skuSize.price)
            response_sku_size_list.append(response_sku_size)
        response_data['skuSizeList'] = response_sku_size_list
        response['code'] = 0
        response['data'] = response_data
        return HttpResponse(json.dumps(response),content_type="application/json")
    elif _method == '2':
        _skuId = request.REQUEST.get('skuId')
        if _skuId == None or _skuId == '':
            response['code'] = -1
            response['errorMsg'] = '商品id为空'
            return HttpResponse(json.dumps(response),content_type="application/json")

        try:
            sku = Sku.select_related().objects.get(id = _skuId)
        except ObjectDoesNotExist:
            response['code'] = -1
            response['errorMsg'] = '商品查询失败'
            return HttpResponse(json.dumps(response),content_type="application/json")

        response['code'] = 0
        response_data = {}
        response_data['skuId'] = sku.id
        response_data['skuName'] = sku.name.encode('utf-8')
        response_data['skuDesc'] = sku.desc.encode('utf-8')
        _img = sku.img.split(',')
        response_data['skuImg'] = _img[0]
        _skuSizeList = sku.skuSize_set.all()
        response_sku_size_list = []
        for skuSize in _skuSizeList:
            response_sku_size = {}
            response_sku_size['skuSizeId'] = skuSize.id
            response_sku_size['skuSizeName'] = skuSize.name.encode('utf-8')
            response_sku_size['skuSizePrice'] = str(skuSize.price)
            response_sku_size_list.append(response_sku_size)
        response_data['skuSizeList'] = response_sku_size_list
        response['data'] = response_data
        return HttpResponse(json.dumps(response),content_type="application/json")
    elif _method == '3':
        _skuId = request.REQUEST.get('skuId')
        if _skuId == None or _skuId == '':
            response['code'] = -1
            response['errorMsg'] = '商品id为空'
            return HttpResponse(json.dumps(response),content_type="application/json")

        try:
            sku = Sku.select_related().objects.get(id = _skuId)
        except ObjectDoesNotExist:
            response['code'] = -1
            response['errorMsg'] = '商品查询失败'
            return HttpResponse(json.dumps(response),content_type="application/json")
        sku.isValid = False
        sku.save()
        response['code'] = 0
        return HttpResponse(json.dumps(response),content_type="application/json")