Example #1
0
def profile(): 

	form = CreatePostForm()		
	# session gets the encrypted ID and hashes it to get the value i.e. the username
	user = User.query.filter_by(username = session['username']).first()

	if user is None:
		return redirect(url_for('signin'))
	else:
		if request.method == 'POST':						
			if form.validate() == False:				
				return render_template('profile.html', form=form,communityform=CreateCommunityForm())
			else:				
				newpost = Post(form.text.data, session['userID'], form.categoryID)				
				db.session.add(newpost)
				
				file = request.files[form.image.name]				
				if file: 					
					filename = secure_filename(file.filename)
					# flush to ge the postID to be used as filename
					db.session.flush()				
					filename = str(newpost.postID) + os.path.splitext(filename)[1]
					newpost.imageURI = filename
					file.save(os.path.join(APP_UPLOADS, filename))								
					flash(filename+" uploaded!")		
										
					
				db.session.commit()
				
				flash("posted!")			
				return redirect(url_for('profile'))
		elif request.method == 'GET':		
			# posts = Post.query.filter_by(username = session['username']).first()
			return render_template('profile.html', form=form, communityform=CreateCommunityForm())
Example #2
0
def create_post(id,dt):
    try:
        board = Forum.objects.filter(id = id)
    except Exception as e:
        print " exception value: ", e
        return redirect(url_for('.index'))
    threads = board[0].threads

    t_ret, sve_cnt = Helper().find_thread(threads,dt)
    form = CreatePostForm()
    if form.validate_on_submit():
        p=Helper().build_post(form,session['username'])
        pe = PostArrayEl()
        pe.key = str(len(t_ret.posts) + 1)
        pe.post = p
        t_ret.posts.append(pe)
        threads[sve_cnt - 1].thread=t_ret
        try:
            Forum.objects(slug=slug).update_one(set__threads=threads)
        except Exception as e:
            print " exception update value: ", e
            return redirect(url_for('.index'))


        return redirect(url_for('.board', slug=slug))



    return render_template('forum/create_post1.html', thread=t_ret,
                           form=form)
Example #3
0
def createpost():
	form = CreatePostForm(request.form)
	if form.validate_on_submit():
		newpost = Post(posttext = form.posttext.data, timestamp = datetime.datetime.now(), author = g.user)
		dbsess.add(newpost)
		dbsess.commit()
    	return redirect(url_for('index'))
	return render_template('index.html',form = form)
Example #4
0
def create():
    form = CreatePostForm()
    if request.method == 'POST':
        if form.validate_on_submit():
            slug = re.sub('[^\w]+', '-', str((form.title.data).lower()))
            filename = secure_filename(form.image.data.filename)
            form.image.data.save(UPLOADS_FOLDER + filename)
            post = Post(title=str(form.title.data), body=mistune.markdown(form.body.data), image=str(form.image.data.filename), slug=str(slug))
            db.session.add(post)
            db.session.commit()
            return redirect(url_for('post', slugs = post.slug))
    return render_template('create.html', form=form)
Example #5
0
def edit_post(post_id):
    post = BlogPost.query.get(post_id)
    edit_form = CreatePostForm(title=post.title,
                               subtitle=post.subtitle,
                               img_url=post.img_url,
                               author=post.author,
                               body=post.body)
    if edit_form.validate_on_submit():
        post.title = edit_form.title.data
        post.subtitle = edit_form.subtitle.data
        post.img_url = edit_form.img_url.data
        post.author = edit_form.author.data
        post.body = edit_form.body.data
        db.session.commit()
        return redirect(url_for("show_post", post_id=post.id))

    return render_template("make-post.html",
                           form=edit_form,
                           current_user=current_user)
Example #6
0
def add_new_post():
    form = CreatePostForm()
    if form.validate_on_submit():
        new_post = BlogPost(title=form.title.data,
                            subtitle=form.subtitle.data,
                            body=form.body.data,
                            img_url=form.img_url.data,
                            author=current_user,
                            date=date.today().strftime("%B %d, %Y"))
        try:
            db.session.add(new_post)
            db.session.commit()
            return redirect(url_for("get_all_posts"))
        except IntegrityError:
            flash(
                "The post title is duplicate with the existing post title. Please try again."
            )
            return redirect(url_for("add_new_post"))
    return render_template("make-post.html", form=form)
Example #7
0
def add_new_post():
    if not current_user.is_authenticated:
        flash("You need to login or register to publish your story.")
        return redirect(url_for("login"))
    else:
        form = CreatePostForm()
        if form.validate_on_submit():
            new_post = BlogPost(title=form.title.data,
                                subtitle=form.subtitle.data,
                                body=form.body.data,
                                img_url=form.img_url.data,
                                author=current_user,
                                date=date.today().strftime("%B %d, %Y"))
            db.session.add(new_post)
            db.session.commit()
            return redirect(url_for("get_all_posts"))

    return render_template("make-post.html",
                           form=form,
                           current_user=current_user)
