Exemple #1
0
def process_export(request):
	if 'export' in request.FILES:
		# back up the old database just in case

		sys.stdout = open('/tmp/locator_backup.json', 'w')
		call_command('dumpdata', 'locus', format='json', indent=2, interactive=False, all=True)
		sys.stdout.close()
		sys.stdout = sys.__stdout__

		locator_dataset_number = request.POST.get('locator_dataset', '1')
		locator_dataset = Promotion.objects.get(id=locator_dataset_number)
		f = open('/tmp/locator_export.txt', 'w')
		for chunk in request.FILES['export'].chunks():
			f.write(chunk)
		f.close()

		import csv
		import os
		import re
		from cartridge.shop.models import ProductVariation
		from django.template.defaultfilters import slugify

		f = open('/tmp/locator_export.txt', 'r')
		notfound = open('/tmp/notfound.txt', 'w')

		csvreader = csv.reader(f, delimiter="\t")

		# hide all locations (so that locations that haven't recieved new inventory won't appear in searches)
		# also clear out the inventory
		for location in Location.objects.all_with_deleted().filter(promotion = locator_dataset):
			location.inventory.clear()
			location.categories.clear()
			location.filtervalues.clear()
			location.do_not_display = True
			location.audio = False
			location.wireless = False
			location.hartke = False
			location.zoom = False
			location.save()

		d = Location()
		count = 0

		for row in csvreader:
			count += 1
			if row[0] == 'fiscal':
				continue
			if d.account_id != row[1] or d.account_key != row[3]:
				try:
					d = Location.objects.get(account_id = row[1], account_key = row[3])
				except Location.DoesNotExist:
					d = Location()
				id = d.id
				d.account_id = row[1]
				d.account_key = row[3]
				d.name = row[4]
				d.addr1 = row[5]
				d.addr2 = row[6]
				d.addr3 = row[7]
				d.city = row[8]
				d.state = row[9][:2]
				d.zip = row[10]
				d.save()
			else:
				print 'skip', row[1]

			d.filtervalues.add(locator_dataset)
			d.save()

			if row[11] == 'A':
				d.audio = True
			if row[11] == 'W':
				d.wireless = True
			if row[11] == 'H':
				d.hartke = True
			if row[11] == 'Z':
				d.zoom = True

			try:
				v = ProductVariation.objects.get(sku = row[12])
				p = v.product
				d.inventory.add(p)
				categories = Category.objects.filter(categories__products = p)
				for category in categories:
					d.categories.add(category)
				d.do_not_display = False
			except ProductVariation.DoesNotExist:
				print "couldn't find ", row[12]
				notfound.write(row[12] + "\n")

			d.save()

		for d in Location.objects.filter(latitude=None):
			d.lookup_address()
			d.save()
		return HttpResponseRedirect("/wheretobuy/ranged_geotagger?start=0&end=10000")
	return HttpResponse("Must upload a file")
Exemple #2
0
    reader = csv.reader(csvfile, delimiter=',', quotechar='"')
    for row in reader:
        try:
            loc = Location()

            loc.longitude = float(row[0])
            loc.latitude = float(row[1])
            loc.name = row[2]

            address = row[3].split(',')

            loc.addr1 = address[0]

            if len(address) > 6:
                # there is an addr2+
                loc.addr2 = address[1]

            loc.city = address[-5]
            loc.state = address[-4]
            loc.zip = address[-3]
            loc.phone = address[-2]

            loc.save()

            loc.datasets.add(dataset)
            loc.categories.add(choice(categories))
            loc.categories.add(choice(categories))

        except Exception as e:
            print e
Exemple #3
0
def process_export(request):
    if 'export' in request.FILES:
        # back up the old database just in case

        sys.stdout = open('/tmp/locator_backup.json', 'w')
        call_command('dumpdata',
                     'locus',
                     format='json',
                     indent=2,
                     interactive=False,
                     all=True)
        sys.stdout.close()
        sys.stdout = sys.__stdout__

        locator_dataset_number = request.POST.get('locator_dataset', '1')
        locator_dataset = Promotion.objects.get(id=locator_dataset_number)
        f = open('/tmp/locator_export.txt', 'w')
        for chunk in request.FILES['export'].chunks():
            f.write(chunk)
        f.close()

        import csv
        import os
        import re
        from cartridge.shop.models import ProductVariation
        from django.template.defaultfilters import slugify

        f = open('/tmp/locator_export.txt', 'r')
        notfound = open('/tmp/notfound.txt', 'w')

        csvreader = csv.reader(f, delimiter="\t")

        # hide all locations (so that locations that haven't recieved new inventory won't appear in searches)
        # also clear out the inventory
        for location in Location.objects.all_with_deleted().filter(
                promotion=locator_dataset):
            location.inventory.clear()
            location.categories.clear()
            location.filtervalues.clear()
            location.do_not_display = True
            location.audio = False
            location.wireless = False
            location.hartke = False
            location.zoom = False
            location.save()

        d = Location()
        count = 0

        for row in csvreader:
            count += 1
            if row[0] == 'fiscal':
                continue
            if d.account_id != row[1] or d.account_key != row[3]:
                try:
                    d = Location.objects.get(account_id=row[1],
                                             account_key=row[3])
                except Location.DoesNotExist:
                    d = Location()
                id = d.id
                d.account_id = row[1]
                d.account_key = row[3]
                d.name = row[4]
                d.addr1 = row[5]
                d.addr2 = row[6]
                d.addr3 = row[7]
                d.city = row[8]
                d.state = row[9][:2]
                d.zip = row[10]
                d.save()
            else:
                print 'skip', row[1]

            d.filtervalues.add(locator_dataset)
            d.save()

            if row[11] == 'A':
                d.audio = True
            if row[11] == 'W':
                d.wireless = True
            if row[11] == 'H':
                d.hartke = True
            if row[11] == 'Z':
                d.zoom = True

            try:
                v = ProductVariation.objects.get(sku=row[12])
                p = v.product
                d.inventory.add(p)
                categories = Category.objects.filter(categories__products=p)
                for category in categories:
                    d.categories.add(category)
                d.do_not_display = False
            except ProductVariation.DoesNotExist:
                print "couldn't find ", row[12]
                notfound.write(row[12] + "\n")

            d.save()

        for d in Location.objects.filter(latitude=None):
            d.lookup_address()
            d.save()
        return HttpResponseRedirect(
            "/wheretobuy/ranged_geotagger?start=0&end=10000")
    return HttpResponse("Must upload a file")