コード例 #1
0
ファイル: api.py プロジェクト: charliephairoj/backend
    def _hydrate_supply(self, bundle, supply_obj):
        """
        Creates or updates an supply for the project
        """
        try:
            project_supply = ProjectSupply.objects.get(project=bundle.obj, supply_id=supply_obj["id"])
        except ProjectSupply.DoesNotExist as e:
            project_supply = ProjectSupply()
            project_supply.project = bundle.obj
            project_supply.supply = Supply.objects.get(pk=supply_obj["id"])

        for field in project_supply._meta.get_all_field_names():
            if field in supply_obj and field not in ["id", "project", "supply"]:
                setattr(project_supply, field, supply_obj[field])

        project_supply.save()
コード例 #2
0
ファイル: api.py プロジェクト: charliephairoj/backend
    def add_supply(self, request, **kwargs):
        """
        Adds a new supply to the project
        """
        if request.method == "POST":
            # Get the project
            project = Project.objects.get(pk=kwargs["pk"])

            supply_data = json.loads(request.body)
            supply = Supply.objects.get(pk=supply_data["id"])

            try:
                ProjectSupply.objects.get(supply=supply, project=project)
            except ProjectSupply.DoesNotExist:
                ps = ProjectSupply(supply=supply, project=project)
                ps.save()

            return self.create_response(request, supply_data)
コード例 #3
0
    def add_supply(self, request, **kwargs):
        """
        Adds a new supply to the project
        """
        if request.method == "POST":
            #Get the project
            project = Project.objects.get(pk=kwargs['pk'])

            supply_data = json.loads(request.body)
            supply = Supply.objects.get(pk=supply_data['id'])

            try:
                ProjectSupply.objects.get(supply=supply, project=project)
            except ProjectSupply.DoesNotExist:
                ps = ProjectSupply(supply=supply, project=project)
                ps.save()

            return self.create_response(request, supply_data)
コード例 #4
0
 def setUp(self):
     """
     Sets up for tests
     """
     super(ProjectResourceTestCase, self).setUp()
     
     self.create_user()
     #self.api_client.client.login(username='******', password='******')
     
     #self.customer = Customer.create(**base_customer)
     #self.project = Project.create(**base_project)
     self.project = Project(codename="Ladawan")
     self.project.save()
     self.supply = Supply(description='Grommet')
     self.supply.save()
     self.supply2 = Supply(description='Hinge')
     self.supply2.save()
     self.project_supply = ProjectSupply(supply=self.supply,
                                         project=self.project,
                                         quantity=2)
     self.project_supply.save()
コード例 #5
0
    def _hydrate_supply(self, bundle, supply_obj):
        """
        Creates or updates an supply for the project
        """
        try:
            project_supply = ProjectSupply.objects.get(
                project=bundle.obj, supply_id=supply_obj['id'])
        except ProjectSupply.DoesNotExist as e:
            project_supply = ProjectSupply()
            project_supply.project = bundle.obj
            project_supply.supply = Supply.objects.get(pk=supply_obj['id'])

        for field in project_supply._meta.get_all_field_names():
            if field in supply_obj and field not in [
                    'id', 'project', 'supply'
            ]:
                setattr(project_supply, field, supply_obj[field])

        project_supply.save()
