def add_business(request): try: user = authenticate_api_request(request) authorize_user(user, request, "add") bus = Business() name=get_json_post_or_error('businessName', request) addr=get_json_post_or_error('streetAddr', request) city = get_json_post_or_error('businessCity', request) state = get_json_post_or_error('businessState', request) phone = get_json_post_or_error('businessPhone', request) #already exists if Business.objects.filter(name=name,address=addr,city=city,state=state).count() == 0: bus = Business(name=name,address=addr,city=city,state=state) bus.save() elif Business.objects.filter(name=name,address=addr,city=city,state=state).count() > 1: #too many Business.objects.filter(name=name,address=addr,city=city,state=state).delete() Business.objects.create(name=name,address=addr,city=city,state=state,phone=phone) else: bus = Business.objects.get(name=name,address=addr,city=city,state=state) except ReadJSONError as e: return server_error(e.value) except (AuthenticationFailed, AuthorizationError) as e: return server_error(e.value) bus.dist = distance.distance(user.current_location,(bus.lat,bus.lon)).miles bus_data = get_single_bus_data_ios(bus,user) return server_data(bus_data)
def create_business(name, address, state, city, lat, lon): bset = Business.objects.filter(name=name,address=address,state=state,city=city) if bset.count() > 0: return b = Business(name=name.encode("utf8"), city=city.encode("utf8"), state=state, address=address.encode("utf8"), lat=lat, lon=lon) b.save() setBusLatLng(b) add_tag_to_bus(b,get_master_summary_tag()) return b
def prepop_businesses(user): if user == None: user = get_default_user() reader = csv.reader(open(settings.BASE_DIR+'/prepop/businesses.csv', 'U'), delimiter=',', quotechar='"') i = 0 for row in reader: i+=1 if i == 1: continue name = row[0] addr = row[1] city = row[2] state = row[3] phurl = row[4] print('name: '+str(name)) print('addr: '+str(addr)) print('city: '+str(city)) print('state: '+str(state)) bset = Business.objects.filter(name=name,address=addr,state=state,city=city) if bset.count() == 0: b = Business(name=name.encode("utf8"), city=city.encode("utf8"), state=state, address=addr.encode("utf8"), lat=0, lon=0) b.save() elif bset.count() > 1: Business.objects.filter(name=name.encode("utf8"), city=city.encode("utf8"), state=state, address=addr.encode("utf8")).delete() b = Business(name=name.encode("utf8"), city=city.encode("utf8"), state=state, address=addr.encode("utf8"), lat=0, lon=0) b.save() else: b = bset[0] setBusLatLng(b) add_tag_to_bus(b, get_master_summary_tag(), get_default_user()) add_photo_by_url(phurl,b,user,default=True)