예제 #1
0
    def get(self, request, department='', category=''):
        path = request.path
        search = json.loads(request.GET['search'])
        words = search['value'].split(' ')
        q = Q()
        q1 = Q()
        context = {}
        wordsAccepted = []
        wordsIgnored = []
        if len(words) > 0:
            if search['all_words']:
                for word in words:
                    q &= Q(name__contains=word) | Q(description__contains=word)
                    q1 &= Q(product__name__icontains=word) | Q(
                        product__description__icontains=word)
            else:
                for word in words:
                    if Product.objects.filter(
                            Q(name__icontains=word)
                            | Q(description__icontains=word)).exists():
                        wordsAccepted.append(word)
                    else:
                        wordsIgnored.append(word)
                    q |= Q(name__contains=word) | Q(description__contains=word)
                    q1 |= Q(product__name__icontains=word) | Q(
                        product__description__icontains=word)
                context['wordsAccepted'] = wordsAccepted
                context['wordsIgnored'] = wordsIgnored

        if len(words) == 0 and cache.get(path):
            products = cache.get(path)
        else:
            if department == '' and category == '':
                products = Product.objects.filter(q)
            elif category != '':
                product_categories = ProductCategory.objects.filter(
                    Q(category__name=category), q1)
                products = []
                for product_category in product_categories:
                    products.append(product_category.product)
            elif department != '':
                product_categories = ProductCategory.objects.filter(
                    Q(category__department__name=department), q1)
                products = []
                for product_category in product_categories:
                    products.append(product_category.product)
                cache.set(path, products, product_chache_time)

        page = self.paginate_queryset(products)
        if page is not None:
            serializer = ProductSerializer(page, many=True)
            return self.get_paginated_response(serializer.data, wordsAccepted,
                                               wordsIgnored)
        else:
            serializer = ProductSerializer(products, many=True)
            return FakePaginationData(serializer.data, wordsAccepted,
                                      wordsIgnored)
예제 #2
0
 def add_item_to_cart(self, product):
     p = ProductSerializer(product).data
     attributes = {}
     for attribute in p['attributes']:
         attributes[attribute] = p['attributes'][attribute][0]
     data = {'product': p, 'attributes': attributes, 'buy_now': 1}
     return self.client.post(reverse('cart'), data, format='json')
예제 #3
0
 def get(self, request, product_id):
   cache_key = product_cache_key(product_id)
   if cache.get(cache_key):
     data = cache.get(cache_key)
   else:
     try:
       product = Product.objects.get(id=product_id)
     except:
       raise ValidationError('Product does not exist')
     data = ProductSerializer(product).data
     cache.set(cache_key, data)
   return Response(data)
예제 #4
0
 def test_add_item_with_different_attributes_increase_items(self):
     response = self.add_item_to_cart(self.p1)
     response = self.client.get(self.cart_url)
     self.assertEqual(len(response.data), 1)
     p = ProductSerializer(self.p1).data
     attributes = {}
     for attribute in p['attributes']:
         if len(p['attributes'][attribute]) > 1:
             attributes[attribute] = p['attributes'][attribute][1]
         else:
             attributes[attribute] = p['attributes'][attribute][0]
     data = {'product': p, 'attributes': attributes, 'buy_now': 1}
     response = self.client.post(self.cart_url, data, format='json')
     response = self.add_item_to_cart(self.p1)
     response = self.client.get(self.cart_url)
     self.assertEqual(len(response.data), 2)
예제 #5
0
 def get(self, request, product_id):
     try:
         product = Product.objects.get(id=product_id)
         return Response(ProductSerializer(product).data)
     except:
         raise ValidationError('Product does not exist')