def registerUser(request): if request.method == "POST": passtemp = request.POST['password'] tmpPhone = '+34' + request.POST['phone'] if (Customer.objects.filter(email=request.POST['email']).count() > 0): return HttpResponse( status=status.HTTP_401_UNAUTHORIZED, content="El email que ha indicado ya está en uso") if (Customer.objects.filter(phone=tmpPhone).count() > 0): return HttpResponse( status=status.HTTP_401_UNAUTHORIZED, content="El teléfono que ha indicado ya está en uso") #elif (passtemp.length() < 4) # return HttpResponse("shortpassword", content_type="text/plain") else: try: c = Customer(email=request.POST['email'], password=passtemp, phone=tmpPhone, image="") code = random.randint(1000, 9999) c.validationCode = code codeEmail = random.randint(1000, 9999) c.validationCodeEmail = codeEmail c.save() msg = { 'reqtype': 'json', 'api_key': '8a352457', 'api_secret': '460e58ff', 'from': 'Taxi Express', 'to': c.phone, 'text': 'Su código de validación de Taxi Express es: ' + str(code) } sms = NexmoMessage(msg) sms.set_text_info(msg['text']) response = sms.send_request() subject = 'Código validación email' from_email = '*****@*****.**' to = [c.email] html_content = 'Su código de validación del email de Taxi Express es: ' + str( codeEmail) msg = EmailMessage(subject, html_content, from_email, to) msg.content_subtype = "html" # Main content is now text/html msg.send() return HttpResponse(status=status.HTTP_201_CREATED) except ValidationError: HttpResponse(status=status.HTTP_401_UNAUTHORIZED, content="Email no válido") else: HttpResponse(status=status.HTTP_400_BAD_REQUEST)
def registerDriver(request): if request.method == "POST": passtemp = request.POST['password'] tmpPhone = '+34' + request.POST['phone'] if (Driver.objects.filter(email=request.POST['email']).count() > 0): return HttpResponse( status=status.HTTP_401_UNAUTHORIZED, content="El email que ha indicado ya está en uso") if (Driver.objects.filter(phone=tmpPhone).count() > 0): return HttpResponse( status=status.HTTP_401_UNAUTHORIZED, content="El teléfono que ha indicado ya está en uso") else: try: #Car data if (Car.objects.filter(plate=request.POST['plate']).count() > 0): return HttpResponse( status=status.HTTP_401_UNAUTHORIZED, content="La matrícula que ha indicado ya está en uso") else: car = Car(plate=request.POST['plate'], company=request.POST['company'], model=request.POST['model'], capacity=request.POST['capacity']) car.save() #Driver data d = Driver(email=request.POST['email'], password=passtemp, phone=tmpPhone, image="", available=False, first_name=request.POST['first_name'], last_name=request.POST['last_name'], license=request.POST['license'], car=car) code = random.randint(1, 9999) d.validationCode = code codeEmail = random.randint(1000, 9999) d.validationCodeEmail = codeEmail d.save() msg = { 'reqtype': 'json', 'api_key': '8a352457', 'api_secret': '460e58ff', 'from': 'Taxi Express', 'to': d.phone, 'text': 'Su código de validación de Taxi Express es: ' + str(code) } sms = NexmoMessage(msg) sms.set_text_info(msg['text']) response = sms.send_request() subject = 'Código validación email' from_email = '*****@*****.**' to = [d.email] html_content = 'Su código de validación del email de Taxi Express es: ' + str( codeEmail) msg = EmailMessage(subject, html_content, from_email, to) msg.content_subtype = "html" # Main content is now text/html msg.send() return HttpResponse(status=status.HTTP_201_CREATED) except ValidationError: HttpResponse(status=status.HTTP_401_UNAUTHORIZED, content="Email no válido") else: HttpResponse(status=status.HTTP_401_UNAUTHORIZED)