Beispiel #1
0
def two_submits():
    form = NewPostForm()
    print(form.validate_on_submit())
    if form.validate_on_submit():
        if form.save.data:
            flash('You click the Save button!')
            print('You click the Save button!')
        elif form.publish.data:
            flash('You click the Publish button!')
            print('You click the Publish button!')
        return redirect(url_for('index'))
    return render_template('2submit.html', form=form)
Beispiel #2
0
def new_post():
    # only signed user can write a new post
    if 'username' not in login_session:
        return redirect('/login')
    form = NewPostForm()
    if form.validate_on_submit():
        post = Post(subject=form.subject.data,
                    user_id=login_session['user_id'],
                    blog_id=login_session['blog_id'],
                    content=form.content.data,
                    created=time(),
                    publish=form.publish.data)
        post.last_modified = post.created
        post.get_short_content()
        if form.image.data and allowed_file(form.image.data.filename):
            file = form.image.data
            # save the file name with prefix(blog_id) and suffix(created)
            post.attached_img = upload(file, str(login_session['blog_id']),
                                       str(int(post.created)))

        session.add(post)
        session.commit()
        return redirect('/')
    print form.errors
    return render_template('post.html',
                           form=form,
                           action=url_for('new_post'),
                           user_id=login_session.get('user_id'),
                           username=login_session.get('username'))
Beispiel #3
0
def add_posts(user_id):
    pForm = NewPostForm()
    uFolder = app.config['UPLOAD_FOLDER']
    
    myid = int(user_id)
    
    if request.method == "POST" and pForm.validate_on_submit():
        caption = request.form['caption']
        pic = request.files['photo']
        
        filename = secure_filename(pic.filename)
        pic.save(os.path.join(uFolder, filename))
        
        now = datetime.datetime.now()
        created = "" + format_date_joined(now.year, now.month, now.day)
        
        myPost = Posts(myid, filename, caption, created)
        
        db.session.add(myPost)
        db.session.commit()
        
        info = {'message': 'Successfully created a new post'}
        
        return jsonify(info=info)
    else:
        errors = form_errors(pForm)
        
        return jsonify(errors=errors)
Beispiel #4
0
def new_post():
    form = NewPostForm()

    if form.validate_on_submit():

        p = models.Post(title=form.title.data, \
                        body=form.body.data, \
                        slug=slugify(form.title.data), \
                        description=form.description.data, \
                        timestamp=datetime.datetime.utcnow(), \
                        author=g.user)

        tagnames = form.tags.data.split(TAG_DELIM)
        p.set_tags_str(tagnames)

        filename = upload_file(request)
        if filename:
            p.thumbnail = "/uploads/thumbs/" + filename
        else:
            p.thumbnail = DEFAULT_THUMB

        db.session.add(p)
        db.session.commit()

        flash(u'New post created successfully.', 'alert-success')
        return redirect(url_for("index"))

    return render_template("new_post.html", form=form)
Beispiel #5
0
def channel(channel_name):
    new_channel_form = NewChannelForm()
    new_post_form = NewPostForm()
    if request.method == "POST":
        if new_channel_form.submit.data and new_channel_form.validate_on_submit(
        ):
            channel = Channel(name=new_channel_form.channel_name.data)
            db.session.add(channel)
            db.session.commit()
            new_channel_form.channel_name.data = ""
            return redirect(url_for("channel", channel_name=channel_name))
        elif new_post_form.submit.data and new_post_form.validate_on_submit():
            post = Post(body=new_post_form.post_body.data,
                        timestamp=datetime.utcnow(),
                        user_id=current_user.id,
                        channel_id=Channel.query.filter_by(
                            name=channel_name).first().id)
            db.session.add(post)
            db.session.commit()
            new_post_form.post_body.data = ""
            return redirect(url_for("channel", channel_name=channel_name))

    channels = Channel.query.all()
    if not any(channel_name == channel.name for channel in channels):
        return redirect(url_for("channels"))
    cur_channel = Channel.query.filter_by(name=channel_name).first()
    posts = cur_channel.posts
    return render_template("channel.html",
                           new_channel_form=new_channel_form,
                           new_post_form=new_post_form,
                           channels=channels,
                           posts=posts)
