Exemple #1
0
    def post():
        article = Post.query.filter(Post.slug == slug).first()
        title = request.form.get("title")
        # if the title changed, check if there is already an article with the
        # new title
        if article.title != title:
            new_slug = title.replace(' ', '-').lower()
            existing_article = Post.query.filter(Post.slug == new_slug)
            if existing_article:
                flash(
                    "Sorry there is already an article with that title, try a different one",
                    "danger")
                return

        article.subtitle = request.form.get("subtitle")
        article.slug = title.replace(' ', '-').lower()
        # turn the title into a slug
        article.featured_image_url = request.form.get(
            "featured-image")  # get image url
        article.content = request.form.get("content")
        article.tags = request.form.get("tags")

        try:
            # Save changes
            db.commit()

            flash("Edits successfully saved", "success")
        except Exception:
            db.rollback()
            flash("There was an error processing your request", "danger")
Exemple #2
0
    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")
Exemple #3
0
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()
Exemple #4
0
 def post():
     name = request.form.get("name")
     password = generate_password_hash(request.form.get('password'))
     user = User.query.filter(User.id == current_user.id).first()
     user.name = name
     user.password = password
     try:
         # save changes to the user
         db.commit()
         flash("Changes saved successfully", "success")
     except Exception:
         db.rollback()
         flash("There was an error processing your request", "danger")
Exemple #5
0
def delete_article(slug):

    article = Post.query.filter(Post.slug == slug).delete()
    if article:
        try:
            db.commit()
            flash("Article deleted", "info")
        except Exception:
            db.rollback()
            flash("There was an error processing your request", "danger")
    else:
        abort(404)

    return redirect(url_for("dashboard.show_pending_approvals"))
Exemple #6
0
def approve(slug):

    article = Post.query.filter(Post.slug == slug).first()
    if article:
        article.published = True
        try:
            db.commit()
            flash("Article approved", "success")
        except Exception:
            db.rollback()
            flash("There was an error processing your request", "danger")
    else:
        abort(404)

    return redirect(url_for("dashboard.show_pending_approvals"))
Exemple #7
0
 def post():
     google_id = request.form.get("google-id")
     if google_id:
         google_id = check_id_integrity(google_id)
         if not google_id:
             flash("Invalid token, has someone been tampering?", "danger")
             return redirect(url_for("login.login"))
     facebook_id = request.form.get("fb-id")
     email = request.form.get("social-email")
     name = request.form.get("social-name")
     image_url = request.form.get("social-image")
     if email is None:
         return render_template("register/register_email_only.html",
                                google_id=google_id,
                                facebook_id=facebook_id,
                                name=name,
                                image_url=image_url)
     else:
         # check if the email is already taken
         user = User.query.filter(User.email == email).first()
         # if it does log them in
         if user:
             login_user(user)
             print("User exists {}".format(user.remote_user))
             # check if this user has used a social login before,
             # try to merge accounts
             # check which id was supplied
             if user.remote_user.google_id == google_id:
                 return redirect(check_current_user_level())
             elif user.remote_user.fb_id == facebook_id:
                 return redirect(check_current_user_level())
             elif google_id:
                 user.remote_user.google_id = google_id
                 db.commit()
             else:
                 user.remote_user.fb_id = facebook_id
                 db.commit()
             # if the user has not verified their account redirect
             # them to the verification portal
             return redirect(url_for("register.verify_user"))
         # user is logged in with this function
         next = create_user(name, email, image_url)
         print(next)
         print("Next finished")
         create_remote_user(google_id, facebook_id)
         return redirect(next)
Exemple #8
0
 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")
Exemple #9
0
    def post():
        user = User.query.filter(User.id == current_user.id).first()

        if (int(request.form.get("form-type")) == 1):
            print("Form type 1")
            user.name = request.form.get("name")
            user.description = request.form.get("description")
        else:
            print("Form type 2")
            password = generate_password_hash(request.form.get('password'))
            user.password = password
        try:
            # save changes to the user
            db.commit()
            flash("Changes saved successfully", "success")
        except Exception:
            db.rollback()
            flash("There was an error processing your request", "danger")
Exemple #10
0
 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)
Exemple #11
0
def activate_user():
    try:
        token = request.args.get("token")
        # check if the token matches the database
        verify_obj = VerifyEmailRequest.query.filter(
            VerifyEmailRequest.token == hashlib.sha256(
                token).hexdigest()).first()
        if (verify_obj is not None):
            # get the related user
            user = User.query.filter(User.id == verify_obj.user_id).first()
            if (user.verified):
                abort(404)
            user.verified = True
            verify_obj.completed = True
            db.commit()
            return render_template("register/successfully_verified.html")
        # else the user is not authorized
        else:
            db.rollback()
            abort(404)
    except Exception:
        abort(404)
Exemple #12
0
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")
Exemple #13
0
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")
Exemple #14
0
    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")