Example #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"])
Example #2
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()
Example #3
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"])
Example #4
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',
                          '',
                          ''])