예제 #1
0
def setup():
    form = SetupForm()
    error = ''
    if form.validate_on_submit():
        author = Author(
            form.fullname.data,
            form.email.data,
            form.username.data,
            form.password.data,
            True
            )
        db.session.add(author)
        db.session.flush()
        if author.id:
            blog = Blog(
                form.name.data,
                author.id
                )
            db.session.add(blog)
            db.session.flush()
        else:
            db.session.rollback()
            error = 'Error creating user'
        if author.id and blog.id:
            db.session.commit()
            flash('Blog created')
            return redirect(url_for('admin'))
        else:
            db.session.rollback()
            error = 'Error creating blog'
            
    
    return render_template('blog/setup.html', form=form, error=error)
예제 #2
0
def setup():
    form = SetupForm()
    error = ""
    if form.validate_on_submit():
        salt = bcrypt.gensalt()
        hashed_password = bcrypt.hashpw(form.password.data, salt)
        author = Author(form.fullname.data, form.email.data,
                        form.username.data, hashed_password, True)
        db.session.add(author)
        db.session.flush()
        if author.id:
            blog = Blog(form.name.data, author.id)
            db.session.add(blog)
            db.session.flush()
        else:
            db.session.rollback()
            error = "Error creating user"
        if author.id and blog.id:
            db.session.commit()
            flash("Blog created")
            return redirect(url_for('index'))
        else:
            db.session.rollback()
            error = "Error creating blog"
    return render_template('blog/setup.html', form=form, error=error)
예제 #3
0
def setup():
    blogs = Blog.query.count()
    if blogs:
        return redirect(url_for('admin'))
    form = SetupForm()
    if form.validate_on_submit():
        salt = bcrypt.gensalt()
        hashed_password = bcrypt.hashpw(form.password.data, salt)
        user = User(form.fullname.data, form.email.data, form.username.data,
                    hashed_password, True)
        db.session.add(user)
        db.session.flush()
        if user.id:
            blog = Blog(form.name.data, user.id)
            db.session.add(blog)
            db.session.flush()
        else:
            db.session.rollback()
            error = "Error creating user"
        if user.id and blog.id:
            db.session.commit()
        else:
            db.session.rollback()
            error = "Error creating blog"
        flash('Blog created')
        return redirect('/admin')
    return render_template('blog/setup.html', form=form)
예제 #4
0
def setup():
    blogs = Blog.query.count()
    if blogs:
        return redirect(url_for('admin'))
    form = SetupForm()
    if form.validate_on_submit():
        salt = bcrypt.gensalt()
        hashed_password = bcrypt.hashpw(form.password.data, salt)
        user = User(
            form.fullname.data,
            form.email.data,
            form.username.data,
            hashed_password,
            True
        )
        db.session.add(user)
        db.session.flush()
        if user.id:
            blog = Blog(form.name.data, user.id)
            db.session.add(blog)
            db.session.flush()
        else:
            db.session.rollback()
            error = "Error creating user"
        if user.id and blog.id:
            db.session.commit()
        else:
            db.session.rollback()
            error = "Error creating blog"
        flash('Blog created')
        return redirect('/admin')
    return render_template('blog/setup.html', form=form)
예제 #5
0
def setup():
    error = ""
    form = SetupForm()
    if form.validate_on_submit():
        salt = bcrypt.gensalt()
        hashed_password = bcrypt.hashpw(form.password.data, salt)
        author=Author(
            form.fullname.data,
            form.email.data,
            form.username.data,
            hashed_password,
            True
            )
        db.session.add(author)
        db.session.flush()
        if author.id:
            blog = Blog(
                form.name.data,
                author.id
                )
            db.session.add(blog)
            db.session.flush()
        else:
            db.session.rollback()
            error = "Error creating user"
        if author.id and blog.id:
            db.session.commit()
            flash("Blog created")
            return redirect(url_for('index'))
        else:
            db.session.rollback()
            error = "Error creating blog"
        
    return render_template('blog/setup.html', form=form, error=error)
예제 #6
0
def setup():
    form = SetupForm()
    error = None
    if form.validate_on_submit():

        # Encrypt password data
        salt = bcrypt.gensalt()
        hashed_password = bcrypt.hashpw(form.password.data, salt)

        # Create Author entitiy
        author = Author(form.fullname.data, form.email.data,
                        form.username.data, hashed_password, True)
        db.session.add(author)
        db.session.flush()

        # Check author entity for errors, create Blog entity if none
        if author.id:
            blog = Blog(form.name.data, author.id)
            db.session.add(blog)
            db.session.flush()
        else:
            db.session.rollback()
            error = "Error creating user"

        # Check Blog entity for errors
        if author.id and blog.id:
            db.session.commit()
            flash("Blog Created")
            return redirect(url_for('admin'))
        else:
            db.session.rollback()
            error = "Error creating blog"

    return render_template('blog/setup.html', form=form)
