示例#1
0
def create_brand(request):
    if request.method == 'POST':
        model = simplejson.loads(request.data)
        brand = Brand(
            title=model['title'])
        brand.put()
        return render_json_response(brand.to_json())
示例#2
0
def get_brand(request, brand_key):

    if request.method == 'GET':
        brand = Brand.get_by_id(brand_key)
        if not brand:
            data = {'api_success': False, 'api_msg': 'Brand %s not found' % brand_key}
            return render_json_response(data)
        return render_json_response(brand.to_json())

    if request.method == 'PUT':
        model = simplejson.loads(request.data)
        brand = Brand.get_by_id(brand_key)
        if not brand:
            data = {'api_success': False, 'api_msg': 'Brand %s not found' % brand_key}
            return render_json_response(data)
        brand.title = model['title']
        brand.available = model['available']
        brand.toggle_available()
        brand.put()
        data = {'api_success': True, 'api_msg': 'Brand %s updated' % brand_key}
        return render_json_response(data)

    if request.method == 'DELETE':
        brand = Brand.get_by_id(brand_key)
        if not brand:
            data = {'api_success': False, 'api_msg': 'Brand %s not found' % brand_key}
            return render_json_response(data)
        brand.pre_delete()
        brand.key.delete()
        data = {'api_success': True, 'api_msg': 'Brand %s deleted' % brand_key}
        return render_json_response(data)
示例#3
0
文件: brand.py 项目: gmist/1businka
def create(data, is_unique_title=False):
    try:
        title = data.get('title', '')
        if not title:
            res = {'api_success':False,
                 'api_msg':'"title" field is required',
                 'api_function':create.__name__}
            logging.warning(res)
            return render_json_response(res)
        if is_unique_title and is_brand_exist(title):
            res = {'api_success':False,
                 'api_msg':'Field "title" must be unique',
                 'api_function':create.__name__}
            logging.warning(res)
            return render_json_response(res)

        available = data.get('available', False)
        brand = Brand.create_new_entity(title=title,
                available=available)

        res = {'api_success': True,
                    'api_msg': 'Brand created',
                    'brand': brand.to_json()}
        logging.info(res)
        return render_json_response(res)
    except Exception,e:
        res = {'api_success': False,
               'api_msg': str(e),
               'api_function':create.__name__}
        logging.error(res)
        return render_json_response(res)
示例#4
0
def filter_brand(request):
    is_available = bool(request.values.get('available', False))
    brand_key = request.values.get('brand')
    if brand_key:
        brand = Brand.get_by_id(int(brand_key))
        products = Product.query().filter(Product.brand == brand.key).filter(Product.available == is_available)
    else:
        products = Product.query().filter(Product.brand == None).filter(Product.available == is_available)

    return render_to_response('shop/admin/product_filter.html', {
        'products':products,
        'available':is_available
    })
示例#5
0
def get_brand_image(request, brand_key, image_key):
    if request.method == 'DELETE':
        brand = Brand.get_by_id(brand_key)
        if brand:
            for image in brand.images:
                if image.uid == image_key:
                    image.pre_delete()
                    brand.images.remove(image)
                    brand.put()
                    data = {'api_success': True,
                            'api_msg': 'BrandImage %s deleted' % image_key}
                    return render_json_response(data)

        data = {'api_success': False,
                'api_msg': 'BrandImage %s not found' % image_key}
        return render_json_response(data)
示例#6
0
def product(request):
    all_product = Product.available_list()
    all_hide_product = Product.not_available_list()

    available_product_without_properties_count = 0
    for pr in all_product:
        if  not len(pr.properties_):
            available_product_without_properties_count += 1


    unavailable_product_without_properties_count = 0
    for pr in all_hide_product:
        if not len(pr.properties_):
            unavailable_product_without_properties_count += 1

    brand_all_available_product = Product.available_list().filter(Product.brand != None)
    logging.warning(brand_all_available_product.count())
    brand_all_not_available_product = Product.not_available_list().filter(Product.brand != None)

    no_brand_all_available_product_count = Product.available_list().filter(Product.brand == None).count()
    no_brand_all_not_available_product_count = Product.not_available_list().filter(Product.brand == None).count()

    available_brand = {}
    for product_ in brand_all_available_product:
        if not product_.brand in available_brand:
            count = Product.available_list().filter(Product.brand == product_.brand).count()
            available_brand[product_.brand] = count

    not_available_brand = {}
    for product_ in brand_all_not_available_product:
        if not product_.brand in not_available_brand:
            count = Product.not_available_list().filter(Product.brand == product_.brand).count()
            not_available_brand[product_.brand] = count

    categories = Category.query(Category.parent_category == None).order(Category.title)
    properties = ProductProperty.query().order(ProductProperty.title)
    brands = Brand.query().order(Brand.title)

    available_uncategories = all_product
    available_uncategories = available_uncategories.filter(Product.category == None)
    unavailable_uncategories = all_hide_product
    unavailable_uncategories = unavailable_uncategories.filter(Product.category == None)
    return render_to_response('shop/admin/product.html',
        locals()
    )
