def setUp(self): """ Sets up environment for tests """ super(TestItemResource, self).setUp() self.create_user() self.client.login(username='******', password='******') #Create supplier, customer and addrss self.customer = Customer(**base_customer) self.customer.save() self.supplier = Supplier(**base_supplier) self.supplier.save() self.address = Address(address1="Jiggle", contact=self.customer) self.address.save() #Create a product to add self.product = Product.create(self.user, **base_product) self.product.save() self.fabric = Fabric.create(**base_fabric) f_data = base_fabric.copy() f_data["pattern"] = "Stripe" self.fabric2 = Fabric.create(**f_data) #Create acknowledgement ack_data = base_ack.copy() del ack_data['customer'] del ack_data['items'] del ack_data['employee'] del ack_data['project'] self.ack = Estimate(**ack_data) self.ack.customer = self.customer self.ack.employee = self.user self.ack.save() #Create an item self.item_data = { 'id': 1, 'quantity': 1, 'is_custom_size': True, 'width': 1500, "fabric": { "id": 1 } } self.item = Item.create(acknowledgement=self.ack, **self.item_data)
def setUp(self): """ Sets up environment for tests """ super(TestItemResource, self).setUp() self.create_user() self.client.login(username='******', password='******') #Create supplier, customer and addrss self.customer = Customer(**base_customer) self.customer.save() self.supplier = Supplier(**base_supplier) self.supplier.save() self.address = Address(address1="Jiggle", contact=self.customer) self.address.save() #Create a product to add self.product = Product.create(self.user, **base_product) self.product.save() self.fabric = Fabric.create(**base_fabric) f_data = base_fabric.copy() f_data["pattern"] = "Stripe" self.fabric2 = Fabric.create(**f_data) #Create acknowledgement ack_data = base_ack.copy() del ack_data['customer'] del ack_data['items'] del ack_data['employee'] del ack_data['project'] self.ack = Estimate(**ack_data) self.ack.customer = self.customer self.ack.employee = self.user self.ack.save() #Create an item self.item_data = {'id': 1, 'quantity': 1, 'is_custom_size': True, 'width': 1500, "fabric": {"id":1}} self.item = Item.create(acknowledgement=self.ack, **self.item_data)
def setUp(self): """ Set up for the Estimate Test Objects created: -User -Customer -Supplier -Address -product -2 fabrics After Creating all the needed objects for the Estimate, test that all the objects have been made. """ super(EstimateResourceTest, self).setUp() # Set Base URL self.base_url = '{0}'.format('/api/v1/estimate/') self.ct = ContentType(app_label="estimates") self.ct.save() #Create the user self.username = '******' self.password = '******' self.user = User.objects.create_user(self.username, '*****@*****.**', self.password) p = Permission(content_type=self.ct, codename="change_estimate") p.save() p2 = Permission(content_type=self.ct, codename="add_estimate") p2.save() self.user.user_permissions.add(p) self.user.user_permissions.add(p2) self.user.save() self.setup_client() #Create supplier, customer and addrss customer = copy.deepcopy(base_customer) del customer['id'] self.customer = Customer(**customer) self.customer.save() self.supplier = Supplier(**base_supplier) self.supplier.save() self.address = Address(address1="Jiggle", contact=self.customer) self.address.save() #Create a product to add self.product = Product.create(self.user, **base_product) self.product.save() #Create custom product self.custom_product = Product() self.custom_product.id = 10436 self.custom_product.save() self.fabric = Fabric.create(**base_fabric) self.fabric.quantity = 26 self.fabric.save() f_data = base_fabric.copy() f_data["pattern"] = "Stripe" self.fabric2 = Fabric.create(**f_data) #Create custom product self.custom_product = Product.create(self.user, description="Custom Custom", id=10436, width=0, depth=0, height=0, price=0, wholesale_price=0, retail_price=0) self.custom_product.id = 10436 self.custom_product.save() self.image = S3Object(key='test', bucket='test') self.image.save() #Create acknowledgement ack_data = base_ack.copy() del ack_data['customer'] del ack_data['items'] del ack_data['employee'] del ack_data['project'] self.ack = Estimate(**ack_data) self.ack.customer = self.customer self.ack.employee = self.user self.ack.save() #Create an item item_data = { 'id': 1, 'quantity': 1, 'is_custom_size': True, 'width': 1500, "fabric": { "id": 1 } } self.item = Item.create(estimate=self.ack, **item_data) item_data = { 'is_custom': True, 'description': 'F-04 Sofa', 'quantity': 3 } self.item2 = Item.create(estimate=self.ack, **item_data) #Create fake S3Objects to test files attached to acknowledgements self.file1 = S3Object(key='test1', bucket='test') self.file2 = S3Object(key='test2', bucket='test') self.file1.save() self.file2.save()
def setUp(self): """ Set up for the Estimate Test Objects created: -User -Customer -Supplier -Address -product -2 fabrics After Creating all the needed objects for the Estimate, test that all the objects have been made. """ super(EstimateResourceTest, self).setUp() self.ct = ContentType(app_label="estimates") self.ct.save() #Create the user self.username = '******' self.password = '******' self.user = User.objects.create_user(self.username, '*****@*****.**', self.password) p = Permission(content_type=self.ct, codename="change_estimate") p.save() p2 = Permission(content_type=self.ct, codename="add_estimate") p2.save() self.user.user_permissions.add(p) self.user.user_permissions.add(p2) self.user.save() #Create supplier, customer and addrss customer = copy.deepcopy(base_customer) del customer['id'] self.customer = Customer(**customer) self.customer.save() self.supplier = Supplier(**base_supplier) self.supplier.save() self.address = Address(address1="Jiggle", contact=self.customer) self.address.save() #Create a product to add self.product = Product.create(self.user, **base_product) self.product.save() #Create custom product self.custom_product = Product() self.custom_product.id = 10436 self.custom_product.save() self.fabric = Fabric.create(**base_fabric) self.fabric.quantity = 26 self.fabric.save() f_data = base_fabric.copy() f_data["pattern"] = "Stripe" self.fabric2 = Fabric.create(**f_data) #Create custom product self.custom_product = Product.create(self.user, description="Custom Custom", id=10436, width=0, depth=0, height=0, price=0, wholesale_price=0, retail_price=0) self.custom_product.id = 10436 self.custom_product.save() self.image = S3Object(key='test', bucket='test') self.image.save() #Create acknowledgement ack_data = base_ack.copy() del ack_data['customer'] del ack_data['items'] del ack_data['employee'] del ack_data['project'] self.ack = Estimate(**ack_data) self.ack.customer = self.customer self.ack.employee = self.user self.ack.save() #Create an item item_data = {'id': 1, 'quantity': 1, 'is_custom_size': True, 'width': 1500, "fabric": {"id":1}} self.item = Item.create(estimate=self.ack, **item_data) item_data = {'is_custom': True, 'description': 'F-04 Sofa', 'quantity': 3} self.item2 = Item.create(estimate=self.ack, **item_data) self.client.login(username="******", password="******") #Create fake S3Objects to test files attached to acknowledgements self.file1 = S3Object(key='test1', bucket='test') self.file2 = S3Object(key='test2', bucket='test') self.file1.save() self.file2.save()
def _update_items(self, instance, items_data): """ Handles creation, update, and deletion of items """ #Maps of id id_list = [item_data.get('id', None) for item_data in items_data] #Update or Create Item for item_data in items_data: try: item = Item.objects.get(pk=item_data['id'], estimate=instance) except KeyError as e: try: item = Item(product=Product.objects.get(pk=item_data['product'])) except TypeError as e: item = Item(product=item_data['product']) item.estimate = instance item.width = item_data.get('width', item.width) item.depth = item_data.get('depth', item.depth) item.height = item_data.get('height', item.height) item.description = item_data.get('description', item.description) item.quantity = item_data.get('quantity', item.quantity) item.unit_price = item_data.get('unit_price', item.unit_price) item.total = item.quantity * item.unit_price try: item.image = S3Object.objects.get(pk=item_data['image']) except TypeError as e: item.image = item_data['image'] except (S3Object.DoesNotExist, KeyError) as e: logger.warn(item_data['image']) logger.warn(e) item.save() """ try: item = Item.objects.get(pk=item_data['id']) serializer = ItemSerializer(item, context={'customer': instance.customer, 'estimate': instance}, data=item_data) if serializer.is_valid(raise_exception=True): serializer.save() item.supply.supplier = instance.supplier item.discount = item_data.get('discount', None) or item.discount item.quantity = item_data.get('quantity', None) or item.quantity item.unit_cost = item_data.get('unit_cost', None) or item.unit_cost #Change the cost of the supply and log price change if item.unit_cost != item.supply.cost: self._change_supply_cost(item.supply, item.unit_cost) item.calculate_total() item.save() except KeyError: item_data['product'] = item_data['product'].id serializer = ItemSerializer(data=item_data, context={'customer': instance.customer, 'estimate': instance}) if serializer.is_valid(raise_exception=True): item = serializer.save() id_list.append(item.id) """ #Delete Items for item in instance.items.all(): if item.id not in id_list: item.delete()