예제 #7
0
def blog_setup():
    blogs = Blog.query.count()
    #if blogs:
        #return redirect(url_for('blog_admin'))
    form = SetupForm()
    #SetupForm().validate_username(form.username.data)
        
    if form.validate_on_submit():
        salt = bcrypt.gensalt()
        hashed_password = bcrypt.hashpw(form.password.data, salt)
        author = Author(
            form.fullname.data,
            form.email.data,
            form.username.data,
            hashed_password,
            True
        )
        
        db.session.add(author)
        db.session.flush()
        
        if author.id:
            blog = Blog(form.name.data, author.id)
            db.session.add(blog)
            db.session.flush()
        else:
            db.session.rollback()
            error = "Error creating user"
        if author.id and blog.id:
            db.session.commit()
            
            subject = "Confirm your email"
            token = security.ts.dumps("*****@*****.**", salt='email-confirm-key')
    
            confirm_url = url_for(
                'confirm_email',
                token=token,
                _external=True)
        
            html = render_template(
                'email/activate.html',
                confirm_url=confirm_url)
    
            send = send_mail.Message("TELEPORT", form.email.data, "subject", html)
            send.send_message()
            
        else:
            db.session.rollback()
            error = "Error creating blog"
        flash('Blog created')
        
    return render_template('blog/blog_setup.html', form=form)
예제 #8
0
def blog_setup():
    blogs = Blog.query.count()
    #if blogs:
    #return redirect(url_for('blog_admin'))
    form = SetupForm()
    #SetupForm().validate_username(form.username.data)

    if form.validate_on_submit():
        salt = bcrypt.gensalt()
        hashed_password = bcrypt.hashpw(form.password.data, salt)
        author = Author(form.fullname.data, form.email.data,
                        form.username.data, hashed_password, True)

        db.session.add(author)
        db.session.flush()

        if author.id:
            blog = Blog(form.name.data, author.id)
            db.session.add(blog)
            db.session.flush()
        else:
            db.session.rollback()
            error = "Error creating user"
        if author.id and blog.id:
            db.session.commit()

            subject = "Confirm your email"
            token = security.ts.dumps("*****@*****.**",
                                      salt='email-confirm-key')

            confirm_url = url_for('confirm_email', token=token, _external=True)

            html = render_template('email/activate.html',
                                   confirm_url=confirm_url)

            send = send_mail.Message("TELEPORT", form.email.data, "subject",
                                     html)
            send.send_message()

        else:
            db.session.rollback()
            error = "Error creating blog"
        flash('Blog created')

    return render_template('blog/blog_setup.html', form=form)
예제 #9
0
def setup():
    form = SetupForm()
    error = ""
    if form.validate_on_submit():
        salt = bcrypt.gensalt()
        hashed_password = bcrypt.hashpw(form.password.data, salt)
        if request.method == "POST":
            print("method is post")
        else:
            print("method is not post")
        #image = request.files.get('image')
        image = request.files.get('image')
        print("Image: ", image)
        filename = None
        try:
            filename = uploaded_images.save(image)
            print("Try filename")
            print("File Name: ", filename)
        except:
            flash("The image was not uploaded.")
            print("The image was not uploaded.")
        author = Author(form.fullname.data, form.email.data,
                        form.username.data, hashed_password, True,
                        form.bio.data, filename)
        db.session.add(author)
        db.session.flush()
        if author.id:
            blog = Blog(form.name.data, author.id)
            db.session.add(blog)
            db.session.flush()
        else:
            db.session.rollback()
            error = "Error creating user"
        if author.id and blog.id:
            db.session.commit()
            flash("Blog created")
            return redirect(url_for('index'))
        else:
            db.session.rollback()
            error = "Error creating blog"
    return render_template('blog/setup.html',
                           form=form,
                           error=error,
                           action="new")