示例#7
0
def brand_add_image(request, brand_key):
    if request.method == 'POST':
        try:
            brand = Brand.get_by_id (brand_key)
            img = Image.create_from_data(
                blob_data=request.files['file'].read(),
                format=request.files['file'].content_type)
            if img.get_cached_url():
                brand.images.append(img)
                brand.put()
                return render_json_response({'api_success': True,
                                         'api_msg': 'BrandImage created'})
            else:
                return render_json_response(
                            {'api_success':False,
                             'api_msg':'Invalid image'})
        except apiproxy_errors.RequestTooLargeError:
            return render_json_response({'api_success': False,
                             'api_msg': u'Файл слишком большой. '
                                        u'Размер файла должен быть мешьше 1 мегабайта.'})
        except Exception, e:
            return render_json_response({'api_success': False,
                                         'api_msg': str(e)})
示例#8
0
def get_brands(request):
    brands = [brand.to_json() for brand in Brand.query()]
    return render_json_response(brands)
示例#9
0
def get_brand_images(request, brand_key):
    brand = Brand.get_by_id(brand_key)
    images = [image.to_json() for image in brand.images]
    return render_json_response(images)
示例#10
0
文件: product.py 项目: gmist/1businka
def create(data):
    id_1c = data.get('id_1c', '')
    if id_1c and is_product_exist_id_1c(id_1c):
        res = {'api_success':False,
             'api_msg':'Field "id_1c" must be unique',
             'api_function':create.__name__}
        logging.warning(res)
        return render_json_response(res)
    title = data.get('title','')
    if not title:
        res = {'api_success':False,
             'api_msg':'"title" field is required',
             'api_function':create.__name__}
        logging.warning(res)
        return render_json_response(res)

    original_title = data.get('original_title', '')
    short_description = data.get('short_description', '')

    rating = int(data.get('rating', 0))
    leftovers = int(data.get('leftovers',0))
    price = data.get('price', 0.0)
    if price:
        if isinstance(price, unicode):
            price = price.replace(",",'.')
    price = float(price)

    available = data.get('available', False)

    category = data.get('category', None)
    if category:
        category_id = category.get('id', None)
        category = Category.get_by_id(category_id).key

    brand = data.get('brand', None)
    if brand:
        brand_id = brand.get('id', None)
        brand = Brand.get_by_id(brand_id).key

    properties_raw = data.get('properties_', None)
    properties_ = []
    if properties_raw:
        properties_raw = [prop.get('id', None) for prop in properties_raw]
        for prop in properties_raw:
            if prop:
                prop = ProductPropertyValue.get_by_id(prop)
                if prop:
                    properties_.append(prop.key)

    product = Product(
        title=title,
        id_1c=id_1c,
        original_title=original_title,
        short_description=short_description,
        brand=brand,
        category=category,
        price=price,
        available=available,
        leftovers=leftovers,
        rating=rating)
    if properties_:
        for prop in properties_:
            product.properties_.append(prop)
    product.put()
    res = {'api_success': True,
                'api_msg': 'Product created',
                'product': product.to_json()}
    logging.info(res)
    return render_json_response(res)
示例#11
0
文件: brand.py 项目: gmist/1businka
def is_brand_exist(title):
    category = Brand.all().filter('title =', title)
    if category.count():
        return True
    return False
示例#12
0
                            category = category_obj.key
                        else:
                            category = None
                else:
                    category = product_obj.category
            else:
                category = None
            product_obj.category = category


            brand = model.get('brand', None)
            if brand:
                brand = brand.get('id', None)
                if not product_obj.brand or (product_obj.brand and product_obj.brand.id() != brand):
                    if brand:
                        brand_obj = Brand.get_by_id(brand)
                        if brand_obj:
                            brand = brand_obj.key
                        else:
                            brand = None
                else:
                    brand = product_obj.brand
            else:
                brand = None
            product_obj.brand = brand


            properties_ = model.get('properties', None)
            if properties_:
                properties_ = [prop.get('id', None) for prop in properties_]
                for prop in product_obj.properties_:
示例#13
0
文件: views.py 项目: gmist/1businka
def brands_json(request):
    brands = [brand.to_json() for brand in Brand.query().order(Brand.title)]
    return render_json_response(brands)