def validate_emails(form, field): for email in [email.strip() for email in form.emails.data.split(",")]: try: email_validator.validate_email(email) except email_validator.EmailNotValidError as e: raise ValidationError(_("The email %(email)s is not valid", email=email))
def handle(self, username, **options): from corehq.apps.users.models import WebUser try: validate_email(username) except EmailSyntaxError: raise CommandError('Your username must be an email address') couch_user = WebUser.get_by_username(username) if couch_user: if not isinstance(couch_user, WebUser): raise CommandError('Username already in use by a non-web user') print("✓ User {} exists".format(couch_user.username)) else: password = self.get_password_from_user() couch_user = WebUser.create(None, username, password) print("→ User {} created".format(couch_user.username)) is_superuser_changed = not couch_user.is_superuser is_staff_changed = not couch_user.is_staff couch_user.is_superuser = True couch_user.is_staff = True if is_superuser_changed or is_staff_changed: couch_user.save() if is_superuser_changed: print("→ User {} is now a superuser".format(couch_user.username)) else: print("✓ User {} is a superuser".format(couch_user.username)) if is_staff_changed: print("→ User {} can now access django admin".format(couch_user.username)) else: print("✓ User {} can access django admin".format(couch_user.username))
def email_promo_code(self, session, group_id, message='', **params): if cherrypy.request.method == 'POST': code = session.lookup_promo_or_group_code(params.get('code')) if not code: message = "This code is invalid. If it has not been claimed, please contact us at {}".format( c.REGDESK_EMAIL) elif not params.get('email'): message = "Please enter an email address" else: try: validate_email(params.get('email')) except EmailNotValidError as e: message = str(e) message = 'Enter a valid email address. ' + message if not message: send_email.delay( c.REGDESK_EMAIL, params.get('email'), 'Claim a {} badge in "{}"'.format(c.EVENT_NAME, code.group.name), render('emails/reg_workflow/promo_code_invite.txt', {'code': code}, encoding=None), model=code.to_dict('id')) raise HTTPRedirect('group_promo_codes?id={}&message={}'.format(group_id, "Email sent!")) else: raise HTTPRedirect('group_promo_codes?id={}&message={}'.format(group_id, message))
def validate_email(request): from itfsite.models import User, Campaign from email_validator import validate_email, EmailNotValidError email = request.POST['email'].strip() # Validate the email address. If it is invalid, return the # error message. See create_pledge below - we repeat this # as server-slide validation again. try: # DE does not accept internationalized addresses # during testing, don't check deliverability so that tests can operate off-line validate_email(email, allow_smtputf8=False, check_deliverability=settings.VALIDATE_EMAIL_DELIVERABILITY) except EmailNotValidError as e: return HttpResponse(str(e), content_type="text/plain") if not User.objects.filter(email=email).exists(): # Store for later, if this is not a user already with an account. # We store a max of one per email address. IncompletePledge.objects.get_or_create( email=email, defaults={ "trigger": Trigger.objects.get(id=request.POST['trigger']), "via_campaign": Campaign.objects.get(id=request.POST['via_campaign']), "extra": { "desired_outcome": request.POST['desired_outcome'], "ref_code": get_sanitized_ref_code(request), } }) return HttpResponse("OK", content_type="text/plain")
def check_email(email): try: validate_email(email, check_deliverability=False) except EmailNotValidError as e: # We re-raise with custom exception not to create dep on # the lib raise InvalidRecipientError( 'Invalid email address format: "{}"'.format(email) ) from e
def check_value(self, value): try: validate_email(value, allow_smtputf8=self.allow_smtputf8, check_deliverability=self.check_deliverability, allow_empty_local=self.allow_empty_local ) return True except EmailNotValidError: return False
def _email_is_valid(email): if not email: return False try: validate_email(email) except EmailNotValidError as e: logger.warn(str(e)) return False return True
def validate_email(email): """Validate the given email address. This checks both if it looks valid and if it has valid MX (or A/AAAA) records. """ email = to_unicode(email) try: email_validator.validate_email(email) except email_validator.EmailNotValidError: return False else: return True
def is_valid_email_address(email_address): """ Check that the string specified appears to be a valid email address. :param str email_address: The email address to validate. :return: Whether the email address appears to be valid or not. :rtype: bool """ if email_address is None: return False try: email_validator.validate_email(email_address, allow_empty_local=False, check_deliverability=False) except email_validator.EmailNotValidError: return False return True
def val_email(email): """Validate email address""" log.info("validating email address") try: if status == "online": v = validate_email(email, check_deliverability=True) # validate and get info. Since we're online, check deliverability elif status == "offline": v = validate_email(email, check_deliverability=False) # validate and get info email = v["email"] # replace with normalized form return True, email except EmailNotValidError as e: # email is not valid, exception message is human-readable log.warning(str(e)) return False, (str(e))
def validateEmail(self, email): try: result = validate_email(email) # validate and get info email = result["email"] # replace with normalized form except: return False return email
def check_email(string): try: if validate_email(string): return True except: errors_msg.append(str('email not valid: ' + string)) return False
def email_validator(key, data, errors, context): email = data[key] try: v = validate_email(email, check_deliverability=False) except Exception as e: errors[key].append('Please provide a valid email address. The email address should be for a mailbox that is regularly monitored.')
def clean_username(self): email = normalise_email(self.cleaned_data['username']) try: v = validate_email(email) email = v["email"] except EmailNotValidError as e: raise forms.ValidationError("The email address is invalid. Perhaps there was a typo? Please try again.") return email
def clean_email(self, email): try: v = validate_email(email) email = v["email"] except EmailNotValidError as e: raise forms.ValidationError("The email address is invalid. Perhaps there was a typo? Please try again.") return email
def __call__(self, form, field): try: if field.data is None: raise email_validator.EmailNotValidError() email_validator.validate_email( field.data, check_deliverability=self.check_deliverability, allow_smtputf8=self.allow_smtputf8, allow_empty_local=self.allow_empty_local, ) except email_validator.EmailNotValidError as e: message = self.message if message is None: if self.granular_message: message = field.gettext(e) else: message = field.gettext("Invalid email address.") raise ValidationError(message)
def answer(self): try: res = validate_email(self.input) email = res["email"] except EmailNotValidError as e: # email is not valid, exception message is human-readable self.next_state = "setemail" return self._make_answer([str(e), "Sorry, a valid email is required. Please try again..."]) return self._make_answer(["$email %s" % email, "Thank you. May I also ask your phone number?"])
def sendEmail(title, body, userEmail, recaptcha, clientIP, test=0): if test: authData = json.load(open("auth.json","r")) emailList = [x.strip() for x in open("email/emails.txt","r").read().split("\n")] else: authData = json.load(open("./model/auth.json","r")) emailList = [x.strip() for x in open("./model/email/emails.txt","r").read().split("\n")] try: validated = email_validator.validate_email(userEmail) except: return({"error": "The email you have entered cannot be verified."}) if any(x.lower() in userEmail.lower() for x in emailList): return({"error": "We do not allow contact from disposable email address services."}) if len(title) > 200: title = title[0:200] if len(body) > 5000: body = body[0:5000] if not userEmail: userEmail = "*****@*****.**" if not test: try: rcTest = json.loads(requests.post("https://www.google.com/recaptcha/api/siteverify", data={"secret": authData["recaptcha"], "response": recaptcha, "remoteip": clientIP}).text) if "success" in rcTest and rcTest["success"] == True: pass else: # Contacted server fine, but their email is no good. return {"error": "Error sending mail. Cannot verify that user is human."} except: # Error contacting Google's reCAPTCHA server return(traceback.format_exc()) return {"error": "Error sending mail. Please try again later."} headers = {"Authorization": "Bearer "+authData["sendgrid"], "Content-Type": "application/json"} topost = {"personalizations": [{"to": [{"email": authData["contact"]}], "subject": title}], "from": {"email": userEmail}, "content": [{"type": "text/plain", "value": body}]} req = requests.post("https://api.sendgrid.com/v3/mail/send", headers=headers, json=topost, verify=False) results = req.text banned = 0 if banned: # Banned users should feel like their email was sent fine. return {"success": 1} if req.status_code==202: # Successful email sent return {"success": 1} else: # Email failed, store contact in text file for manual check try: fileName = datetime.datetime.now().strftime("%Y-%m-%d") + "-"+clientIP+"-"+hashlib.sha256(body).hexdigest()[0:10]+".txt" with open("email/"+fileName,"w") as f: f.write("User email: "+userEmail+"\nTitle: "+title+"\n\n"+body) return {"success": 1} except: # Error writing to file. return {"error": "Error sending email. Please try again later."}
def invite(): # start with giving them the form if request.method == 'GET': return render_template('invite.html') # if we've reached here we are POST, and they've asked us to invite # validate the email address email = request.form.get('email', '') if not email: flash('Email cannot be blank.') return render_template('invite.html') try: v = validate_email(email) # validate and get info email = v["email"] # replace with normalized form except EmailNotValidError as exc: # email is not valid, exception message is human-readable flash(str(exc)) return render_template('invite.html') # email is good, lets invite them try: if g.uaac.does_origin_user_exist( app.config['UAA_CLIENT_ID'], app.config['UAA_CLIENT_SECRET'], email, app.config['IDP_PROVIDER_ORIGIN']): flash('User already has a valid account.') return render_template('invite.html') redirect_uri = os.path.join(request.url_root, 'oauth', 'login') logging.info('redirect for invite: {0}'.format(redirect_uri)) invite = g.uaac.invite_users(email, redirect_uri) if send_verification_code_email(app, email, invite): return render_template('invite_sent.html') except UAAError as exc: # if UAA complains that our access token is invalid then force them back through the login # process. # TODO: Fix this properly by implementing the refresh token oauth flow # in the UAAClient when it detects it's token is no longer valid if 'Invalid access token' in str(exc): return redirect(url_for('logout')) else: logging.exception('An error occured communicating with UAA') except Exception: logging.exception('An error occured during the invite process') return render_template('error/internal.html'), 500
def process_email(email): """Validates that the email is valid. Return email ascii encoded if valid, None if not. """ if not email: return None email = force_unicode(email) try: # NOTE SFDC doesn't support SMPTUTF8, so we cannot enable support # here until they do or we switch providers info = validate_email(email, allow_smtputf8=False, check_deliverability=False) except EmailNotValidError: return None return info.get('email_ascii', None)
def index(): # start with giving them the form if request.method == 'GET': return render_template('index.html') # if we've reached here we are POST, and they've asked us to invite # validate the email address email = request.form.get('email', '') if not email: flash('Email cannot be blank.') return render_template('index.html') try: v = validate_email(email) # validate and get info email = v["email"] # replace with normalized form except EmailNotValidError as exc: # email is not valid, exception message is human-readable flash(str(exc)) return render_template('index.html') # email is good, lets invite them try: invite = g.uaac.invite_users(email, app.config['UAA_BASE_URL']) if len(invite['failed_invites']): raise RuntimeError('UAA failed to invite the user.') invite = invite['new_invites'][0] branding = { 'company_name': app.config['BRANDING_COMPANY_NAME'] } # we invited them, send them the link to validate their account subject = render_template('email/subject.txt', invite=invite, branding=branding).strip() body = render_template('email/body.html', invite=invite, branding=branding) send_email(app, email, subject, body) return render_template('invite_sent.html') except Exception: logging.exception('An error occured during the invite process') return render_template('error/internal.html'), 500
def filter_bad_email(email): """ Bad emails are filtered and returned as an empty string if filtered. Otherwise normalized email is returned. Normalization of email is a function of the validate_email() from email_validator package. Meaning that if an email can/is corrected by the email_validator package the actual results are returned. for example: "joe @ yahoo.com" is normalized as "*****@*****.**" :param str email: :return: Normalized email for valid email... '' (empty string) if the email is bad :rtype: str """ try: normalized_email = validate_email(email)['email'] if normalized_email != email: pass # TODO: use your own logging here # print '*** Email Normalized changed emails (original, normalized): {0}, {1}'\ # .format(user.email, normalized_email) # TBD: Domain validation if warranted return normalized_email except EmailNotValidError: return ''
def __call__(self, form, field): try: email_validator.validate_email(field.data) except email_validator.EmailNotValidError: raise ValidationError('Ungültige E-Mail Adresse')
def cart(): if request.method == "POST": first_name = request.form.get("first-name") last_name = request.form.get("last-name") city = request.form.get("city") delivery = request.form.get("delivery") phone = request.form.get("phone") email = request.form.get("email") password = request.form.get("password") additional = request.form.get( "additional") # TODO additional information changes = ast.literal_eval(request.form.get("changes")) for id in changes: session['cart'][id]['quantity'] += changes[id] session['total_cart'] = get_total(session['cart']) try: v = email_validator.validate_email(email, check_deliverability=False) email = v["email"] except email_validator.EmailNotValidError as e: return apology("Некорректный email: " + str(e)) if len(first_name) < 1 or len(last_name) < 1 or len(city) < 1 or len( delivery) < 1: return apology("Некорректные данные") if len(phone) < 8: return apology('Некорректный номер телефона') db = db_init() user = db.execute("SELECT * FROM users WHERE username = :username", { "username": email }).fetchall() if len( user ) == 1: # TODO: debug save user + make based on id instead of username db.execute( "UPDATE users SET firstname = :firstname, lastname = :lastname, " "phone = :phone, delivery = :delivery WHERE username = :username", { "firstname": first_name, "lastname": last_name, "phone": phone, "delivery": delivery, "username": email }) else: if password is not None: # TODO: check if db doesn't contain user already db.execute( "INSERT INTO users (username, password, firstname, lastname, phone, delivery) " "VALUES(:username, :hash, :firstname, :lastname, :phone, :delivery)", { "username": email, "hash": pwd_context.hash(password), "firstname": first_name, "lastname": last_name, "phone": phone, "delivery": delivery }) date = str(datetime.now()) db.execute( "INSERT INTO orders " "(email, phone, city, delivery, cart, total_sum, date, additional_information) " "VALUES (:email, :phone, :city, :delivery, :cart, :total, :date, :additional)", { 'email': email, 'phone': phone, 'city': city, 'delivery': delivery, 'cart': get_short_cart(session['cart']), 'total': session['total_cart'], 'date': date, 'additional': additional }) order = db.execute("SELECT id FROM orders WHERE date IS :date", { 'date': date }).fetchone() db.commit() session['cart'].clear() save_cart(db, session['user_id'], session['cart']) return url_for('success', id=order['id']) else: if is_cart_empty(): col_md = 9 else: col_md = 15 if session.__contains__('user_id'): db = db_init() user = get_user_for_checkout(db) return render_template('checkout.html', email=user['username'], firstname=user['firstname'], col_md=col_md, lastname=user['lastname'], city=user['city'], delivery=user['delivery']) return render_template('checkout.html', col_md=col_md)
def test_ascii_free_email(self): email = self.factory.ascii_free_email() validate_email(email, check_deliverability=False) assert email.split('@')[0] == 'joaosimoes'
def menu(): chave = 0 while True: print( "MENU \n" " 0. finalizar programa \n 1. cadastrar pessoa\n 2. editar dados pessoais\n 3. excluir dados pessoais\n 4. consultar dados" ) opcao = int(input("Digite qual a opcao desejada: ")) if opcao == 0: break elif opcao == 1: chave = 0 while chave == 0: pergunta = input("Digite o nome: ") if len(pergunta) < 150: nome = pergunta chave = 1 else: print("Digite um valor valido") chave = 0 while chave == 0: pergunta = input("Digite o CPF(apenas os numeros): ") if len(pergunta) == 11: if pergunta.isdigit() == True: mongo = ConexaoMongo.connection() print(mongo.find_one({"cpf": pergunta})) if mongo.find_one({"cpf": pergunta}) == None: cpf = pergunta chave = 1 else: print("Digite um valor valido") else: print("Digite um valor valido") else: print("Digite um valor valido") chave = 0 while chave == 0: pergunta = input("Digite o E-mail: ") if len(pergunta) < 400: try: valid = validate_email(pergunta) mail = valid.email if mail == pergunta: email = pergunta chave = 1 else: print("Digite um e-mail valido") except EmailNotValidError as error: print(str(error)) else: print("Digite um email valido") chave = 0 pessoa = Pessoa(nome, cpf, email) print("Nome: {}, CPF: {}, E-mail: {}".format( pessoa.nome, pessoa.cpf, pessoa.email)) print("Deseja modificar algum dado?") pergunta = input('Digite "s" ou "n": ') if pergunta.lower() == "n": ConexaoMongo.salvar(nome, cpf, email) elif pergunta.lower() == "s": chave = 0 while chave == 0: pergunta = input("Digite o nome: ") if len(pergunta) < 150: nome = pergunta chave = 1 else: print("Digite um valor valido") chave = 0 while chave == 0: pergunta = input("Digite o CPF: ") if len(pergunta) == 11: if pergunta.isdigit() == True: mongo = ConexaoMongo.connection() if mongo.find_one({"cpf": pergunta}) == None: cpf = pergunta chave = 1 else: print("Digite um valor valido") else: print("Digite um valor valido") else: print("Digite um valor valido") chave = 0 while chave == 0: pergunta = input("Digite o E-mail: ") if len(pergunta) < 400: try: valid = validate_email(pergunta) mail = valid.email if mail == pergunta: email = pergunta chave = 1 else: print("Digite um valor valido") except EmailNotValidError as error: print(str(error)) else: print("Digite um email valido") chave = 0 pessoa = Pessoa(nome, cpf, email) ConexaoMongo.salvar(pessoa.nome, pessoa.cpf, pessoa.email) elif opcao == 2: print(" 1. Nome; \n 2. CPF; \n 3. E-mail;") tipo = int( input( "Digite qual coluna de informacoes devera ser feita a troca" )) valor_velho = input("Digite o valor que deve ser trocado: ") valor_novo = input("Digite o valor novo que sera adicionado: ") if tipo == 1: if len(valor_novo) < 150: mongo = ConexaoMongo() mongo.update(valor_velho, valor_novo, "nome") else: print("Digite um valor valido") elif tipo == 2: if len(valor_novo) == 11: if valor_novo.isdigit(): mongo = ConexaoMongo() mongo.update(valor_velho, valor_novo, "cpf") # if mongo.pesquisar("cpf", valor_novo) == "None": # # else: print("Digite um valor valido") else: print("Digite um valor valido") else: print("Digite um valor valido") elif tipo == 3: if len(valor_novo) < 400: try: valid = validate_email(valor_novo) mail = valid.email except EmailNotValidError as error: print(str(error)) mongo = ConexaoMongo() mongo.update(valor_velho, valor_novo, "email") elif opcao == 3: print(" 1. Nome; \n 2. CPF; \n 3. E-mail;") tipo = int(input("Digite qual coluna sera o filtro: ")) valor = input("Digite o valor que devera ser deletado: ") if tipo == 1: mongo = ConexaoMongo() try: mongo = ConexaoMongo() dado_deletado = mongo.pesquisar("nome", valor) file = open("Dados_Deletados.txt", "a") file.write(str(dado_deletado)) file.write("\n") file.close() except Exception as error: print("Erro ao salvar dado deletado", error) mongo.delete("nome", valor) elif tipo == 2: mongo = ConexaoMongo() try: mongo = ConexaoMongo() dado_deletado = mongo.pesquisar("cpf", valor) file = open("Dados_Deletados.txt", "a") file.write(str(dado_deletado)) file.write("\n") file.close() except Exception as error: print("Erro ao salvar dado deletado", error) mongo.delete("cpf", valor) elif tipo == 3: mongo = ConexaoMongo() try: mongo = ConexaoMongo() dado_deletado = mongo.pesquisar("email", valor) file = open("Dados_Deletados.txt", "a") file.write(str(dado_deletado)) file.write("\n") file.close() except Exception as error: print("Erro ao salvar dado deletado", error) mongo.delete("email", valor) elif opcao == 4: print(" 1. Nome; \n 2. CPF; \n 3. E-mail;") tipo = int(input("Digite qual coluna sera o filtro: ")) pesquisa = input( "Digite uma informacao da pessoa a ser pesquisada: ") if tipo == 1: mongo = ConexaoMongo() print(mongo.pesquisar("nome", pesquisa)) elif tipo == 2: mongo = ConexaoMongo() print(mongo.pesquisar("cpf", pesquisa)) elif tipo == 3: mongo = ConexaoMongo() print(mongo.pesquisar("email", pesquisa)) elif opcao == 5: mongo = ConexaoMongo() mongo.listar()
from main import db, app from db import User from email_validator import validate_email import sys if __name__ == "__main__": email = validate_email(sys.argv[1]).email password = sys.argv[2] input(f"Do you wish to create a update the password of: {email}?" "\nPress Enter to continue, or CTRL+C to quit.") print("Updating password...") with app.app_context(): user = User.query.get(email) user.set_password(password) db.session.commit() print(f"Successfully updated user: {email} with the password: {password}")
def create_new_user(): """Creates a new user and send OTP to the email This is using docstrings for specifications. All the parameter values are base64 encoded and are sent. --- parameters: - name: name in: body type: string format: byte required: true - name: email in: body type: string format: byte required: true - name: password in: body type: string format: byte required: true responses: 200: schema: type: object properties: message: type: string description: ok if valid, else error error: type: string description: describes the reason for error """ response_json = {} response_json["message"] = "error" try: ## data = request.get_json() ## app.logger.debug("Test") userName = getDataFromRequest(dataObj=data, keyValue="name") email = getDataFromRequest(dataObj=data, keyValue="email") password = getDataFromRequest(dataObj=data, keyValue="password") ## password = helper.encryptValue(password) # print(email) app.logger.debug(email) validate_email(email) ## # Print out some data about the table. # This will cause a request to be made to DynamoDB and its attribute # values will be set based on the response. app.logger.debug(table.creation_date_time) ## sentOTP = helper.sendOTPMail(email) # insert values into the database , along with time and otp and return message table.put_item( Item={ 'USER_NAME': userName, 'EMAIL_ID': email, 'PASSWORD': password, 'OTP': sentOTP, 'OTP_GEN_TIME': int(round(time.time() * 1000)) }) #trigger email to the user mail response_json["message"] = "ok" except EmailNotValidError as emailError: app.logger.debug("email id given is wrong - " + str(emailError)) response_json[ "error"] = "email id given is either not reachable or invalid" except Exception as e: app.logger.debug(e) # return json.dumps(response_json)
def check_email(email): try: v = validate_email(email) return v except EmailNotValidError: return False
def skmer(): ext = "txt, csv, hist, fa, fq, fastq, fna, fasta" if request.method == 'POST': email = request.form.get('userEmail') try: valid = validate_email(email, allow_smtputf8=False) email = valid.ascii_email print(email) except EmailNotValidError as e: flash('Your email does not exist. Try again', 'error') return redirect(request.url) if 'folder' not in request.files: flash('No directory', 'error') return redirect(request.url) timestamp = datetime.now().strftime('%Y-%m-%d_%H%M%S') print(request.files.getlist('folder')) #making the input and output directory input_dir = os.path.join(app.config['IMAGE_UPLOADS'],'skmer',timestamp) if not os.path.exists(input_dir): os.makedirs(input_dir,exist_ok=True) output_dir = os.path.join(app.config['IMAGE_UPLOADS'],'skmer',timestamp+'_results') if not os.path.exists(output_dir): os.makedirs(output_dir,exist_ok=True) for file in request.files.getlist('folder'): # if no uploaded file if file.filename == '': flash('No selected file', 'error') tools.get_rid_of_folders(input_dir, output_dir) return redirect(request.url) #check for file format if file and tools.allowedFile(file.filename): #uploads the files to uploads/skmer filename = secure_filename(file.filename) uploaded = os.path.join(input_dir, filename) file.save(uploaded) print(filename) # if extension is not acceptable else: flash("Unacceptable extension. Only accept: {}".format(ext + " and .gz version"), 'warning') tools.get_rid_of_folders(input_dir, output_dir) return redirect(request.url) librarydir = os.path.join(output_dir,'processed_library') dist_matrix_prefix = os.path.join(output_dir,'ref-dist-mat') c = "skmer reference {0} -l {1} -o {2} ".format(input_dir,librarydir,dist_matrix_prefix) #runs skmer tools.run_command(c) try: shutil.rmtree(input_dir) except OSError as e: print("Error: %s : %s" % (input_dir, e.strerror)) flash("Successfully Uploaded Files! This might take a while. You can safely exit out of this page", "info") # send email when the respect is done running getResultdir = 'skmer/'+timestamp+'_results' # tools.sendResults(timestamp, email, output_dir, getResultdir) return redirect(url_for("result", result_dir = getResultdir)) return render_template("skmer.html", title = "SKMER", id = "skmer")
def forgot_password(): identity_token = uuid.uuid4().hex # start with giving them the form if request.method == "GET": return render_template("forgot_password.html") # if we've reached here we are POST so we can email user link email = request.form.get("email_address", "") if not email: flash("Email cannot be blank.") return render_template("forgot_password.html") try: v = validate_email(email) # validate and get info email = v["email"] # replace with normalized form except EmailNotValidError as exc: # email is not valid, exception message is human-readable flash(str(exc)) return render_template("forgot_password.html") # If we've made it this far, it's a valid email so we'll generate and store a # token and send an email. logging.info("generating validation token for user") branding = {"company_name": app.config["BRANDING_COMPANY_NAME"]} reset = { "verifyLink": url_for("reset_password", validation=identity_token, _external=True) } logging.info(reset["verifyLink"]) password = {"changeLink": changeLink} subject = render_template("email/subject-password.txt", reset=reset, branding=branding).strip() body = render_template( "email/body-password.html", reset=reset, branding=branding, password=password, ) uaac = UAAClient(app.config["UAA_BASE_URL"], None, verify_tls=app.config["UAA_VERIFY_TLS"]) user_exists = uaac.does_origin_user_exist( app.config["UAA_CLIENT_ID"], app.config["UAA_CLIENT_SECRET"], email, app.config["IDP_PROVIDER_ORIGIN"], ) if user_exists: try: r.setex(email, FORGOT_PW_TOKEN_EXPIRATION_IN_SECONDS, identity_token) except redis.exceptions.RedisError: return render_template("error/internal.html"), 500 send_email(app, email, subject, body) else: logging.info( "{} does not exist. Forgot password email not sent".format( email)) return render_template("forgot_password.html", email_sent=True, email=email)
def check_email_validator(email: str): valid_mail = validate_email(email, check_deliverability=False) return valid_mail.email
def handle(self, *args, **options): solr = pysolr.Solr(settings.SOLR_SD) # Verify Drupal file if not path.exists(options['drupal_csv']): raise CommandError('Druapl CSV file not found: ' + options['drupal_csv']) # Verify CKAN file if not path.exists(options['ckan_jsonl']): raise CommandError('CKAN JSONL file not found: ' + options['ckan_jsonl']) ckan_status = {} with open(options['ckan_jsonl'], 'r', encoding='utf-8', errors="ignore") as ckan_file: self.logger.info("Processing " + options['ckan_jsonl']) records = ckan_file.readlines() for record in records: ds = json.loads(record) # Assumption made here that the mandatory 'id', 'status', and 'date_forwarded' fields are present last_status_date = datetime(2000, 1, 1) current_status = '' if 'status' in ds: for status_update in ds['status']: status_date = datetime.strptime(status_update['date'], '%Y-%m-%d') if status_date > last_status_date: last_status_date = status_date current_status = status_update['reason'] ckan_status[ds['id']] = current_status with open(options['drupal_csv'], 'r', encoding='utf-8-sig', errors="ignore") as sd_file: sd_reader = csv.DictReader(sd_file, dialect='excel') self.logger.info("Processcing " + options['drupal_csv']) for sd in sd_reader: if sd['email']: try: # Check for valid email before proceeding valid = validate_email(sd['email']) submitter_email = valid.email # Check Solr for old status code q = "id:{0}".format(sd['uuid']) results = solr.search(q=q) if len(results.docs) == 1: old_status = results.docs[0]['status_s'] # Compare against new status from CKAN if sd['uuid'] in ckan_status: self.logger.info("CKAN ID: {0}, New Status: {1}, Old Status: {2}".format(sd['uuid'], ckan_status[sd['uuid']], old_status)) if ckan_status[sd['uuid']] != old_status and ckan_status[sd['uuid']] != "": self.logger.info("Status change detected for " + sd['uuid']) # Send an email status_email = EmailMessage() status_email['Subject'] = "Status update to your suggested dataset / L’équipe du gouvernement ouvert" # By design, don't send HTML email to own corporate domain if 'tbs-sct.gc.ca' == valid.domain: status_email['From'] = settings.SD_ALERT_EMAIL_FROM[0] status_email["To"] = submitter_email status_email.set_content(EMAIL_TEXT_TEMPLATE.format(settings.SD_RECORD_URL_EN + sd['uuid'], settings.SD_RECORD_URL_FR + sd['uuid'])) else: status_email['From'] = Address(settings.SD_ALERT_EMAIL_FROM[0], settings.SD_ALERT_EMAIL_FROM[1], settings.SD_ALERT_EMAIL_FROM[2]) status_email["To"] = Address(submitter_email, valid.local_part, valid.domain) status_email.set_content(EMAIL_TEXT_TEMPLATE.format(settings.SD_RECORD_URL_EN + sd['uuid'], settings.SD_RECORD_URL_FR + sd['uuid'])) status_email.add_alternative(EMAIL_HTML_TEMPLATE.format(settings.SD_RECORD_URL_EN + sd['uuid'], settings.SD_RECORD_URL_FR + sd['uuid']), subtype="html") self.logger.info(status_email) with smtplib.SMTP("localhost") as mail: mail.send_message(status_email) else: # the record does not exist in Solr, so no further acton is possible self.logger.info("No record found in Solr for " + sd['uuid']) pass except EmailNotValidError as e: # The recipient's email address is not valid so no further action is possible self.logger.info(e) pass
def reset_password(): # start with giving them the form if request.method == 'GET': if 'validation' not in request.args: flash( 'The password validation link is incomplete. Please verify your link is correct and try again.' ) return render_template('reset_password.html', validation_code=None) return render_template('reset_password.html', validation_code=request.args['validation']) # if we've reached here we are POST so we can email user link token = request.form.get('_validation_code', '') email = request.form.get('email_address', '') if not email: flash('Email cannot be blank.') return render_template('reset_password.html') try: v = validate_email(email) # validate and get info email = v["email"] # replace with normalized form except EmailNotValidError as exc: # email is not valid, exception message is human-readable flash(str(exc)) return render_template('reset_password.html') # If we've made it this far, it's a valid email so let's verify the generated # token with their email address. if r: userToken = r.get(email) if userToken.decode('utf-8') == token: logging.info('Successfully verified token {0} for {1}'.format( userToken, email)) r.delete(email) else: flash( 'Valid token not found. Please try your forgot password request again.' ) return render_template('reset_password.html') temporaryPassword = generate_temporary_password() try: g.uaac = UAAClient(app.config['UAA_BASE_URL'], None, verify_tls=app.config['UAA_VERIFY_TLS']) if g.uaac.set_temporary_password( app.config['UAA_CLIENT_ID'], app.config['UAA_CLIENT_SECRET'], email, temporaryPassword): logging.info( 'Set temporary password for {0}'.format(email)) return render_template('reset_password.html', password=temporaryPassword) else: flash( 'Unable to set temporary password. Did you use the right email address?' ) return render_template('reset_password.html') except Exception: logging.exception('Unable to set your temporary password.') return render_template('error/internal.html'), 500
def invite(): # start with giving them the form if request.method == 'GET': return render_template('invite.html') # if we've reached here we are POST, and they've asked us to invite # validate the email address email = request.form.get('email', '') if not email: flash('Email cannot be blank.') return render_template('invite.html') try: v = validate_email(email) # validate and get info email = v["email"] # replace with normalized form except EmailNotValidError as exc: # email is not valid, exception message is human-readable flash(str(exc)) return render_template('invite.html') # email is good, lets invite them try: if g.uaac.does_origin_user_exist( app.config['UAA_CLIENT_ID'], app.config['UAA_CLIENT_SECRET'], email, app.config['IDP_PROVIDER_ORIGIN']): flash('User already has a valid account.') return render_template('invite.html') redirect_uri = os.path.join(request.url_root, 'oauth', 'login') logging.info('redirect for invite: {0}'.format(redirect_uri)) invite = g.uaac.invite_users(email, redirect_uri) if len(invite['failed_invites']): raise RuntimeError('UAA failed to invite the user.') invite = invite['new_invites'][0] branding = {'company_name': app.config['BRANDING_COMPANY_NAME']} # we invited them, send them the link to validate their account subject = render_template('email/subject.txt', invite=invite, branding=branding).strip() body = render_template('email/body.html', invite=invite, branding=branding) send_email(app, email, subject, body) return render_template('invite_sent.html') except UAAError as exc: # if UAA complains that our access token is invalid then force them back through the login # process. # TODO: Fix this properly by implementing the refresh token oauth flow # in the UAAClient when it detects it's token is no longer valid if 'Invalid access token' in str(exc): return redirect(url_for('logout')) else: logging.exception('An error occured communicating with UAA') except Exception: logging.exception('An error occured during the invite process') return render_template('error/internal.html'), 500
@api.base @api.guest_only @api.args({ "name": And( StrippedString, lambda x: 2 <= len(x) <= 64, error="Il nome deve essere compreso tra 2 e 64 caratteri" ), "surname": And( StrippedString, lambda x: 2 <= len(x) <= 64, error="Il cognome deve essere compreso tra 2 e 64 caratteri" ), "email": And( StrippedString, lambda x: validate_email(x)["email"], error="Indirizzo email non valido" ), "password": Password, }) async def post(request: Request, *, params): async with EmaNews().db.acquire() as conn: async with conn.cursor() as cur: # Controlla se l'indirizzo email è stato già usato await cur.execute("SELECT id FROM users WHERE email = %s LIMIT 1", (params["email"],)) other_user = await cur.fetchone() if other_user: # Già usato raise ConflictError("Esiste già un altro utente registrato con questo indirizzo email.") # Ok, hash password con bcrypt
def test_ascii_free_email(self): email = self.factory.ascii_free_email() validate_email(email, check_deliverability=False) self.assertEqual(email.split('@')[0], 'asyl')
def test_email(self): email = self.factory.email() validate_email(email, check_deliverability=False)
def api_centros(): """ API endpoint: /centros Method GET: Este servicio permite obtener TODOS los centros de ayuda. Method POST: Este servicio permite cargar UN centro de ayuda. """ if request.method == 'GET': # Obtener todos los Centros de Ayuda # centers = HelpCenter.query.filter_by(status_id=1).all() # Serializar a JSON los Centros de Ayuda # help_center_schema = HelpCenterSchema(many=True) output = help_center_schema.dump(centers) # Crear una respuesta HTTP 200 OK con el JSON de Centros de Ayuda # res = make_response(jsonify(output), 200, {'Content-Type': 'charset=utf-8'}) elif request.method == 'POST': # Recibir un Centro de Ayuda en JSON # try: data = request.form.to_dict() protocol_file = None if request.files: protocol_file = request.files['visit_protocol'] if protocol_file: filename_vp = secure_filename(protocol_file.filename) protocol_path = path.join(current_app.root_path, 'static/uploads', filename_vp) protocol_file.save(protocol_path) data['visit_protocol'] = protocol_path else: data['visit_protocol'] = '' # Instancia HelpCenter validando que esten todas las claves correspondientes en el JSON # if not data["latitude"]: data["latitude"] = 0 if not data["longitude"]: data["longitude"] = 0 if data["email"]: # validar Email validate_email(data["email"]) # validar Horarios if not bool( re.compile(r'^(([01]\d|2[0-3]):([0-5]\d)|24:00)$').match( data["opening_time"])): raise Exception("Horario de apertura invalido") if not bool( re.compile(r'^(([01]\d|2[0-3]):([0-5]\d)|24:00)$').match( data["close_time"])): raise Exception("Horario de cierre invalido") help_center = HelpCenter( **{ k: data[k] for k in ("name_center", "address", "phone", "opening_time", "close_time", "town", "web", "email", "visit_protocol", "center_type_id", "latitude", "longitude") if k in data }) # Actualiza BD con el nuevo Centro de Ayuda # db.session.add(help_center) db.session.commit() # Crea una respuesta HTTP 201 Created con el JSON del nuevo Centro de Ayuda # res = make_response( jsonify(data), 201, {'Content-Type': 'application/json; charset=utf-8'}) except EmailNotValidError: res = make_response(jsonify({"mensaje": "E-mail inválido."}), 400) except Exception: # Crea una respuesta HTTP 400 Bad Request si falla la creacion # res = make_response( jsonify({"mensaje": "No se pudo cargar el centro de ayuda."}), 400) return res
def validate_email(email: str) -> bool: try: email_validator.validate_email(email) # validate and get info except email_validator.EmailNotValidError: return False return True
def api_centro_reserva(centro_id): """ API endpoint: /centros/<centro_id>/reserva Method POST: Este servicio permite realizar una reserva de un turno para un centro de ayuda en un dia en particular Parametros: ID del Centro """ try: if not HelpCenter.query.filter_by(id=centro_id).first(): raise Exception("El centro de ayuda no existe.") data = request.get_json() # validar formato Horarios if type(data["start_time"]) != str or not bool( re.compile(r'^(([01]\d|2[0-3]):([0-5]\d)|24:00)$').match( data["start_time"])): raise Exception("Horario de apertura invalido") if type(data["end_time"]) != str or not bool( re.compile(r'^(([01]\d|2[0-3]):([0-5]\d)|24:00)$').match( data["end_time"])): raise Exception("Horario de cierre invalido") center_accepted = HelpCenter.query.filter_by(id=centro_id).filter_by( status_id=1).first() opening_center = HelpCenter.query.filter_by( id=centro_id).first().opening_time close_center = HelpCenter.query.filter_by( id=centro_id).first().close_time request_start = datetime.datetime.strptime(data["start_time"], '%H:%M').time() request_end = datetime.datetime.strptime(data["end_time"], '%H:%M').time() appointment_start = request_start appointment_end = request_end exists = Appointment.query.filter_by(center_id=centro_id).filter_by( appointment_date=data["appointment_date"]).filter_by( start_time=data["start_time"]).first() # validar formato Email validate_email(data["email"]) # validar estado del centro if not center_accepted: raise Exception("El centro de ayuda no está disponible.") # validar franjas horarias if not (appointment_start >= opening_center and appointment_end <= close_center): raise Exception( "El horario solicitado está fuera del rango del horario de atención." ) correct_end = ( datetime.datetime.combine(datetime.date(1, 1, 1), request_start) + datetime.timedelta(minutes=30)).time() if not (correct_end == request_end): raise Exception("Los bloques deben ser de 30 minutos.") if exists: raise Exception("El turno solicitado ya está reservado.") appointment = Appointment( **{ k: data[k] for k in ("email", "first_name", "last_name", "phone", "start_time", "end_time", "appointment_date", "center_id") if k in data }) db.session.add(appointment) db.session.commit() res = make_response( jsonify(data), 201, {'Content-Type': 'application/json; charset=utf-8'}) except EmailNotValidError: res = make_response(jsonify({"mensaje": "E-mail inválido."}), 400) except Exception as err: res = make_response(jsonify({"mensaje": str(err)}), 400) return res
def validateEmail(email): try: validate_email(email) return True except EmailNotValidError as e: return False
# pip install email_validator from email_validator import validate_email, EmailNotValidError email = "*****@*****.**" try: # Validate. valid = validate_email(email) # Update with the normalized form. email = valid.email except EmailNotValidError as e: # email is not valid, exception message is human-readable print(str(e))
def create_user( # noqa: C901 password: str = None, user_roles: Union[Dict[str, str], List[Dict[str, str]], str, List[str]] = None, check_email_deliverability: bool = True, account_name: Optional[str] = None, **kwargs, ) -> User: """ Convenience wrapper to create a new User object. It hashes the password. In addition to the user, this function can create - new Role objects (if user roles do not already exist) - an Account object (if it does not exist yet) - a new DataSource object that corresponds to the user Remember to commit the session after calling this function! """ # Check necessary input explicitly before anything happens if password is None or password == "": raise InvalidFlexMeasuresUser("No password provided.") if "email" not in kwargs: raise InvalidFlexMeasuresUser("No email address provided.") email = kwargs.pop("email").strip() try: email_info = validate_email(email, check_deliverability=False) # The mx check talks to the SMTP server. During testing, we skip it because it # takes a bit of time and without internet connection it fails. if check_email_deliverability and not current_app.testing: try: validate_email_deliverability(email_info.domain, email_info["domain_i18n"]) except EmailUndeliverableError as eue: raise InvalidFlexMeasuresUser( "The email address %s does not seem to be deliverable: %s" % (email, str(eue))) except EmailNotValidError as enve: raise InvalidFlexMeasuresUser("%s is not a valid email address: %s" % (email, str(enve))) if "username" not in kwargs: username = email.split("@")[0] else: username = kwargs.pop("username").strip() # Check integrity explicitly before anything happens existing_user_by_email = User.query.filter_by(email=email).one_or_none() if existing_user_by_email is not None: raise InvalidFlexMeasuresUser("User with email %s already exists." % email) existing_user_by_username = User.query.filter_by( username=username).one_or_none() if existing_user_by_username is not None: raise InvalidFlexMeasuresUser("User with username %s already exists." % username) # check if we can link/create an account if account_name is None: raise InvalidFlexMeasuresUser( "Cannot create user without knowing the name of the account which this user is associated with." ) account = db.session.query(Account).filter_by( name=account_name).one_or_none() if account is None: print(f"Creating account {account_name} ...") account = Account(name=account_name) db.session.add(account) user_datastore = SQLAlchemySessionUserDatastore(db.session, User, Role) kwargs.update(password=hash_password(password), email=email, username=username) user = user_datastore.create_user(**kwargs) user.account = account # add roles to user (creating new roles if necessary) if user_roles: if not isinstance(user_roles, list): user_roles = [user_roles] # type: ignore for user_role in user_roles: if isinstance(user_role, dict): role = user_datastore.find_role(user_role["name"]) else: role = user_datastore.find_role(user_role) if role is None: if isinstance(user_role, dict): role = user_datastore.create_role(**user_role) else: role = user_datastore.create_role(name=user_role) user_datastore.add_role_to_user(user, role) # create data source db.session.add(DataSource(user=user)) return user
row['Subject'] ) #Sets dataframe variable, 'subject' to cells in column 'Subject' body = ( row['Email HTML Body'] ) #Sets dataframe variable, 'body' to cells in column 'Email HTML Body' #--- Print warnings --- if pd.isnull( email ): #Skips over rows where one of the cells in the three main columns is blank print('Sheet %s row %s: - Warning - email is null.' % (s, (index + 2))) else: try: email2 = (str(email)) validate_email(email2) # validate and get info except EmailNotValidError as e: print('Sheet %s row %s: - Warning - %s' % (s, (index + 2), str(e))) if pd.isnull(subject): print('Sheet %s row %s: - Warning - subject is null.' % (s, (index + 2))) continue if pd.isnull(body): print('Sheet %s row %s: - Warning - email body is null.' % (s, (index + 2))) continue if testflag > 1: #--- iterate through all sheets
def create_pledge_object(request): # Creates an un-saved Pledge instance. # # Raises an AlreadyPledgedError if the user has already made a Pledge # for the specified Trigger. p = Pledge() # trigger try: p.trigger = Trigger.objects.get(id=request.POST['trigger']) except Trigger.DoesNotExist: raise InvalidArgumentError("The trigger ID is invalid.") if p.trigger.status == TriggerStatus.Draft: raise InvalidArgumentError("This trigger is still a draft. A contribution cannot yet be made.") elif p.trigger.status not in (TriggerStatus.Open, TriggerStatus.Executed): raise InvalidArgumentError("This trigger is in the wrong state to make a contribution.") p.made_after_trigger_execution = (p.trigger.status == TriggerStatus.Executed) # ref_code (i.e. utm_campaign code) and Campaign ('via_campaign') from itfsite.models import Campaign, AnonymousUser p.ref_code = get_sanitized_ref_code(request) p.via_campaign = Campaign.objects.get(id=request.POST['via_campaign']) # user / anon_user if request.user.is_authenticated(): # This is an authentiated user. p.user = request.user exists_filters = { 'user': p.user } else: # This is an anonymous user. email = request.POST.get('email').strip() # If the user makes multiple actions anonymously, we'll associate # a single AnonymousUser instance with all of the Pledges. anon_user = AnonymousUser.objects.filter(id=request.session.get("anonymous-user")).first() if anon_user and anon_user.email == email: # Reuse this AnonymousUser instance. p.anon_user = anon_user else: # Validate email. See our function validate_email above. from email_validator import validate_email, EmailNotValidError try: validate_email(email, allow_smtputf8=False, check_deliverability=settings.VALIDATE_EMAIL_DELIVERABILITY) except EmailNotValidError as e: raise HumanReadableValidationError(str(e)) # Create a new AnonymousUser instance. p.anon_user = AnonymousUser.objects.create(email=email) # Record in the session so the user can reuse this instance and # to grant the user temporary (within the session cookie's session) # access to the resources the user creates while anonymous. request.session['anonymous-user'] = p.anon_user.id exists_filters = { 'anon_user': p.anon_user } # If the user has already made this pledge, it is probably a # synchronization problem. Just redirect to that pledge. p_exist = Pledge.objects.filter(trigger=p.trigger, **exists_filters).first() if p_exist is not None: raise AlreadyPledgedError(p_exist) # Field values & validation. def set_field(model_field, form_field, converter): try: setattr(p, model_field, converter(request.POST[form_field])) except ValueError: raise InvalidArgumentError("%s is out of range" % form_field) set_field('algorithm', 'algorithm', int) set_field('desired_outcome', 'desired_outcome', int) set_field('incumb_challgr', 'incumb_challgr', int) # -1, 0, 1 --- but one day we want a slider so model field is a float set_field('amount', 'amount', decimal.Decimal) if request.POST.get("contribTipOrg"): set_field('tip_to_campaign_owner', 'tip_amount', decimal.Decimal) if request.POST['filter_party'] in ('DR', 'RD'): p.filter_party = None # no filter else: p.filter_party = ActorParty.from_letter(request.POST['filter_party']) # Validation. Some are checked client side, so errors are internal # error conditions and not validation problems to show the user. if p.algorithm != Pledge.current_algorithm()["id"]: raise InvalidArgumentError("algorithm is out of range") if not (0 <= p.desired_outcome < len(p.trigger.outcomes)): raise InvalidArgumentError("desired_outcome is out of range") if not (p.trigger.get_minimum_pledge() <= p.amount <= Pledge.current_algorithm()["max_contrib"]): raise InvalidArgumentError("amount is out of range") if p.incumb_challgr not in (-1, 0, 1): raise InvalidArgumentError("incumb_challgr is out of range") if p.filter_party == ActorParty.Independent: raise InvalidArgumentError("filter_party is out of range") if p.tip_to_campaign_owner < 0: raise InvalidArgumentError("tip_to_campaign_owner is out of range") if p.tip_to_campaign_owner > 0 and (not p.via_campaign.owner or not p.via_campaign.owner.de_recip_id): raise InvalidArgumentError("tip_to_campaign_owner cannot be non-zero") if (p.trigger.trigger_type.extra or {}).get("monovalent") and p.incumb_challgr != 0: # With a monovalent trigger, Actors only ever take outcome zero. # Therefore not all filters make sense. A pledge cannot be filtered # to incumbents who take action 1 or to the opponents of actors # who do not take action 0. raise InvalidArgumentError("monovalent triggers do not permit an incumbent/challenger filter") tcust = TriggerCustomization.objects.filter(owner=p.via_campaign.owner, trigger=p.trigger).first() if tcust and tcust.incumb_challgr and p.incumb_challgr != tcust.incumb_challgr: raise InvalidArgumentError("incumb_challgr is out of range (campaign customization)") if tcust and tcust.filter_party and p.filter_party != tcust.filter_party: raise InvalidArgumentError("filter_party is out of range (campaign customization)") p.extra = { } # If the Trigger was a super-trigger, copy in the sub-triggers to the Pledge, # indicating the desired outcome of each sub-trigger through the outcome-map # on the Trigger. if p.trigger.extra and "subtriggers" in p.trigger.extra: p.extra["triggers"] = [ [rec["trigger"], rec["outcome-map"][p.desired_outcome]] for rec in p.trigger.extra["subtriggers"] ] return p
def process_user_input(inp, col, typ, tz=None): """ INPUT: - ``inp`` -- unsanitized input, as a string (or None) - ''col'' -- column name (names ending in ''link'', ''page'', ''time'', ''email'' get special handling - ``typ`` -- a Postgres type, as a string """ if inp and isinstance(inp, str): inp = inp.strip() if inp == "": return False if typ == "boolean" else ("" if typ == "text" else None) if col in maxlength and len(inp) > maxlength[col]: raise ValueError("Input exceeds maximum length permitted") if typ == "time": # Note that parse_time, when passed a time with no date, returns # a datetime object with the date set to today. This could cause different # relative orders around daylight savings time, so we store all times # as datetimes on Jan 1, 2020. if inp.isdigit(): inp += ":00" # treat numbers as times not dates t = parse_time(inp) t = t.replace(year=2020, month=1, day=1) assert tz is not None return localize_time(t, tz) elif (col.endswith("page") or col.endswith("link")) and typ == "text": # allow lists of URLs for speakers if col.startswith("speaker"): urls = [s.strip() for s in inp.split(SPEAKER_DELIMITER)] if any([not valid_url(x) for x in urls if x]): raise ValueError("Invalid URL") return (' ' + SPEAKER_DELIMITER + ' ').join(urls) if not valid_url(inp): raise ValueError("Invalid URL") return inp elif col.endswith("email") and typ == "text": # allow lists of emails for speakers if col.startswith("speaker"): emails = [s.strip() for s in inp.split(SPEAKER_DELIMITER)] return (' ' + SPEAKER_DELIMITER + ' ').join([ (validate_email(x)["email"] if x else '') for x in emails ]) return validate_email(inp)["email"] elif typ == "timestamp with time zone": assert tz is not None return localize_time(parse_time(inp), tz) elif typ == "daytime": res = validate_daytime(inp) if res is None: raise ValueError("Invalid time of day, expected format is hh:mm") return res elif typ == "daytimes": inp = cleanse_dashes(inp) res = validate_daytimes(inp) if res is None: raise ValueError( "Invalid times of day, expected format is hh:mm-hh:mm") return res elif typ == "weekday_number": res = int(inp) if res < 0 or res >= 7: raise ValueError( "Invalid day of week, must be an integer in [0,6]") return res elif typ == "date": return parse_time(inp).date() elif typ == "boolean": if inp in ["yes", "true", "y", "t", True]: return True elif inp in ["no", "false", "n", "f", False]: return False raise ValueError("Invalid boolean") elif typ == "text": if col.endswith("timezone"): return inp if pytz.timezone(inp) else "" # should sanitize somehow? return "\n".join(inp.splitlines()) elif typ in ["int", "smallint", "bigint", "integer"]: return int(inp) elif typ == "text[]": if inp == "": return [] elif isinstance(inp, str): if inp[0] == "[" and inp[-1] == "]": res = [elt.strip().strip("'") for elt in inp[1:-1].split(",")] if res == [""]: # was an empty array return [] else: return res else: # Temporary measure until we incorporate https://www.npmjs.com/package/select-pure (demo: https://www.cssscript.com/demo/multi-select-autocomplete-selectpure/) return [inp] elif isinstance(inp, Iterable): return [str(x) for x in inp] else: raise ValueError("Unrecognized input") else: raise ValueError("Unrecognized type %s" % typ)
def test_ascii_company_email(self): email = self.factory.ascii_company_email() validate_email(email, check_deliverability=False) assert email.split('@')[0] == 'andrecaua'
def normalize_email(email): return validate_email(email, check_deliverability=False)['email']
def reset_password(): # start with giving them the form if request.method == "GET": if "validation" not in request.args: flash( "The password validation link is incomplete. Please verify your link is correct and try again." ) return render_template("reset_password.html", validation_code=None) return render_template("reset_password.html", validation_code=request.args["validation"]) # if we've reached here we are POST so we can email user link token = request.form.get("_validation_code", "") email = request.form.get("email_address", "") if not email: flash("Email cannot be blank.") return render_template("reset_password.html") try: v = validate_email(email) # validate and get info email = v["email"] # replace with normalized form except EmailNotValidError as exc: # email is not valid, exception message is human-readable flash(str(exc)) return render_template("reset_password.html") # If we've made it this far, it's a valid email so let's verify the generated # token with their email address. try: userToken = r.get(email) except redis.exceptions.RedisError: return render_template("error/internal.html"), 500 if userToken is not None and userToken.decode("utf-8") == token: logging.info("Successfully verified token {0} for {1}".format( userToken, email)) try: r.delete(email) except redis.exceptions.RedisError: return render_template("error/internal.html"), 500 else: flash( "Valid token not found. Please try your forgot password request again." ) return render_template("reset_password.html") temporaryPassword = generate_temporary_password() try: g.uaac = UAAClient( app.config["UAA_BASE_URL"], None, verify_tls=app.config["UAA_VERIFY_TLS"], ) if g.uaac.set_temporary_password( app.config["UAA_CLIENT_ID"], app.config["UAA_CLIENT_SECRET"], email, temporaryPassword, ): logging.info("Set temporary password for {0}".format(email)) return render_template( "reset_password.html", password=temporaryPassword, loginLink=app.config["UAA_BASE_URL"], ) else: flash( "Unable to set temporary password. Did you use the right email address?" ) return render_template("reset_password.html") except Exception: logging.exception("Unable to set your temporary password.")
def test_ascii_safe_email(self): email = self.factory.ascii_safe_email() validate_email(email, check_deliverability=False) assert email.split('@')[0] == 'fabinn'
def test_ascii_safe_email(self): email = self.factory.ascii_safe_email() validate_email(email, check_deliverability=False) assert email.split('@')[0] == 'vitoriamagalhaes'
def validate(email): try: validate_email(email) except: return False return True
# Search the hostname in each supported engine. for search in theharvester_wrapper.ENGINES: print "Searching on: %s" % search.__doc__ try: results = search(word) except Exception: errors.append(traceback.format_exc()) continue errors.extend(results.get("errors", [])) emails.update(results.get("emails", [])) hostnames.update(results.get("hostnames", [])) # Convert the emails to the Golismero data model. for email in sorted(emails): try: validate_email(email) except EmailNotValidError: errors.append(traceback.format_exc()) continue output.append( { "_id": mmh3.hash128(email), "_type": "email", "_tool": TOOL, "email": email, } ) # Convert the hostnames to the Golismero data model. for hostname in sorted(hostnames): if validate_hostname(hostname):
def respect(): ext = "txt, csv, hist, fa, fq, fastq, fna, fasta" if request.method == 'POST': email = request.form.get('userEmail') print(email) # if the user email is valid and existing, warn and ask to submit again # print(validate_email(email_address=email, check_format=True)) try: valid = validate_email(email, allow_smtputf8=False) email = valid.ascii_email print(email) except EmailNotValidError as e: flash('Your email does not exist. Try again', 'error') return redirect(request.url) if 'folder' not in request.files: print("is it here?") flash('No directory', 'error') return redirect(request.url) timestamp = datetime.now().strftime('%Y-%m-%d_%H%M%S') print(request.files.getlist('folder')) hasHist = False hasMapping = False #making the input and output directory input_dir = os.path.join(app.config['IMAGE_UPLOADS'],'respect',timestamp) if not os.path.exists(input_dir): os.makedirs(input_dir,exist_ok=True) output_dir = os.path.join(app.config['IMAGE_UPLOADS'],'respect',timestamp+'_results') if not os.path.exists(output_dir): os.makedirs(output_dir,exist_ok=True) #for all input files in the directory for file in request.files.getlist('folder'): # if no uploaded file if file.filename == '': flash('No selected file', 'error') tools.get_rid_of_folders(input_dir, output_dir) return redirect(request.url) #check for file format if file and tools.allowedFile(file.filename): #checking if there's a histogram file if file.filename.rsplit('.',1)[1].lower() == 'hist': hasHist = True filename = secure_filename(file.filename) print(filename) uploaded = os.path.join(input_dir, filename) file.save(uploaded) print(filename) # if extension is not acceptable else: flash("Unacceptable File Type. Only accept: {}".format(ext + " and .gz version"), 'warning') tools.get_rid_of_folders(input_dir, output_dir) return redirect(request.url) ## for histogram info file if (hasHist): if 'hist-file' not in request.files: flash('No file part', 'error') tools.get_rid_of_folders(input_dir, output_dir) return redirect(request.url) h_file = request.files['hist-file'] # if no uploaded histogram info file if h_file.filename == '': print("Need Histogram Info File") flash('No file uploaded') tools.get_rid_of_folders(input_dir, output_dir) return redirect(request.url) # if there is an input file and the file extension is acceptable if h_file and tools.allowedFile(h_file.filename): hist_info_filename = secure_filename(h_file.filename) hist_file_path = os.path.join(input_dir, hist_info_filename) h_file.save(hist_file_path) print(hist_info_filename + ' saved as histogram info file') # if exension is not acceptable else: flash("Unacceptable extension. Only accept: {}".format(ext + " and .gz version"), 'warning') tools.get_rid_of_folders(input_dir, output_dir) return redirect(request.url) ##for mapping file if 'mapping-file' in request.files: m_file = request.files['mapping-file'] # if mapping file is accepted if m_file.filename != '' and tools.allowedFile(m_file.filename): hasMapping = True mapping_filename = secure_filename(m_file.filename) mapping_file_path = os.path.join(input_dir, mapping_filename) m_file.save(mapping_file_path) print(mapping_filename + ' saved as mapping file') # if file format is not accepted elif m_file.filename != '': flash("Unacceptable extension. Only accept: {}".format(ext + " and .gz version"), 'warning') tools.get_rid_of_folders(input_dir, output_dir) return redirect(request.url) kmer_size = request.form.get('kmer_size') num_iter = request.form.get('iter') if num_iter == '' or 'None': num_iter = "1000" temp = request.form.get('temp') if temp == '' or 'None': temp = "1.0" uniq = request.form.get('uniq') if uniq == '' or 'None': uniq = "0.1" norm = request.form.get('norm') if norm == '' or 'None': norm = "1" spec_num = request.form.get('spec-num') if spec_num == '' or 'None': spec_num = "50" print(num_iter + temp + uniq + norm + spec_num) #passing over to shell command # only for testing data we use -N 10, if it was published, we need to get the input from form if((hasHist == False) and (hasMapping == False)): c = "respect -d {0} -N 10 --debug -o {1} -k {2} -T {3} -r {4} -l {5} -n {6}".format(input_dir,output_dir,\ kmer_size, temp, uniq, norm, spec_num) elif ((hasHist == True) and (hasMapping == False)): c = "respect -d {0} -I {1} -N 10 --debug -o {2} -k {3} -T {4} -r {5} -l {6} -n {7}".format(input_dir,hist_file_path, output_dir,\ kmer_size, temp, uniq, norm, spec_num) elif ((hasHist == False) and (hasMapping == True)): c = "respect -d {0} -m {1} -N 10 --debug -o {2} -k {3} -T {4} -r {5} -l {6} -n {7}".format(input_dir,mapping_file_path, output_dir,\ kmer_size, temp, uniq, norm, spec_num) else: c = "respect -d {0} -m {1} -I {2} -N 10 -k {3} -T {4} -r {5} -l {6} -n {7} --debug -o {8}".format(input_dir,mapping_file_path, \ hist_file_path, kmer_size, temp, uniq, norm, spec_num, output_dir) print(c) # return redirect(request.url) #runs respect tools.run_command(c) # delete input folder (and all files in directory) try: shutil.rmtree(input_dir) except OSError as e: print("Error: %s : %s" % (input_dir, e.strerror)) flash("Successfully Uploaded Files! This might take a while. You can safely exit out of this page", "info") # send email when the respect is done running getResultdir = 'respect/'+timestamp+'_results' tools.sendResults(timestamp, email, output_dir, getResultdir) return redirect(url_for("result", result_dir = getResultdir)) return render_template("respect.html", title = "RESPECT", id = "respect")
def check_email(mail): try: email = validate_email(mail, check_deliverability=False) return email['email'] except ValueError: return None
def test_ascii_company_email(self): email = self.factory.ascii_company_email() validate_email(email, check_deliverability=False) self.assertEqual(email.split('@')[0], 'fabienne')