Пример #1
0
    def test_travel_locations_in_use(self):
        travel = TravelFactory(
            traveler=self.unicef_staff,
            status=Travel.COMPLETED,
        )
        travel_activity = TravelActivityFactory(
            travels=[travel],
            primary_traveler=self.unicef_staff,
            travel_type=TravelType.SPOT_CHECK,
        )
        travel_activity.locations.add(self.location_no_geom.id,
                                      self.location_with_geom.id)
        travel_activity.save()

        response = self.forced_auth_req(
            "get",
            reverse("management_gis:locations-gis-in-use"),
            user=self.unicef_staff,
            data={"country_id": self.country.id},
        )

        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assertEqual(
            sorted(response.data[0].keys()),
            ["gateway_id", "id", "level", "name", "p_code", "parent_id"])
Пример #2
0
    def test_filtering_options(self):
        t1 = TravelFactory(traveler=self.traveler, supervisor=self.unicef_staff)
        a1 = TravelActivityFactory(travel_type=TravelType.MEETING, primary_traveler=self.unicef_staff)
        a1.travels.add(t1)

        result = ResultFactory()
        t2 = TravelFactory(traveler=self.traveler, supervisor=self.unicef_staff)
        a2 = TravelActivityFactory(
            travel_type=TravelType.PROGRAMME_MONITORING,
            primary_traveler=self.unicef_staff,
            result=result
        )
        a2.travels.add(t2)

        data = {
            'f_travel_type': TravelType.PROGRAMME_MONITORING,
            'f_month': t2.start_date.month - 1,  # Frontend sends 0-11
            'f_cp_output': result.id,
        }
        response = self.forced_auth_req('get', reverse('t2f:travels:list:index'),
                                        data=data, user=self.unicef_staff)

        response_json = json.loads(response.rendered_content)
        self.assertIn('data', response_json)
        self.assertEqual(len(response_json['data']), 1)
        self.assertEqual(response_json['data'][0]['id'], t2.id)
Пример #3
0
 def addTravelLocations(self):
     # add travel locations and test the response
     traveller = UserFactory()
     travel = TravelFactory(
         traveler=traveller,
         status=Travel.COMPLETED,
     )
     travel_activity = TravelActivityFactory(
         travels=[travel],
         primary_traveler=self.unicef_staff,
         travel_type=TravelType.SPOT_CHECK,
     )
     travel_activity.locations.add(self.location_no_geom.id, self.location_with_geom.id)
     travel_activity.save()
Пример #4
0
    def test_filtering(self):
        t1 = TravelFactory(traveler=self.traveler, supervisor=self.unicef_staff)
        a1 = TravelActivityFactory(travel_type=TravelType.MEETING, primary_traveler=self.unicef_staff)
        a1.travels.add(t1)

        t2 = TravelFactory(traveler=self.traveler, supervisor=self.unicef_staff)
        a2 = TravelActivityFactory(travel_type=TravelType.PROGRAMME_MONITORING, primary_traveler=self.unicef_staff)
        a2.travels.add(t2)

        response = self.forced_auth_req('get', reverse('t2f:travels:list:index'),
                                        data={'f_travel_type': TravelType.MEETING},
                                        user=self.unicef_staff)
        response_json = json.loads(response.rendered_content)
        self.assertIn('data', response_json)
        self.assertEqual(len(response_json['data']), 1)
Пример #5
0
 def test_t2f_related_str(self):
     action_point = ActionPointFactory(
         travel_activity=TravelActivityFactory())
     self.assertEqual(action_point.related_object_str,
                      'Task not assigned to Visit')
     action_point.travel_activity.travels.add(
         TravelFactory(
             traveler=action_point.travel_activity.primary_traveler))
     self.assertEqual(
         action_point.related_object_str, 'Task No 1 for Visit {}'.format(
             action_point.travel_activity.travel.reference_number))
Пример #6
0
    def test_travel_activity(self):
        tz = timezone.get_default_timezone()
        travel = TravelFactory()
        activity_date_none = TravelActivityFactory(
            travel_type=TravelType.SPOT_CHECK,
            date=None,
        )
        activity_date_none.travels.add(travel)
        self.assertEqual(str(activity_date_none),
                         f"{TravelType.SPOT_CHECK} - None")

        activity = TravelActivityFactory(
            travel_type=TravelType.SPOT_CHECK,
            date=datetime.datetime(2001, 1, 1, 12, 10, 10, 0, tzinfo=tz),
        )
        activity.travels.add(travel)
        self.assertEqual(
            str(activity),
            f"{TravelType.SPOT_CHECK} - 2001-01-01 12:10:10+00:00",
        )
