def setUp(self): self.equipment = Equipment(**base_equipment) self.equipment.save() self.employee = Employee(first_name="John", last_name="Smith", department="Carpentry") self.employee.save() self.image = S3Object() self.image.save()
def setUp(self): """ Sets up for tests """ customer_data = copy.deepcopy(base_customer) del customer_data['addresses'] self.customer = Customer.objects.create(**customer_data) self.customer.save() self.project = Project.objects.create(codename="Ladawan") self.room = Room.objects.create(description="Living Room", project=self.project) self.file1 = S3Object(key='test', bucket='test') self.file1.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() # 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 hydrate(self, bundle): """ Implements the hydrate function """ #Set the country, in order to retrieve the correct quantity bundle = self._set_country(bundle) #Takes supplier and add to supplier list in data if "supplier" in bundle.data: try: suppliers = bundle.data['suppliers'] except KeyError: bundle.data['suppliers'] = [] suppliers = [] if bundle.data['supplier']['id'] not in [ s['id'] for s in suppliers ]: bundle.data['suppliers'].append(bundle.data['supplier']) del bundle.data['supplier'] #Adds new types try: if bundle.data['type'].lower() == 'custom': bundle.obj.type = bundle.data['custom-type'] del bundle.data['custom-type'] else: bundle.obj.type = bundle.data['type'] bundle.data['type'] = bundle.obj.type except KeyError as e: logger.warn(e) #Adds the quantity try: logger.debug("{0} : {1}".format(bundle.obj.quantity, bundle.data['quantity'])) logger.debug(bundle.obj.id) logger.debug( bool( Decimal(str(bundle.obj.quantity)) != Decimal( bundle.data['quantity']) and bundle.obj.quantity != None)) if Decimal(str(bundle.obj.quantity)) != Decimal( bundle.data['quantity']) and bundle.obj.quantity != None: action = "ADD" if float(bundle.data['quantity'] ) > bundle.obj.quantity else "SUBTRACT" diff = abs( Decimal(str(bundle.obj.quantity)) - Decimal(bundle.data['quantity'])) bundle.obj.quantity = float(bundle.data['quantity']) log = Log(supply=bundle.obj, action=action, quantity=diff, message=u"{0}ed {1}{2} {3} {4}".format( action.capitalize(), diff, bundle.obj.units, "to" if action == "ADD" else "from", bundle.obj.description)) log.save() except KeyError: pass #Adds the image if "image" in bundle.data: try: bundle.obj.image = S3Object.objects.get( pk=bundle.data['image']['id']) except KeyError: #Create a new S3object for the image s3_obj = S3Object() s3_obj.key = bundle.data['image']['key'] s3_obj.bucket = bundle.data['image']['bucket'] s3_obj.save() bundle.obj.image = s3_obj except S3Object.DoesNotExist: raise """ #Change the quantity if "quantity" in bundle.data and bundle.obj.pk: #Equalize the 2 quantities so that #they are easier to work with and compare client_quantity = float(bundle.data['quantity']) server_quantity = float(bundle.obj.quantity) difference = abs(client_quantity - server_quantity) #Get the description description = bundle.obj.description purchasing_units = bundle.obj.purchasing_units #Checks if quantity is added if client_quantity > server_quantity: if bundle.request.user.has_perm('supplies.add_quantity'): bundle.obj.quantity = client_quantity logger.info("Added {0}{1} to {2} inventory".format(difference, purchasing_units, description)) else: bundle.data['quantity'] = server_quantity bundle.data['quantity'] = server_quantity warning = "{0} is not authorized to add to the quantity of Fabric {1}".format(bundle.request.user.username, bundle.obj.description) logger.warn(warning) #raise Unauthorized("User does not have authorization to add to inventory.") #Checks if quantity was subtracted elif client_quantity < server_quantity: if bundle.request.user.has_perm('supplies.subtract_quantity'): bundle.obj.quantity = client_quantity logger.info("Subtracted {0}{1} from {2} inventory".format(difference, purchasing_units, description)) else: bundle.data['quantity'] = server_quantity warning = "{0} is not authorized to subtract from the quantity of Fabric {1}".format(bundle.request.user.username, bundle.obj.description) logger.warn(warning) #raise Unauthorized("User does not have authorization to subtract from inventory.") """ return bundle