def get(self, request, id_category, type_fetched, osm_name = 'monoprix', osm_type='shipping', osm_location=None): category = self.get_object(id_category) global_keys = globals().keys() serialized = None # Brands filter parameter brands = request.QUERY_PARAMS.getlist('brands[]') # Ordering price_ordering = 0 if 'filter' in request.QUERY_PARAMS: price_ordering = int(request.QUERY_PARAMS['filter']) # Pagination arguments if 'PRODUCTS_PER_PAGE' in request.GET: CategoryProducts.PRODUCTS_PER_PAGE = request.QUERY_PARAMS.get('PRODUCTS_PER_PAGE') if 'page' in request.GET: page = request.QUERY_PARAMS.get('page') else: page = CategoryProducts.PAGE # Getting query from api helper class products_query_set = ApiHelper.get_products_query_set(category, type_fetched, osm_name, osm_type, osm_location, brands) if products_query_set is not None: # Price ordering if price_ordering == 1: # ordering by price products_query_set = sorted(products_query_set, key =(lambda p: p.history_set.all()[0].price if p.history_set.all().exists() else 0)) elif price_ordering == 2: # ordering by unit price products_query_set = sorted(products_query_set, key =(lambda p: p.history_set.all()[0].unit_price if p.history_set.all().exists() else 0)) # Generating pagination object paginator = Paginator(products_query_set, CategoryProducts.PRODUCTS_PER_PAGE) try: products = paginator.page(page) except PageNotAnInteger: # If page is not an integer, deliver first page. products = paginator.page(1) except EmptyPage: # If page is out of range (e.g. 9999), # deliver last page of results. products = paginator.page(paginator.num_pages) serializer_class_name = '%sProductsPaginationSerializer'%osm_name.capitalize() serializer = None if serializer_class_name in global_keys: cart = getattr(request.cart_controller.metacart, osm_name+'_cart') context = {'osm': {'name':osm_name,'type': osm_type, 'location':osm_location}, 'time':datetime.now(), 'cart': cart, 'type': type_fetched} Serializer_class = globals()[serializer_class_name] serializer = Serializer_class(products, context = context) if serializer is None: return Response(404, status=status.HTTP_400_BAD_REQUEST) else: response = serializer.data return response
def category_promotion_agregate(category = None, osm_name = 'monoprix', osm_type='shipping', osm_location=None): """ Return pomotions summary for a category that is not a leaf but is a direct parent to a leaf category. """ query_set = ApiHelper.get_products_query_set(category, type_fetched = 'promotions', osm_name = osm_name , osm_type=osm_type, osm_location=osm_location) promotions = { 'id': category.id, # Putting parent catgory as id for promotions 'name': 'promotions', 'leave': True, 'parent_category': category.id, 'position': 0, 'subs': [], 'brands': {'count':0, 'content':[]}, 'url': category.url+"/promotions" } # Removing duplicates products = query_set.all() promotions['count']= len(products) # Now getting brands information promotions['brands']['content'] = set([ p.brand.brandmatch_set.all()[0].dalliz_brand for p in list(products) if p.brand is not None and p.brand.brandmatch_set.all().count()==1]) promotions['brands']['count'] = len(promotions['brands']['content']) # Serializing brands promotions['brands']['content'] = BrandSerializer(promotions['brands']['content'], many = True).data return promotions
def all(category = None, leaves = False, osm_name = 'monoprix', osm_type='shipping', osm_location=None): """ This class method return all category architecture. """ categories = CategorySerializer.get_subs_dalliz(category) serialized = [] if category is not None and category.parent_category is not None and category.category_set.all().count()>0 and leaves: # Adding promotion category here serialized.append(CategorySerializer.category_promotion_agregate(category = category, osm_name = osm_name, osm_type=osm_type, osm_location=osm_location)) for c in categories: data = CategorySerializer(c).data data.update({ 'subs': CategorySerializer.all(c) }) if len(data['subs'])>0: data['leave'] = False else: data['leave'] = True if leaves: products = ApiHelper.get_products_query_set(c, type_fetched = 'products', osm_name = osm_name , osm_type=osm_type, osm_location=osm_location) products_count = products.count() # Adding total count of products in category # Now getting brands information brands = set([ p.brand.brandmatch_set.all()[0].dalliz_brand for p in products[:] if p.brand is not None and p.brand.brandmatch_set.all().count()==1]) brands_count = len(brands) # Updating data data['count'] = products_count data['brands'] = {'count': brands_count, 'content': BrandSerializer(brands, many = True).data} serialized.append(data) return serialized
def all(category=None, leaves=False, osm_name='monoprix', osm_type='shipping', osm_location=None): """ This class method return all category architecture. """ categories = CategorySerializer.get_subs_dalliz(category) serialized = [] if category is not None and category.parent_category is not None and category.category_set.all( ).count() > 0 and leaves: # Adding promotion category here serialized.append( CategorySerializer.category_promotion_agregate( category=category, osm_name=osm_name, osm_type=osm_type, osm_location=osm_location)) for c in categories: data = CategorySerializer(c).data data.update({'subs': CategorySerializer.all(c)}) if len(data['subs']) > 0: data['leave'] = False else: data['leave'] = True if leaves: products = ApiHelper.get_products_query_set( c, type_fetched='products', osm_name=osm_name, osm_type=osm_type, osm_location=osm_location) products_count = products.count( ) # Adding total count of products in category # Now getting brands information brands = set([ p.brand.brandmatch_set.all()[0].dalliz_brand for p in products[:] if p.brand is not None and p.brand.brandmatch_set.all().count() == 1 ]) brands_count = len(brands) # Updating data data['count'] = products_count data['brands'] = { 'count': brands_count, 'content': BrandSerializer(brands, many=True).data } serialized.append(data) return serialized
def category_promotion_agregate(category=None, osm_name='monoprix', osm_type='shipping', osm_location=None): """ Return pomotions summary for a category that is not a leaf but is a direct parent to a leaf category. """ query_set = ApiHelper.get_products_query_set(category, type_fetched='promotions', osm_name=osm_name, osm_type=osm_type, osm_location=osm_location) promotions = { 'id': category.id, # Putting parent catgory as id for promotions 'name': 'promotions', 'leave': True, 'parent_category': category.id, 'position': 0, 'subs': [], 'brands': { 'count': 0, 'content': [] }, 'url': category.url + "/promotions" } # Removing duplicates products = query_set.all() promotions['count'] = len(products) # Now getting brands information promotions['brands']['content'] = set([ p.brand.brandmatch_set.all()[0].dalliz_brand for p in list(products) if p.brand is not None and p.brand.brandmatch_set.all().count() == 1 ]) promotions['brands']['count'] = len(promotions['brands']['content']) # Serializing brands promotions['brands']['content'] = BrandSerializer( promotions['brands']['content'], many=True).data return promotions
def get(self, request, id_category, type_fetched, osm_name='monoprix', osm_type='shipping', osm_location=None): category = self.get_object(id_category) global_keys = globals().keys() serialized = None # Brands filter parameter brands = request.QUERY_PARAMS.getlist('brands[]') # Ordering price_ordering = 0 if 'filter' in request.QUERY_PARAMS: price_ordering = int(request.QUERY_PARAMS['filter']) # Pagination arguments if 'PRODUCTS_PER_PAGE' in request.GET: CategoryProducts.PRODUCTS_PER_PAGE = request.QUERY_PARAMS.get( 'PRODUCTS_PER_PAGE') if 'page' in request.GET: page = request.QUERY_PARAMS.get('page') else: page = CategoryProducts.PAGE # Getting query from api helper class products_query_set = ApiHelper.get_products_query_set( category, type_fetched, osm_name, osm_type, osm_location, brands) if products_query_set is not None: # Price ordering if price_ordering == 1: # ordering by price products_query_set = sorted( products_query_set, key=(lambda p: p.history_set.all()[0].price if p.history_set.all().exists() else 0)) elif price_ordering == 2: # ordering by unit price products_query_set = sorted( products_query_set, key=(lambda p: p.history_set.all()[0].unit_price if p.history_set.all().exists() else 0)) # Generating pagination object paginator = Paginator(products_query_set, CategoryProducts.PRODUCTS_PER_PAGE) try: products = paginator.page(page) except PageNotAnInteger: # If page is not an integer, deliver first page. products = paginator.page(1) except EmptyPage: # If page is out of range (e.g. 9999), # deliver last page of results. products = paginator.page(paginator.num_pages) serializer_class_name = '%sProductsPaginationSerializer' % osm_name.capitalize( ) serializer = None if serializer_class_name in global_keys: cart = getattr(request.cart_controller.metacart, osm_name + '_cart') context = { 'osm': { 'name': osm_name, 'type': osm_type, 'location': osm_location }, 'time': datetime.now(), 'cart': cart, 'type': type_fetched } Serializer_class = globals()[serializer_class_name] serializer = Serializer_class(products, context=context) if serializer is None: return Response(404, status=status.HTTP_400_BAD_REQUEST) else: response = serializer.data return response