Beispiel #6
0
def two_submits_button():
    form = NewPostForm()
    if form.validate_on_submit():
        if form.save.data:
            flash('保存成功!')
        elif form.publish.data:
            flash('发布成功!')
        return redirect(url_for('index'))
    return render_template('two_submits.html', form=form)
Beispiel #7
0
def new_post():
    form = NewPostForm()
    if form.validate_on_submit():
        form.save()
        flash(u'成功添加文章')
        return go_delay_redirect(url_for("admin.index"))
    return render_template('admin/new_or_edit_post.html',
                           form=form,
                           new=True)
Beispiel #8
0
def twobutton():
    form = NewPostForm()
    if form.validate_on_submit():
        print(form.data)
        if form.save.data:
            flash('You clicked Save')
        elif form.publish.data:
            flash('You clicked Publish')
    return render_template('2button.html', form=form)
Beispiel #9
0
def two_submit():
    form = NewPostForm()
    if form.validate_on_submit():
        if form.save.data:
            flash("You click the save button.")
        elif form.publish.data:
            flash('You click the publis button')
        return redirect(url_for('index'))
    return render_template('2submit.html', form=form)
Beispiel #10
0
def two_submits():
    form = NewPostForm()
    if form.validate_on_submit():
        if form.save.data:
            flash('You click the "save" button.')
        elif form.publish.data:
            flash('You click the "publish" button')
        return redirect(url_for("index"))
    return render_template('2submit.html', form=form)
Beispiel #11
0
def post_page():
    form = NewPostForm()
    if form.validate_on_submit():
        post = Post()
        form.populate_obj(post)
        db.session.add(post)
        db.session.commit()
        return redirect("/post_page")
    return render_template("post_page.html", form=form)
Beispiel #12
0
def add_post():
	form = NewPostForm()
	if form.validate_on_submit():
		post = Post()
		form.populate_obj(post)
		db.session.add(post)
		db.session.commit()
		return redirect('/')
	return render_template('add_post.html', form = form)
Beispiel #13
0
def add_post():
    form = NewPostForm()
    if form.validate_on_submit():
        post = Post()
        form.populate_obj(post)
        post.author_id = current_user.id
        db.session.add(post)
        db.session.commit()
        return redirect('/')
    return render_template("add_post.html", form=form)
def add_post():
        form = NewPostForm()
        if form.validate_on_submit():
                post = Post()
                form.populate_obj(post)
                post.author_id = current_user.id
                db.session.add(post)
                db.session.commit()
                return redirect('/')
        return render_template("add_post.html", form = form)
Beispiel #15
0
def two_submits():
    form = NewPostForm()
    if form.validate_on_submit():
        if form.save.data:
            # save it...
            flash('You click the "Save" button.')
        elif form.publish.data:
            # publish it...
            flash('You click the "Publish" button.')
        return redirect(url_for('index'))
    return render_template('2submit.html', form=form)
Beispiel #16
0
def pathway():
    form = NewPostForm()
    if form.validate_on_submit():
        post = Post()
        form.populate_obj(post)
        db.session.add(post)
        db.session.commit()
        return redirect('/pathway')
    users = User.query.all
    posts = Post.query.filter_by(topic='pathway')
    return render_template('pathway.html', form=form, users=users, posts=posts)
Beispiel #17
0
def two_submits():
    form = NewPostForm()
    if form.validate_on_submit():
        if form.save.data:
            #save it...
            flash('You click the "Save" button')
        elif form.publish.data:
            #publish it...
            flash('You click the "Publish" button')
        return redirect(url_for('index'))
    return render_template('2submit.html', form=form)