Пример #7
0
 def test_delete_with_trips(self):
     TravelActivityFactory(partner=self.partner)
     response = self.forced_auth_req(
         'delete',
         self.url,
         user=self.unicef_staff,
     )
     self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
     self.assertEqual(response.data[0],
                      "This partner has trips associated to it")
     self.assertTrue(
         PartnerOrganization.objects.filter(pk=self.partner.pk).exists())
Пример #8
0
    def test_travel_locations_in_use(self):
        travel = TravelFactory(
            traveler=self.unicef_staff,
            status=Travel.COMPLETED,
        )
        travel_activity = TravelActivityFactory(
            travels=[travel],
            primary_traveler=self.unicef_staff,
            travel_type=TravelType.SPOT_CHECK,
        )
        travel_activity.locations.add(self.location_no_geom.id, self.location_with_geom.id)
        travel_activity.save()

        response = self.forced_auth_req(
            "get",
            reverse("management_gis:locations-gis-in-use"),
            user=self.unicef_staff,
            data={"country_id": self.country.id},
        )

        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assertEqual(sorted(response.data[0].keys()), ["gateway_id", "id", "level", "name", "p_code", "parent_id"])
Пример #9
0
    def test_travel_filter(self):
        travel_activity = TravelActivityFactory()
        ActionPointFactory(
        )  # common action point, shouldn't appear while filtering
        ActionPointFactory(travel_activity=travel_activity)

        response = self.forced_auth_req(
            'get',
            reverse('action-points:action-points-list'),
            data={'travel_activity': travel_activity.id},
            user=self.unicef_user)
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assertEqual(len(response.data['results']), 1)
Пример #10
0
    def test_create_t2f_related(self):
        travel_activity = TravelActivityFactory()
        data = {'travel_activity': travel_activity.id}
        data.update(self.create_data)

        response = self.forced_auth_req(
            'post',
            reverse('action-points:action-points-list'),
            data=data,
            user=self.unicef_user)
        self.assertEqual(response.status_code, status.HTTP_201_CREATED)
        self.assertIsNotNone(response.data['travel_activity'])
        self.assertEqual(response.data['travel_activity'], travel_activity.id)
Пример #11
0
    def setUpTestData(cls):
        cls.unicef_staff = UserFactory(is_staff=True)
        cls.traveler1 = UserFactory()
        cls.traveler2 = UserFactory()

        cls.travel = TravelFactory(
            reference_number=make_travel_reference_number(),
            traveler=cls.traveler1,
            status=Travel.APPROVED,
            supervisor=cls.unicef_staff)
        # to filter against
        cls.travel_activity = TravelActivityFactory(
            primary_traveler=cls.traveler1)
        cls.travel_activity.travels.add(cls.travel)
Пример #12
0
    def test_list_csv(self):
        ActionPointFactory(status='open', comments__count=1)
        ActionPointFactory(status='open',
                           comments__count=1,
                           engagement=MicroAssessmentFactory())
        ActionPointFactory(status='open',
                           comments__count=1,
                           tpm_activity=TPMVisitFactory(
                               tpm_activities__count=1).tpm_activities.first())
        traveler = UserFactory()
        ActionPointFactory(status='open',
                           travel_activity=TravelActivityFactory(
                               primary_traveler=traveler,
                               travels=[TravelFactory(traveler=traveler)]))

        self._test_export(self.pme_user,
                          'action-points:action-points-export/csv')
Пример #13
0
    def get_reference_number(self):
        travel = TravelFactory()
        activity = TravelActivityFactory()
        self.assertIsNone(activity.get_reference_number())

        activity.travels.add(travel)
        self.assertEqual(
            activity.get_reference_number(),
            travel.reference_number,
        )

        activity._reference_number = "123"
        self.assertEqual(activity.get_reference_number(), "123")
Пример #14
0
 def test_t2f_related(self):
     action_point = ActionPointFactory(
         travel_activity=TravelActivityFactory())
     self.assertEqual(action_point.related_module,
                      ActionPoint.MODULE_CHOICES.t2f)
