Esempio n. 1
0
    def post(self, category_id=None):
        if category_id:
            abort(400)
        else:
            args = category_post_parser.parse_args(strict=True)

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

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

            if args['items']:
                for item in args['items']:
                    tag = Item.query.filter_by(title=item).first()

                    # Add the tag if it exists. If not, make a new tag
                    if tag:
                        new_post.tags.append(tag)
                    else:
                        new_tag = Item(item)
                        new_post.tags.append(new_tag)

            db.session.add(new_post)
            db.session.commit()
            return new_post.id, 201
Esempio n. 2
0
def new_post():
    form = CategoryForm()

    if form.validate_on_submit():
        new_post_model = Category(form.title.data)
        new_post_model.text = form.text.data
        new_post_model.publish_date = datetime.datetime.now()
        new_post_model.user = User.query.filter_by(
            username=current_user.username).one()

        db.session.add(new_post_model)
        db.session.commit()

    return render_template('new.html', form=form)