示例#1
0
def new_post():

    for key, value in request.form.items():
        print("key: {0}, value: {1}".format(key, value))

    form = PostForm()
    if form.validate_on_submit():

        print(form.data)

        post = Post(title=form.title.data,
                    content=form.content.data,
                    user=current_user,
                    category=form.category.data)
        db.session.add(post)
        db.session.commit()

        for tag in form.tags:
            print(tag)
            tag_entry = Tag(tag=tag.data.strip(), post=post, user=current_user)
            db.session.add(tag_entry)
        db.session.commit()

        flash('Your post has been created!', 'success')
        return redirect(url_for('main.home'))
    return render_template('create_post.html',
                           title='New Post',
                           form=form,
                           legend='New Post')
示例#2
0
def update_post(post_id):
    post = Post.query.get_or_404(post_id)

    if post.author != current_user:
        abort(403)

    form = PostForm()

    if form.validate_on_submit():
        post.title   = form.title.data
        post.content = form.content.data

        db.session.commit()

        flash('Your post has been updated!', 
              'success')

        return redirect(url_for('posts.post', post_id = post.id))

    elif request.method == 'GET':
        form.title.data   = post.title
        form.content.data = post.content

    return render_template('new_post.html', 
                           title  = 'Update Post',
                           legend = 'Update Post',
                           form   = form )
示例#3
0
def new_post():
    form = PostForm()
    if form.validate_on_submit():
        flash('Your post has been created!', 'success')
        hashtag = ''
        hashtags = form.content.data
        print(hashtags)
        newstr = hashtags.split()
        for char in newstr:
            if char.startswith("#"):
                hashtag = char.strip("#")
                print(hashtag)
                # if form.picture.data:
                #     picture_file = save_picture(form.picture.data)
                picture_file = save_picture(form.picture.data)
                # video_file = save_video(form.video.data )
                image = url_for('static', filename='images/' + picture_file)

                # video = url_for('static',filename='videos/'+video_file)

                post = Pitch(title=form.title.data,
                             content=form.content.data,
                             author=current_user,
                             hashtags=hashtag,
                             image=image)
                db.session.add(post)
                db.session.commit()
                return redirect(url_for('main.home'))

    return render_template('create_post.html',
                           title='New Post',
                           form=form,
                           legend='New Post')
示例#4
0
def update_post(post_id):
    post = Post.query.get_or_404(post_id)
    if post.author != current_user:
        abort(403)
    form = PostForm()
    tagForm = TagForm()
    tags = Tag.query.all()
    form.tags.choices = [(tag.id, tag.value) for tag in tags]
    if form.validate_on_submit():
        post.title = form.title.data
        post.content = repr(form.content.data)
        post.tags = Tag.query.filter(Tag.id.in_(form.tags.data)).all()
        db.session.commit()
        flash(f'Your Post has been updated!!', category='success')
        return redirect(url_for('posts.post', post_id=post.id))
    elif request.method == 'GET':
        form.title.data = post.title
        form.content.data = post.content
        selectedTags = post.tags
        return render_template('create_post.html',
                               title='Update Post',
                               legend='Update Post',
                               form=form,
                               tags=tags,
                               tagForm=tagForm,
                               selectedTags=selectedTags)
    return render_template('create_post.html',
                           title='Update Post',
                           legend='Update Post',
                           form=form,
                           tags=tags,
                           tagForm=tagForm)
示例#5
0
def update_post(post_id):
    post = Post.query.get_or_404(post_id)
    form = PostForm()
    if form.validate_on_submit():
        if form.picture.data:
            picture_file = save_picture(form.picture.data)
            post.image_file = picture_file
        post.city = form.city.data
        post.category = form.category.data
        post.title = form.title.data
        post.story = form.story.data
        post.summary = form.summary.data
        db.session.commit()
        flash(' post has been updated!', 'success')
        return redirect(url_for('posts.postn', post_id=post.id))
    elif request.method == 'GET':
        form.city.data = post.city
        form.category.data = post.category
        form.title.data = post.title
        form.story.data = post.story
        form.summary.data = post.summary
    return render_template('posts/create_post.html',
                           title='Update Post',
                           form=form,
                           legend='Update Post')