Пример #15
0
    def test_completed_counts(self):
        currency = PublicsCurrencyFactory()
        expense_type = PublicsTravelExpenseTypeFactory()
        business_area = PublicsBusinessAreaFactory()
        dsa_region = PublicsDSARegionFactory()

        wbs = PublicsWBSFactory(business_area=business_area)
        grant = wbs.grants.first()
        fund = grant.funds.first()
        traveler = UserFactory(is_staff=True)
        traveler.profile.vendor_number = 'usrvend'
        traveler.profile.save()

        travel = TravelFactory(reference_number=make_travel_reference_number(),
                               traveler=traveler,
                               status=Travel.CERTIFIED,
                               supervisor=self.unicef_staff)
        data = {
            'cost_assignments': [{
                'wbs': wbs.id,
                'grant': grant.id,
                'fund': fund.id,
                'share': 100
            }],
            'deductions': [{
                'date': '2016-11-03',
                'breakfast': True,
                'lunch': True,
                'dinner': False,
                'accomodation': True
            }],
            'itinerary': [{
                'origin': 'Berlin',
                'destination': 'Budapest',
                'departure_date': '2017-04-14T17:06:55.821490',
                'arrival_date': '2017-04-15T17:06:55.821490',
                'dsa_region': dsa_region.id,
                'overnight_travel': False,
                'mode_of_travel': ModeOfTravel.RAIL,
                'airlines': []
            }, {
                'origin': 'Budapest',
                'destination': 'Berlin',
                'departure_date': '2017-05-20T12:06:55.821490',
                'arrival_date': '2017-05-21T12:06:55.821490',
                'dsa_region': dsa_region.id,
                'overnight_travel': False,
                'mode_of_travel': ModeOfTravel.RAIL,
                'airlines': []
            }],
            'traveler':
            traveler.id,
            'ta_required':
            True,
            'report':
            'Some report',
            'currency':
            currency.id,
            'supervisor':
            self.unicef_staff.id,
            'expenses': [{
                'amount': '120',
                'type': expense_type.id,
                'currency': currency.id,
                'document_currency': currency.id
            }]
        }
        act1 = TravelActivityFactory(
            travel_type=TravelType.PROGRAMME_MONITORING,
            primary_traveler=traveler)
        act2 = TravelActivityFactory(travel_type=TravelType.SPOT_CHECK,
                                     primary_traveler=traveler)
        act1.travels.add(travel)
        act2.travels.add(travel)
        partner_programmatic_visits = PartnerOrganization.objects.get(
            id=act1.partner.id)
        partner_spot_checks = PartnerOrganization.objects.get(
            id=act2.partner.id)
        response = self.forced_auth_req('post',
                                        reverse(
                                            't2f:travels:details:state_change',
                                            kwargs={
                                                'travel_pk':
                                                travel.id,
                                                'transition_name':
                                                'mark_as_completed'
                                            }),
                                        user=traveler,
                                        data=data)

        response_json = json.loads(response.rendered_content)
        partner_programmatic_visits_after_complete = PartnerOrganization.objects.get(
            id=act1.partner.id)
        partner_spot_checks_after_complete = PartnerOrganization.objects.get(
            id=act2.partner.id)
        self.assertEqual(response_json['status'], Travel.COMPLETED)
        self.assertEqual(
            partner_programmatic_visits.hact_values['programmatic_visits']
            ['completed']['total'] + 1,
            partner_programmatic_visits_after_complete.
            hact_values['programmatic_visits']['completed']['total'])
        self.assertEqual(
            partner_spot_checks.hact_values['spot_checks']['completed']
            ['total'] + 1,
            partner_spot_checks_after_complete.hact_values['spot_checks']
            ['completed']['total'])
