def post(self, request, reference): if 'price' not in request.data.keys(): raise ValidationError('Price must be specified in POST field') product = ProductRecords.get_by_reference(reference) if product is None: return Response(display_product_json( ProductRecords.create(reference, request.data['price'])), status=HTTP_201_CREATED) else: return Response(display_product_json(product))
def display_cart_json(cart, parity=1): content = [] for ref in cart.bought_products.keys(): elem = display_product_json(cart.bought_products[ref], parity) elem['number'] = cart.product_number[ref] content.append(elem) return {'content': content, 'value': cart.total_value(parity)}
def get(self, request, reference): product = ProductRecords.get_by_reference(reference) if product is None: raise NotFound(detail='No product with this reference') display_param = request.query_params.get('display_format', None) if display_param == 'xml': return Response(display_product_xml(product)) return Response(display_product_json(product))
def add_products(request, reference): if 'number' not in request.data.keys(): raise ValidationError('Number is missing') product = fill_available_products( ProductRecords.get_by_reference(reference), int(request.data['number'])) ProductRecords.update(reference, product) return Response(display_product_json(product))
def test_json(self): p = Product('AAA100', 100, 10, 10) self.assertEqual(display_product_json(p), { 'reference': 'AAA100', 'price': 100 })