コード例 #6
0
class ProjectResourceTestCase(APITestCase):
    def setUp(self):
        """
        Sets up for tests
        """
        super(ProjectResourceTestCase, self).setUp()
        
        self.create_user()
        #self.api_client.client.login(username='******', password='******')
        
        #self.customer = Customer.create(**base_customer)
        #self.project = Project.create(**base_project)
        self.project = Project(codename="Ladawan")
        self.project.save()
        self.supply = Supply(description='Grommet')
        self.supply.save()
        self.supply2 = Supply(description='Hinge')
        self.supply2.save()
        self.project_supply = ProjectSupply(supply=self.supply,
                                            project=self.project,
                                            quantity=2)
        self.project_supply.save()
    
    def create_user(self):
        self.user = User.objects.create_user('test', '*****@*****.**', 'test')
        self.ct = ContentType(app_label='projects')
        self.ct.save()
        #self._create_and_add_permission('view_cost', self.user)
        self._create_and_add_permission('change_project', self.user)
        #self._create_and_add_permission('add_supply', self.user)
        #self._create_and_add_permission('add_quantity', self.user)
        #self._create_and_add_permission('subtract_quantity', self.user)
       
        
    def _create_and_add_permission(self, codename, user):
        p = Permission(content_type=self.ct, codename=codename)
        p.save()
        user.user_permissions.add(p)
        
    def test_get(self):
        """
        Test retrieving resources via GET
        """
        resp = self.api_client.get('/api/v1/project')
        
        self.assertHttpOK(resp)
        obj = self.deserialize(resp)
        self.assertEqual(len(obj['objects']), 1)
        project = obj['objects'][0]
        self.assertEqual(project['id'], 1)
        self.assertEqual(project['codename'], 'Ladawan')
        
    def test_get_obj(self):
        """
        Tests retrieving a single object via GET
        """
        resp = self.api_client.get('/api/v1/project/1')
        
        self.assertHttpOK(resp)
        
        obj = self.deserialize(resp)
        self.assertEqual(obj['id'], 1)
        self.assertEqual(obj['codename'], 'Ladawan')
        self.assertIn('supplies', obj)
        self.assertEqual(len(obj['supplies']), 1)
        supply = obj['supplies'][0]
        self.assertEqual(supply['id'], 1)
        self.assertEqual(supply['description'], 'Grommet')
        self.assertEqual(int(supply['quantity']), 2)
        
    def test_get_all_projects(self):
        """
        Test retrieving full list of projects over 100"""
        for i in xrange(0, 100):
            project = Project(codename=i)
            project.save()

        self.assertEqual(Project.objects.all().count(), 101)
        
        resp = self.client.get('/api/v1/project/?page_size=99999', format='json')
        
        self.assertEqual(resp.status_code, 200)

        data = resp.data['results']
        self.assertEqual(len(data), 101)
        
    @unittest.skip('')
    def test_create_project(self):
        """
        Tests creating a project
        """
        self.assertIsNotNone(self.project)
        self.assertIsInstance(self.project, Project)
        self.assertIsInstance(self.project.customer, Customer)
        self.assertEqual(self.project.customer, self.customer)
        self.assertEqual(self.project.reference, "S5")
        self.assertEqual(self.project.type, "Condominium")
        self.assertEqual(self.project.due_date, base_due_date.date())
        self.assertEqual(self.project.codename, "Haze")

    def test_update_project(self):
        """
        Tests updating a project
        """
        data = {'codename': "Ladawan 329",
                'supplies': [{'id': 1,
                              'description': 'Grommet',
                              'quantity': 5},
                              {'id': 2,
                               'quantity': 10}]}
        
        resp = self.api_client.put('/api/v1/project/1', 
                                   format='json',
                                   data=data)
        self.assertHttpOK(resp)
        
        obj = self.deserialize(resp)
        self.assertEqual(obj['id'], 1)
        self.assertEqual(obj['codename'], "Ladawan 329")
        self.assertEqual(len(obj['supplies']), 2)
        
        supply1 = obj['supplies'][0]
        self.assertEqual(supply1['id'], 1)
        self.assertEqual(supply1['description'], 'Grommet')
        self.assertEqual(int(supply1['quantity']), 5)
        
        supply2 = obj['supplies'][1]
        self.assertEqual(supply2['id'], 2)
        self.assertEqual(supply2['description'], 'Hinge')
        self.assertEqual(int(supply2['quantity']), 10)

    def test_update_project_deleting_supply(self):
        """
        Tests deleting a project supply via PUT
        """
        data = {'codename': "Ladawan 329",
                'supplies': [{'id': 2,
                              'description': 'Hinge',
                              'quantity': 10}]}   
                    
        resp = self.api_client.put('/api/v1/project/1', 
                                   format='json',
                                   data=data)
        self.assertHttpOK(resp)
        
        obj = self.deserialize(resp)
        self.assertEqual(obj['id'], 1)
        self.assertEqual(obj['codename'], "Ladawan 329")
        self.assertEqual(len(obj['supplies']), 1)
        
        supply1 = obj['supplies'][0]
        self.assertEqual(supply1['id'], 2)
        self.assertEqual(supply1['description'], 'Hinge')
        self.assertEqual(int(supply1['quantity']), 10)
      
        
    @unittest.skip('')
    def _update_due_date(self):
        """
        Tests the updating of the due date
        """
        due_date = dateutil.parser.parse("2013-04-26T13:59:01.143Z")
        self.project.update(due_date=due_date.isoformat())
        self.assertEqual(self.project.due_date, due_date.date())
        delta = datetime.timedelta(days=5)
        due_date2 = due_date + delta
        self.project.update(due_date=due_date2)
        self.assertEqual(self.project.due_date, due_date2.date())