def post(self, store): data = self.get_json() if 'product' not in data: abort(400, 'There is no product data on payload') sellable_id = data.get('sellable_id') barcode = data.get('barcode') description = data.get('description') base_price = self._price_validation(data) if sellable_id and store.get(Sellable, sellable_id): abort(400, 'Product with this id already exists') if barcode and store.find(Sellable, barcode=barcode): abort(400, 'Product with this barcode already exists') sellable = Sellable(store=store) if sellable_id: sellable.id = sellable_id sellable.code = barcode sellable.barcode = barcode sellable.description = description # FIXME The sellable is created with STATUS_CLOSED because we need the taxes info # to start selling so this is just a temporary sellable just to save it on the # database so the override can be created sellable.status = Sellable.STATUS_CLOSED sellable.base_price = base_price product_data = data.get('product') product = Product(store=store, sellable=sellable) product.manage_stock = product_data.get('manage_stock', False) return make_response( jsonify({ 'message': 'Product created', 'data': { 'id': sellable.id, 'barcode': sellable.barcode, 'description': sellable.description, 'status': sellable.status, } }), 201)
def testCreateEvent(self): store_list = [] p_data = _ProductEventData() ProductCreateEvent.connect(p_data.on_create) ProductEditEvent.connect(p_data.on_edit) ProductRemoveEvent.connect(p_data.on_delete) try: # Test product being created store = new_store() store_list.append(store) sellable = Sellable( store=store, description=u'Test 1234', price=Decimal(2), ) product = Product( store=store, sellable=sellable, ) store.commit() self.assertTrue(p_data.was_created) self.assertFalse(p_data.was_edited) self.assertFalse(p_data.was_deleted) self.assertEqual(p_data.product, product) p_data.reset() # Test product being edited and emmiting the event just once store = new_store() store_list.append(store) sellable = store.fetch(sellable) product = store.fetch(product) sellable.notes = u'Notes' sellable.description = u'Test 666' product.weight = Decimal(10) store.commit() self.assertTrue(p_data.was_edited) self.assertFalse(p_data.was_created) self.assertFalse(p_data.was_deleted) self.assertEqual(p_data.product, product) self.assertEqual(p_data.emmit_count, 1) p_data.reset() # Test product being edited, editing Sellable store = new_store() store_list.append(store) sellable = store.fetch(sellable) product = store.fetch(product) sellable.notes = u'Notes for test' store.commit() self.assertTrue(p_data.was_edited) self.assertFalse(p_data.was_created) self.assertFalse(p_data.was_deleted) self.assertEqual(p_data.product, product) self.assertEqual(p_data.emmit_count, 1) p_data.reset() # Test product being edited, editing Product itself store = new_store() store_list.append(store) sellable = store.fetch(sellable) product = store.fetch(product) product.weight = Decimal(1) store.commit() self.assertTrue(p_data.was_edited) self.assertFalse(p_data.was_created) self.assertFalse(p_data.was_deleted) self.assertEqual(p_data.product, product) self.assertEqual(p_data.emmit_count, 1) p_data.reset() # Test product being edited, editing Product itself store = new_store() store_list.append(store) sellable = store.fetch(sellable) product = store.fetch(product) product.weight = Decimal(1) store.commit() #self.assertTrue(p_data.was_edited) self.assertFalse(p_data.was_created) self.assertFalse(p_data.was_deleted) #self.assertEqual(p_data.product, product) #self.assertEqual(p_data.emmit_count, 1) p_data.reset() finally: # Test product being removed store = new_store() store_list.append(store) sellable = store.fetch(sellable) product = store.fetch(product) sellable.remove() store.commit() self.assertTrue(p_data.was_deleted) self.assertFalse(p_data.was_created) self.assertFalse(p_data.was_edited) self.assertEqual(p_data.product, product) self.assertEqual(p_data.emmit_count, 1) p_data.reset() for store in store_list: store.close()
def post(self, store): data = self.get_json() log.debug("POST /sellable station: %s payload: %s", self.get_current_station(store), data) if 'product' not in data: abort(400, 'There is no product data on payload') sellable_id = data.get('sellable_id') barcode = data.get('barcode') description = data.get('description') base_price = self._price_validation(data) sellable = store.get(Sellable, sellable_id) sellable_created_via_sale = sellable and Sellable.NOTES_CREATED_VIA_SALE in sellable.notes if sellable and not sellable_created_via_sale: message = 'Product with id {} already exists'.format(sellable_id) log.warning(message) return make_response(jsonify({ 'message': message, }), 200) if barcode and store.find(Sellable, barcode=barcode): message = 'Product with barcode {} already exists'.format(barcode) log.warning(message) return make_response(jsonify({ 'message': message, }), 200) if not sellable: sellable = Sellable(store=store) if sellable_id: sellable.id = sellable_id sellable.code = barcode sellable.barcode = barcode sellable.description = description # FIXME The sellable is created with STATUS_CLOSED because we need the taxes info # to start selling so this is just a temporary sellable just to save it on the # database so the override can be created sellable.status = Sellable.STATUS_CLOSED sellable.base_price = base_price # If the sellable was pre-created on a sale it has a notes informing it and to # proceed this note is removed sellable.notes = sellable.notes.replace( Sellable.NOTES_CREATED_VIA_SALE, "") product = sellable.product if sellable_created_via_sale else (Product( store=store, sellable=sellable)) product_data = data.get('product') product.manage_stock = product_data.get('manage_stock', False) # For clients that will control their inventory, we have to create a Storable if product.manage_stock and not store.get(Storable, product.id): storable = Storable(store=store, product=product) storable.maximum_quantity = 1000 return make_response( jsonify({ 'message': 'Product created', 'data': { 'id': sellable.id, 'barcode': sellable.barcode, 'description': sellable.description, 'status': sellable.status, } }), 201)
def test_events(self): store_list = [] p_data = _ServiceEventData() ServiceCreateEvent.connect(p_data.on_create) ServiceEditEvent.connect(p_data.on_edit) ServiceRemoveEvent.connect(p_data.on_delete) try: # Test service being created store = new_store() store_list.append(store) sellable = Sellable( store=store, description=u'Test 1234', price=decimal.Decimal(2), ) service = Service( store=store, sellable=sellable, ) store.commit() self.assertTrue(p_data.was_created) self.assertFalse(p_data.was_edited) self.assertFalse(p_data.was_deleted) self.assertEqual(p_data.service, service) p_data.reset() # Test service being edited and emmiting the event just once store = new_store() store_list.append(store) sellable = store.fetch(sellable) service = store.fetch(service) sellable.notes = u'Notes' sellable.description = u'Test 666' service.weight = decimal.Decimal(10) store.commit() self.assertTrue(p_data.was_edited) self.assertFalse(p_data.was_created) self.assertFalse(p_data.was_deleted) self.assertEqual(p_data.service, service) self.assertEqual(p_data.emit_count, 1) p_data.reset() # Test service being edited, editing Sellable store = new_store() store_list.append(store) sellable = store.fetch(sellable) service = store.fetch(service) sellable.notes = u'Notes for test' store.commit() self.assertTrue(p_data.was_edited) self.assertFalse(p_data.was_created) self.assertFalse(p_data.was_deleted) self.assertEqual(p_data.service, service) self.assertEqual(p_data.emit_count, 1) p_data.reset() finally: # Test service being removed store = new_store() store_list.append(store) sellable = store.fetch(sellable) service = store.fetch(service) sellable.remove() store.commit() self.assertTrue(p_data.was_deleted) self.assertFalse(p_data.was_created) self.assertFalse(p_data.was_edited) self.assertEqual(p_data.service, service) self.assertEqual(p_data.emit_count, 1) p_data.reset() for store in store_list: store.close()
def testCreateEvent(self): store_list = [] p_data = _ProductEventData() ProductCreateEvent.connect(p_data.on_create) ProductEditEvent.connect(p_data.on_edit) ProductRemoveEvent.connect(p_data.on_delete) try: # Test product being created store = new_store() store_list.append(store) sellable = Sellable( store=store, description=u'Test 1234', price=Decimal(2), ) product = Product( store=store, sellable=sellable, ) store.commit() self.assertTrue(p_data.was_created) self.assertFalse(p_data.was_edited) self.assertFalse(p_data.was_deleted) self.assertEqual(p_data.product, product) p_data.reset() # Test product being edited and emmiting the event just once store = new_store() store_list.append(store) sellable = store.fetch(sellable) product = store.fetch(product) sellable.notes = u'Notes' sellable.description = u'Test 666' product.weight = Decimal(10) store.commit() self.assertTrue(p_data.was_edited) self.assertFalse(p_data.was_created) self.assertFalse(p_data.was_deleted) self.assertEqual(p_data.product, product) self.assertEqual(p_data.emmit_count, 1) p_data.reset() # Test product being edited, editing Sellable store = new_store() store_list.append(store) sellable = store.fetch(sellable) product = store.fetch(product) sellable.notes = u'Notes for test' store.commit() self.assertTrue(p_data.was_edited) self.assertFalse(p_data.was_created) self.assertFalse(p_data.was_deleted) self.assertEqual(p_data.product, product) self.assertEqual(p_data.emmit_count, 1) p_data.reset() # Test product being edited, editing Product itself store = new_store() store_list.append(store) sellable = store.fetch(sellable) product = store.fetch(product) product.weight = Decimal(1) store.commit() self.assertTrue(p_data.was_edited) self.assertFalse(p_data.was_created) self.assertFalse(p_data.was_deleted) self.assertEqual(p_data.product, product) self.assertEqual(p_data.emmit_count, 1) p_data.reset() # Test product being edited, editing Product itself store = new_store() store_list.append(store) sellable = store.fetch(sellable) product = store.fetch(product) product.weight = Decimal(1) store.commit() # self.assertTrue(p_data.was_edited) self.assertFalse(p_data.was_created) self.assertFalse(p_data.was_deleted) # self.assertEqual(p_data.product, product) # self.assertEqual(p_data.emmit_count, 1) p_data.reset() finally: # Test product being removed store = new_store() store_list.append(store) sellable = store.fetch(sellable) product = store.fetch(product) sellable.remove() store.commit() self.assertTrue(p_data.was_deleted) self.assertFalse(p_data.was_created) self.assertFalse(p_data.was_edited) self.assertEqual(p_data.product, product) self.assertEqual(p_data.emmit_count, 1) p_data.reset() for store in store_list: store.close()