def handle(self, *args, **options): if len(args) <= 0: raise CommandError('please specify file name') if not os.path.exists(args[0]): raise CommandError("file %s doesn't exist" % args[0]) with open(args[0], 'r') as f: result = json.load(f) counter = Counter() try: for i in result: try: place = Place.objects.get(vendor_id=i['id']) except Place.DoesNotExist: print "%s place id doesn't exist" % i['id'] counter["badid"] += 1 continue counter["Valid locations"] += 1 geo_result = geo_code(i['address'], i['city']) if geo_result["status"] != "OK": counter[geo_result["status"]] += 1 continue print "results for station #%s" % (i['id']) for l in geo_result["results"]: marker = Placemark() marker.place = place marker.city = i['city'] marker.address = i['address'] location = l["geometry"]["location"] marker.lat = location["lat"] marker.lng = location["lng"] try: marker.save() counter["Newly added"] += 1 print "\t %f ,%f saved successfully" % (marker.lat, marker.lng) except IntegrityError: counter["Already exist"] += 1 print "\t %f ,%f record is not unique" % (marker.lat, marker.lng) continue print "import markers completed" finally: for k, v in counter.items(): print k, v
def create_markers(places): counter = Counter() # TODO - change to regex blacklist = [u'מס.',u"מס'",u'א' ,u'ב' ,u'ג' ,u'ד',u"א'",u"ב'",u"ג'",u"ד'",u"ה'",u"רח'",u"רח"] for place in places: data = json.loads(place.data) address = re.sub('([\d]+)([\S]*)','\g<1>',data['address']) clean_address = ' '.join([x for x in address.split() if x not in blacklist]) geo_result = geo_code(clean_address, data['city']) city_result = geo_code(data['city'], data['city']) city_location = "" if city_result["status"] == "OK": city_location = city_result["results"][0]["geometry"]["location"] if geo_result["status"] != "OK": counter[geo_result["status"]] +=1 print "geo_result status of place id " + str(place.id) + "was " + geo_result["status"] continue for l in geo_result["results"]: location = l["geometry"]["location"] if location == city_location: counter["Failed"] += 1 continue marker = Placemark() marker.place = place marker.city = data['city'] marker.address = clean_address marker.lat = location["lat"] marker.lng = location["lng"] payload = {"size": "240x240", "location": clean_address + " " + data['city'], "sensor": "false"} print payload r = requests.get("http://maps.googleapis.com/maps/api/streetview", params=payload) print r.url input_file = StringIO(r.content) output_file = StringIO() img = Image.open(input_file) if img.mode != "RGB": img = img.convert("RGB") img.save(output_file, "JPEG") try: marker.save() counter["Newly added"] += 1 marker.image.save(str(marker.id) + ".jpg", ContentFile(output_file.getvalue()), save=True) except IntegrityError: counter["Already exist"] += 1 return counter
def handle(self, *args, **options): if len(args) <= 0: raise CommandError('please specify file name') if not os.path.exists(args[0]): raise CommandError("file %s doesn't exist" % args[0]) with open(args[0], 'r') as f: result = json.load(f) counter = Counter() try: for i in result: try: place = Place.objects.get(vendor_id=i['id']) except Place.DoesNotExist: print "%s place id doesn't exist" % i['id'] counter["badid"] += 1 continue counter["Valid locations"] += 1 geo_result = geo_code(i['address'], i['city']) if geo_result["status"] != "OK": counter[geo_result["status"]] +=1 continue print "results for station #%s" % (i['id']) for l in geo_result["results"]: marker = Placemark() marker.place = place marker.city = i['city'] marker.address = i['address'] location = l["geometry"]["location"] marker.lat = location["lat"] marker.lng = location["lng"] try: marker.save() counter["Newly added"] += 1 print "\t %f ,%f saved successfully" % (marker.lat, marker.lng) except IntegrityError: counter["Already exist"] += 1 print "\t %f ,%f record is not unique" % (marker.lat, marker.lng) continue print "import markers completed" finally: for k, v in counter.items(): print k, v
def addplacemark(request): if not request.method == 'POST': return HttpResponseNotAllowed("Wrong request method") if Placemark.objects.filter(place__id=request.POST['place'], address=request.POST['address'], city=request.POST['city'], lat=request.POST['lat'], lng=request.POST['lng']).exists(): return HttpResponse("exists") newplacemark = Placemark(); newplacemark.place_id = int(request.POST['place']) newplacemark.city = request.POST['city'] newplacemark.address = request.POST['address'] newplacemark.lat = float(request.POST['lat']) newplacemark.lng = float(request.POST['lng']) newplacemark.save() newvote = Vote() newvote.placemark = newplacemark newvote.user = request.user newvote.positive = 'True' newvote.save() return HttpResponse(newplacemark.id)
def addplacemark(request): if not request.method == 'POST': return HttpResponse("Wrong request method") if Placemark.objects.filter(place__id=request.POST['place'], address=request.POST['address'], city=request.POST['city'], lat=request.POST['lat'], lng=request.POST['lng']).exists(): return HttpResponse("exists") newplacemark = Placemark(); newplacemark.place_id = int(request.POST['place']) newplacemark.city = request.POST['city'] newplacemark.address = request.POST['address'] newplacemark.lat = float(request.POST['lat']) newplacemark.lng = float(request.POST['lng']) newplacemark.save() newvote = Vote() newvote.placemark = newplacemark newvote.user = request.user newvote.positive = 'True' newvote.save() return HttpResponse("OK")
def setUp(self): User.objects.create_user("segalle", email=None, password="******") User.objects.create_user("moshe", email=None, password="******") p = Place() p.vendor_id = 1000 p.data = { "city": "\u05d9\u05e8\u05d5\u05e9\u05dc\u05d9\u05dd", "name": "\u05e9\u05d9\u05da \u05d2'\u05e8\u05d0\u05d7", "district": "\u05d9\u05e8\u05d5\u05e9\u05dc\u05d9\u05dd", "phones": "02-5871923", "notes": "", "subdistrict": "\u05d9\u05e8\u05d5\u05e9\u05dc\u05d9\u05dd", "days": [ "8:00-17:00", "8:00-17:00", "8:00-17:00", "8:00-17:00", "8:00-13:00", "\u05e1\u05d2\u05d5\u05e8" ], "address": "\u05de\u05e8\u05db\u05d6 \u05e8\u05e4\u05d5\u05d0\u05d9", "owner": "\u05e2\u05d9\u05e8\u05d9\u05d9\u05ea \u05d9\u05e8\u05d5\u05e9\u05dc\u05d9\u05dd", "id": 1000 } p.save() self.pm = Placemark() self.pm.place = p self.pm.city = "\u05d9\u05e8\u05d5\u05e9\u05dc\u05d9\u05dd" self.pm.address = "\u05de\u05e8\u05db\u05d6 \u05e8\u05e4\u05d5\u05d0\u05d9" self.pm.lat = 31.15 self.pm.lng = 32.16 self.pm.save() self.pm1 = Placemark() self.pm1.place = p self.pm1.city = "\u05d9\u05e8\u05d5\u05e9\u05dc\u05d9\u05dd" self.pm1.address = "\u05de\u05e8\u05db\u05d6 \u05e8\u05e4\u05d5\u05d0\u05d9" self.pm1.lat = 31.25 self.pm1.lng = 32.26 self.pm1.save() self.pm2 = Placemark() self.pm2.place = p self.pm2.city = "\u05d9\u05e8\u05d5\u05e9\u05dc\u05d9\u05dd" self.pm2.address = "\u05de\u05e8\u05db\u05d6 \u05e8\u05e4\u05d5\u05d0\u05d9" self.pm2.lat = 31.35 self.pm2.lng = 32.36 self.pm2.save()
class PlacesTest(TestCase): def setUp(self): User.objects.create_user("segalle", email=None, password="******") User.objects.create_user("moshe", email=None, password="******") p = Place() p.vendor_id = 1000 p.data = { "city": "\u05d9\u05e8\u05d5\u05e9\u05dc\u05d9\u05dd", "name": "\u05e9\u05d9\u05da \u05d2'\u05e8\u05d0\u05d7", "district": "\u05d9\u05e8\u05d5\u05e9\u05dc\u05d9\u05dd", "phones": "02-5871923", "notes": "", "subdistrict": "\u05d9\u05e8\u05d5\u05e9\u05dc\u05d9\u05dd", "days": [ "8:00-17:00", "8:00-17:00", "8:00-17:00", "8:00-17:00", "8:00-13:00", "\u05e1\u05d2\u05d5\u05e8" ], "address": "\u05de\u05e8\u05db\u05d6 \u05e8\u05e4\u05d5\u05d0\u05d9", "owner": "\u05e2\u05d9\u05e8\u05d9\u05d9\u05ea \u05d9\u05e8\u05d5\u05e9\u05dc\u05d9\u05dd", "id": 1000 } p.save() self.pm = Placemark() self.pm.place = p self.pm.city = "\u05d9\u05e8\u05d5\u05e9\u05dc\u05d9\u05dd" self.pm.address = "\u05de\u05e8\u05db\u05d6 \u05e8\u05e4\u05d5\u05d0\u05d9" self.pm.lat = 31.15 self.pm.lng = 32.16 self.pm.save() self.pm1 = Placemark() self.pm1.place = p self.pm1.city = "\u05d9\u05e8\u05d5\u05e9\u05dc\u05d9\u05dd" self.pm1.address = "\u05de\u05e8\u05db\u05d6 \u05e8\u05e4\u05d5\u05d0\u05d9" self.pm1.lat = 31.25 self.pm1.lng = 32.26 self.pm1.save() self.pm2 = Placemark() self.pm2.place = p self.pm2.city = "\u05d9\u05e8\u05d5\u05e9\u05dc\u05d9\u05dd" self.pm2.address = "\u05de\u05e8\u05db\u05d6 \u05e8\u05e4\u05d5\u05d0\u05d9" self.pm2.lat = 31.35 self.pm2.lng = 32.36 self.pm2.save() def test_votes(self): c = Client() c.login(username="******", password="******") response = c.post('/vote/', {'id': str(self.pm.id), 'positive': 'True'}) self.assertEqual(response.content, "OK") def test_login(self): c = Client() response = c.post('/vote/', {'id': str(self.pm.id), 'positive': 'True'}) self.assertRedirects(response, '/login/?next=/vote/') def test_double_entry(self): c = Client() c.login(username="******", password="******") response = c.post('/vote/', {'id': str(self.pm1.id), 'positive': 'True'}) response1 = c.post('/vote/', {'id': str(self.pm1.id), 'positive': 'True'}) self.assertEqual(response.content, "OK") self.assertEqual(response1.content, "Updated") def test_request_method(self): c = Client() c.login(username="******", password="******") response = c.post('/vote/', {'id': str(self.pm1.id), 'positive': 'True'}) response1 = c.get('/vote/', {'id': str(self.pm1.id), 'positive': 'True'}) self.assertEqual(response.content, "OK") self.assertEqual(response1.content, "Wrong request method") def test_addplacemark(self): c = Client() c.login(username="******", password="******") response = c.post('/addplacemark/', { 'place': self.pm.place.id, 'address': self.pm.address, 'city': self.pm.city, 'lat': self.pm.lat, 'lng': self.pm.lng }) self.assertEqual(response.content, "exists") response1= c.post('/addplacemark/', { 'place': self.pm.place.id, 'address': self.pm.address, 'city': u'Tel Aviv', 'lat': self.pm.lat, 'lng': self.pm.lng }) self.assertEqual(response1.content, "OK") response2= c.post('/addplacemark/', { 'place': self.pm.place.id, 'address': self.pm.address, 'city': u'Tel Aviv', 'lat': self.pm.lat, 'lng': self.pm.lng }) self.assertEqual(response2.content, "exists") response3= c.post('/addplacemark/', { 'place': 99999, 'address': self.pm.address, 'city': u'Tel Aviv', 'lat': self.pm.lat, 'lng': self.pm.lng }) self.assertEqual(response3.content, "OK") def test_leading_placemark(self): c = Client() c.login(username="******", password="******") response = c.post('/vote/', {'id': str(self.pm.id), 'positive': 'True'}) response = c.post('/vote/', {'id': str(self.pm1.id), 'positive': 'True'}) response = c.post('/vote/', {'id': str(self.pm2.id), 'positive': 'false'}) c.logout() c.login(username="******", password="******") response = c.post('/vote/', {'id': str(self.pm.id), 'positive': 'True'}) response = c.post('/vote/', {'id': str(self.pm1.id), 'positive': 'false'}) response = c.post('/vote/', {'id': str(self.pm2.id), 'positive': 'false'}) c.logout() place = Place.objects.get(vendor_id=1000) leading = Place.get_leading_placemark(place) self.assertEqual(leading.id,1) def test_export_feature(self): c = Client() c.login(username="******", password="******") response = c.post('/vote/', {'id': str(self.pm.id), 'positive': 'True'}) response = c.post('/vote/', {'id': str(self.pm1.id), 'positive': 'True'}) response = c.post('/vote/', {'id': str(self.pm2.id), 'positive': 'false'}) c.logout() c.login(username="******", password="******") response = c.post('/vote/', {'id': str(self.pm.id), 'positive': 'True'}) response = c.post('/vote/', {'id': str(self.pm1.id), 'positive': 'false'}) response = c.post('/vote/', {'id': str(self.pm2.id), 'positive': 'false'}) c.logout() place = Place.objects.get(vendor_id=1000) feature = Place.export_feature(place) print feature self.assertEqual(1,1)
class PlacesTest(TestCase): def setUp(self): User.objects.create_user("segalle", email=None, password="******") User.objects.create_user("moshe", email=None, password="******") p = Place() p.vendor_id = 1000 p.data = { "city": "\u05d9\u05e8\u05d5\u05e9\u05dc\u05d9\u05dd", "name": "\u05e9\u05d9\u05da \u05d2'\u05e8\u05d0\u05d7", "district": "\u05d9\u05e8\u05d5\u05e9\u05dc\u05d9\u05dd", "phones": "02-5871923", "notes": "", "subdistrict": "\u05d9\u05e8\u05d5\u05e9\u05dc\u05d9\u05dd", "days": [ "8:00-17:00", "8:00-17:00", "8:00-17:00", "8:00-17:00", "8:00-13:00", "\u05e1\u05d2\u05d5\u05e8" ], "address": "\u05de\u05e8\u05db\u05d6 \u05e8\u05e4\u05d5\u05d0\u05d9", "owner": "\u05e2\u05d9\u05e8\u05d9\u05d9\u05ea \u05d9\u05e8\u05d5\u05e9\u05dc\u05d9\u05dd", "id": 1000 } p.save() self.pm = Placemark() self.pm.place = p self.pm.city = "\u05d9\u05e8\u05d5\u05e9\u05dc\u05d9\u05dd" self.pm.address = "\u05de\u05e8\u05db\u05d6 \u05e8\u05e4\u05d5\u05d0\u05d9" self.pm.lat = 31.15 self.pm.lng = 32.16 self.pm.save() self.pm1 = Placemark() self.pm1.place = p self.pm1.city = "\u05d9\u05e8\u05d5\u05e9\u05dc\u05d9\u05dd" self.pm1.address = "\u05de\u05e8\u05db\u05d6 \u05e8\u05e4\u05d5\u05d0\u05d9" self.pm1.lat = 31.25 self.pm1.lng = 32.26 self.pm1.save() self.pm2 = Placemark() self.pm2.place = p self.pm2.city = "\u05d9\u05e8\u05d5\u05e9\u05dc\u05d9\u05dd" self.pm2.address = "\u05de\u05e8\u05db\u05d6 \u05e8\u05e4\u05d5\u05d0\u05d9" self.pm2.lat = 31.35 self.pm2.lng = 32.36 self.pm2.save() def test_votes(self): c = Client() c.login(username="******", password="******") response = c.post('/vote/', {'id': str(self.pm.id), 'positive': 'True'}) self.assertEqual(response.content, "OK") def test_login(self): c = Client() response = c.post('/vote/', {'id': str(self.pm.id), 'positive': 'True'}) self.assertRedirects(response, '/login/?next=/vote/') def test_double_entry(self): c = Client() c.login(username="******", password="******") response = c.post('/vote/', {'id': str(self.pm1.id), 'positive': 'True'}) response1 = c.post('/vote/', {'id': str(self.pm1.id), 'positive': 'True'}) self.assertEqual(response.content, "OK") self.assertEqual(response1.content, "Updated") def test_request_method(self): c = Client() c.login(username="******", password="******") response = c.post('/vote/', {'id': str(self.pm1.id), 'positive': 'True'}) response1 = c.get('/vote/', {'id': str(self.pm1.id), 'positive': 'True'}) self.assertEqual(response.content, "OK") self.assertEqual(response1.content, "Wrong request method") def test_addplacemark(self): c = Client() c.login(username="******", password="******") response = c.post('/addplacemark/', { 'place': self.pm.place.id, 'address': self.pm.address, 'city': self.pm.city, 'lat': self.pm.lat, 'lng': self.pm.lng }) self.assertEqual(response.content, "exists") response1= c.post('/addplacemark/', { 'place': self.pm.place.id, 'address': self.pm.address, 'city': u'Tel Aviv', 'lat': self.pm.lat, 'lng': self.pm.lng }) self.assertEqual(response1.content, "OK") response2= c.post('/addplacemark/', { 'place': self.pm.place.id, 'address': self.pm.address, 'city': u'Tel Aviv', 'lat': self.pm.lat, 'lng': self.pm.lng }) self.assertEqual(response2.content, "exists") response3= c.post('/addplacemark/', { 'place': 99999, 'address': self.pm.address, 'city': u'Tel Aviv', 'lat': self.pm.lat, 'lng': self.pm.lng }) self.assertEqual(response3.content, "OK") def test_leading_placemark(self): c = Client() c.login(username="******", password="******") response = c.post('/vote/', {'id': str(self.pm.id), 'positive': 'True'}) response = c.post('/vote/', {'id': str(self.pm1.id), 'positive': 'True'}) response = c.post('/vote/', {'id': str(self.pm2.id), 'positive': 'false'}) c.logout() c.login(username="******", password="******") response = c.post('/vote/', {'id': str(self.pm.id), 'positive': 'True'}) response = c.post('/vote/', {'id': str(self.pm1.id), 'positive': 'false'}) response = c.post('/vote/', {'id': str(self.pm2.id), 'positive': 'false'}) c.logout() place = Place.objects.get(vendor_id=1000) leading = Place.get_leading_placemark(place) self.assertEqual(leading.id,1) def test_export_feature(self): c = Client() c.login(username="******", password="******") response = c.post('/vote/', {'id': str(self.pm.id), 'positive': 'True'}) response = c.post('/vote/', {'id': str(self.pm1.id), 'positive': 'True'}) response = c.post('/vote/', {'id': str(self.pm2.id), 'positive': 'false'}) c.logout() c.login(username="******", password="******") response = c.post('/vote/', {'id': str(self.pm.id), 'positive': 'True'}) response = c.post('/vote/', {'id': str(self.pm1.id), 'positive': 'false'}) response = c.post('/vote/', {'id': str(self.pm2.id), 'positive': 'false'}) c.logout() place = Place.objects.get(vendor_id=1000) feature = Place.export_feature(place) print feature self.assertEqual(1,1) def test_file_handler(self): # f = tempfile.TemporaryFile() # f.file.write("""id,title,address,city\n # 1,Home,Ha'tkufa 12,Jerusalem\n # 2,Apt,Nisim Bachar 5,Jerusalem""") # # How to send the file object?? working weird... # data = handleUploadedFile(f.file) # expectedList = [ { "id":1 , "title":"Home", "address":"Ha'tkufa 12", "city":"Jerusalem" }, # { "id":2 , "title":"Apt", "address":"Nisim Bachar 55", "city":"Jerusalem" } ] # self.assertListEqual(data, expectedList, "File handler failed") self.assertEqual(1, 1)