示例#6
0
def new_post():
    form = PostForm()

    if form.validate_on_submit():
        pic = None
        if form.image.data:
            picture_file = save_picture(form.image.data)
            final_pic = picture_file
            pic = final_pic
        post = Post(title=form.title.data,
                    content=form.content.data,
                    author=current_user,
                    category=form.category.data,
                    image=pic)
        db.session.add(post)
        db.session.commit()
        flash('Your post has been published!', 'success')
        return redirect(url_for('main.home'))

    myposts = Post.query.order_by(Post.posted_date.desc())
    return render_template('new-post.html',
                           title='New Post',
                           form=form,
                           legend='New Post',
                           myposts=myposts,
                           quotes=quotes)
示例#7
0
def update_post(post_id):
    post = Post.query.get_or_404(post_id)
    final_pic = " "
    if post.author != current_user:
        abort(403)
    form = PostForm()
    if form.validate_on_submit():
        if form.image.data:
            picture_file = save_picture(form.image.data)
            final_pic = picture_file

        post.title = form.title.data
        post.content = form.content.data
        post.category = form.category.data
        post.image = final_pic
        db.session.commit()
        flash('Your post has been updated!', 'success')
        return redirect(url_for('posts.post', post_id=post.id))
    elif request.method == 'GET':
        form.title.data = post.title
        form.content.data = post.content
        form.category.data = post.category
    myposts = Post.query.order_by(Post.posted_date.desc())
    return render_template('new-post.html',
                           title='Update Post',
                           form=form,
                           legend='Update Post',
                           quotes=quotes,
                           myposts=myposts)
示例#8
0
def post():
    form_post = PostForm()
    if form_post.validate_on_submit():
        if form_post.picture_for_posts.data:
            file = request.files['picture_for_posts']
            pic = saving_pictures_post(file)
            form_post.picture_for_posts = pic
        post = Post(title=form_post.title.data,
                    content=form_post.content.data,
                    image=form_post.picture_for_posts,
                    location=form_post.location.data,
                    space_size=form_post.space_size.data,
                    author=current_user)
        db.session.add(post)
        db.session.commit()
        flash('you have successfully posted your space!!', 'success')
        return redirect(url_for('main.home_page'))
    image = url_for('static',
                    filename='post_pictures/' +
                    str(form_post.picture_for_posts))
    return render_template('post.html',
                           title='Post',
                           content='content',
                           image=image,
                           form=form_post)
示例#9
0
    def dispatch_request(self):
        form = PostForm()
        username = session['active_user']['username']
        author = db_user(username=username)
        posts = db_posts(author=author)

        if request.method == 'POST':
            if form.validate_on_submit():
                title = form.title.data
                content = form.content.data
                tag = form.tag.data

                try:
                    new_post = Post(author=author,
                                    title=title,
                                    content=content,
                                    tag=tag)
                    db.session.add(new_post)
                    db.session.commit()
                    flash('Your post is successfully submitted', 'success')
                    return redirect(url_for(request.endpoint))
                except Exception as e:
                    flash('Invalid inputs.', 'error')
                    return redirect(url_for(request.endpoint))

        return render_template('admin.html',
                               username=username,
                               form=form,
                               posts=posts)
示例#10
0
def update_post(post_id):
    post = Post.query.get_or_404(post_id)
    if post.author != current_user and not current_user.is_admin:
        logger.debug(
            "User %r tried unsuccessfully to edit post %d",
            current_user.username,
            post.id,
        )
        abort(403)

    form = PostForm()
    if form.validate_on_submit():
        post.title = form.title.data
        post.content = form.content.data
        db.session.commit()

        logger.debug("User %r updated post %d", current_user.username, post.id)
        flash("Your post has been updated!", "success")
        return redirect(url_for("posts.get_post", post_id=post.id))

    if request.method == "GET":
        form.title.data = post.title
        form.content.data = post.content

    return render_template(
        "create_post.html", title="Update Post", form=form, legend="Update Post"
    )
