예제 #1
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)
예제 #2
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)
예제 #3
0
파일: views.py 프로젝트: SeavantUUz/Kutoto
def new_post(request,tid=0,pid=0,posted_by=1,template_name='post.html'):
    ''' This function would create a new topic or
        create a replied-post.
        It depends on whether a tid was passed '''
    args = {}
    args.update(csrf(request))
    # Using default Tag
    # If you want change the Tag 
    # you should rewrite you urls
    # or add a choices-box 
    tag = get_object_or_404(Tag,pk=1)
    user = get_object_or_404(User,pk=1)
    # reply
    if tid:
        topic = get_object_or_404(Topic,pk=tid)
    # new topic
    else:
        topic=None
    if request.method == "POST":
        f = NewPostForm(request.POST,topic=topic,tag=tag,user=user)
        if f.is_valid():
            post = f.save()
        return HttpResponseRedirect(reverse(index))
    else:
        f = NewPostForm(topic=topic)
    args['form'] = f
    return render_to_response('post.html',args)
예제 #4
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)
예제 #5
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'))
예제 #6
0
파일: app.py 프로젝트: lxiin00/Learn_Flask
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)
예제 #7
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)
예제 #8
0
파일: admin.py 프로젝트: hssgg/Niflheim
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)
예제 #9
0
파일: app.py 프로젝트: yeshan333/Web-Learn
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)
예제 #10
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)
예제 #11
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)
예제 #12
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)
예제 #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)
예제 #14
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)
예제 #15
0
파일: app.py 프로젝트: zx996/helloflask
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)
예제 #16
0
파일: views.py 프로젝트: pumzi/policyroot
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)
예제 #17
0
def new_post():
    """
    Creates a new post.
    """
    form = NewPostForm(request.form)
    if request.method == 'POST' and form.validate():
        post = Post(title=form.title.data, body=form.body.data)
        db.session.add(post)
        db.session.commit()
        return redirect(url_for('index'))
    return render_template("new.html", form=form)
예제 #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)
예제 #19
0
파일: views.py 프로젝트: Adrianacmy/blogz
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)
예제 #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')
예제 #21
0
파일: views.py 프로젝트: mpobrien/moblog
def newpost():
    if request.method == 'GET':
        return render_template("write.html", form=NewPostForm(), mode="new")
    else:
        form = NewPostForm(request.form)
        if form.validate():
            title = form.title.data
            body = form.content.data
            md = Markdown()
            html = md.convert(gfm(body))
            db.posts.insert({"title":title, "body":body, "html":html, "created_at":datetime.now()})
            return redirect(url_for("home"))
        else:
            return render_template("write.html", form=form, mode="new")
예제 #22
0
def update(id):
    """
    Updates the post and returns to the post details view.
    """
    post = Post.query.filter_by(id=id).one()
    form = NewPostForm(obj=post)

    if request.method == 'POST':
        form = NewPostForm(request.form)
        if form.validate():
            post.title = form.title.data
            post.body = form.body.data
            db.session.commit()
            return redirect(url_for('post', id=id))
    return render_template("edit.html", form=form)
예제 #23
0
파일: views.py 프로젝트: pumzi/policyroot
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)
예제 #24
0
파일: views.py 프로젝트: mpobrien/moblog
def edit(post_id):
    try:
        oid = ObjectId(post_id)
    except:
        return redirect(url_for("newpost"))
    post = db.posts.find_one({"_id": oid})
    if request.method == 'GET':
        form = EditPostForm()
        form.post_id.data = post_id
        form.title.data = post['title']
        form.content.data = post['body']
        return render_template("write.html", form=form, post_id=post_id)
    else:
        form = NewPostForm(request.form)
        if form.validate():
            title = form.title.data
            body = form.content.data
            md = Markdown()
            html = md.convert(gfm(body))
            db.posts.update(
                {"_id": oid},
                {"$set": {
                    "title": title,
                    "body": body,
                    "html": html
                }})
            return redirect(url_for("home"))
        else:
            return render_template("write.html", form=form, post_id=post_id)
