Пример #1
0
    def put(self, post_id=None):

        if not post_id:
            abort(400)

        post = Post.query.filter_by(id=post_id).first()
        if not post:
            abort(404)

        args = parsers.post_put_parser.parse_args()
        user = User.verify_auth_token(args['token'])

        if not user:
            abort(401)
        if user != post.user:
            abort(403)

        if args['title']:
            post.title = args['title']
        if args['text']:
            post.text = args['text']
        if args['tags']:
            for item in args['tags']:
                tag = Tag.query.filter_by(name=item).first()
                if tag:
                    post.tags.append(tag)
                else:
                    new_tag = Tag()
                    new_tag.name = item
                    post.tags.append(new_tag)

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

        return (post.id, 201)
Пример #2
0
    def post(self, post_id=None):

        if not post_id:
            abort(400)
        else:
            args = parsers.post_post_parser.parse_args(strict=True)

            user = User.verify_auth_token(args['token'])
            if not user:
                abort(401)

            new_post = Post()
            new_post.title = args['title']
            new_post.date = datetime.datetime.now()
            new_post.text = args['text']
            new_post.user = user

            if args['tags']:
                for item in args['tags']:
                    tag = Tag.query.filter_by(name=item).first()

                    if tag:
                        new_post.tags.append(tag)

                    else:
                        new_tag = Tag()
                        new_tag.name = item
                        new_post.tags.append(new_tag)
        db.session.add(new_post)
        db.session.commit()
        return (new_post.id, 201)
Пример #3
0
def new_post():
    form = PostForm()
    if form.validate_on_submit():
        cleaned_content = bleach.clean(form.content.data,
                                       tags=bleach.sanitizer.ALLOWED_TAGS +
                                       ['p'])
        create_post = Post(title=form.title.data,
                           content=cleaned_content,
                           author=current_user._get_current_object())

        if form.tags.data[0]:
            for i in form.tags.data:
                tags = Tag(name=i)  # adds tag to TAG db Table
                create_post.tag.append(
                    tags)  # adds tag to post_tag table with post id.

        db.session.add(create_post)
        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')
Пример #4
0
def new_blog():
    form = BlogForm()
    if form.validate_on_submit():

        blogs = Blog.query.filter_by(user_id=current_user.id).all()

        blogged_today = []
        for blog in blogs:
            if datetime.utcnow().__str__()[:10] in blog.date_blogged.__str__():
                blogged_today.append(blog)

        if len(blogged_today) > 1:
            flash('You can only create two blogs per day!', 'danger')
        else:
            tags = [Tag(tag=tag) for tag in form.tags.data.split()]
            new_blog = Blog(subject=form.subject.data,
                            description=form.description.data,
                            bloggedBy=current_user,
                            tag=tags)
            db.session.add(new_blog)
            db.session.commit()
            flash('Your blog has been created!', 'success')

        return redirect(url_for('main.home'))
    return render_template('create_blog.html',
                           subject='New Blog',
                           form=form,
                           legend='New Blog')
Пример #5
0
 def init_db(self):
     post = Post(title='Test Post',
                 author='John Doe',
                 description='Test Subtitle',
                 content='## Title\nHello World.',
                 slug='test-post')
     post.tags.append(Tag(text='testing'))
     post.category = Category(text='testing')
     db.session.add(post)
     db.session.commit()
Пример #6
0
def fake_tags(count=50):
    for i in range(count):
        tag = Tag(
            name=fake.word() + str(i)
            # body=fake.text(2000),
            # category=Category.query.get(random.randint(1, Category.query.count())),
            # timestamp=fake.date_time_this_year()
        )

        db.session.add(tag)
    db.session.commit()
Пример #7
0
def create_tags(tags: list):
    result = []
    for tag_name in tags:
        existing_check = Tag.query.filter_by(name=tag_name).first()
        if not existing_check:
            create_tag = Tag(name=tag_name)
            db.session.add(create_tag)
            db.session.commit()
            select_tag = Tag.query.filter_by(name=tag_name).first()
            result.append(select_tag)
        else:
            result.append(existing_check)
    return result