示例#11
0
def managepost():
    category_list = [(g.id, g.title) for g in Category.query.all()]
    form = PostForm()
    form.category.choices = category_list
    if form.validate_on_submit():
        language = guess_language(form.post.data)
        if language == 'UNKNOWN' or len(language) > 5:
            language = ''
        post = Post(title=form.title.data,
                    body_html=form.post.data,
                    author=current_user,
                    language=language,
                    category_id=form.category.data)
        db.session.add(post)
        db.session.commit()
        flash(_('Your post is now live!'))
        return redirect(url_for('posts.managepost'))
    page = request.args.get('page', 1, type=int)
    posts = current_user.posts.order_by(Post.timestamp.desc()).paginate(
        page, current_app.config['POSTS_PER_PAGE'], False)
    next_url = url_for('posts.managepost', page=posts.next_num) \
        if posts.has_next else None
    prev_url = url_for('posts.managepost', page=posts.prev_num) \
        if posts.has_prev else None
    return render_template('manage_post.html',
                           title=_('Posts'),
                           form=form,
                           posts=posts.items,
                           next_url=next_url,
                           prev_url=prev_url)
示例#12
0
    def dispatch_request(self, post_id: int):
        username = session['active_user']['username']
        post = db_posts(id=post_id)

        if username != post.author.username:
            abort(403)

        form = PostForm()
        if request.method == 'POST':
            if form.validate_on_submit():
                try:
                    post.title = form.title.data
                    post.content = form.content.data
                    post.tag = form.tag.data
                    db.session.commit()
                    flash('Your post is successfully updated.', 'success')
                    return redirect(url_for(request.endpoint, post_id=post_id))
                except Exception as e:
                    flash('Invalid update', 'error')
                    return redirect(url_for(request.endpoint, post_id=post_id))

        form.title.data = post.title
        form.content.data = post.content
        form.tag.data = post.tag
        return render_template('editpost.html', form=form)
示例#13
0
def update_post(post_id):
    post = Post.query.get_or_404(post_id)
    if post.author != current_user:
        abort(403)
    form = PostForm()
    if form.validate_on_submit():
        if form.picture.data:
            print('Hello World')
            picture_file = save_picture(form.picture.data)
            post.image_file = picture_file
        post.title = form.title.data
        post.content = form.content.data
        post.category = form.category.data
        post.summary = form.summary.data
        db.session.commit()
        flash('Your post has been updated!', 'success')
        return redirect(url_for('posts.post', post_id=post.id))
    elif request.method == 'GET':
        form.title.data = post.title
        form.content.data = post.content
        form.category.data = post.category
        form.summary.data = post.summary
    return render_template('create_post.html',
                           title='Update Post',
                           form=form,
                           legend='Update Post')
示例#14
0
def update_post(post_id):
    post = Post.query.get_or_404(post_id)
    if post.author != current_user:
        abort(403)
    form = PostForm()  # Create an instance of the PostForm()

    if form.validate_on_submit():  # If the input is valid

        title_check = predict([form.title.data])
        content_check = predict([form.content.data])

        if (title_check[0] == 1 or content_check[0]
                == 1) and (current_user.profanity == False):
            post.title = censor.censor(
                form.title.data)  # The form title overwrites the post title
            post.content = censor.censor(form.content.data)
            current_user.reputation -= 1  # reputation score decremented by 1
            current_user.profanity = True
            db.session.commit()
            flash(
                'Warning! Profanity detected! Since this is your first offense, Your reputation score has been reduced by 1. Any subsequent offense will reduce your reputation score by 5!',
                'warning')
            return redirect(url_for('posts.post', post_id=post.id))

        if (title_check[0] == 1
                or content_check[0] == 1) and (current_user.profanity == True):
            post.title = censor.censor(
                form.title.data)  # The form title overwrites the post title
            post.content = censor.censor(
                form.content.data
            )  # The form content overwrites the post content
            current_user.reputation -= 5  # reputation score decremented by 5
            db.session.commit()
            flash(
                'Warning! Profanity detected! Your reputation score has been reduced by 5. You were warned!',
                'danger')
            return redirect(url_for('posts.post', post_id=post.id))

        # When there is no profanity:
        post.title = form.title.data  # The form title overwrites the post title
        post.content = form.content.data  # The form content overwrites the post content
        db.session.commit(
        )  # Commit changes to db!                                                                             # We do not need to add anything into the db, these objects are already in the db and being overwritten!
        flash(
            'Your post has been successfully updated!', 'success'
        )  # Display success message. For anyone wondering 'success' is a bootstrap class it gives a green-ish hue.
        return redirect(url_for(
            'posts.post', post_id=post.id))  # Redirect to the post/id page

    if request.method == 'GET':
        form.title.data = post.title  # This ensures that the fields are populated with the previous text! But only if it's a 'GET' request.
        form.content.data = post.content  # This ensures that the fields are populated with the previous text! But only if it's a 'GET' request.

    return render_template(
        'create_post.html',
        title='Update Post',
        form=form,
        legend='Update Post'
    )  # Redirect to Create_Post.html, but with some pre-filled text, and a newer legend!
