def test_follow_posts(self):
     # make four users
     u1 = User(nickname='john', email='*****@*****.**')
     u2 = User(nickname='susan', email='*****@*****.**')
     u3 = User(nickname='mary', email='*****@*****.**')
     u4 = User(nickname='david', email='*****@*****.**')
     db.session.add(u1)
     db.session.add(u2)
     db.session.add(u3)
     db.session.add(u4)
     # make four posts
     utcnow = datetime.utcnow()
     p1 = Post(body="post from john",
               author=u1,
               timestamp=utcnow + timedelta(seconds=1))
     p2 = Post(body="post from susan",
               author=u2,
               timestamp=utcnow + timedelta(seconds=2))
     p3 = Post(body="post from mary",
               author=u3,
               timestamp=utcnow + timedelta(seconds=3))
     p4 = Post(body="post from david",
               author=u4,
               timestamp=utcnow + timedelta(seconds=4))
     db.session.add(p1)
     db.session.add(p2)
     db.session.add(p3)
     db.session.add(p4)
     db.session.commit()
     # setup the followers
     u1.follow(u1)  # john follows himself
     u1.follow(u2)  # john follows susan
     u1.follow(u4)  # john follows david
     u2.follow(u2)  # susan follows herself
     u2.follow(u3)  # susan follows mary
     u3.follow(u3)  # mary follows herself
     u3.follow(u4)  # mary follows david
     u4.follow(u4)  # david follows himself
     db.session.add(u1)
     db.session.add(u2)
     db.session.add(u3)
     db.session.add(u4)
     db.session.commit()
     # check the followed posts of each user
     f1 = u1.followed_posts().all()
     f2 = u2.followed_posts().all()
     f3 = u3.followed_posts().all()
     f4 = u4.followed_posts().all()
     assert len(f1) == 3
     assert len(f2) == 2
     assert len(f3) == 2
     assert len(f4) == 1
     assert f1 == [p4, p2, p1]
     assert f2 == [p3, p2]
     assert f3 == [p4, p3]
     assert f4 == [p4]
Exemple #2
0
    def post(self):
        schema = PostSchema()

        args = post_parser.parse_args()

        text = args.get('text')
        if text is None:
            text = ''

        image = request.files['image']
        if not image:
            flash('必须添加图片!')
            return redirect(url_for('home.index'))  #422

        if not allowed_file(image.filename):
            flash('仅支持 jpeg、jpg、png 图片!')
            return redirect(url_for('home.index'))  #422

        file_name = str(int(datetime.now().timestamp() *
                            1000)) + '-' + secure_filename(image.filename)

        image.save(str(Path(current_app.config['UPLOAD_FOLDER']) / file_name))
        post = Post(user_id=current_user.id,
                    user_name=current_user.name,
                    text=text,
                    image=file_name)

        db.session.add(post)
        db.session.commit()

        flash('发布成功!')
        return redirect(url_for('home.index'))  #201
    def post(self):
        schema = PostSchema()

        args = post_parser.parse_args()

        text = args.get('text')
        if text is None:
            text = ''

        image = args.get('image')
        if image is None:
            return {'msg': 'you must post file.'}, 422

        file_name = str(int(datetime.now().timestamp() *
                            1000)) + '-' + secure_filename(image.filename)

        image.save(str(Path(current_app.config['UPLOAD_FOLDER']) / file_name))
        post = Post(user_id=current_user.id,
                    user_name=current_user.name,
                    text=text,
                    image=file_name)

        db.session.add(post)
        db.session.commit()

        return {"msg": "post created", "post": schema.dump(post)}, 201
    def form_valid(self, form):
        title = form.cleaned_data.get('title')
        slug = form.cleaned_data.get('slug')
        comment = form.cleaned_data.get('comments')

        post = Post(title=title)
        post.slug = slug
        # post.comments = comment
        post.save()
        return HttpResponseRedirect(reverse_lazy('user:user_index'))