Beispiel #18
0
def two_submits():
    form = NewPostForm()
    if form.validate_on_submit():
        if form.save.data:
            # title = form.title.data
            # body = form.body.data
            # save it
            flash('Your press the "Save" Button')
        elif form.publish.data:
            flash('Your press the "Publish" Button')
        return redirect(url_for('index'))
    return render_template('2submit.html', form=form)
Beispiel #19
0
def newpost():
    error = None
    form = NewPostForm()
    if form.validate_on_submit():
        newpost = Post(title=form.title.data,
                       body=form.body.data,
                       pub_date=form.pub_date.data,
                       author_id=form.author_id.data)
        db.session.add(newpost)
        db.session.commit()
        return redirect('/blog?id=' + str(newpost.id))
    else:
        return render_template("new_post.html", form=form, error=error)
Beispiel #20
0
def createPost():
    form = NewPostForm()
    if session.get('loggedIn') == True:
        if form.validate_on_submit():
            newPost = WallPost(body=form.body.data, user_id=session['user_id'])
            if request.method == 'POST':
                db.session.add(newPost)
                db.session.commit()
            return redirect('/profile')
        return render_template('newPost.html', form=form)
    else:
        flash("Please sign in first", 'danger')
        return redirect('/login')
Beispiel #21
0
def Undocuqueer():
    form = NewPostForm()  #calling on NewPostForm from forms.py
    if form.validate_on_submit(
    ):  #if statement: saying the if the submit button is pushed then,
        post = Post()  #it prepares a new row for the info to be passed in
        form.populate_obj(post)  #then populates new post info entered by user
        db.session.add(post)  #creates new post
        db.session.commit()  #after it is submitted, it redirects back to the,
        return redirect('/Undocuqueer')  #Undocuqueer page
    users = User.query.all  #This GETs users to show on the page
    posts = Post.query.filter_by(
        topic='Undocuqueer')  #with post filtered by the topic
    return render_template('Undocuqueer.html',
                           form=form,
                           users=users,
                           posts=posts)
Beispiel #22
0
def update_post(post_id):

    # Find the post to be edited using the post id
    post = db.posts.find_one({"_id": ObjectId(post_id)})

    print "post", post

    # If the post doesn't exist return 404
    if not post:
        return render_template('/error_pages/404.html',
                               reason="That post doesn't exist"), 404

    # If this user didn't make the post, abort the action
    if post['author'] != current_user.username:
        return render_template('/error_pages/403.html'), 403

    # Create form instance
    form = NewPostForm()

    # Ensure form entries are valid
    if form.validate_on_submit():

        # Update fields
        post['title'] = form.title.data
        post['content'] = form.content.data

        # Save changes
        db.posts.update({"_id": post['_id']}, post)

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

        return redirect(url_for('discover'))

    elif request.method == 'GET':

        # Set the fields to be the current post data
        form.title.data = post['title']
        form.content.data = post['content']

    return render_template('create_post.html',
                           form=form,
                           legend='Update Post',
                           action='/edit/post/' + post_id)
def create_new_post(community):
    postform = NewPostForm()
    c = Community.query.filter_by(name=community).first()
    if postform.validate_on_submit():
        user = g.user
        post = Posts(title=request.form['title'], body=request.form['body'], author=user, community=c)
        # users can only post if they a part of the community
        if c.is_joined(user):
            db.session.add(post)
            db.session.commit()
        else:
            flash('User must be a part of the community to create a post')
        return redirect(url_for('community', community=community))
    kwargs = {
        'postform': postform,
        'community': community,
        'c': c
    }
    return render_template('new_post.html', **kwargs)
Beispiel #24
0
def new_post(thread_id):
    """
    POST API endpoint for creating a new post for the thread
    with given `thread_id`. Also returns a 404 if it doesn't exist.

    """

    t = Thread.query.get_or_404(thread_id)
    form = NewPostForm()
    if form.validate_on_submit():

        data = form.data.copy()
        poster = Poster.from_details(data.pop('name'))
        poster.create_post(t, **data)
        return redirect(url_for('thread', thread_id=thread_id))

    else:
        flash_form_errors(form)

    return redirect(url_for('thread', thread_id=thread_id))
