class StoresStoreModelTestCase(TestCase): """ Basic tests against the Store model """ fixtures = ['countries'] def setUp(self): country = Country.objects.get(name='United Kingdom') self.addr = Address(name='test', address1='Bridge Street', city='Warrington', country=country, postcode='WA3') self.addr.save() def test_slug_creation(self): obj = Store(name='Test Store 1', address=self.addr) obj.save() self.assertEqual(obj.slug, 'test-store-1') def test_absolute_url(self): obj = Store(name='Test Store 2', address=self.addr) obj.save() self.assertEqual(str(obj.get_absolute_url()), '/stores/test-store-2/') def test_chain_name(self): obj = Store(name='Test Store 3', address=self.addr) self.assertEqual(str(obj), 'Test Store 3')
def handle(self, *args, **options): fn = args[0] if fn.startswith('http'): self.stdout.write("Downloading %s\n" % fn) f = StringIO(requests.get(fn).text) else: self.stdout.write("Opening file %s\n" % fn) f = open(fn, 'r') self.stdout.write('Formatting data...') # Generate the dataset reader = csv.reader(f) headers = [h.lower().strip() for h in reader.next()] data = [] for row in reader: values = [] for h, v in zip(headers, row): if v == '': value = None else: value = v.strip() values.append((h, value)) data.append(dict(values)) self.stdout.write('%d entries found.' % len(data)) for row in data: self.stdout.write("Importing %s... " % row['name']) try: obj = Store.objects.get(name=row['name']) except Store.DoesNotExist: pass else: if obj.address.postcode == row['postcode']: self.stdout.write("Store by that name already exists, skipping.\n") continue with transaction.commit_on_success(): if not row['country']: row['country'] = 'United Kingdom' country, created = Country.objects.get_or_create(name=row['country']) county, created = County.objects.get_or_create(name=row['county'], country=country) addr = Address(name=row['name'], address1=row['address1'], address2=row['address2'], address3=row['address3'], city=row['city'], county=county, country=country, postcode=row['postcode'], geo_latitude=row['y'], geo_longitude=row['x']) addr.save(no_lookup=True) store = Store(name=row['name'], address=addr, website=row['website'], email=row['email'], phone=row['phone']) if row['type']: if row['type'] == 'Online': store.store_type = Store.STORE_TYPE_ONLINE if row['type'] == 'Retail': store.store_type = Store.STORE_TYPE_RETAIL if row['type'] == 'Both': store.store_type = Store.STORE_TYPE_BOTH elif row['website']: store.store_type = Store.STORE_TYPE_BOTH store.save() if row['twitter']: Link(object_id=store.pk, object_type=ContentType.objects.get_for_model(store.__class__), account_type=LinkType.objects.get(name='Twitter'), account_name=row['twitter'].split('/')[-1]).save() self.stdout.write("Done\n") f.close() self.stdout.write("Done\n")
def test_skip_geo_lookup(self): country = Country.objects.get(name='United Kingdom') addr = Address(name='test', address1='Bridge Street', city='Warrington', country=country, postcode='WA3') addr.save(no_lookup=True) self.assertIsNone(addr.geo_latitude) self.assertIsNone(addr.geo_longitude)
def setUp(self): country = Country.objects.get(name='United Kingdom') self.addr = Address(name='test', address1='Bridge Street', city='Warrington', country=country, postcode='WA3') self.addr.save()