Exemple #5
0
def create_post():
    if request.method == 'POST':
        title = request.form['title']
        content = request.form['content']
        is_form_valid = True

        if title.strip() == '':
            is_form_valid = False
            flash('username should be filled!', 'error')
        if content.strip() == '':
            is_form_valid = False
            flash('content should be filled!', 'error')

        # check if the post request has the file part
        if 'image_file' not in request.files:
            flash('No file part', 'error')
            return render_template('create_post.html')

        filename = None
        file = request.files['image_file']

        # if user does not select file, browser also
        # submit an empty part without filename
        # if file.filename == '':
        #     flash('No selected file')
        #     return redirect(request.url)

        if file and not allowed_file(file.filename):
            is_form_valid = False
            flash('file not allowed!', 'error')
        elif file:
            filename = secure_filename(file.filename)
            now = datetime.datetime.now()
            filename = now.strftime("%d%m%Y%H%M%S") + filename
            file.save(os.path.join(app.app.config['UPLOAD_FOLDER'], filename))

        if not is_form_valid:
            return render_template('create_post.html')

        username = session.get('username')
        user = User.query.filter_by(username=username).first()
        if user is None:
            flash('user not found!', 'error')
            return render_template('create_post.html')

        post = Post()
        post.user = user
        post.title = title
        post.content = content
        post.image_path = filename
        db.session.add(post)
        db.session.commit()
        return redirect(url_for('[post].list_post'))
    else:
        return render_template('create_post.html')
Exemple #6
0
def new():
    method = 'POST'
    action = '.'
    form = PostForm(request.form)
    if form.validate_on_submit():
        post = Post()
        post = set_post(post,form)
        print(post)
        post.put()
        flash('Post created!')
        return redirect(url_for('.index'))
    return render_template("posts/new.html", form=form, method=method, action=action)
Exemple #7
0
def new():
    if not current_user.is_authenticated and not current_user.is_confirmed:
        return redirect(url_for('main.home'))
    form = PostForm()
    if form.validate_on_submit():
        p = Post(images=form.images.data,
                 caption=form.caption.data,
                 user=current_user)
        db.session.add(p)
        db.session.commit()
        return redirect(url_for('main.home'))
    return render_template('new.html', form=form)
Exemple #8
0
def post_test():
    tags = [tag_pastor(), tag_computer()]
    post = Post(title='My Test Post',
                description='My Test Description',
                content='My test content',
                post_type='post',
                post_status='published',
                author=test_user(),
                category=category_sermon(),
                tags=tags)
    post.url = post.slugify(post.title)
    post.put()
    return post
Exemple #9
0
def page_test():
    tags = [tag_event]
    page = Post(title='My Test Page',
                description='My Test Page Description',
                content='My test page content',
                post_type='page',
                post_status='publisehd',
                author=test_user,
                category=category_worship,
                tags=tags)
    page.url = page.slugify(page.title)
    page.put()
    return page
Exemple #10
0
def create():
    title = request.form.get('title')
    content = request.form.get('content')
    poster_id = request.form.get('poster_id')
    pst = Post(title, content, poster_id)
    pst.createPost()
    message = {
        "data": {
            "status": "success",
            "action": "create",
            "scope": "post" + str(title)
        }
    }
    return jsonify(message), 201
Exemple #11
0
def new():
    if not (current_user.is_authenticated and current_user.is_confirmed):
        flash('Go and confirm your email, Baka', 'link')
        return redirect(url_for('main.home'))
    form = PostForm()
    if form.validate_on_submit():
        p = Post(images=form.images.data,
                 caption=form.caption.data,
                 user=current_user)
        db.session.add(p)
        db.session.commit()
        flash('New post added', 'success')
        return redirect(url_for('main.home'))
    return render_template('new.html', form=form)
Exemple #12
0
def create_post():
    form = CreatePost()
    #set dynamic for post type choices
    form.post_type.choices = [(a.id, a.post_type)
                              for a in Post_type.query.all()]
    #Handle Post method
    if form.validate_on_submit():
        post = Post(form.title.data, form.content.data, form.post_type.data,
                    current_user.id)
        db.session.add(post)
        db.session.commit()
        flash("Postingan anda telah tersimpan")
        return redirect(url_for('.create_post'))
    return render_template('create_post.html', form=form)
Exemple #13
0
def insert_post():
    content = request.get_json()
    try:
        post = Post(title=content['title'],
                    body=content['body'],
                    sub_body='test',
                    category_id=content['category_id'])
        data, errors = PostSchema().load(post.__dict__)
        if errors:
            return jsonify({'status': 'ko', 'code': 422, 'msg': errors}), 422
    except:
        return jsonify({'status': 'ko', 'code': 415, 'msg': 'Fail'}), 415
    try:
        db.session.add(post)
        db.session.commit()
        return jsonify({
            'status': 'ok',
            'code': 201,
            'msg': 'Create a new post successfully!'
        }), 201
    except:
        return jsonify({'status': 'ko', 'code': 403, 'msg': 'Fail'})