Exemplo n.º 1
0
 def test_codes_as_list(self):
     """
     Test `OfficeAdminExtraGeoLocation.codes_as_list()`.
     """
     codes = "   57616\n\n\n\n\n\n     75110  \n  54      "
     codes_as_list = OfficeAdminExtraGeoLocation.codes_as_list(codes)
     self.assertEqual(sorted(codes_as_list), ['54', '57616', '75110'])
     codes = "75\r57\n13"
     codes_as_list = OfficeAdminExtraGeoLocation.codes_as_list(codes)
     self.assertEqual(sorted(codes_as_list), ['13', '57', '75'])
Exemplo n.º 2
0
    def test_update_offices_geolocations(self):
        """
        Test `update_offices_geolocations`.
        """
        # Add an entry in the OfficeAdminExtraGeoLocation table with extra geolocations.
        extra_geolocation = OfficeAdminExtraGeoLocation(
            siret=self.office1.siret,
            codes="75110\n13055",  # Paris 10 + Marseille
        )
        extra_geolocation.save(commit=True)

        script.update_offices_geolocations()

        # The office should now have 3 geolocations in ES (the original one + Paris 10 + Marseille).
        res = self.es.get(index=settings.ES_INDEX,
                          doc_type=es.OFFICE_TYPE,
                          id=self.office1.siret)
        expected_locations = [
            {
                'lat': 43.25996690043557,
                'lon': 5.370740865779022
            },
            {
                'lat': 48.8815994262695,
                'lon': 2.36229991912841
            },
            {
                'lat': 49.1044,
                'lon': 6.17952
            },
        ]
        self.assertEqual(
            sorted(res['_source']['locations'],
                   key=lambda x: (x['lat'], x['lon'])), expected_locations)

        office = Office.get(self.office1.siret)
        self.assertTrue(office.has_multi_geolocations)

        # Make `extra_geolocation` instance out-of-date.
        extra_geolocation.date_end = datetime.datetime.now(
        ) - datetime.timedelta(days=1)
        extra_geolocation.update()
        self.assertTrue(extra_geolocation.is_outdated())

        script.update_offices_geolocations()

        # The office extra geolocations should now be reset.
        res = self.es.get(index=settings.ES_INDEX,
                          doc_type=es.OFFICE_TYPE,
                          id=self.office1.siret)
        expected_locations = [
            {
                'lat': 49.1044,
                'lon': 6.17952
            },
        ]
        self.assertEqual(res['_source']['locations'], expected_locations)

        office = Office.get(self.office1.siret)
        self.assertFalse(office.has_multi_geolocations)
Exemplo n.º 3
0
 def test_codes_as_geolocations(self):
     """
     Test `OfficeAdminExtraGeoLocation.codes_as_geolocations()`.
     """
     codes = "75\n57616"
     codes_as_geolocations = OfficeAdminExtraGeoLocation.codes_as_geolocations(
         codes)
     expected = [
         # Found for the departement 75.
         (48.8274002075195, 2.36660003662109),
         (48.8367004394531, 2.39689993858337),
         (48.8611984252929, 2.3833999633789),
         (48.8815994262695, 2.36229991912841),
         (48.8866996765136, 2.30349993705749),
         (48.8712005615234, 2.28929996490478),
         (48.8445014953613, 2.29769992828369),
         (48.8316993713378, 2.32319998741149),
         (48.8825988769531, 2.39109992980957),
         (48.8917007446289, 2.35100007057189),
         (48.8646011352539, 2.40639996528625),
         (48.84495371275856, 2.3760858842364394),
         (48.872200012207, 2.31680011749267),
         (48.8801002502441, 2.34039998054504),
         (48.8544006347656, 2.36240005493164),
         (48.8446998596191, 2.35419988632202),
         (48.8470001220703, 2.33459997177124),
         (48.8564987182617, 2.31369996070861),
         (48.8536415100097, 2.34842991828918),
         (48.8694992065429, 2.34479999542236),
         (48.8627014160156, 2.3652000427246),
         # Found for 57616.
         (49.135208952059884, 6.207906756168173),
     ]
     self.assertEqual(sorted(expected), sorted(codes_as_geolocations))
Exemplo n.º 4
0
 def test_codes_as_json_geolocations(self):
     """
     Test `OfficeAdminExtraGeoLocation.codes_as_json_geolocations()`.
     """
     codes = "75110"
     codes_as_json_geolocations = OfficeAdminExtraGeoLocation.codes_as_json_geolocations(
         codes)
     expected = '[[48.8815994262695, 2.36229991912841]]'
     self.assertEqual(expected, codes_as_json_geolocations)
Exemplo n.º 5
0
 def test_is_outdated(self):
     """
     Test `OfficeAdminExtraGeoLocation.is_outdated()`.
     """
     extra_geolocation = OfficeAdminExtraGeoLocation(
         siret="38524664000176",
         codes="75108",
     )
     extra_geolocation.save()
     self.assertFalse(extra_geolocation.is_outdated())
     # Make `extra_geolocation` instance out-of-date.
     extra_geolocation.date_end = datetime.datetime.now(
     ) - datetime.timedelta(days=1)
     extra_geolocation.update()
     self.assertTrue(extra_geolocation.is_outdated())
    def validate_form(self, form):
        is_valid = super(OfficeAdminExtraGeoLocationModelView, self).validate_form(form)
        if is_valid and form.data.get('date_end'):
            if datetime.datetime.utcnow() >= form.data['date_end']:
                msg = ("La date de fin doit être dans le futur.")
                flash(msg, 'error')
                return False
        if is_valid and form.data.get('codes'):
            for code in OfficeAdminExtraGeoLocation.codes_as_list(form.data['codes']):
                if not any([geocoding.is_departement(code), geocoding.is_commune_id(code)]):
                    msg = (
                        "`%s` n'est pas un code commune (INSEE) ou un numéro de département valide."
                        "<br>"
                        "Assurez-vous de ne saisir qu'un élément par ligne."
                        % code
                    )
                    flash(Markup(msg), 'error')
                    return False

        return is_valid
Exemplo n.º 7
0
 def test_clean(self):
     """
     Test `OfficeAdminExtraGeoLocation.clean()`.
     """
     extra_geolocation = OfficeAdminExtraGeoLocation(
         siret="38524664000176",
         codes="75110\n\n\n\n\n\n\n57616",
         reason="Paris 10 + Metz Saint Julien",
     )
     db_session.add(extra_geolocation)
     db_session.commit()
     # The `clean()` method should have been called automatically.
     extra_geolocation = db_session.query(
         OfficeAdminExtraGeoLocation).first()
     # Multiple newlines should have been removed.
     self.assertEqual(extra_geolocation.codes, '57616\n75110')
     # Corresponding Lat/Lon coords should have been found and stored.
     self.assertEqual(
         extra_geolocation.geolocations,
         '[[49.135208952059884, 6.207906756168173], [48.8815994262695, 2.36229991912841]]'
     )