def validate_phone(request): local_phone = request.POST.get('local_phone') verification_code = int(request.POST.get('verification_code', -1)) country = get_object_or_404(Country, code=request.POST.get('country_code', "")) stored_code, stored_phone = request.session.get(SESSION_VERIFICATION_KEY, (None, None)) intl_phone_number = get_international_phone(country, local_phone) if not (stored_code and stored_phone): return HttpResponseBadRequest( _("Error validating phone (check that your browser accepts cookies)" )) if intl_phone_number != stored_phone or verification_code != int( stored_code): return HttpResponseBadRequest(_("Invalid verification code")) # there is a user if request.user.is_authenticated(): #TODO_WB: check if user already has a passenger try: # has a passenger? update phone passenger = Passenger.objects.get(user=request.user) passenger.phone = local_phone passenger.phone_verified = True passenger.save() return HttpResponse(local_phone) except Passenger.DoesNotExist: # create passenger passenger = create_passenger(None, country, local_phone) passenger.user = request.user passenger.save() # no user, get a passenger else: try: passenger = Passenger.objects.filter(country=country).filter( phone=local_phone).get() except Passenger.DoesNotExist: passenger = create_passenger(None, country, local_phone) except Passenger.MultipleObjectsReturned: return HttpResponseBadRequest( _("Phone has multiple passengers" )) # shouldn't happen to real passengers (only staff) request.session[CURRENT_PASSENGER_KEY] = passenger # reset login token after validation # TODO_WB: check if login_token exists # TODO_WB: add phone number to salt the token passenger.login_token = hashlib.sha1( generate_random_token(length=40)).hexdigest() passenger.save() return JSONResponse({PASSENGER_TOKEN: passenger.login_token})
def validate_phone(request): local_phone = request.POST.get("local_phone") verification_code = int(request.POST.get("verification_code", -1)) country = get_object_or_404(Country, code=request.POST.get("country_code", "")) stored_code, stored_phone = request.session.get(SESSION_VERIFICATION_KEY, (None, None)) intl_phone_number = get_international_phone(country, local_phone) if not (stored_code and stored_phone): return HttpResponseBadRequest(_("Error validating phone (check that your browser accepts cookies)")) if intl_phone_number != stored_phone or verification_code != int(stored_code): return HttpResponseBadRequest(_("Invalid verification code")) # there is a user if request.user.is_authenticated(): # TODO_WB: check if user already has a passenger try: # has a passenger? update phone passenger = Passenger.objects.get(user=request.user) passenger.phone = local_phone passenger.phone_verified = True passenger.save() return HttpResponse(local_phone) except Passenger.DoesNotExist: # create passenger passenger = create_passenger(None, country, local_phone) passenger.user = request.user passenger.save() # no user, get a passenger else: try: passenger = Passenger.objects.filter(country=country).filter(phone=local_phone).get() except Passenger.DoesNotExist: passenger = create_passenger(None, country, local_phone) except Passenger.MultipleObjectsReturned: return HttpResponseBadRequest( _("Phone has multiple passengers") ) # shouldn't happen to real passengers (only staff) request.session[CURRENT_PASSENGER_KEY] = passenger # reset login token after validation # TODO_WB: check if login_token exists # TODO_WB: add phone number to salt the token passenger.login_token = hashlib.sha1(generate_random_token(length=40)).hexdigest() passenger.save() return JSONResponse({PASSENGER_TOKEN: passenger.login_token})
def save(self, commit=True): model = super(BusinessRegistrationForm, self).save(commit=False) user = create_user(self.cleaned_data['email'], self.cleaned_data['password'], email=self.cleaned_data['email'], first_name=self.cleaned_data['name'], save=False) passenger = create_passenger(user, Country.objects.get(code=settings.DEFAULT_COUNTRY_CODE), self.cleaned_data['phone'], save=False) passenger.default_station = self.cleaned_data['default_station'] model.address = self.cleaned_data['address'] model.city = City.objects.get(id=self.cleaned_data['city']) model.street_address = self.cleaned_data['street_address'] model.house_number = self.cleaned_data['house_number'] model.lon = self.cleaned_data['lon'] model.lat = self.cleaned_data['lat'] model.confine_orders = self.cleaned_data['confine_orders'] model.from_station = self.cleaned_data['from_station'] if commit: user.save() passenger.user = user passenger.save() model.passenger = passenger model.save() return model
def register_new_user(request, passenger=None): logging.info("registration %s" % request.POST) name = request.POST.get("name") email = request.POST.get("email") password = request.POST.get("password") phone = request.POST.get("phone") if not all([name, email, password, phone]): return None first_name, last_name = get_name_parts(name) user = create_user(email, password, email, first_name, last_name) user = authenticate(username=user.username, password=password) login(request, user) if passenger: passenger.user = user else: # create a new one country = Country.objects.get(code=settings.DEFAULT_COUNTRY_CODE) passenger = create_passenger(request.user, country, phone, save=False) passenger.login_token = hashlib.sha1(generate_random_token(length=40)).hexdigest() passenger.save() request.session[CURRENT_PASSENGER_KEY] = passenger return user
def register_new_user(request, passenger=None): logging.info("registration %s" % request.POST) name = request.POST.get("name") email = request.POST.get("email") password = request.POST.get("password") phone = request.POST.get("phone") if not all([name, email, password, phone]): return None first_name, last_name = get_name_parts(name) user = create_user(email, password, email, first_name, last_name) user = authenticate(username=user.username, password=password) login(request, user) if passenger: passenger.user = user else: # create a new one country = Country.objects.get(code=settings.DEFAULT_COUNTRY_CODE) passenger = create_passenger(request.user, country, phone, save=False) passenger.login_token = hashlib.sha1( generate_random_token(length=40)).hexdigest() passenger.save() request.session[CURRENT_PASSENGER_KEY] = passenger return user
def do_register_passenger(request): """ A one stop shop for handling registration of a new phone number, generationg a login token and updating the session. We make sure the request is made by an authenticated user. In case of validating: 1. Existing phone number (existing passenger) If request.user is already a passenger, merge request.user.passenger into the existing passenger. Connect the (merged) existing passenger to request.user and delete any previous passenger.user. 2. New phone number (new passenger) If request.user is already a passenger change his phone number. Otherwise, create a new passenger connected to request.user. """ if not request.user.is_authenticated(): return HttpResponseForbidden(_("You must be logged in to validate your phone.")) local_phone = request.POST.get('local_phone') country = Country.objects.get(code=settings.DEFAULT_COUNTRY_CODE) verification_code = int(request.POST.get('verification_code', -1)) response, passenger = validate_passenger_phone(request, local_phone, country, verification_code) if response.status_code != 200: # verification failed return response if passenger: # existing passenger try: user_passenger = request.user.passenger except Passenger.DoesNotExist: user_passenger = None if user_passenger and passenger != user_passenger: #TODO_WB: merge passengers notify_by_email("Merge Passengers Required", u"request.user.passenger:%s\nvalidated passenger:%s" % (request.user.passenger, passenger)) return HttpResponseBadRequest(_("We are sorry but your phone cannot be changed now. We will contact you to resolve this issue as soon as possible")) # request.user should be is_authenticated if we get here if passenger.user and passenger.user != request.user: safe_delete_user(passenger.user, remove_from_db=True) passenger.user = request.user else: # new passenger try: # user is already a passenger, change phone passenger = request.user.passenger passenger.phone = local_phone except Passenger.DoesNotExist: # user is not a passenger, create new passenger = create_passenger(request.user, country, local_phone, save=False) # request.session[CURRENT_PASSENGER_KEY] = passenger passenger.login_token = hashlib.sha1(generate_random_token(length=40)).hexdigest() passenger.save() request.session[CURRENT_PASSENGER_KEY] = passenger return JSONResponse({"redirect": reverse(post_login_redirect)})
def create_selenium_passenger(user): passenger = create_passenger(user=user, country=Country.objects.filter(code="IL").get(), phone=SELENIUM_PHONE) return passenger
def register_device(request): def report_conversion(udid): url = "http://tracking.taptica.com/aff_u" payload = urlencode({ "tt_adv_id": 612, "tt_deviceid": udid, "tt_appid": 501682022, "tt_time": default_tz_now().strftime("%Y-%m-%d %H:%M:%S.%f"), }) url = "%s?%s" % (url, payload) logging.info("report_conversion: %s" % url) res = safe_fetch(url, method="GET", deadline=15) if res: res = simplejson.loads(res.content)["tt_cid"] return res local_phone = request.POST.get("local_phone") udid = request.POST.get("UDID") gudid = request.POST.get("GUDID") app_udid = request.POST.get("APP_UDID") app_name = request.POST.get("application_name") installed_app = InstalledApp.by_app_udid(app_udid) device = Device.by_udid(udid) passenger_created = False cid = report_conversion(udid) if cid: logging.info("cid = %s" % cid) if installed_app and device: assert installed_app.device == device if not device: logging.info("creating device with udid=%s and gudid=%s" % (udid, gudid)) device = Device(udid=udid, gudid=gudid) device.save() if not installed_app: logging.info("creating installed app with app_udid=%s" % app_udid) installed_app = InstalledApp( app_udid=app_udid, name=app_name, cid=cid, device=device, user_agent=request.META.get("HTTP_USER_AGENT")) else: logging.info("installed app exists, updating") installed_app.install_count += 1 if cid: installed_app.cid = cid country = get_object_or_404(Country, code=request.POST.get('country_code', "")) passengers = Passenger.objects.filter(country=country, phone=local_phone) if passengers: passenger = passengers[0] else: passenger = create_passenger(None, country, local_phone, save=False) passenger.login_token = hashlib.sha1( generate_random_token(length=40)).hexdigest() passenger.save() passenger_created = True request.session[CURRENT_PASSENGER_KEY] = passenger if passenger_created: installed_app.passenger = passenger installed_app.user_agent = request.META.get("HTTP_USER_AGENT") installed_app.save() if local_phone == settings.APPLE_TESTER_PHONE_NUMBER: return JSONResponse({PASSENGER_TOKEN: passenger.login_token}) if installed_app.passenger != passenger: return HttpResponseBadRequest( _("The phone number is already registered.")) return JSONResponse({PASSENGER_TOKEN: passenger.login_token})
def create_selenium_passenger(user): passenger = create_passenger( user=user, country=Country.objects.filter(code="IL").get(), phone=SELENIUM_PHONE) return passenger
def do_register_passenger(request): """ A one stop shop for handling registration of a new phone number, generationg a login token and updating the session. We make sure the request is made by an authenticated user. In case of validating: 1. Existing phone number (existing passenger) If request.user is already a passenger, merge request.user.passenger into the existing passenger. Connect the (merged) existing passenger to request.user and delete any previous passenger.user. 2. New phone number (new passenger) If request.user is already a passenger change his phone number. Otherwise, create a new passenger connected to request.user. """ if not request.user.is_authenticated(): return HttpResponseForbidden( _("You must be logged in to validate your phone.")) local_phone = request.POST.get('local_phone') country = Country.objects.get(code=settings.DEFAULT_COUNTRY_CODE) verification_code = int(request.POST.get('verification_code', -1)) response, passenger = validate_passenger_phone(request, local_phone, country, verification_code) if response.status_code != 200: # verification failed return response if passenger: # existing passenger try: user_passenger = request.user.passenger except Passenger.DoesNotExist: user_passenger = None if user_passenger and passenger != user_passenger: #TODO_WB: merge passengers notify_by_email( "Merge Passengers Required", u"request.user.passenger:%s\nvalidated passenger:%s" % (request.user.passenger, passenger)) return HttpResponseBadRequest( _("We are sorry but your phone cannot be changed now. We will contact you to resolve this issue as soon as possible" )) # request.user should be is_authenticated if we get here if passenger.user and passenger.user != request.user: safe_delete_user(passenger.user, remove_from_db=True) passenger.user = request.user else: # new passenger try: # user is already a passenger, change phone passenger = request.user.passenger passenger.phone = local_phone except Passenger.DoesNotExist: # user is not a passenger, create new passenger = create_passenger(request.user, country, local_phone, save=False) # request.session[CURRENT_PASSENGER_KEY] = passenger passenger.login_token = hashlib.sha1( generate_random_token(length=40)).hexdigest() passenger.save() request.session[CURRENT_PASSENGER_KEY] = passenger return JSONResponse({"redirect": reverse(post_login_redirect)})
def register_device(request): def report_conversion(udid): url = "http://tracking.taptica.com/aff_u" payload = urlencode( { "tt_adv_id": 612, "tt_deviceid": udid, "tt_appid": 501682022, "tt_time": default_tz_now().strftime("%Y-%m-%d %H:%M:%S.%f"), } ) url = "%s?%s" % (url, payload) logging.info("report_conversion: %s" % url) res = safe_fetch(url, method="GET", deadline=15) if res: res = simplejson.loads(res.content)["tt_cid"] return res local_phone = request.POST.get("local_phone") udid = request.POST.get("UDID") gudid = request.POST.get("GUDID") app_udid = request.POST.get("APP_UDID") app_name = request.POST.get("application_name") installed_app = InstalledApp.by_app_udid(app_udid) device = Device.by_udid(udid) passenger_created = False cid = report_conversion(udid) if cid: logging.info("cid = %s" % cid) if installed_app and device: assert installed_app.device == device if not device: logging.info("creating device with udid=%s and gudid=%s" % (udid, gudid)) device = Device(udid=udid, gudid=gudid) device.save() if not installed_app: logging.info("creating installed app with app_udid=%s" % app_udid) installed_app = InstalledApp( app_udid=app_udid, name=app_name, cid=cid, device=device, user_agent=request.META.get("HTTP_USER_AGENT") ) else: logging.info("installed app exists, updating") installed_app.install_count += 1 if cid: installed_app.cid = cid country = get_object_or_404(Country, code=request.POST.get("country_code", "")) passengers = Passenger.objects.filter(country=country, phone=local_phone) if passengers: passenger = passengers[0] else: passenger = create_passenger(None, country, local_phone, save=False) passenger.login_token = hashlib.sha1(generate_random_token(length=40)).hexdigest() passenger.save() passenger_created = True request.session[CURRENT_PASSENGER_KEY] = passenger if passenger_created: installed_app.passenger = passenger installed_app.user_agent = request.META.get("HTTP_USER_AGENT") installed_app.save() if local_phone == settings.APPLE_TESTER_PHONE_NUMBER: return JSONResponse({PASSENGER_TOKEN: passenger.login_token}) if installed_app.passenger != passenger: return HttpResponseBadRequest(_("The phone number is already registered.")) return JSONResponse({PASSENGER_TOKEN: passenger.login_token})