示例#1
0
class ProductTest(TestCase):
  def setUp(self):
    self.testCategory = ProductCategory(name="Test Category", icon="category icon path")
    self.testVendor = Vendor(name="Test Vendor", icon="test vendor icon")
    self.testVendor.save()
    self.testProduct = Product(name="Test Product", sku="testproduct", price="100.00", descr="This is a test product", icon="product icon", vendor=self.testVendor)
    self.testCategory.save()
    self.testProduct.save()
    self.testProduct.category.add(self.testCategory)
    self.c = Client()

  def test_model_productCategory_not_none(self):
    self.assertNotEqual(self.testCategory, None)

  def test_model_productCategory_has_id(self):
    self.assertEqual(self.testCategory.id, 1)

  def test_model_productCategory_saved_name(self):
    self.assertEqual(self.testCategory.name, "Test Category")

  def test_model_productCategory_saved_icon(self):
    self.assertEqual(self.testCategory.icon,"category icon path")

  def test_model_productCategory_product_list(self):
    self.assertEqual(self.testCategory.product_set.all()[0].name, "Test Product")

  def test_view_undefined_productCategory_json(self):
    response = self.c.get('/catalog/a/product/category', {'id': 0})
    self.assertEqual(response.content, "{}")

  def test_view_productCategory_json(self):
    response = self.c.get('/catalog/a/product/category', {'id': 1})
    self.assertEqual(response.content, "{\"icon\": \"category icon path\", \"products\": [{\"icon\": \"product icon\", \"vendor\": {\"icon\": \"test vendor icon\", \"id\": 1, \"name\": \"Test Vendor\"}, \"id\": 1, \"name\": \"Test Product\"}], \"id\": 1, \"name\": \"Test Category\"}")
示例#2
0
    def render_to_response(self, context, **response_kwargs):
        obj = context['object']
        request = self.request
        products = self.model.get_products(obj)
        if self.request.is_ajax():
            context['filters'] = Product.get_filters(products)
            products, _ = Product.filter_products(products, request)
        products_count = products.count()
        context['total'] = products_count
        context['tile'] = False
        if products_count:
            try:
                page = int(request.GET.get('page', 1))
            except ValueError:
                page = 1

            pagin = pagination(products, page, 12, count=context['total'])
            products = pagin.items
            context['pagination'] = get_template(
                "includes/pagination.html").render({'PAGIN': pagin})
            if products_count <= 10:
                context['pagination'] = None
            products_template = []
            display_type = request.GET.get('display_type', None)
            if not display_type:
                display_type = request.session.get('display_tile', 'wide')

            template_name = 'catalog/includes/product.html'
            if display_type == 'tile':
                context['display_tile'] = True
                template_name = 'catalog/includes/product_card.html'
                request.session['display_tile'] = 'tile'

            if display_type == 'wide':
                request.session['display_tile'] = 'wide'

            request.session.modified = True

            for product in products:
                products_template.append(
                    get_template(template_name).render({
                        'product': product,
                    }))
            context['products'] = products_template

        if self.request.is_ajax():
            context['template_filters'] = get_template(
                'catalog/includes/filters.html').render({
                    'attribute_list':
                    context['filters'],
                })
            del (context["object"], context["view"], context['category'],
                 context['filters'])
            return self.render_to_json_response(context, **response_kwargs)

        if not self.request.is_ajax():
            del products
            return super().render_to_response(context, **response_kwargs)
示例#3
0
文件: product.py 项目: aeud/ecommerce
def new(request):
    if request.method == 'POST':
        post_name = request.POST.get('name', None)
        p = Product(name=post_name)
        p.save()
        p.index()
        return redirect('apps.editor.controllers.product.index')
    else:
        return render(request, 'editor/product/new.html')
示例#4
0
文件: product.py 项目: aeud/ecommerce
def index(request):
    products = Product.objects.all()
    results = Product.search()
    return render(request, 'editor/product/index.html', {
        'products': products,
        'results': results
    })
示例#5
0
文件: product.py 项目: aeud/ecommerce
def indexJSON(request):
    function_score = json.loads(request.GET.get('function_score', '[]'))
    results = Product.search(query={
        'function_score': function_score
#        'function_score': {
#            'functions': [
#                {
#                    'gauss': {
#                        'product_id': {
#                            'origin': 2,
#                            'scale': 1
#                        }
#                    }
#                }, {
#                    'gauss': {
#                        'product_id': {
#                            'origin': 0,
#                            'scale': 1
#                        }
#                    },
#                    'weight': 0.01
#                }
#            ],
#            'query': {
#                'query_string': {
#                    'query': 'variants.variant_id:3 OR variants.variant_id:2'
#                }
#            },
#            'min_score': 0
#        }
    })
    return HttpResponse(json.dumps(results), content_type="application/json")
