def test_stock_min_value(self): p = Product(id="product", name="product", value=30, discount_value=3, stock=-1) try: p.full_clean() except ValidationError as e: self.assertTrue('stock' in e.message_dict)
def test_name_min_length(self): p = Product(id="product", name="pr", value=23, discount_value=2, stock=1) try: p.full_clean() except ValidationError as e: self.assertTrue('name' in e.message_dict)
def test_value_max_value(self): p = Product(id="product", name="product", value=99999.9, discount_value=2, stock=1) try: p.full_clean() except ValidationError as e: self.assertTrue('value' in e.message_dict)
def safe_mutate(cls, root, info, **kwargs): user = info.context.user image = None if kwargs.get('image'): image = Image.objects.get(pk=kwargs.get('image')) kwargs['image'] = image product = Product(**kwargs) product.seller = user product.full_clean() product.save() requests = Request.objects.filter(title=kwargs.get('title')) for req in requests: req.sendNotif(product.id) return cls(product=product)
def post(self, request, *args, **kwargs): data = json.loads(request.body.decode("utf-8")) error_products = [] new_products = [] parse_errors = 0 products = data.get("products") if products: for product in products: e = [] try: new_product = Product(**product) new_product.full_clean() except ValidationError as errors: for error in errors: e.append(error[1][0]) error_products.append({ "product_id": new_product.id, "errors": e }) except Exception: parse_errors += 1 else: new_products.append(new_product) if error_products or parse_errors: return JsonResponse(status=422, data={ "status": "ERROR", "products_report": error_products, "number_of_products_unable_to_parse": parse_errors }, safe=False) else: for new_product in new_products: new_product.save() return JsonResponse(status=200, data={"status": "OK"}) else: return JsonResponse(status=404, data={"error": "No products found"})