Esempio n. 1
0
def new():
    """Create a new blog post.

    **Route:** ``/admin/posts/new``

    **Methods:** ``POST``
    """
    form = CreateBlogPostForm(request.form)
    form.author.choices = [
            (str(u.id), u.name + " (You)" if u == g.user else u.name)
            for u in User.objects()]
    form.author.data = str(g.user.id)
    upload_form = UploadImageForm()
    if form.validate_on_submit():
        author = User.objects().get(id=ObjectId(form.author.data))
        post = BlogPost(title=form.title.data,
                        slug=form.slug.data,
                        images=[Image.objects().get(filename=fn) for fn in form.images.data],
                        markdown_content=form.body.data,
                        author=author,
                        posted_by=g.user, tags=form.tags.data)
        post.save()

        if form.published.data:
            post.publish()
        else:
            post.unpublish()

        return redirect(url_for('.index'))
    images = Image.objects()
    return render_template('admin/posts/edit.html', user=g.user, form=form,
                           images=images, upload_form=upload_form)
Esempio n. 2
0
def new():
    """Create a new blog post.

    **Route:** ``/admin/posts/new``

    **Methods:** ``POST``
    """
    form = CreateBlogPostForm(request.form)
    form.author.choices = [
            (str(u.id), u.name + " (You)" if u == g.user else u.name)
            for u in User.objects()]
    form.author.data = str(g.user.id)
    upload_form = UploadImageForm()
    if form.validate_on_submit():
        author = User.objects().get(id=ObjectId(form.author.data))
        post = BlogPost(title=form.title.data,
                        slug=form.slug.data,
                        images=[Image.objects().get(filename=fn) for fn in form.images.data],
                        markdown_content=form.body.data,
                        author=author,
                        posted_by=g.user)
        post.save()

        if form.published.data:
            post.publish()
        else:
            post.unpublish()

        return redirect(url_for('.index'))
    images = Image.objects()
    return render_template('admin/posts/edit.html', user=g.user, form=form,
                           images=images, upload_form=upload_form)
Esempio n. 3
0
 def post(self):
     user = current_user
     args = parser.parse_args()
     title = args['title']
     content = args['content']
     if title:
         post = BlogPost(title=title, content=content)
         post.author_id = user.id
         post.save()
         response = post.to_json()
         return response, 201
Esempio n. 4
0
def backfill_from_jekyll(path_to_jekyll_posts):

    connect('eventum')
    author = User.objects().get(gplus_id='super')

    filenames = os.listdir(path_to_jekyll_posts)

    for filename in filenames:
        slug = filename.split('.')[0]
        title = None
        date_published = None
        published = True
        markdown_content = ''

        with open(os.path.join(path_to_jekyll_posts, filename)) as md_file:
            content = False
            for line in md_file:
                if not title and line.startswith('## '):
                    title = line.replace('## ', '').strip()
                elif not date_published and line.startswith('### '):
                    datestring = line.replace('### ', '').strip()
                    date_published = datetime.strptime(datestring, '%d %b %Y')
                    content = True
                elif content:
                    markdown_content += line

        if BlogPost.objects(slug=slug).count() > 0:
            bp = BlogPost.objects().get(slug=slug)
            bp.delete()

        blog_post = BlogPost(title=title,
                             date_published=date_published,
                             published=published,
                             markdown_content=markdown_content,
                             slug=slug,
                             author=author)

        blog_post.save()

    print "Backfilled %s blog posts." % len(filenames)