示例#15
0
def new_post():
    form = PostForm()  # Create instance of the  PostForm()

    if form.validate_on_submit(
    ):  # If user input is valid (i.e) no null characters, etc

        title_check = predict([form.title.data])
        content_check = predict([form.content.data])

        if (title_check[0] == 1 or content_check[0]
                == 1) and (current_user.profanity == False):
            title = censor.censor(
                form.title.data)  # Feature # 11: Profanity Filter ...
            content = censor.censor(form.content.data)
            post = Post(
                title=title, content=content, author=current_user
            )  # Create instance of Post() and pass in user input given to the form
            current_user.reputation -= 1
            current_user.profanity = True
            db.session.commit()
            flash(
                'Warning! Profanity detected! Since this is your first offense, Your reputation score has been reduced by 1. Any subsequent offense will reduce your reputation score by 5!',
                'warning')
            return redirect(url_for('main.home'))  # Redirect to Home page

        if (title_check[0] == 1
                or content_check[0] == 1) and (current_user.profanity == True):
            title = censor.censor(
                form.title.data)  # Feature # 11: Profanity Filter ...
            content = censor.censor(form.content.data)
            post = Post(
                title=title, content=content, author=current_user
            )  # Create instance of Post() and pass in user input given to the form
            current_user.reputation -= 5
            db.session.commit()
            flash(
                'Warning! Profanity detected! Your reputation score has been reduced by 5. You were warned!',
                'danger')
            return redirect(url_for('main.home'))  # Redirect to Home page

        title = censor.censor(
            form.title.data)  # Feature # 11: Profanity Filter ...
        content = censor.censor(form.content.data)
        post = Post(
            title=title, content=content, author=current_user
        )  # Create instance of Post() and pass in user input given to the form
        db.session.add(post)  # Mark this 'post' to be added to the database
        db.session.commit()  # Commit changes to db
        flash(
            'Your post has been created!', 'success'
        )  # Display success message! For anyone wondering 'success' is just a bootstrap class, it gives a green-ish hue.
        return redirect(url_for('main.home'))  # Redirect to Home page

    return render_template('create_post.html',
                           title='New Post',
                           form=form,
                           legend='New Post')
示例#16
0
def create_post():
    form = PostForm()
    if form.validate_on_submit() and request.method == 'POST':
        post = Post(title=form.title.data, body=form.body.data, author=current_user)
        db.session.add(post)
        db.session.commit()
        post.set_tags(tags=form.tags.data)
        return redirect(url_for('posts.posts'))
    return render_template('posts/create_post.html', form=form)
示例#17
0
def new_post():
    form = PostForm()
    if form.validate_on_submit():
        post = Post(title=form.title.data, content=form.content.data, author=current_user)
        db.session.add(post)
        db.session.commit()
        flash(f"New post is live!", category="success")
        return redirect(url_for("main.home"))
    return render_template("create_post.html", title="New Post", form=form, legend="New Post")
示例#18
0
def new_post():
	form = PostForm()
	if form.validate_on_submit():
		post = Post(title = form.title.data, content = form.content.data, user = current_user)
		db.session.add(post)
		db.session.commit()
		flash('Your story has been posted, thank you', 'success')
		return redirect(url_for('main.home'))
	return render_template('create_post.html', title = 'New Post', form = form, legend = 'New Story')
示例#19
0
def new_post():
    form = PostForm()
    if form.validate_on_submit():
        post = Post(title=form.title.data, content=form.content.data, author=current_user)
        db.session.add(post)
        db.session.commit()
        flash('post has been created')
        return redirect(url_for('users.user', username=current_user.username))
    return render_template('create_post.html', form=form, title='New Post')
