示例#1
0
    def hydrate(self, bundle):
        """
        Prepare the fabric to be saved to the database
        """
        bundle.obj.description = "Pattern:{0}, Col: {1}".format(
            bundle.data['pattern'], bundle.data['color'])
        bundle.obj.type = "Fabric"
        bundle.obj.units = 'm'

        if "cost" in bundle.data:
            if float(bundle.data['cost']) > 0:
                try:
                    try:
                        p = Product.objects.get(
                            supply=bundle.obj,
                            supplier=Supplier.objects.get(
                                pk=bundle.data['supplier']['id']))
                    except Product.DoesNotExist:
                        p = Product()
                        p.supply = bundle.obj
                        p.supplier = Supplier.objects.get(
                            pk=bundle.data['supplier']['id'])
                        p.cost = bundle.data['cost']
                    p.save()
                except KeyError:
                    pass

        return bundle
示例#2
0
    def setUp(self):
        """
        Set up the view 
        
        -login the user
        """
        super(SupplyAPITest, self).setUp()

        self.create_user()
        self.client.login(username='******', password='******')

        self.supplier = Supplier(**base_supplier)
        self.supplier.save()
        self.supplier2 = Supplier.objects.create(**base_supplier)
        self.supply = Supply.create(**base_supply)
        self.assertIsNotNone(self.supply.pk)
        self.supply2 = Supply.create(**base_supply)
        self.assertIsNotNone(self.supply2.pk)
        self.supply3 = Supply.create(**base_supply)
        self.assertIsNotNone(self.supply3.pk)
        self.supply4 = Supply.create(**base_supply)
        self.assertIsNotNone(self.supply4.pk)

        self.product = Product(supplier=self.supplier, supply=self.supply)
        self.product.save()

        self.employee = Employee(first_name="John", last_name="Smith")
        self.employee.save()
示例#3
0
    def obj_update(self, bundle, **kwargs):
        """
        Updates the supply, the product relationship witht the supplier
        
        Extra suppliers before the parent obj_update method destroys it.
        """
        #Get suppliers data before erased by parent method
        try:
            suppliers = bundle.data['suppliers']
        except KeyError as e:
            try:
                suppliers = [bundle.data['supplier']]
            except KeyError:
                raise

        bundle = super(SupplyResource, self).obj_update(bundle, **kwargs)

        for supplier_data in suppliers:
            supplier = Supplier.objects.get(pk=supplier_data['id'])
            try:
                product = Product.objects.get(supplier=supplier,
                                              supply=bundle.obj)
            except Product.DoesNotExist as e:
                product = Product(supplier=supplier, supply=bundle.obj)
            product = self.hydrate_product(product,
                                           bundle=bundle,
                                           **supplier_data)
            product.save()

        return bundle
示例#4
0
    def obj_create(self, bundle, **kwargs):
        """
        Creates the supply, and then respectively 
        creates the intermediary product instance for the
        many to many relationship with suppliers
        
        We must separate the suppliers data before calling the parent 
        obj_create method
        """

        try:
            suppliers = bundle.data['suppliers']
        except KeyError:
            suppliers = [bundle.data['supplier']]

        #Initial supply creation
        bundle = super(SupplyResource, self).obj_create(bundle, **kwargs)

        #Creates products to establish supplier/supply relationship
        for supplier in suppliers:
            #Gets or creates a new product
            try:
                product = Product(
                    supply=bundle.obj,
                    supplier=Supplier.objects.get(pk=supplier['id']))
            except Product.DoesNotExist as e:
                product = Product(
                    supplier=Supplier.objects.get(pk=supplier['id']),
                    supply=bundle.obj)
            product = self.hydrate_product(product=product,
                                           bundle=bundle,
                                           **supplier)
            product.save()

            log = Log(
                supply=product.supply,
                supplier=product.supplier,
                action="PRICE CHANGE",
                quantity=None,
                cost=product.cost,
                message=u"Price set to {0}{1} for {2} [Supplier: {3}]".format(
                    product.cost, product.supplier.currency,
                    product.supply.description, product.supplier.name))
            log.save()

        return bundle
示例#5
0
    def create_product(self, supply, supplier, cost, upc=None):
        """
        Creates a product
        """
        if not isinstance(supply, Supply) and isinstance(supply, dict):
            supply = Supply.objects.get(pk=supply['id'])

        if not isinstance(supplier, Supplier) and isinstance(supplier, dict):
            supplier = Supplier.objects.get(pk=supplier['id'])

        product = Product(supply=supply, supplier=supplier, cost=cost, upc=upc)
        product.save()
        return product
示例#6
0
    def setUp(self):
        """
        Set up the view 
        
        -login the user
        """
        super(FabricAPITestCase, self).setUp()

        self.create_user()
        self.client.login(username='******', password='******')

        self.supplier = Supplier(**base_supplier)
        self.supplier.save()
        self.supply = Fabric.create(**base_fabric)
        self.assertIsNotNone(self.supply.pk)
        self.supply2 = Fabric.create(**base_fabric)
        self.assertIsNotNone(self.supply.pk)

        self.product = Product(supplier=self.supplier, supply=self.supply)
        self.product.save()