示例#6
0
 def setUp(self):
   self.testCategory = ProductCategory(name="Test Category", icon="category icon path")
   self.testVendor = Vendor(name="Test Vendor", icon="test vendor icon")
   self.testVendor.save()
   self.testProduct = Product(name="Test Product", sku="testproduct", price="100.00", descr="This is a test product", icon="product icon", vendor=self.testVendor)
   self.testCategory.save()
   self.testProduct.save()
   self.testProduct.category.add(self.testCategory)
   self.c = Client()
示例#7
0
class VendorTest(TestCase):
  def setUp(self):
    self.testCategory = VendorCategory(name="Test Category", icon="category icon path")
    self.testVendor = Vendor(name="Test Vendor", icon="test vendor icon", descr="Test vendor description", website="test vendor website")
    self.testVendor.save()
    self.testProduct = Product(name="Test Product", sku="testproduct", price="100.00", descr="This is a test product", icon="product icon", vendor=self.testVendor)
    self.testCategory.save()
    self.testProduct.save()
    self.testVendor.category.add(self.testCategory)
    self.c = Client()

  def test_model_vendor_not_none(self):
    self.assertNotEqual(self.testVendor, None)

  def test_model_vendor_has_id(self):
    self.assertEqual(self.testVendor.id, 1)

  def test_model_vendor_saved_name(self):
    self.assertEqual(self.testVendor.name, "Test Vendor")

  def test_model_vendor_saved_descr(self):
    self.assertEqual(self.testVendor.descr, "Test vendor description")

  def test_model_vendor_saved_website(self):
    self.assertEqual(self.testVendor.website, "test vendor website")

  def test_model_vendor_saved_category(self):
    self.assertEqual(self.testVendor.category.all()[0].name, "Test Category")

  def test_model_vendor_product_list(self):
    self.assertEqual(self.testVendor.product_set.all()[0].name, "Test Product")

  def test_view_undefined_vendor_json(self):
    response = self.c.get('/catalog/a/vendor', {'id': 0})
    self.assertEqual(response.content, "{}")

  def test_view_vendor_json(self):
    response = self.c.get('/catalog/a/vendor', {'id': 1})
    self.assertEqual(response.content, "{\"website\": \"test vendor website\", \"description\": \"Test vendor description\", \"name\": \"Test Vendor\", \"products\": [{\"icon\": \"product icon\", \"id\": 1, \"name\": \"Test Product\"}], \"id\": 1, \"icon\": \"test vendor icon\"}")
示例#8
0
    sys.path.append(BASE_DIR)
    sys.path.append(PROJECT_DIR)

# assumes that the entire project is inside the virtualenv directory
site.addsitedir(BASE_DIR + '/../lib/python2.7/site-packages')

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "ninshirts.settings")

from django.core.files.base import ContentFile
from apps.catalog.models import Product, Option

zf = zipfile.ZipFile(sys.argv[1])
products = {}
for file in sorted(zf.namelist()):
    name, color, order, ext = file.split('.')
    if name not in products:
        products[name] = {'color': color, 'images': [file]}
    else:
        products[name]['images'].append(file)

for k, v in products.items():
    product = Product(name=k, tag=k)
    product.save()
    color = Option.objects.get(abbreviation=v['color'])
    product.options.add(color)
    for file in v['images']:
        img = product.productimage_set.create()
        content = ContentFile(zf.read(file))
        img.original.save(file, content)

zf.close()
示例#9
0
文件: map_es.py 项目: aeud/ecommerce
def run():
    Product.es_map()
示例#10
0
    sys.path.append(PROJECT_DIR)

# assumes that the entire project is inside the virtualenv directory
site.addsitedir(BASE_DIR + '/../lib/python2.7/site-packages')

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "ninshirts.settings")


from django.core.files.base import ContentFile
from apps.catalog.models import Product, Option

zf = zipfile.ZipFile(sys.argv[1])
products = {}
for file in sorted(zf.namelist()):
    name, color, order, ext = file.split('.')
    if name not in products:
        products[name] = {'color': color, 'images': [file]}
    else:
        products[name]['images'].append(file)

for k,v in products.items():
    product = Product(name=k, tag=k)
    product.save()
    color = Option.objects.get(abbreviation=v['color'])
    product.options.add(color)
    for file in v['images']:
        img = product.productimage_set.create()
        content = ContentFile(zf.read(file))
        img.original.save(file, content)

zf.close()