예제 #10
0
def setup():

    form = SetupForm()
    error = None
    error2 = None  #, blogs_from_author
    if form.validate_on_submit():
        #More secure passwords
        salt = bcrypt.gensalt()
        hashed_password = bcrypt.hashpw(form.password.data, salt)

        author = Author(form.fullname.data, form.email.data,
                        form.username.data, hashed_password, True)
        error = flush_obj(author)
        if error:
            db.session.rollback()
            db.session.close()
            flash("Error registering admin user")
            return render_template('blog/setup.html', form=form, error=error)
        else:
            blog = Blog(form.name.data, author.id)
            #TODO: Create view with author's blog, verify if name matches one of them using
            #blogs_from_author = Blog.query.filter_by().join etc
            #Throw error if author have blog with same name, else continue
            # blogs_from_author = db.query.filter_by(admin=author.id)
            # for blg in blogs_from_author:
            #     if blg.name==blog.name:
            #         error2="Blog and user already exists"
            #         db.sesssion.rollback()
            #         db.session.close()
            #         flash(error2)
            #         return render_template('blog/setup.html', form=form, error=error2)
            error2 = flush_commit(blog)
            if error2:
                flash("Unexpected Database Error registering Blog")
                return render_template('blog/setup.html',
                                       form=form,
                                       error=error2)
            else:
                session['username'] = form.username.data
                flash("Blog created")
                return redirect(url_for('admin'))
    return render_template('blog/setup.html', form=form, error=error)
예제 #11
0
def setup():
    form = SetupForm()
    data = request.form
    error = ""

    blog_name = data.get('name')
    username = data.get('username')
    password = data.get('password')
    fullname = data.get('fullname')
    email = data.get('email')

    print("form.validate_on_submit(): {}".format(form.validate_on_submit()))
    print("data: {}".format(data))

    if form.validate_on_submit():
        author = Author(password=password,
                        email=email,
                        fullname=fullname,
                        username=username,
                        is_author=True)

        db.session.add(author)
        db.session.flush()

        if author.id:
            blog = Blog(admin=author.id, name=blog_name)
            db.session.add(blog)
            db.session.flush()
        else:
            db.session.rollback()
            error = "Error creating Author"

        if author.id and blog.id:
            db.session.commit()
            flash("Blog created")
            return redirect(url_for("admin"))
        else:
            db.session.rollback()
            error = "Error creating Blog"

    return render_template('blog/setup.html', form=form, error=error)
예제 #12
0
파일: views.py 프로젝트: skols/flask_blog
def setup():
    form = SetupForm()
    error = ""
    if form.validate_on_submit():
        # For secure passwords
        salt = bcrypt.gensalt()
        hashed_password = bcrypt.hashpw(form.password.data, salt)
        
        author = Author(
            form.fullname.data,
            form.email.data,
            form.username.data,
            hashed_password, # in place of form.password.data
            True
        )
        db.session.add(author)
        # flush will try to simulate that the record is written to the db to give us the id
        db.session.flush()
        
        if author.id:
            blog = Blog(
                form.name.data,
                author.id
            )
            db.session.add(blog)
            db.session.flush()
        else:
            db.session.rollback()
            error = "Error creating user"
        
        if author.id and blog.id:
            db.session.commit()
            flash("Blog created")
            return redirect(url_for('index'))
        else:
            db.session.rollback()
            error = "Error creating blog"
            

    return render_template('blog/setup.html', form=form)
예제 #13
0
def setup():
    form = SetupForm()
    error = ""
    
    if form.validate_on_submit():
        salt = bcrypt.gensalt()
        hashed_password = bcrypt.hashpw(form.password.data, salt)
        author = Author(
            form.fullname.data,
            form.email.data,
            form.username.data,
            hashed_password,
            True
            )
        db.session.add(author)
        # this will flush data, but not commit, and this gives us an author.id
        db.session.flush()
        if author.id:
            # have author id, so record does not already exit in author table
            # now build blog record
            blog = Blog(
                form.name.data,
                author.id
                )
            db.session.add(blog)
            db.session.flush()
        else:
            db.session.rollback()
            error = "Error creating user"
        
        if author.id and blog.id:
            db.session.commit()
            flash("Blog created")
            return redirect(url_for('index'))
        else:
            db.session.rollback()
            error = "Error creating blog"
            
    return render_template('blog/setup.html', form=form, error=error)
예제 #14
0
def setup():
    form = SetupForm()
    error = ''
    if form.validate_on_submit():
        salt = bcrypt.gensalt()
        hash_pass = bcrypt.hashpw(form.password.data.encode('utf-8'), salt)

        author = Author(
            form.fullname.data,
            form.email.data,
            form.username.data,
            hash_pass,
            True
        )
        db.session.add(author)
        db.session.flush()
        if author.id:
            blog = Blog(
                form.name.data,
                author.id
            )
            db.session.add(blog)
            db.session.flush()
        else:
            db.session.rollback()
            error = 'ERROR creating user'

        if author.id and blog.id:
            db.session.commit()
            flash('blog created')
            return redirect(url_for('admin'))
        else:
            db.session.rollback()
            error = 'error making blog'

        # flash()

    return render_template('blog/setup.html', form=form, error=error)