示例#7
0
        fabric.grade = f['grade']
        fabric.repeat = f['repeat']
        fabric.units = 'yd'
        fabric.type = 'fabric'
        fabric.save()

        filename = "{0} Col: {1}".format(fabric.pattern, fabric.color)
        fabric.description = filename
        fabric.save()

        print fabric.id, fabric.description

        try:
            product = Product.objects.get(supply=fabric, supplier=supplier)
        except Product.DoesNotExist:
            product = Product(supply=fabric, supplier=supplier)

        product.purchasing_units = 'yd'
        try:
            product.cost = Decimal(fabric.grade) * Decimal('1.10')
        except Exception:
            product.cost = 0

        product.save()

        if 'image' in f:
            f['image'].save(filename + ".jpg")
            image = S3Object.create(
                filename + ".jpg",
                "supply/image/{0}-{1}.jpg".format(fabric.pattern,
                                                  fabric.color),
示例#8
0
    def setUp(self):
        """
        Set up dependent objects
        """
        super(PurchaseOrderTest, self).setUp()

        self.ct = ContentType(app_label="po")
        self.ct.save()
        self.p = Permission(codename="add_purchaseorder", content_type=self.ct)
        self.p.save()
        self.p2 = Permission(codename="change_purchaseorder",
                             content_type=self.ct)
        self.p2.save()
        #Create the user
        self.username = '******'
        self.password = '******'
        self.user = User.objects.create_user(
            self.username, '*****@*****.**', self.password)
        self.user.save()
        self.user.user_permissions.add(self.p)
        self.user.user_permissions.add(self.p2)
        self.client.login(username=self.username, password=self.password)
        self.client.force_authenticate(self.user)

        self.supplier = Supplier(**base_supplier)
        self.supplier.save()
        self.address = Address(**base_address)
        self.address.contact = self.supplier
        self.address.save()
        self.contact = SupplierContact(name='test',
                                       email='*****@*****.**',
                                       telephone=1234,
                                       primary=True)
        self.contact.supplier = self.supplier
        self.contact.save()

        # Create Custom Supply
        # not implemented

        # Create Fabric
        self.supply = Fabric.create(**base_fabric)

        #self.supply.units = "m^2"
        self.supply.save()
        self.supply1 = self.supply

        self.product = Product(supply=self.supply,
                               supplier=self.supplier,
                               cost=base_fabric['unit_cost'],
                               purchasing_units='m')
        self.product.save()
        self.supply2 = Fabric.create(**base_fabric2)
        self.supply2.discount = 5
        self.supply2.save()
        self.product2 = Product(supply=self.supply2,
                                supplier=self.supplier,
                                cost=base_fabric['unit_cost'])
        self.product2.save()
        self.supply1.supplier = self.supplier
        self.supply2.supplier = self.supplier

        #Create supply with no target item
        self.supply3 = Supply.objects.create(description='test supply')
        self.supply3.id = 203
        self.supply3.save()

        #Create a project
        self.project = Project()
        self.project.codename = 'MC House'
        self.project.save()

        self.po = PurchaseOrder()
        self.po.employee = self.user
        self.po.supplier = self.supplier
        self.po.terms = self.supplier.terms
        self.po.vat = 7
        self.order_date = datetime.datetime(2017,
                                            1,
                                            15,
                                            15,
                                            30,
                                            0,
                                            0,
                                            tzinfo=timezone('Asia/Bangkok'))
        self.po.order_date = self.order_date
        self.po.receive_date = datetime.datetime.now()
        self.po.save()
        #self.po.create_and_upload_pdf()

        self.item = Item.create(supplier=self.supplier,
                                id=1,
                                **base_purchase_order['items'][0])
        self.item.purchase_order = self.po
        self.item.save()

        self.po.calculate_total()
        self.po.save()
示例#9
0
def work():
    data = []
    review = []
    supplier = Supplier.objects.get(name__icontains="blue international")
    with open("/Users/Charlie/Sites/employee/backend/blue-inter.csv",
              'r') as f:
        reader = csv.reader(f)
        for row in reader:
            data.append({
                'reference': row[0],
                'description': row[1],
                'cost': row[2],
                'units': row[3]
            })

    for index, d in enumerate(data):
        for key in d.keys():
            try:
                d[key] = d[key].encode('utf-8')
            except UnicodeDecodeError as e:
                pass

            if key == "cost":
                try:
                    d[key] = Decimal(d[key].replace(',', ''))
                except InvalidOperation as e:
                    review.append(data.pop(index))

            if key == "units":
                if not d[key]:
                    review.append(data.pop(index))

    for index, d in enumerate(data):
        try:
            try:
                supply = Supply.objects.get(description=d['description'])
            except Supply.DoesNotExist:
                supply = Supply()

            supply.description = d['description']
            supply.units = d['units']
            supply.full_clean()
            supply.save()

            try:
                product = Product.objects.get(supply=supply)
            except Product.DoesNotExist:
                product = Product(supply=supply, supplier=supplier)

            product.supplier = supplier
            product.supply = supply
            product.reference = d['reference']
            product.cost = d['cost']
            product.purchasing_units = d['units']
            product.full_clean()
            product.save()

        except ValidationError as e:
            print e
            review.append(data.pop(index))

        assert Supply.objects.filter(description=d['description']).count() == 1

    with open('blue-inter-review.csv', 'w') as f:
        fieldnames = ['reference', 'description', 'cost', 'units']
        writer = csv.DictWriter(f)
        writer.write_header()
        for d in review:
            writer.writerow(d)

    assert supplier.supplies.all().count() == len(data)