示例#20
0
def new_post():
    form = PostForm()
    if form.validate_on_submit():
        post = Post(title=form.title.data, content=form.content.data, author=current_user)
        db.session.add(post)
        db.session.commit()
        flash('Post created successfully!', 'success')
        return redirect(url_for('main.home'))
    return render_template('create_post.html', title='New Post', form=form, legend='New Post')
示例#21
0
def new_post(user):
    form = PostForm()
    if form.validate_on_submit():
        post = Post(title=form.title.data,
                    content=form.content.data, author=current_user)
        db.session.add(post)
        db.session.commit()
        return redirect(url_for("main.logged_in", user=current_user.username))
    return render_template('create_post.html', user=current_user.username, form=form)
示例#22
0
def create_post():
    form = PostForm()
    if form.validate_on_submit():
        post = Post(title=form.title.data,
                    pre_body=form.pre_body.data,
                    body=form.body.data)
        post.author = current_user
        db.session.add(post)
        db.session.commit()
        return redirect(url_for('posts.news'))
    return render_template('posts/create_post.html', create_post_form=form)
def new_post():
    form = PostForm()
    if form.validate_on_submit():
        post = Post(title=form.title.data,
                    content=form.content.data,
                    author=current_user)
        db.session.add(post)
        db.session.commit()
        flash('Your post has been created!', 'success')
        return redirect(url_for('main.home'))
    return render_template("post.html", title="New post", form=form)
示例#24
0
def new_post():
    form = PostForm()
    if form.validate_on_submit():
        post = Post(title=form.title.data,
                    body=form.post.data,
                    author=current_user)
        db.session.add(post)
        db.session.commit()
        flash('Your post is now live!')
        return redirect(url_for('main.index'))
    return render_template("new_post.html", title='Home Page', form=form)
示例#25
0
def index():
    form = PostForm()
    if form.validate_on_submit():
        post = Post(author=current_user, body=form.body.data)
        post.commit()
        # Reload the page to avoid resubmit.
        return redirect(url_for('users.index'))
    posts = Post.query\
        .filter_by(author=current_user, active=True)\
        .order_by(Post.created.desc())\
        .all()
    return render_template('users/index.jinja', form=form, posts=posts)
示例#26
0
文件: routes.py 项目: visieur/Backup
def new_blog():
    if current_user.is_authenticated:
        return render_template('blog.html',title='New Blog')
        form = PostForm()
        if form.validate_on_submit():
           post = Post(title=form.title.data, content=form.content.data, author=current_user, post=post)
           db.session.add(post)
           db.session.commit()
           flash('Your post has been created!', 'success')
           return redirect(url_for('main.home'))
    return render_template('layout.html', title='New Post',
                           form=form,legend='New Post')
示例#27
0
def create_post():  # todo: check if only available to admin
    form = PostForm()

    if form.validate_on_submit():

        post = Post(title=form.title.data,
                    text=form.text.data,
                    user_id=current_user.id)
        db.session.add(post)
        db.session.commit()
        return redirect(url_for('settings_blueprint.administration'))

    return render_template('create_post.html', form=form)
示例#28
0
def book(book_num):
    if db.session.execute("SELECT * FROM books WHERE book_num = :book_num", {
            "book_num": book_num
    }).rowcount == 0:
        return render_template("errors/404.html"), 404
    else:
        book = db.session.execute(
            "SELECT * FROM books WHERE book_num = :book_num", {
                "book_num": book_num
            }).fetchone()

    posts = Post.query.filter_by(book_num=book_num).all()

    # Get APIs from Goodreads.
    res = requests.get("https://www.goodreads.com/book/review_counts.json",
                       params={
                           "key": "OZ4Itd0OJDETG4j2dgo68g",
                           "isbns": book_num
                       })
    if res.status_code != 200:
        raise Exception("ERROR: API request unsuccessful for.")
    data = res.json()
    num_ratings = data["books"][0]["work_ratings_count"]
    avg_rating = data["books"][0]["average_rating"]
    our_avg_rate = db.session.execute(
        "SELECT AVG(Cast(rating as Float)) FROM post WHERE book_num = :book_num",
        {
            "book_num": book_num
        }).fetchone()[0]
    # Post a User's Review.
    form = PostForm()
    if form.validate_on_submit():
        rating = int(request.form.get("rating"))
        post1 = Post(title=form.title.data,
                     content=form.content.data,
                     author=current_user,
                     book_num=book_num,
                     rating=rating)
        db.session.add(post1)
        db.session.commit()
        flash('Your post has been created!', 'success')
        return redirect(url_for('main.home'))
    return render_template("book.html",
                           title=book.name,
                           book=book,
                           posts=posts,
                           form=form,
                           legend="Write a Review",
                           num_ratings=num_ratings,
                           avg_rating=avg_rating,
                           our_avg_rate=our_avg_rate)
