def create_countries(): """Populate the DB with countries.""" for iso_code, _ in ISO_3166_COUNTRIES: Country.objects.get_or_create( iso_code=iso_code, defaults=Country.fields_from_iso_code(iso_code) )
def location_data(location_tree): if location_tree is None: return [] iso_code = find_text(location_tree, 'iso_code').lower() country, created = Country.objects.get_or_create(**Country.fields_from_iso_code(iso_code)) country = country.id latitude = find_text(location_tree, 'latitude') or 0 longitude = find_text(location_tree, 'longitude') or 0 primary = True return [dict(latitude=latitude, longitude=longitude, country=country, primary=primary)]
def custom_get_or_create_country(iso_code, country=None): """ add the missing fields to a skeleton country object from the admin or create a new one with the given iso_code if it doesn't already exist """ # for some reason, maybe some circular import issue importing Country at the module level doesn't work from akvo.rsr.models import Country iso_code = iso_code.lower() if not country: try: country = Country.objects.get(iso_code=iso_code) return country except: country = Country() country.iso_code = iso_code continent_code = COUNTRY_CONTINENTS[iso_code] country.name = dict(ISO_3166_COUNTRIES)[iso_code] country.continent = dict(CONTINENTS)[continent_code] country.continent_code = continent_code country.save() return country
def custom_get_or_create_country(iso_code, country=None): """ add the missing fields to a skeleton country object from the admin or create a new one with the given iso_code if it doesn't already exist """ # Importing Country at the module level doesn't work, because of circular imports from akvo.rsr.models import Country iso_code = iso_code.lower() if not country: try: country = Country.objects.get(iso_code=iso_code) return country except: country = Country() country.iso_code = iso_code continent_code = COUNTRY_CONTINENTS[iso_code] country.name = dict(ISO_3166_COUNTRIES)[iso_code] country.continent = dict(CONTINENTS)[continent_code] country.continent_code = continent_code country.save() return country
def test_should_create_organisation_location(self): # Given country, _ = Country.objects.get_or_create( **Country.fields_from_iso_code('ng')) url = '/rest/v1/organisation_location/?format=json' data = { "latitude": 6.275766, "longitude": 7.006839, "city": "Ukpo", "location_target": self.org.id, "iati_country": "NG", "country": country.pk, "postcode": "101010" } # When response = self.c.post(url, data) # Then self.assertEqual(response.status_code, 201) for key in data: self.assertEqual(data[key], response.data[key], '{} has diefferent values'.format(key))
def populate_test_data(seed=42): """Populate the DB for tests using the factories defined.""" reseed_random(seed) random.seed(seed) check_auth_groups(settings.REQUIRED_AUTH_GROUPS) UserFactory.create(is_admin=True, is_superuser=True, is_staff=True) UserFactory.create_batch(4) OrganisationFactory.create_batch(3) OrganisationCustomFieldFactory.create_batch(9) ProjectFactory.create_batch(10) RecipientCountryFactory.create_batch(10) CrsAddFactory.create_batch(10) FssFactory.create_batch(10) PartnershipFactory.create_batch(10) ProjectCustomFieldFactory.create_batch(20) ProjectCommentFactory.create_batch(20) RelatedProjectFactory.create_batch(10) ProjectUpdateFactory.create_batch(100) ResultFactory.create_batch(40) IndicatorFactory.create_batch(80) IndicatorReferenceFactory.create_batch(80) IndicatorPeriodFactory.create_batch(240) IndicatorPeriodActualDimensionFactory.create_batch(240) IndicatorPeriodTargetDimensionFactory.create_batch(240) IndicatorPeriodActualLocationFactory.create_batch(240) IndicatorPeriodTargetLocationFactory.create_batch(240) IndicatorPeriodDataFactory.create_batch(1200) IndicatorPeriodDataCommentFactory.create_batch(1200) IatiExportFactory.create_batch(3) IatiImportFactory.create_batch(3) for _ in range(10): created = False while not created: country_code = random.choice(ISO_3166_COUNTRIES)[0] country_info = Country.fields_from_iso_code(country_code) country, created = Country.objects.get_or_create(**country_info) EmploymentFactory.create_batch(30) KeywordFactory.create_batch(20) for keyword in Keyword.objects.all(): for project in Project.objects.all(): if keyword.id % project.id != 0: continue project.keywords.add(keyword) ProjectEditorValidationSetFactory.create_batch(2) ProjectEditorValidationFactory.create_batch(20) ReportFormatFactory.create_batch(4) ReportFactory.create_batch(4) # FIXME: Enforce this! verify_model_instances()
def create_countries(): """Populate the DB with countries.""" for iso_code, _ in ISO_3166_COUNTRIES: Country.objects.get_or_create( iso_code=iso_code, defaults=Country.fields_from_iso_code(iso_code))