Пример #16
0
    def test_completed_counts(self):
        currency = PublicsCurrencyFactory()
        dsa_region = PublicsDSARegionFactory()

        traveler = UserFactory(is_staff=True)
        traveler.profile.vendor_number = 'usrvend'
        traveler.profile.save()

        travel = TravelFactory(reference_number=make_travel_reference_number(),
                               traveler=traveler,
                               status=Travel.APPROVED,
                               supervisor=self.unicef_staff)
        data = {
            'itinerary': [{
                'origin': 'Berlin',
                'destination': 'Budapest',
                'departure_date': '2017-04-14T17:06:55.821490',
                'arrival_date': '2017-04-15T17:06:55.821490',
                'dsa_region': dsa_region.id,
                'overnight_travel': False,
                'mode_of_travel': ModeOfTravel.RAIL,
                'airlines': []
            }, {
                'origin': 'Budapest',
                'destination': 'Berlin',
                'departure_date': '2017-05-20T12:06:55.821490',
                'arrival_date': '2017-05-21T12:06:55.821490',
                'dsa_region': dsa_region.id,
                'overnight_travel': False,
                'mode_of_travel': ModeOfTravel.RAIL,
                'airlines': []
            }],
            'traveler':
            traveler.id,
            'ta_required':
            True,
            'report':
            'Some report',
            'currency':
            currency.id,
            'supervisor':
            self.unicef_staff.id
        }
        act1 = TravelActivityFactory(
            travel_type=TravelType.PROGRAMME_MONITORING,
            primary_traveler=traveler)
        act2 = TravelActivityFactory(travel_type=TravelType.SPOT_CHECK,
                                     primary_traveler=traveler)
        act1.travels.add(travel)
        act2.travels.add(travel)
        partner_programmatic_visits = PartnerOrganization.objects.get(
            id=act1.partner.id)
        partner_spot_checks = PartnerOrganization.objects.get(
            id=act2.partner.id)
        response = self.forced_auth_req(
            'post',
            reverse('t2f:travels:details:state_change',
                    kwargs={
                        'travel_pk': travel.id,
                        'transition_name': Travel.COMPLETE
                    }),
            user=traveler,
            data=data)

        response_json = json.loads(response.rendered_content)
        partner_programmatic_visits_after_complete = PartnerOrganization.objects.get(
            id=act1.partner.id)
        partner_spot_checks_after_complete = PartnerOrganization.objects.get(
            id=act2.partner.id)
        self.assertEqual(response_json['status'], Travel.COMPLETED)
        self.assertEqual(
            partner_programmatic_visits.hact_values['programmatic_visits']
            ['completed']['total'] + 1,
            partner_programmatic_visits_after_complete.
            hact_values['programmatic_visits']['completed']['total'])
        self.assertEqual(
            partner_spot_checks.hact_values['spot_checks']['completed']
            ['total'] + 1,
            partner_spot_checks_after_complete.hact_values['spot_checks']
            ['completed']['total'])