예제 #25
0
def update_post(request, post_id):
    post = get_object_or_404(Post, pk=post_id)
    form = NewPostForm(instance=post)
    return render(request, 'django_blog/update_post.html',
                  {
                      'form': form,
                      'post_id': post_id
                  })
예제 #26
0
def thread(thread_id):
    """
    Detail page for threads, which returns a 404 if
    the thread with the given `thread_id` does not exist.

    """
    return render_template('thread.html',
                           thread=Thread.query.get_or_404(thread_id),
                           form=NewPostForm())
예제 #27
0
def newpost(request):
    ''' create new post'''
    if request.POST == {}:
        form = NewPostForm()
        return render(request,'newpost.html', {'form': form})
    print request
    form = NewPostForm(request.POST)
    if form.is_valid():
        title = form.cleaned_data['title']
        content = form.cleaned_data['text']
        author_id = 1
        slug = slugify(title)
        p = Post( title = title, text = content, author_id = author_id, slug = slug)
        p.save()
        message = 'sucess'
    else:
        message = 'You submitted an empty form.'
    return HttpResponse(message)
예제 #28
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)
예제 #29
0
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)
예제 #30
0
파일: views.py 프로젝트: mpobrien/moblog
def newpost():
    if request.method == 'GET':
        return render_template("write.html", form=NewPostForm(), mode="new")
    else:
        form = NewPostForm(request.form)
        if form.validate():
            title = form.title.data
            body = form.content.data
            md = Markdown()
            html = md.convert(gfm(body))
            db.posts.insert({
                "title": title,
                "body": body,
                "html": html,
                "created_at": datetime.now()
            })
            return redirect(url_for("home"))
        else:
            return render_template("write.html", form=form, mode="new")
예제 #31
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))
예제 #32
0
파일: views.py 프로젝트: GMI914/helpless
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)
예제 #33
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))
예제 #34
0
def account():
    form = UpdateAccountForm()
    form1 = NewPostForm()
    if form.validate_on_submit():
        current_user.username = form.username.data
        current_user.email = form.email.data
        db.session.commit()
        flash('Account updated successfully', 'success')
        return redirect(url_for('account'))
    elif request.method == 'GET':
        form.username.data = current_user.username
        form.email.data = current_user.email
    return render_template('account.html', form=form, form1=form1)
예제 #35
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)
예제 #36
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")
예제 #37
0
def update_post_save(request, post_id):
    if request.method == 'POST':
        post_for_update = get_object_or_404(Post, pk=post_id)
        form = NewPostForm(request.POST, instance=post_for_update)
        if form.is_valid():
            form.save()
            return render(request, 'django_blog/admin_post.html',
                          {
                              'form': form,
                              'success_msg': 'پست مورد نظر ویرایش شد'
                          })
        else:
            return render(request, 'django_blog/update_post.html',
                          {
                              'form': form,
                              'error_msg': 'لطفا فیلدهای الزامی را پر کنید'
                          })
    else:
        form = NewPostForm()
        return render(request, 'django_blog/update_post.html', {'form': form})
예제 #38
0
def new_post_save(request):
    if request.method == 'POST':
        create_new_post = Post(create_date=timezone.now(), author_id=request.user)
        form = NewPostForm(request.POST, instance=create_new_post)
        if form.is_valid():
            form.save()
            return render(request, 'django_blog/admin_post.html',
                          {
                              'form': form,
                              'success_msg': 'پست جدید در سیستم ثبت شد'
                          })
        else:
            return render(request, 'django_blog/new_post.html',
                          {
                              'form': form,
                              'error_msg': 'لطفا فیلدهای الزامی را پر کنید'
                          })
    else:
        form = NewPostForm()
        return render(request, 'django_blog/new_post.html', {'form': form})