Пример #8
0
def new_blog():
    form = BlogForm()
    if form.validate_on_submit():
        tags = [Tag(tag=tag) for tag in form.tags.data.split()]
        new_blog = Blog(subject=form.subject.data,
                        description=form.description.data,
                        bloggedBy=current_user,
                        tag=tags)
        db.session.add(new_blog)
        db.session.commit()
        flash('Your blog has been created!', 'success')
        return redirect(url_for('main.home'))
    return render_template('create_blog.html',
                           subject='New Blog',
                           form=form,
                           legend='New Blog')
Пример #9
0
    def get_context(self, slug=None):
        if slug:
            tag = Tag.objects.get_or_404(slug=slug)
            form_class = model_form(Tag)
            if request.method == 'POST':
                form = form_class(request.form, initial=tag._data)
            else:
                form = form_class(obj=tag)
        else:
            tag = Tag()
            form_class = model_form(Tag)
            form = form_class(request.form)

        context = {
            'tag': tag,
            'form': form,
            'create_tag': slug is None,
            'tags': Tag.objects.all()
        }
        return context
Пример #10
0
def update_blog(blog_id):
    blog = Blog.query.get_or_404(blog_id)
    if blog.bloggedBy != current_user:
        abort(403)
    form = BlogForm()
    if form.validate_on_submit():
        blog.subject = form.subject.data
        blog.description = form.description.data
        blog.tag = [Tag(tag=tag) for tag in form.tags.data.split()]
        db.session.commit()
        flash('Your blog has been updated!', 'success')
        return redirect(url_for('blogs.blog', blog_id=blog.blog_id))
    elif request.method == 'GET':
        form.subject.data = blog.subject
        tags = get_tags(blog.tag)
        form.tags.data = tags
        form.description.data = blog.description
    return render_template('create_blog.html',
                           title='Update Blog',
                           form=form,
                           legend='Update Blog')
Пример #11
0
def update_post(post_id):
    post_update = Post.query.get_or_404(post_id)

    if post_update.author != current_user and not current_user.can(
            Permission.ADMINISTRATOR):
        abort(
            403
        )  # abort function is for showing the passed error page (error 403 - forbidden page)
    form = PostForm()
    if form.validate_on_submit():
        post_update.title = form.title.data
        post_update.content = bleach.clean(form.content.data,
                                           tags=bleach.sanitizer.ALLOWED_TAGS +
                                           ['p'])
        for i in form.tags.data:
            exists = db.session.query(
                db.exists().where(Tag.name == i)).scalar()
            if not exists:
                tags = Tag(name=i)  # adds tag to TAG db Table
                post_update.tag.append(
                    tags)  # adds tag to post_tag table with post id.
        try:
            db.session.commit()
            flash('Your post has been updated!', 'success')
        except exc.IntegrityError:
            db.session.rollback()
            flash('Your post has been couldn\'t be updated!', 'warning')

        return redirect(url_for('posts.post', post_id=post_update.id))
    elif request.method == 'GET':
        form.title.data = post_update.title
        form.content.data = post_update.content
        tags = [x.name for i, x in enumerate(post_update.tag.all())
                ]  # loop through all the tags and display them
        form.tags.data = tags

    return render_template('create_post.html',
                           title='Update Post',
                           form=form,
                           legend='Update Post')
Пример #12
0
#!/usr/bin/env python
# coding=utf-8

import random
import datetime
from uuid import uuid4

from flaskblog.models import db, User, Tag, Post

user = User(id=str(uuid4()), username='******', password='******')
db.session.add(user)
db.session.commit()

user = db.session.query(User).first()
tag_one = Tag(id=str(uuid4()), name='Python')
tag_two = Tag(id=str(uuid4()), name='Flask')
tag_three = Tag(id=str(uuid4()), name='SQLALchemy')
tag_four = Tag(id=str(uuid4()), name='Vagrant')
tag_list = [tag_one, tag_two, tag_three, tag_four]

s = "EXAMPLE TEXT"

for i in xrange(100):
    new_post = Post(id=str(uuid4()), title="Post" + str(i))
    new_post.user = user
    new_post.publish_date = datetime.datetime.now()
    new_post.text = s
    new_post.tags = random.sample(tag_list, random.randint(1, 3))
    db.session.add(new_post)

db.session.commit()