def init_threaded_duplication(web, user, form, temp_recipe, instances, form_submitted): with web.app_context(): temp_recipe = db.session.query(Recipe).filter( Recipe.uid == temp_recipe.uid).first() copies = int(form.amount.data) instance_count = 0 for i in range(1, 1 + copies): new_recipe = Recipe(name=form.name.data.replace('%i', str(i)).replace( '%n', str(copies)), description=form.description.data, active=form.active.data, cookies=temp_recipe.cookies, interval=temp_recipe.interval) for temp_step in temp_recipe.steps: new_step = RecipeStep( sort=temp_step.sort, type=temp_step.type, value=temp_step.value, use_random_item_instead_of_value=temp_step. use_random_item_instead_of_value, use_data_item_instead_of_value=temp_step. use_data_item_instead_of_value, active=temp_step.active) for temp_item in temp_step.items: new_step.items.append( RecipeStepItem(value=temp_item.value)) new_recipe.steps.append(new_step) if form.user_privileges.data is True: new_recipe.owner = temp_recipe.owner for temp_privilege in temp_recipe.privileged_users: new_recipe.privileged_users.append( UserRecipePrivilege( user=temp_privilege.user, allowed_to_edit=temp_privilege.allowed_to_edit)) else: new_recipe.owner = user instance_count = 0 for temp_instance in instances: if ('instance_' + str(temp_instance.uid)) in form_submitted: if form_submitted['instance_' + str(temp_instance.uid)] == 'y': instance_count = instance_count + 1 new_recipe.instances.append( RecipeOrder(instance=temp_instance)) db.session.add(new_recipe) db.session.commit() msg = Message('Your ScrapeBot recipe-duplication request', sender='ScrapeBot <*****@*****.**>', recipients=[user.email]) msg.body = render_template('email/duplication.txt', user=user, copies=copies, copied_privileges=form.user_privileges.data, copied_active=form.active.data, copied_instances=instance_count, recipe=temp_recipe) mail.send(msg)
def sendEmail(message): with app.app_context(): msg = Message('Attention Please!', sender='*****@*****.**', recipients=['*****@*****.**']) msg.body = message mail.send(msg)
def password_change(): form = PasswordChangeForm() if form.validate_on_submit(): user = form.user user.password = generate_password_hash(form.new_password.data) db.session.commit() if current_app.config['MAIL']: message = '''Hello %s,\n\n This is e-mail is to inform you that you have changed your password successfully. \nIf this request was not made by you please contact support immediately.\n \nThank you.\n Pimat\n\n''' % user.username subject = "Pimat Password Change Notice - %s" % user.username msg = Message(recipients=[user.email], body=message, subject=subject) mail.send(msg) flash( 'Password changed successfully, you should logout and login again!', 'success') return redirect(url_for("core.dashboard")) else: for field, errors in form.errors.items(): for error in errors: flash(error, 'warning') return render_template('auth/password_change.html', version=version)
def send_reset_email(user): token = user.get_reset_token() msg = Message('Password Reset Request', sender='*****@*****.**', recipients=[user.email]) msg.body = f'''Reset Password Click The Following Link: {url_for('reset_password', token=token, _external=True)} If you did not reset your password ignore this email. ''' mail.send(msg)
def send_reset_email(user): token = user.get_reset_token() msg = Message('Password Reset Request', sender='*****@*****.**', recipients=[user.email]) msg.body = f'''To reset your Password, visit the following link: {url_for('Admin.reset_password_admin', token=token, _external=True)} If you did not make this request, Please Ignore this!! ''' mail.send(msg)
def sign(): # valid username and password email = request.form['email'] if re.match('\S+@\S+\.\S+', email) is None: flash('Invalid email address.') return redirect(url_for('login')) password = request.form['password'] if re.match('[0-9a-f]{32}', password) is None: flash('Invalid password.') return redirect(url_for('login')) user = User.query.filter_by(email=email).first() if user is not None: flash('Email already exist.') return redirect(url_for('login')) try: msg = Message("Account", sender=app.config.get('MAIL_USERNAME'), recipients=[email]) msg.body = app.config.get('HOST_URL') + "/validate?vcode=%s&email=%s" % \ (md5(app.config.get('SECRET_KEY') + email), email) mail.send(msg) except: import traceback traceback.print_exc() flash('Mail error happend. Contact us!') return redirect(url_for('login')) user = User() user.email = email user.login_pass = password user.service_pass = str(random.randint(1000000, 99999999)) user.transfer_download = 0 user.transfer_upload = 0 user.transfer_enable = 0 user.service_enable = 0 user.service_switch = 0 user.last_service_time = 0 user.last_get_gift_time = 0 user.type = 0 user.service_port = 0 db.session.add(user) flash('Validate email send.') return redirect(url_for('login'))
def send_email(recipient, subject, template, **kwargs): with app.app_context(): msg = Message( subject, sender=("MIT Endurance Project", "*****@*****.**"), recipients=[recipient], ) # msg.body = render_template(template + '.txt', **kwargs) msg.html = render_template(template + ".html", **kwargs) result = mail.send(msg) print("RESULT\n\n\n\n") print(result)
def forget(): form = ForgetPasswordForm() if form.validate_on_submit(): users = User.get_all() check = False for i in users: if (form.email.data == i.email): check = True user_mail = form.email.data random_pass = (''.join([random.choice(list('123456789qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM')) for x in range(8)])) msg = Message('Восстановление пароля', sender="*****@*****.**", recipients=[user_mail]) msg.html = render_template('mail.html', password=random_pass, login=i.login, name=i.name) mail.send(msg) secret_pass = pbkdf2_sha256.hash(random_pass) User.query.filter_by(login=i.login).update(dict(password=secret_pass)) db.session.commit() if check != True: return render_template('forget.html', error_text='Пользователя с таким email не существует.', form=form) return redirect('/') return render_template('forget.html', form=form)
def password_forgot(): form = PasswordForgotForm() if form.validate_on_submit(): user_details = form.user s = Serializer(current_app.config['SECRET_KEY'], expires_in=600) token = s.dumps({'id': user_details.id}) message = '''Hello, \n\n To reset your password go to: http://%s/password_reset \n\n Token: \n %s''' % \ (current_app.config['SERVER_IP'], token) subject = "Pimat Password Reset - %s" % user_details.username msg = Message(recipients=[user_details.email], body=message, subject=subject) mail.send(msg) flash('Please verify you mailbox!', 'success') return redirect(url_for("auth.password_reset")) else: for field, errors in form.errors.items(): for error in errors: flash(error, 'warning') return render_template('auth/password_forgot.html', version=version, form=form)
def password_reset(): form = PasswordResetForm() if form.validate_on_submit(): user = form.user s = Serializer(current_app.config['SECRET_KEY']) try: data = s.loads(form.token.data) except SignatureExpired: flash('Expired Token', 'danger') return render_template('auth/password_reset_form.html', version=version, form=form) except BadSignature: flash('Invalid Token', 'danger') return render_template('auth/password_reset_form.html', version=version, form=form) user.password = generate_password_hash(form.new_password.data) db.session.commit() message = '''Hello %s,\n\n This is e-mail is to inform you that you have reset your password successfully. \nIf this request was not made by you please contact support immediately.\n \nThank you.\n Pimat\n\n''' % user.username subject = "Pimat Password Reset Notice - %s" % user.username msg = Message(recipients=[user.email], body=message, subject=subject) mail.send(msg) flash('Password updated successfully, Please login.', 'success') return redirect(url_for("login")) else: for field, errors in form.errors.items(): for error in errors: flash(error, 'warning') return render_template('auth/password_reset_form.html', version=version, form=form)
def instance(instance_uid): temp_instance = db.session.query(Instance).filter( Instance.uid == instance_uid).first() if temp_instance is not None and temp_instance.is_visible_to_user( current_user): user_recipes = current_user.recipes_owned for privilege in current_user.recipe_privileges: if privilege.allowed_to_edit: user_recipes.append(privilege.recipe) form_privilege = PrivilegeForm() if form_privilege.validate_on_submit() and form_privilege.email.data: if temp_instance.owner_uid is current_user.uid: temp_user = db.session.query(User).filter( User.email == form_privilege.email.data).first() if temp_user is None or temp_user is temp_instance.owner: flash('User not found') else: temp_privilege = db.session.query(UserInstancePrivilege)\ .filter(UserInstancePrivilege.user == temp_user, UserInstancePrivilege.instance == temp_instance)\ .first() if temp_privilege is None: temp_privilege = UserInstancePrivilege( user=temp_user, allowed_to_edit=form_privilege.allowed_to_edit.data ) temp_instance.privileged_users.append(temp_privilege) elif not temp_privilege.allowed_to_edit and form_privilege.allowed_to_edit.data: temp_privilege.allowed_to_edit = True else: flash('Access already granted') return redirect( url_for('main.instance', instance_uid=instance_uid)) db.session.commit() msg = Message('Access to new ScrapeBot instance granted', sender='ScrapeBot <*****@*****.**>', recipients=[temp_user.email]) msg.body = render_template('email/privilege_instance.txt', user=temp_user, instance=temp_instance, privilege=temp_privilege) mail.send(msg) flash('Privilege added and user informed via email') else: flash( 'You are not allowed to do this as only an instance\'s owner can permit privileges' ) return redirect(url_for('main.instance', instance_uid=instance_uid)) form = InstanceForm() if form.validate_on_submit(): temp_instance.description = form.description.data for temp_recipe in user_recipes: temp_order = db.session.query(RecipeOrder).filter( RecipeOrder.recipe == temp_recipe, RecipeOrder.instance == temp_instance).first() if request.form.get('recipe_' + str(temp_recipe.uid)) == 'y': if temp_order is None: temp_recipe.instances.append( RecipeOrder(instance=temp_instance)) elif temp_order is not None: db.session.delete(temp_order) db.session.commit() return redirect(url_for('main.instance', instance_uid=instance_uid)) privileged_users = [] recipes = [] form.name.data = temp_instance.name form.description.data = temp_instance.description for temp_recipe in user_recipes: recipes.append({ 'uid': temp_recipe.uid, 'name': temp_recipe.name, 'interval': temp_recipe.interval, 'active': temp_instance.runs_recipe(temp_recipe, False), 'recipe_active': temp_recipe.active }) if temp_instance.owner_uid == current_user.uid: for temp_privilege in temp_instance.privileged_users: privileged_users.append(temp_privilege.jsonify()) return render_template('main/instance.html', instance=temp_instance, form=form, recipes=recipes, form_privilege=form_privilege, privileged_users=privileged_users) flash('You do not have the permission to view this instance.') return render_template('main/dashboard.html')
def send_async_mail(msg): return mail.send(msg)
def send_async_email(msg): with app.app_context(): mail.send(msg)
def send_email(to, subject, template): msg = Message(subject, recipients=[to], html=template, sender=app.config['MAIL_DEFAULT_SENDER']) mail.send(msg)
def recipe(recipe_uid): temp_recipe = None if recipe_uid is not None: temp_recipe = db.session.query(Recipe).filter( Recipe.uid == int(recipe_uid)).first() if not temp_recipe.is_visible_to_user(current_user): flash('You do not have the permission to view this recipe.') return redirect(url_for('main.dashboard')) instances = current_user.instances_owned for privilege in current_user.instance_privileges: if privilege.allowed_to_edit: instances.append(privilege.instance) form = RecipeForm() if form.validate_on_submit() and form.name.data: if recipe_uid is None: temp_recipe = Recipe() temp_recipe.name = form.name.data temp_recipe.description = form.description.data temp_recipe.interval = form.interval.data temp_recipe.cookies = form.cookies.data temp_recipe.active = form.active.data for temp_instance in instances: if request.form.get('instance_' + str(temp_instance.uid)) == 'y': temp_order = None if recipe_uid is not None: temp_order = db.session.query(RecipeOrder).filter( RecipeOrder.recipe == temp_recipe, RecipeOrder.instance == temp_instance).first() if temp_order is None: temp_recipe.instances.append( RecipeOrder(instance=temp_instance)) elif recipe_uid is not None: temp_order = db.session.query(RecipeOrder).filter( RecipeOrder.recipe == temp_recipe, RecipeOrder.instance == temp_instance).first() if temp_order is not None: db.session.delete(temp_order) current_user.recipes_owned.append(temp_recipe) db.session.commit() if recipe_uid is None: temp_recipe = db.session.query(Recipe).filter(Recipe.owner == current_user)\ .order_by(Recipe.created.desc()).first() recipe_uid = temp_recipe.uid return redirect(url_for('main.recipe', recipe_uid=recipe_uid)) form_privilege = PrivilegeForm() if form_privilege.validate_on_submit() and form_privilege.email.data: if temp_recipe.owner_uid is current_user.uid: temp_user = db.session.query(User).filter( User.email == form_privilege.email.data).first() if temp_user is None or temp_user is temp_recipe.owner: flash('User not found') else: temp_privilege = db.session.query(UserRecipePrivilege)\ .filter(UserRecipePrivilege.user == temp_user, UserRecipePrivilege.recipe == temp_recipe)\ .first() if temp_privilege is None: temp_privilege = UserRecipePrivilege( user=temp_user, allowed_to_edit=form_privilege.allowed_to_edit.data) temp_recipe.privileged_users.append(temp_privilege) elif not temp_privilege.allowed_to_edit and form_privilege.allowed_to_edit.data: temp_privilege.allowed_to_edit = True else: flash('Access already granted') return redirect( url_for('main.recipe', recipe_uid=recipe_uid)) db.session.commit() msg = Message('Access to new ScrapeBot recipe granted', sender='ScrapeBot <*****@*****.**>', recipients=[temp_user.email]) msg.body = render_template('email/privilege_recipe.txt', user=temp_user, recipe=temp_recipe, privilege=temp_privilege) mail.send(msg) flash('Privilege added and user informed via email') else: flash( 'You are not allowed to do this as only a recipe\'s owner can permit privileges' ) return redirect(url_for('main.recipe', recipe_uid=recipe_uid)) user_instances = [] privileged_users = [] if temp_recipe is None: for temp_instance in instances: user_instances.append({ 'uid': temp_instance.uid, 'name': temp_instance.name, 'active': False }) else: form.name.data = temp_recipe.name form.description.data = temp_recipe.description form.interval.data = temp_recipe.interval form.cookies.data = temp_recipe.cookies form.active.data = temp_recipe.active for temp_instance in instances: user_instances.append({ 'uid': temp_instance.uid, 'name': temp_instance.name, 'active': temp_instance.runs_recipe(temp_recipe, False) }) if temp_recipe.owner_uid == current_user.uid: for temp_privilege in temp_recipe.privileged_users: privileged_users.append(temp_privilege.jsonify()) average_runtime = 0 if temp_recipe is not None: average_runtime = temp_recipe.get_average_runtime() return render_template('main/recipe.html', form=form, instances=user_instances, recipe=temp_recipe, average_runtime=average_runtime, form_privilege=form_privilege, privileged_users=privileged_users)
def init_threaded_download(web, user, instance_uids, recipe_uids): with web.app_context(): temp_name = 'order_' + hashlib.md5( bytes(user.email + str(time.time()), encoding='utf-8')).hexdigest() + '.csv' temp_file = open(temp_name, 'w', newline='') # extrasaction='ignore' tells DictWriter not to check on the keys in every single iteration csv_data = csv.DictWriter(temp_file, fieldnames=[ 'run', 'instance', 'recipe', 'recipe_name', 'recipe_status', 'step', 'step_name', 'data_creation', 'data_value' ], extrasaction='ignore') csv_data.writeheader() for instance_uid in instance_uids: instance = db.session.query(Instance).filter( Instance.uid == instance_uid).one_or_none() if instance and instance.is_visible_to_user(user): for recipe_uid in recipe_uids: recipe = db.session.query(Recipe).filter( Recipe.uid == recipe_uid).one_or_none() if recipe and recipe.is_visible_to_user(user): rows = [] for run_data in \ db.session.query( Data.created, Data.value, RecipeStep.sort, RecipeStep.type, Run.created, Run.status )\ .filter( Data.step_uid == RecipeStep.uid, Data.run_uid == Run.uid, Run.instance_uid == instance_uid, Run.recipe_uid == recipe_uid ): rows.append({ 'run': str(run_data[4]), 'instance': instance.name, 'recipe': str(recipe_uid), 'recipe_name': recipe.name, 'recipe_status': run_data[5].name, 'step': run_data[2], 'step_name': run_data[3].name, 'data_creation': str(run_data[0]), 'data_value': run_data[1] }) csv_data.writerows(rows) file_size = temp_file.tell() temp_file.close() delete = True msg = Message('Your ScrapeBot data request', sender='ScrapeBot <*****@*****.**>', recipients=[user.email]) if file_size < 2000000: msg.body = render_template('email/download.txt', user=user, link='') temp_file = open(temp_name, 'r') msg.attach(temp_name, 'text/csv', temp_file.read()) temp_file.close() else: link = temp_name if config.get('Database', 'AWSaccess') is not None and \ config.get('Database', 'AWSsecret') is not None and \ config.get('Database', 'AWSbucket') is not None: import boto3 client = boto3.client( 's3', aws_access_key_id=config.get('Database', 'AWSaccess'), aws_secret_access_key=config.get('Database', 'AWSsecret')) with open(temp_name, 'rb') as data: client.upload_fileobj(data, config.get('Database', 'AWSbucket'), temp_name) link = 's3://' + config.get('Database', 'AWSbucket') + '/' + temp_name else: delete = False msg.body = render_template('email/download.txt', user=user, link=link) if delete: os.remove(temp_name) mail.send(msg)