def post(): title = request.form.get("title") subtitle = request.form.get("subtitle") # turn the title into a slug by removing all non alphanumeric or numeric # characters slug = re.sub(r'[^a-zA-Z0-9]', '-', title) # make it all lowercase slug = slug.lower() print(slug) featured_image_url = request.form.get( "featured-image") # get image url content = request.form.get("content") tags = request.form.get("tags") if Post.query.filter(Post.title == title).first(): flash("Sorry there is already an article with that title", "danger") else: try: post = Post(user_id=current_user.id, title=title, subtitle=subtitle, content=content, featured_image_url=featured_image_url, slug=slug, tags=tags) # add article to database db.add(post) db.commit() flash("New article has been successfully created", "success") except Exception: db.rollback() flash("There was an error processing your request", "danger")
def create_remote_user(google_id, facebook_id): # hence current_user can be accessed afterwards if google_id: remote_user = RemoteSourceUser(user_id=current_user.id, google_id=google_id) else: remote_user = RemoteSourceUser(user_id=current_user.id, fb_id=facebook_id) try: db.add(remote_user) db.commit() except Exception: db.rollback()
def post(): email = request.form.get("email") user = User.query.filter(User.email == email).first() if (user): try: # Create a new request to verify email email_token = generate_hash() # email THIS to the user hashed_id = hashlib.sha256(email_token).hexdigest() password_reset_request = PasswordResetRequest(user_id=user.id, token=hashed_id) db.add(password_reset_request) db.commit() from app.http.controllers.mail_senders import send_recovery_email send_recovery_email(user.name, user.email, email_token) return render_template("login/reset_password.html") except Exception: flash("There was an error processing your request", "danger") return render_template("login/begin_reset.html")
def post(): name = request.form.get("name") email = request.form.get("email") image_url = request.form.get("social-image") google_id = request.form.get("google-id") facebook_id = request.form.get("fb-id") # user is logged in with this function next = create_user(name, email, image_url) # hence current_user can be accessed afterwards if google_id: remote_user = RemoteSourceUser(user_id=current_user.id, google_id=google_id) else: remote_user = RemoteSourceUser(user_id=current_user.id, fb_id=facebook_id) try: db.add(remote_user) db.commit() except Exception: db.rollback() return redirect(next)
def create_user(name, email, profile_image_url=None, password=""): if profile_image_url is None: profile_image_url = url_for('static', filename='images/default_logo.png') user = User(name=name, email=email, password=password, profile_image_url=profile_image_url) # save the new user db.add(user) db.commit() # check if there is a user logged in, if so log them out if (current_user): logout_user() # login the current user so that we have a handle on the object login_user(user) print("Attempting to send email") from app.http.controllers.mail_senders import send_verify_email send_verify_email(user) return url_for("register.verify_user")
def send_verify_email(user=current_user): # Create a new request to verify email email_token = generate_hash() # email THIS to the user hashed_id = hashlib.sha256(email_token).hexdigest() verify_email = VerifyEmailRequest(user_id=user.id, token=hashed_id) # Add the request to the database db.add(verify_email) db.commit() recipients = list() recipients.append(user.email) url = "https://www.israelfl.com/register/activate?token={}".format( email_token) email = Message(subject="Verify your email with Israel FL", sender="*****@*****.**", recipients=recipients) email.html = render_template("emails/verify_email.html", name=user.name, url=url) print(recipients) mail.send(email) print("email sent")
def post(): try: password = generate_password_hash(request.form.get('password')) email = request.form.get("email") name = request.form.get("name") username = request.form.get("username") # Check if an account with the given credentials already exists if (User.query.filter(User.email == email).first()): flash( 'Sorry, there is already an account associated with that email', "danger") elif (User.query.filter(User.display_name == username).first()): flash('Sorry, that username has already been taken', "danger") else: user = User(name=name, display_name=username, email=email, password=password, profile_image_url=url_for( 'static', filename='images/default_logo.png')) # save the new user db.add(user) db.commit() # check if there is a user logged in, if so log them out if (current_user): logout_user() # login the current user so that we have a handle on the object from app.http.controllers.mail_senders import send_verify_email send_verify_email(user) flash( "The user was created successfully and a verification email has been sent", "success") except Exception as e: print(e) db.rollback() flash("There was an error processing your request", "danger")