示例#29
0
def new_post():
    form = PostForm()
    if form.validate_on_submit():
        post = Post(title=form.title.data,
                    description=form.description.data,
                    content=form.content.data,
                    labels=form.labels.data,
                    author=current_user,
                    blog_id=current_user.blog[0].id)
        db.session.add(post)
        db.session.commit()
        flash('Post created.', 'success')
        return redirect(url_for('users.myblog'))
    return render_template('create_post.html', form=form, legend='New Post')
示例#30
0
def new_post():
    form = PostForm()
    if form.validate_on_submit():
        post = Post(category=form.category.data,
                    content=form.content.data,
                    author=current_user)
        db.session.add(post)
        db.session.commit()
        flash('Your pitch has been created!', 'success')
        return redirect(url_for('main.home'))
    return render_template('new_post.html',
                           title='New Pitch',
                           form=form,
                           legend='New Pitch')
示例#31
0
def edit_post(id):
    post = Post.query.get(id)
    form = PostForm(obj=post)
    if form.validate_on_submit():
        tags_names = form.tags.data
        tags = []
        if "," in tags_names:
            tags_names = tags_names.split(",")
        elif tags_names == "":
            tags_names = []
        else:
            tags_names = [tags_names]
        # if len(tags_names) == 1:
        #     if Tag.query.filter_by(name=tags_names[0]).first():
        #         tags.append(Tag.query.filter_by(name=tags_names[0]).first())
        #     else:
        #         new_tag = Tag(tags_names[0])
        #         tags.append(new_tag)
        #         db.session.add(new_tag)
        #         db.session.commit()
        # else:
        for tag_name in tags_names:
            if Tag.query.filter_by(name=tag_name).first():
                tags.append(Tag.query.filter_by(name=tag_name).first())
            else:
                new_tag = Tag(tag_name)
                tags.append(new_tag)
                db.session.add(new_tag)
                db.session.commit()
        post.title = form.title.data
        post.body = form.body.data
        post.tags = tags
        db.session.add(post)
        db.session.commit()
        flash("Post updated successfully")
        return redirect(url_for("posts.list_posts"))
    else:
        return render_template(
            "posts/form.html", form=form, url=url_for("posts.edit_post", id=post.id), post_id=post.id
        )
示例#32
0
def new_post():
    form = PostForm()
    if form.validate_on_submit():
        tags_names = form.tags.data
        tags = []
        if "," in tags_names:
            tags_names = tags_names.split(",")
        elif tags_names == "":
            tags_names = []
        else:
            tags_names = [tags_names]
        if len(tags_names) == 1:
            if Tag.query.filter_by(name=tags_names[0]).first():
                tags.append(Tag.query.filter_by(name=tags_names[0]).first())
            else:
                new_tag = Tag(tags_names[0])
                tags.append(new_tag)
                db.session.add(new_tag)
                db.session.commit()
        else:
            for tag_name in tags_names:
                if Tag.query.filter_by(name=tag_name).first():
                    tags.append(Tag.query.filter_by(name=tag_name).first())
                else:
                    new_tag = Tag(tag_name)
                    tags.append(new_tag)
                    db.session.add(new_tag)
                    db.session.commit()
        post = Post(title=form.title.data, body=form.body.data, tags=tags)
        db.session.add(post)
        db.session.commit()
        session["number_of_posts"] = len(Post.query.all())
        flash("Post created successfully")
        return redirect(url_for("posts.list_posts"))
    else:
        return render_template("posts/form.html", form=form, url=url_for("posts.new_post"))