Beispiel #25
0
def new_post():
    form = NewPostForm()

    if form.validate_on_submit():
        post = Post(user_id=current_user.id,
                    title=form.title.data,
                    content=form.content.data)
        db.session.add(post)
        db.session.commit()
        if form.photo.data:
            cur_post = Post.query.filter_by(
                user_id=current_user.id,
                title=form.title.data).order_by('id')[-1]
            img_name = photos.save(form.photo.data,
                                   name='blog_' + str(cur_post.id) + '.jpeg')
            cur_post.photo = '/media/img/' + img_name
        db.session.commit()

        return redirect(url_for('my_posts'))

    return render_template('new_post.html', form=form)
Beispiel #26
0
def edit_post(post_id):
    post = session.query(Post).filter_by(id=post_id).one()
    creator = session.query(User).filter_by(id=post.user_id).one()

    # Populate the form from the data
    form = NewPostForm(obj=post)

    # only creator of the post can edit it
    if login_session['user_id'] != creator.id:
        login_session.pop('_flashes', None)
        flash('You do not have authorization to edit this post!')
        return redirect(url_for('view_post', post_id=post_id))

    if form.validate_on_submit():
        old_img = post.attached_img
        form.populate_obj(post)
        post.get_short_content()
        post.last_modified = time()
        if form.image.data and allowed_file(form.image.data.filename):
            # if there's previoused attached image
            if old_img:
                # first delete the previous file in the upload_folder
                deleting_file = os.path.join(app.config['UPLOAD_FOLDER'],
                                             old_img)
                os.remove(deleting_file)
            # then upload the new file
            file = form.image.data
            # save file with prefix(blog_id) and suffix(last_modified time)
            post.attached_img = upload(file, str(login_session['blog_id']),
                                       str(int(post.last_modified)))
        session.add(post)
        session.commit()
        return redirect(url_for('view_post', post_id=post_id))
    print form.errors
    return render_template('post.html',
                           form=form,
                           post=post,
                           user_id=login_session.get('user_id'),
                           username=login_session.get('username'),
                           action=url_for('edit_post', post_id=post.id))
Beispiel #27
0
def edit_post(slug):

    post = models.Post.query.filter_by(slug=slug).first()

    if g.user.id != post.user_id and g.user.role != 1:
        # post is editable, only if owner of the post or admin
        flash(u'You have no access to edit this post.', 'alert-warning')
        return redirect(url_for("posts", slug=slug))

    p = models.Post.query.filter_by(slug=slug).first()

    tags = TAG_DELIM.join(p.get_tags_str())

    form = NewPostForm(title=p.title, body=p.body, slug=p.slug, \
                       description=p.description, tags=tags)

    if form.validate_on_submit():
        p.title = form.title.data
        p.body = form.body.data
        p.slug = form.slug.data
        p.description = form.description.data
        p.edited = datetime.datetime.utcnow()

        tagnames = form.tags.data.split(TAG_DELIM)
        p.set_tags_str(tagnames)

        filename = upload_file(request)
        if filename:
            p.thumbnail = "/uploads/thumbs/" + filename

        db.session.commit()

        flash(u'Post edited succesfully', 'alert-success')

        return redirect(url_for("posts", slug=p.slug))

    return render_template("edit_post.html", form=form)
Beispiel #28
0
def new_post():

    # Create form instance
    form = NewPostForm()

    # Check that fields are valid
    if form.validate_on_submit():

        # Submit post to database
        newUser = db.posts.insert_one({
            "author": current_user.username,
            "content": form.content.data,
            "title": form.title.data,
            "createdAt": datetime.now()
        })

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

        return redirect(url_for('discover'))

    return render_template('create_post.html',
                           form=form,
                           legend="New Post",
                           action="/create/post")