예제 #15
0
def setup_page():
    blog = Blog.query.count()

    if blog:
        return redirect(url_for("admin_page"))

    form = SetupForm()

    error = ""

    if form.validate_on_submit():
        salt = bcrypt.gensalt()
        hashed_password = bcrypt.hashpw(form.password.data, salt)
        user = User(form.fullname.data, form.email.data, form.username.data,
                    hashed_password, True)
        try:
            db.session.add(user)
            db.session.flush()
        except Exception as e:
            print(e)
            error = "Choose a unique username and email"
            return render_template("blog/setup.html", form=form, error=error)

        if user.user_id:
            blog = Blog(form.name.data, user.user_id)
            db.session.add(blog)
            db.session.flush()
        else:
            db.session.rollback()

        if user.user_id and blog.blog_id:
            db.session.commit()
            flash("Blog created")
            return redirect(url_for("admin_page"))
        else:
            db.session.rollback()
            error = "Error creating blog"
    return render_template("blog/setup.html", form=form, error=error)
예제 #16
0
def setup_page():
    blog = Blog.query.count()

    if blog:
        return redirect(url_for("admin_page"))

    form = SetupForm()

    error = ""

    if form.validate_on_submit():
        salt = bcrypt.gensalt()
        hashed_password = bcrypt.hashpw(form.password.data, salt)
        user = User(form.fullname.data, form.email.data, form.username.data, hashed_password, True)
        try:
            db.session.add(user)
            db.session.flush()
        except Exception as e:
            print(e)
            error = "Choose a unique username and email"
            return render_template("blog/setup.html", form=form, error=error)

        if user.user_id:
            blog = Blog(form.name.data, user.user_id)
            db.session.add(blog)
            db.session.flush()
        else:
            db.session.rollback()

        if user.user_id and blog.blog_id:
            db.session.commit()
            flash("Blog created")
            return redirect(url_for("admin_page"))
        else:
            db.session.rollback()
            error = "Error creating blog"
    return render_template("blog/setup.html", form=form, error=error)
예제 #17
0
def setup():
    form = SetupForm()
    error = ""
    if form.validate_on_submit():
        # securing passwords using bcrypt
        # generate a salt
        salt = bcrypt.gensalt()
        hashed_password = bcrypt.hashpw(form.password.data, salt)
        # above storing the hashed pass using the bcrypt function hashpw encoded with the salt. always generates a 60char string
        author = Author(
            form.fullname.data, # calling data from the forms
            form.email.data,
            form.username.data,
            hashed_password, # store hashed password
            True # is_author
            )
        db.session.add(author)
        db.session.flush() 
        if author.id:
            blog = Blog(
                form.name.data,
                author.id
                )
            db.session.add(blog)
            db.session.flush()
        else:
            db.session.rollback() # undo operations and go back if error
            error = "Error creating user"
        if author.id and blog.id:
            db.session.commit()
            flash("Blog Created")
            return redirect(url_for('index'))
        else:
            db.session.rollback()
            error = "Error creating blog"
    return render_template('blog/setup.html', form=form, error=error) # form as context, sending error back
예제 #18
0
def setup():
    error = None

    form = SetupForm()
    blog = Blog.query.first()

    if request.method == 'GET':

        if blog:
            return redirect(url_for('index'))
        else:

            return render_template("/blog/setup.html", form=form)

    if not blog and request.method == 'POST':
        if form.validate_on_submit():
            salt = bcrypt.gensalt()
            hashed_password = bcrypt.hashpw(form.password.data.encode('utf8'), salt)
            author = Author(
                form.fullname.data,
                form.email.data,
                form.username.data,
                hashed_password,
                True,
                True,
                True
                )
            db.session.add(author)
            db.session.flush()


            if author.id:
                blog = Blog(
                form.title.data,
                author.id
                )
                db.session.add(blog)
                db.session.flush()
            else:
                db.session.rollback()
                error = "Error creating new Entry"

            if blog.id:
                maincategories = [MainCategory(form.category1.data),
                                    MainCategory(form.category2.data),
                                    MainCategory(form.category3.data)]

                db.session.add_all(maincategories)
                db.session.flush()

            if author.id and blog.id:
                db.session.commit()
                session['username'] = form.username.data
                session['is_author'] = author.is_author
                session['is_admin'] = author.is_admin
                session['is_active'] = author.is_active
                flash("Blog created successfully")
                return redirect(url_for('admin_app.admin'))
            else:
                db.session.rollback()
                error = "Error creating new Blog"

    return render_template('/blog/setup.html', form = form, error=error, blog=blog)