def test_get_substitutes(self, mock_urllib_request_urlopen): """ test get_substitutes function of api manager """ mock_urllib_request_urlopen.side_effect = side_effect product = ApiManager.get_product("3029330003458") substitutes = ApiManager.get_substitutes(product) self.assertIsNotNone(product) self.assertEqual(len(substitutes), 9)
def index(request): """render index page""" if request.method == "POST": navbar_search_form = SearchForm(request.POST, prefix="navbar") head_search_form = SearchForm(request.POST, prefix="head") valid = False search_post = None if navbar_search_form.is_valid(): valid = True search_post = navbar_search_form.cleaned_data['search'] head_search_form = SearchForm(prefix="head") elif head_search_form.is_valid(): valid = True search_post = head_search_form.cleaned_data['search'] navbar_search_form = SearchForm(prefix="navbar") if valid: products = ApiManager.do_research(search_post) return render( request, 'purbeurre/search.html', { 'search': search_post, 'products': products, 'navbar_search_form': navbar_search_form, }) else: navbar_search_form = SearchForm(prefix="navbar") head_search_form = SearchForm(prefix="head") context = { 'navbar_search_form': navbar_search_form, 'head_search_form': head_search_form } return render(request, 'purbeurre/index.html', context)
def test_save_product(self, mock_urllib_request_urlopen): """ test save product function of database manager """ mock_urllib_request_urlopen.side_effect = side_effect product = ApiManager.get_product("3029330003458") substitute = ApiManager.get_product("3029330003533") product_count = Product.objects.count() product_substitute_product = \ ProductSubstituteProduct.objects.filter(users=self.user).count() DatabaseManager.save_product(self.user, product, (substitute, )) product_count_new = Product.objects.count() product_substitute_product_new = \ ProductSubstituteProduct.objects.filter(users=self.user).count() self.assertEqual(product_count, product_count_new - 2) self.assertEqual(product_substitute_product, product_substitute_product_new - 1)
def get_substitutes(request, bar_code): """ render substitutes page """ product = ApiManager.get_product(bar_code) substitutes = ApiManager.get_substitutes(product) navbar_search_form = SearchForm(prefix="navbar") sign = signing.dumps({ 'product': bar_code, 'substitutes': list(substitute['code'] for substitute in substitutes) }) return render( request, 'purbeurre/substitutes.html', { 'product': product, 'substitutes': substitutes, 'navbar_search_form': navbar_search_form, 'sign': sign })
def test_wash_product(self, mock_urllib_request_urlopen): """ test wash_product function of api manager """ mock_urllib_request_urlopen.side_effect = side_effect product = ApiManager.get_product("3029330003458") washed_product = ApiManager.wash_product(product) for category in washed_product.get('categories_hierarchy', ()): self.assertNotIn(':', category) self.assertIsInstance(washed_product.get('categories'), (list, tuple)) for category in washed_product.get('categories'): self.assertNotIn(':', category) self.assertIsInstance(washed_product.get('ingredients_text_fr', ()), (tuple, types.GeneratorType)) if not washed_product.get('ingredients_text_fr', None): self.assertIsInstance(washed_product.get('ingredients', ()), (tuple, types.GeneratorType))
def test_get_product(self, mock_urllib_request_urlopen): """ test get_product function of api manager """ mock_urllib_request_urlopen.side_effect = side_effect product = ApiManager.get_product("3029330003458") self.assertIsNotNone(product)
def test_do_research(self, mock_urllib_request_urlopen): """ test do_research function of api manager """ mock_urllib_request_urlopen.side_effect = side_effect products = ApiManager.do_research('nutella') self.assertEqual(len(products), 9)
def handle(self, *args, **options): with transaction.atomic(): products_db = Product.objects.all() product_count = products_db.count() self.stdout.write(self.style.SUCCESS( "il y a " + str(product_count) + " produits." )) for i, product_db in enumerate(products_db, start=1): product_from_api = ApiManager.get_product(product_db.bar_code, with_clean=True) nutriments = product_from_api.get('nutriments', {}) product_db.name = product_from_api.get('product_name', None) product_db.generic_name = product_from_api.get('generic_name', None) product_db.nutrition_grades = product_from_api.get( 'nutrition_grades', None) product_db.fat = str(nutriments.get('fat_100g', None)) product_db.saturated_fat = str( nutriments.get('saturated-fat_100g', None)) product_db.sugars = str(nutriments.get('sugars_100g', None)) product_db.salt = str(nutriments.get('salt_100g', None)) product_db.image_url = product_from_api.get('image_url', None) product_db.save() categories, categories_temoin = [], [] categories_of_product = Category.objects.filter( product=product_db) for category in product_from_api.get('categories', ()): category_db, created = Category.objects.get_or_create( name=category) if category_db not in categories_of_product: categories.append(category_db) categories_temoin.append(category_db) product_db.categories.add(*categories) product_db.categories.remove( *tuple( set(categories_of_product).union(set(categories_temoin)) - set(categories_temoin))) iteration_ingredients = () if product_from_api.get('ingredients_text_fr', ()): iteration_ingredients = product_from_api \ .get('ingredients_text_fr') elif product_from_api.get('ingredients', ()): iteration_ingredients = product_from_api.get('ingredients') ingredients, ingredients_temoin = [], [] ingredients_of_product = Ingredient.objects.filter( product=product_db) for ingredient in iteration_ingredients: ingredient_db, created = Ingredient.objects.get_or_create( name=ingredient) if ingredient_db not in ingredients_of_product: ingredients.append(ingredient_db) ingredients_temoin.append(ingredient_db) product_db.ingredients.add(*ingredients) product_db.ingredients.remove( *tuple( set(ingredients_of_product).union( set(ingredients_temoin)) - set(ingredients_temoin))) brands, brands_temoin = [], [] brands_of_product = Brand.objects.filter(product=product_db) for brand in product_from_api.get('brands_tags', ()): brand_db, created = Brand.objects.get_or_create(name=brand) if brand_db not in brands_of_product: brands.append(brand_db) brands_temoin.append(brand_db) product_db.brands.add(*brands) product_db.brands.remove( *tuple(set(brands_of_product).union(set(brands_temoin)) - set(brands_temoin))) stores, stores_temoin = [], [] stores_of_product = Store.objects.filter(product=product_db) for store in product_from_api.get('stores_tags', ()): store_db, created = Store.objects.get_or_create(name=store) if store_db not in stores_of_product: brands.append(store_db) stores_temoin.append(store_db) product_db.stores.add(*stores) product_db.brands.remove( *tuple(set(stores_of_product).union(set(stores_temoin)) - set(stores_temoin))) self.stdout.write(self.style.SUCCESS( str(round(i / product_count * 100, 2)) + "% avec " + str(len(connection.queries)) + " requêtes." )) reset_queries()
def create_substitute_link(request, sign, substitute_bar_code): """ render the creating link beetween product """ try: _dict = signing.loads(sign) except signing.BadSignature: raise Exception('Altération détectée !') if substitute_bar_code not in _dict['substitutes']: raise Exception('Mauvaise requête !') do_save = True do_save_p_s_p = True urllib_product = True urllib_substitute = True product, substitute = None, None try: product = Product.objects.get(bar_code=_dict['product']) urllib_product = False except Product.DoesNotExist: pass try: substitute = Product.objects.get(bar_code=substitute_bar_code) urllib_substitute = False except Product.DoesNotExist: pass if not urllib_product and not urllib_substitute: do_save = False p_s_p_db = ProductSubstituteProduct.objects.filter( users=request.user, from_product=product, to_product=substitute) if p_s_p_db.exists(): do_save_p_s_p = False messages.success(request, 'Substitut déjà sauvergardé' ' pour ce produit !') if do_save or do_save_p_s_p: if do_save: if urllib_product: product = ApiManager.get_product(_dict['product'], with_clean=True) if not product: raise Exception('Produit non existant.') if urllib_substitute: substitute = ApiManager.get_product(substitute_bar_code, with_clean=True) if not substitute: raise Exception('Substitut non existant.') if (urllib_product and not urllib_substitute) or \ (not urllib_product and urllib_substitute): if urllib_product and not urllib_substitute: product = DatabaseManager.save_product( request.user, product, None) else: substitute = DatabaseManager.save_product( request.user, substitute, None) DatabaseManager.save_link_p_s_p(request.user, product, substitute) else: DatabaseManager.save_product(request.user, product, (substitute, )) else: DatabaseManager.save_link_p_s_p(request.user, product, substitute) messages.success(request, 'Substitut sauvegardé !') navbar_search_form = SearchForm(prefix="navbar") return render( request, 'purbeurre/create_substitute_link.html', { 'product': product, 'substitute': substitute, 'navbar_search_form': navbar_search_form })