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 = Acknowledgement(**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 Acknowledgement Test Objects created: -User -Customer -Supplier -Address -product -2 fabrics After Creating all the needed objects for the Acknowledgement, test that all the objects have been made. """ super(ShippingResourceTest, self).setUp() self.ct = ContentType(app_label="shipping") self.ct.save() #Create the user self.username = '******' self.password = '******' self.user = User.objects.create_user(self.username, '*****@*****.**', self.password) self.user.save() p = Permission(content_type=self.ct, codename="change_shipping") p.save() p2 = Permission(content_type=self.ct, codename="add_shipping") 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 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 project self.project = Project.objects.create(codename="Ladawan") #Create phase self.phase = Phase.objects.create(description="Phase 1/6", project=self.project) #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'] self.ack = Acknowledgement(**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 = AckItem.create(acknowledgement=self.ack, **item_data) #Create an item item_data = { 'id': 1, 'quantity': 2, 'is_custom_size': True, 'width': 1500, "fabric": { "id": 1 } } self.item2 = AckItem.create(acknowledgement=self.ack, **item_data)
def setUp(self): """ Set up for the Acknowledgement Test Objects created: -User -Customer -Supplier -Address -product -2 fabrics After Creating all the needed objects for the Acknowledgement, test that all the objects have been made. """ super(ShippingResourceTest, self).setUp() self.ct = ContentType(app_label="shipping") self.ct.save() #Create the user self.username = '******' self.password = '******' self.user = User.objects.create_user(self.username, '*****@*****.**', self.password) self.user.save() p = Permission(content_type=self.ct, codename="change_shipping") p.save() p2 = Permission(content_type=self.ct, codename="add_shipping") 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 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 project self.project = Project.objects.create(codename="Ladawan") #Create phase self.phase = Phase.objects.create(description="Phase 1/6", project=self.project) #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'] self.ack = Acknowledgement(**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 = AckItem.create(acknowledgement=self.ack, **item_data) #Create an item item_data = {'id': 1, 'quantity': 2, 'is_custom_size': True, 'width': 1500, "fabric": {"id":1}} self.item2 = AckItem.create(acknowledgement=self.ack, **item_data)
def setUp(self): """ Set up for the Acknowledgement Test Objects created: -User -Customer -Supplier -Address -product -2 fabrics After Creating all the needed objects for the Acknowledgement, test that all the objects have been made. """ super(AcknowledgementResourceTest, self).setUp() self.ct = ContentType(app_label="acknowledgements") 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_acknowledgement") p.save() p2 = Permission(content_type=self.ct, codename="add_acknowledgement") 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 = Acknowledgement(**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(acknowledgement=self.ack, **item_data) item_data = {'is_custom': True, 'description': 'F-04 Sofa', 'quantity': 3} self.item2 = Item.create(acknowledgement=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() codename = u"MC House" self.project = Project.objects.create(codename=codename) self.room = Room.objects.create(description="Kitchen", project=self.project) self.phase = Phase.objects.create(description="Phase 1", quantity=1, project=self.project)
class AcknowledgementResource(ModelResource): items = fields.ToManyField('acknowledgements.api.ItemResource', 'items', readonly=True, null=True, full=True) customer = fields.ToOneField('contacts.api.CustomerResource', 'customer', readonly=True, full=True) class Meta: queryset = Acknowledgement.objects.filter( deleted=False).order_by('-id') resource_name = 'acknowledgement' allowed_methods = ['get', 'post', 'put', 'patch'] fields = [ 'time_created', 'deleted', 'last_modified', 'po_id', 'subtotal', 'vat', 'total', 'remarks', 'status', 'delivery_date', 'id' ] always_return_data = True validation = AcknowledgementValidation() authorization = DjangoAuthorization() format = 'json' def dehydrate(self, bundle): """ Implements the dehydrate method Adds the urls for the acknowledgement and the production pdf to the data """ #Add URLS for the acknowledgement #and the production pdf to the data #bundle if bundle.request.GET.get('pdf'): try: ack = bundle.obj.acknowledgement_pdf production = bundle.obj.production_pdf bundle.data['pdf'] = { 'acknowledgement': ack.generate_url(), 'production': production.generate_url() } except AttributeError as e: logger.warn(e) logger.warn('Missing acknowledgement or production pdf') try: label = bundle.obj.label_pdf bundle.data['pdf']['label'] = label.generate_url() except AttributeError: logger.warn("Missing label pdf") #Adds a dictionary for the project if it exists if bundle.obj.project: bundle.data['project'] = { 'id': bundle.obj.project.id, 'codename': bundle.obj.project.codename } return bundle def apply_filters(self, request, applicable_filters): obj_list = super(AcknowledgementResource, self).apply_filters(request, applicable_filters) if request.GET.has_key('q'): query = request.GET.get('q') obj_list = obj_list.filter( Q(customer__name__icontains=query) | Q(pk__icontains=query)) return obj_list def obj_create(self, bundle, **kwargs): """ Creates the acknowledgement resource """ logger.info("Creating a new acknowledgement...") #Create the object bundle.obj = Acknowledgement() #hydrate bundle = self.full_hydrate(bundle) #Set the customer try: logger.info("Setting customer...") bundle.obj.customer = Customer.objects.get( pk=bundle.data["customer"]["id"]) bundle.obj.discount = bundle.obj.customer.discount except: logger.error("Customer with ID {0} could not be found.".format( bundle.data['customer']['id'])) raise #Set the employee try: logger.info("Setting employee...") bundle.obj.employee = bundle.request.user except User.DoesNotExist: logger.error("User with ID {0} could not be found".format( bundle.data['employee']['id'])) raise except KeyError: logger.critical("Missing employee ID.") raise #Set Status bundle.obj.status = "ACKNOWLEDGED" #Set the project or create a new one if "project" in bundle.data: try: project = Project.objects.get(pk=bundle.data['project']['id']) except KeyError, Project.DoesNotExist: try: project = Project() project.codename = bundle.data['project']['codename'] project.save() except KeyError: project = None bundle.obj.project = project #Create items without saving them logger.info("Creating items...") self.items = [ Item.create(acknowledgement=bundle.obj, commit=False, **product) for product in bundle.data["items"] ] #Calculate the total price logger.info("Calculating balance of the order...") bundle.obj.calculate_totals(self.items) bundle = self.save(bundle) #Save the items logger.info("Saving the items to the database...") for item in self.items: item.acknowledgement = bundle.obj item.save() log_message = "Ack {0} created on {1}. Schedule to be delivered on {1}" log_message = log_message.format( bundle.obj.id, bundle.obj.time_created.strftime('%B %d, %Y'), bundle.obj.delivery_date.strftime('%B %d, %Y')) log = Log(message=log_message, delivery_date=bundle.obj.delivery_date, acknowledgement=bundle.obj) log.save() #Create and upload the pdfs to the #S3 system. The save the pdfs as #Attributes of the acknowledgement logger.info("Creating PDF documents...") bundle.obj.create_and_upload_pdfs() #Add the url of the pdf to the outgoing data #only for when an acknowledgement is create try: ack = bundle.obj.acknowledgement_pdf production = bundle.obj.production_pdf bundle.data['pdf'] = { 'acknowledgement': ack.generate_url(), 'production': production.generate_url() } except AttributeError: logger.warn('Missing acknowledgement or production pdf') #Conditionally email ack to Decoroom if "decoroom" in bundle.obj.customer.name.lower(): try: logger.info("Emailing Decoroom Co., Ltd. the order details...") bundle.obj.email_decoroom() except Exception as e: logger.error("Unable to mail decoroom.") logger.error(e) logger.info(u"Acknowledgement #{0} created for {1}".format( bundle.obj.id, bundle.obj.customer.name)) return bundle