Example #8
0
def new_post():
    form = CreatePostForm()
    if form.validate_on_submit():
        post_date = dt.today().strftime("%B %d, %Y")
        new_post = BlogPost(title=form.title.data,
                            subtitle=form.subtitle.data,
                            date=post_date,
                            author=current_user.name,
                            user_id=current_user.id,
                            img_url=form.img_url.data,
                            body=form.body.data)
        db.session.add(new_post)
        db.session.commit()
        return redirect(url_for('get_all_posts'))

    return render_template("make-post.html",
                           form=form,
                           title="New Post",
                           submit="Submit Post",
                           logged_in=current_user.is_authenticated)
Example #9
0
def add_new_post():
    form = CreatePostForm()
    if form.validate_on_submit():
        new_post = BlogPost(
            title=form.title.data,
            subtitle=form.subtitle.data,
            body=form.body.data,
            img_url=form.img_url.data,
            author=current_user,
            date=date.today().strftime("%B %d, %Y"),
        )
        db.session.add(new_post)
        db.session.commit()
        return redirect(url_for("get_all_posts"))
    return render_template(
        "make-post.html",
        form=form,
        logged_in=current_user.is_authenticated,
        current_user=current_user,
    )
Example #10
0
def edit_post(post_id):
    edit_post = BlogPost.query.get(post_id)
    #this populates the form with the post to be edited data
    form = CreatePostForm(title=edit_post.title,
                          subtitle=edit_post.subtitle,
                          img_url=edit_post.img_url,
                          body=edit_post.body)
    if form.validate_on_submit():
        edit_post.title = form.title.data
        edit_post.subtitle = form.subtitle.data
        edit_post.img_url = form.img_url.data
        edit_post.body = form.body.data
        db.session.commit()
        return redirect(url_for("show_post", index=edit_post.id))
    return render_template("make-post.html",
                           submit="Edit Post",
                           form=form,
                           title="Edit Post",
                           is_edit=True,
                           index=edit_post.id,
                           logged_in=current_user.is_authenticated)
Example #11
0
def add_new_post():
    form = CreatePostForm()
    author = User.query.get(current_user.id)
    if form.validate_on_submit():


        new_post = BlogPost(
            title=form.title.data,
            subtitle=form.subtitle.data,
            body=form.body.data,
            img_url=form.img_url.data,
            author=current_user,


            date=date.today().strftime("%B %d, %Y")
        )
        db.session.add(new_post)
        db.session.commit()
        print(author.name)
        return redirect(url_for("get_all_posts"))
    return render_template("make-post.html", form=form)
Example #12
0
def edit_post(post_id):
    post = BlogPost.query.get(post_id)
    edit_form = CreatePostForm(title=post.title,
                               subtitle=post.subtitle,
                               img_url=post.img_url,
                               body=post.body)
    if edit_form.validate_on_submit():
        post.title = edit_form.title.data
        post.subtitle = edit_form.subtitle.data
        post.img_url = edit_form.img_url.data
        post.body = edit_form.body.data
        db.session.commit()

        # POST FOR ABOUT
        if post.id == 1:
            return redirect(url_for("about"))
        return redirect(url_for("show_post", post_id=post.id))

    return render_template("make-post.html",
                           form=edit_form,
                           logged_in=current_user.is_authenticated)
Example #13
0
def edit_post(post_id):
    '''
    Edit a Post by Id.

    Update all Post data on submit.
    '''
    post = mongo.db.blog_posts.find_one({"_id": ObjectId(post_id)})

    edit_form = CreatePostForm(title=post["title"],
                               subtitle=post["subtitle"],
                               img_url=post["img_url"],
                               author=session["user"],
                               body=post["body"])
    if edit_form.validate_on_submit():
        post["title"] = edit_form.title.data
        post["subtitle"] = edit_form.subtitle.data
        post["img_url"] = edit_form.img_url.data
        post["body"] = edit_form.body.data
        mongo.db.blog_posts.update({"_id": ObjectId(post_id)}, post)
        return redirect(url_for("show_post", post_id=post_id))
    return render_template("create_post.html", form=edit_form, is_edit=True)
Example #14
0
def add_new_post():
    """displays the page and form to allow the admin to create new blog posts.
        this route is restricted to the admin

    GET: displays the form to allow the admin to create new blog posts
    POST: submits a request to add a new blog post to the database, redirects to the landing page if successful
    """

    form = CreatePostForm()
    if form.validate_on_submit():
        new_post = BlogPost(
            title=form.title.data,
            subtitle=form.subtitle.data,
            body=form.body.data,
            img_url=form.img_url.data,
            author_id=current_user.id, # author_id is now an integer that is a foreign key reference to users table
            date=date.today().strftime("%B %d, %Y")
        )
        db.session.add(new_post)
        db.session.commit()
        return redirect(url_for("get_all_posts"))
    return render_template("make-post.html", form=form)