Пример #17
0
    def test_activity_export(self):
        tz = timezone.get_default_timezone()
        office = OfficeFactory(name='Budapest')
        section_health = SectorFactory(name='Health')
        section_education = SectorFactory(name='Education')

        location_ABC = LocationFactory(name='Location ABC')
        location_345 = LocationFactory(name='Location 345')
        location_111 = LocationFactory(name='Location 111')

        partnership_A1 = InterventionFactory(title='Partnership A1')
        partner = partnership_A1.agreement.partner
        partner.name = 'Partner A'
        partner.save()

        partnership_A2 = InterventionFactory(title='Partnership A2')
        agreement = partnership_A2.agreement
        agreement.partner = partner
        agreement.save()

        partnership_B3 = InterventionFactory(title='Partnership B3')
        partner = partnership_B3.agreement.partner
        partner.name = 'Partner B'
        partner.save()

        partnership_C1 = InterventionFactory(title='Partnership C1')
        partner = partnership_C1.agreement.partner
        partner.name = 'Partner C'
        partner.save()

        # Some results
        result_A11 = ResultFactory(name='Result A11')
        result_A21 = ResultFactory(name='Result A21')

        # set up travels
        user_joe_smith = UserFactory(first_name='Joe',
                                     last_name='Smith')
        user_alice_carter = UserFactory(first_name='Alice',
                                        last_name='Carter')
        user_lenox_lewis = UserFactory(first_name='Lenox',
                                       last_name='Lewis')
        travel_1 = TravelFactory(reference_number='2016/1000',
                                 traveler=user_joe_smith,
                                 office=office,
                                 sector=section_health,
                                 start_date=datetime(2017, 11, 8, tzinfo=tz),
                                 end_date=datetime(2017, 11, 14, tzinfo=tz),
                                 )
        supervisor = UserFactory()
        travel_2 = TravelFactory(reference_number='2016/1211',
                                 supervisor=supervisor,
                                 traveler=user_alice_carter,
                                 office=office,
                                 sector=section_education,
                                 start_date=datetime(2017, 11, 8, tzinfo=tz),
                                 end_date=datetime(2017, 11, 14, tzinfo=tz),
                                 )

        # Do some cleanup
        TravelActivity.objects.all().delete()

        # Create the activities finally
        activity_1 = TravelActivityFactory(travel_type=TravelType.PROGRAMME_MONITORING,
                                           date=datetime(2016, 12, 3, tzinfo=UTC),
                                           result=result_A11,
                                           primary_traveler=user_joe_smith)
        activity_1.travels.add(travel_1)
        activity_1.locations.set([location_ABC, location_345])
        activity_1.partner = partnership_A1.agreement.partner
        activity_1.partnership = partnership_A1
        activity_1.save()

        activity_2 = TravelActivityFactory(travel_type=TravelType.PROGRAMME_MONITORING,
                                           date=datetime(2016, 12, 4, tzinfo=UTC),
                                           result=result_A21,
                                           primary_traveler=user_lenox_lewis)
        activity_2.travels.add(travel_1)
        activity_2.locations.set([location_111])
        activity_2.partner = partnership_A2.agreement.partner
        activity_2.partnership = partnership_A2
        activity_2.save()

        activity_3 = TravelActivityFactory(travel_type=TravelType.MEETING,
                                           date=datetime(2016, 12, 3, tzinfo=UTC),
                                           result=None,
                                           primary_traveler=user_joe_smith)
        activity_3.travels.add(travel_1)
        activity_3.locations.set([location_ABC])
        activity_3.partner = partnership_B3.agreement.partner
        activity_3.partnership = partnership_B3
        activity_3.save()

        activity_4 = TravelActivityFactory(travel_type=TravelType.SPOT_CHECK,
                                           date=datetime(2016, 12, 6, tzinfo=UTC),
                                           result=None,
                                           primary_traveler=user_alice_carter)
        activity_4.travels.add(travel_2)
        activity_4.locations.set([location_111, location_345])
        activity_4.partner = partnership_C1.agreement.partner
        activity_4.partnership = partnership_C1
        activity_4.save()

        with self.assertNumQueries(6):
            response = self.forced_auth_req('get', reverse('t2f:travels:list:activity_export'),
                                            user=self.unicef_staff)
        export_csv = csv.reader(StringIO(response.content.decode('utf-8')))
        rows = [r for r in export_csv]

        self.assertEqual(len(rows), 5)

        # check header
        self.assertEqual(rows[0],
                         ['reference_number',
                          'traveler',
                          'office',
                          'section',
                          'status',
                          'trip_type',
                          'partner',
                          'partnership',
                          'results',
                          'locations',
                          'start_date',
                          'end_date',
                          'is_secondary_traveler',
                          'primary_traveler_name'])

        self.assertEqual(rows[1],
                         ['2016/1000',
                          'Joe Smith',
                          'Budapest',
                          'Health',
                          'planned',
                          'Programmatic Visit',
                          'Partner A',
                          'Partnership A1',
                          'Result A11',
                          'Location 345, Location ABC',
                          '08-Nov-2017',
                          '14-Nov-2017',
                          '',
                          ''])

        self.assertEqual(rows[2],
                         ['2016/1000',
                          'Joe Smith',
                          'Budapest',
                          'Health',
                          'planned',
                          'Programmatic Visit',
                          'Partner A',
                          'Partnership A2',
                          'Result A21',
                          'Location 111',
                          '08-Nov-2017',
                          '14-Nov-2017',
                          'YES',
                          'Lenox Lewis'])

        self.assertEqual(rows[3],
                         ['2016/1000',
                          'Joe Smith',
                          'Budapest',
                          'Health',
                          'planned',
                          'Meeting',
                          'Partner B',
                          'Partnership B3',
                          '',
                          'Location ABC',
                          '08-Nov-2017',
                          '14-Nov-2017',
                          '',
                          ''])

        self.assertEqual(rows[4],
                         ['2016/1211',
                          'Alice Carter',
                          'Budapest',
                          'Education',
                          'planned',
                          'Spot Check',
                          'Partner C',
                          'Partnership C1',
                          '',
                          'Location 111, Location 345',
                          '08-Nov-2017',
                          '14-Nov-2017',
                          '',
                          ''])