Example #15
0
def edit_post(post_id):
    post = BlogPost.query.get(post_id)
    edit_form = CreatePostForm(
        title=post.title,
        subtitle=post.subtitle,
        img_url=post.img_url,
        author=post.author,
        body=post.body
        )
    if request.method == "POST":
        if edit_form.validate_on_submit():
            post.title = edit_form.title.data
            post.subtitle = edit_form.subtitle.data
            post.img_url = edit_form.img_url.data
            #post.author = edit_form.author.data
            post.body = edit_form.body.data
            db.session.commit()
            return redirect(url_for("show_post", post_id=post.id))
    else:
        return render_template("make-post.html",
                               form=edit_form,
                               logged_in=current_user.is_authenticated)
Example #16
0
def add_new_post():
    print("in add new post")
    form = CreatePostForm()
    if form.validate_on_submit():
        print("inside if")
        new_post = BlogPost(

            title=form.title.data,
            subtitle=form.subtitle.data,
            body=form.body.data,
            img_url=form.img_url.data,
            # author=current_user
            author_id=current_user.id,
            date=date.today().strftime("%B %d, %Y")
        )
        print("outside new_post = Blogpost")
        db.session.add(new_post)
        print("db.session.add(new_post")
        db.session.commit()
        print("db.session.commit()")

        return redirect(url_for("get_all_posts"))
    return render_template("make-post.html", form=form, current_user=current_user)
Example #17
0
def new_post():
    '''Render a page that allows a user to create a new post.'''
    form = CreatePostForm()

    if form.validate_on_submit():
        # grab the data from the post and add it into the database
        posted_date = datetime.datetime.now()
        created_post = BlogPost(
            title=request.form['title'],
            subtitle=request.form['subtitle'],
            author_id=current_user.id,
            date=posted_date.strftime("%B %d, %Y"),
            img_url=request.form['img_url'],
            body=request.form.get('body'),
        )

        # commit the created post into the database.
        db.session.add(created_post)
        db.session.commit()

        # redirect the user to come back to the home page.
        return redirect(url_for("get_all_posts"))
    return render_template("make-post.html", form=form, logged_in=current_user)
Example #18
0
def add_new_post():
    form = CreatePostForm()
    if form.validate_on_submit():
        if BlogPost.query.filter_by(title=form.title.data).first():
            form.title.errors.append("This title is already exist.")
        else:
            new_post = BlogPost(title=form.title.data,
                                subtitle=form.subtitle.data,
                                body=form.body.data,
                                img_url=form.img_url.data,
                                author=current_user,
                                date=get_jkt_timezone(
                                    datetime.now()).strftime("%B %d, %Y"),
                                views=0)
            db.session.add(new_post)
            db.session.commit()
            return redirect(url_for("get_all_posts"))
        return render_template("make-post.html",
                               form=form,
                               logged_in=current_user.is_authenticated)
    return render_template("make-post.html",
                           form=form,
                           logged_in=current_user.is_authenticated)
Example #19
0
def edit_post(post_id):
    requested_post = BlogPost.query.get(post_id)
    if requested_post:
        if current_user.is_authenticated:
            if requested_post.author.email == current_user.email:
                form = CreatePostForm(title=requested_post.title,
                                      subtitle=requested_post.subtitle,
                                      img_url=requested_post.img_url,
                                      body=requested_post.body)
                if form.validate_on_submit():
                    return post_edition(requested_post=requested_post,
                                        form=form,
                                        request='POST')
                else:
                    return post_edition(requested_post=requested_post,
                                        form=form,
                                        request='GET')
            else:
                return abort(403)
        else:
            return abort(401)
    else:
        return abort(404)
Example #20
0
def create_post():
    '''
    Create a new Post.

    Inject all form data to a new blog post document on submit.
    '''
    if "user" in session:
        # create a Form for data entry
        form = CreatePostForm()
        if form.validate_on_submit():
            new_post = {
                "title": form.title.data,
                "subtitle": form.subtitle.data,
                "body": form.body.data,
                "img_url": form.img_url.data,
                "author": session["user"],
                "date": date.today().strftime("%B %d, %Y")
            }
            mongo.db.blog_posts.insert_one(new_post)
            flash("Post Successfully Added")
            return redirect(url_for("get_all_posts"))
        return render_template("create_post.html", form=form)
    else:
        return redirect(url_for("login"))
Example #21
0
def add_new_post():
    form = CreatePostForm()
    if request.method == 'POST':
        new_post = BlogPost(title=form.title.data,
                            subtitle=form.subtitle.data,
                            body=form.body.data,
                            img_url=form.img_url.data,
                            author=current_user.name,
                            date=date.today().strftime("%B %d, %Y"),
                            the_user=current_user)
        db.session.add(new_post)
        db.session.commit()
        return redirect(url_for("get_all_posts"))
    else:
        return render_template("make-post.html", form=form)
Example #22
0
def add_post():
    form = CreatePostForm()
    if form.validate_on_submit():
        return post_addition(form)
    return render_template('make-post.html', form=form)