Ejemplo n.º 1
0
def blog_archive(index):
    """View older blog posts.

    **Route:** ``/blog/<index>``

    **Methods:** ``GET``
    """
    if index <= 0:
        return redirect(url_for('.index'))

    blog_posts = BlogPost.objects(published=True).order_by('-date_published')

    # If the index is too high, check the previous page.
    if len(blog_posts) <= 10 * index:
        return redirect(url_for('blog.blog_archive', index=index - 1))

    # Hide the next button if there are no more posts after this page.
    if len(blog_posts) <= 10 * (index + 1):
        next_index = None
    else:
        next_index = index + 1
    previous_index = index - 1

    return render_template('blog/blog.html',
                           posts=list(blog_posts[10 * index:10 * (index + 1)]),
                           previous_index=previous_index,
                           next_index=next_index)
Ejemplo n.º 2
0
def index():
    """View the ADI homepage.

    **Route:** ``/``

    **Methods:** ``GET``
    """
    # cast date.today() to a datetime
    today = datetime.combine(date.today(), datetime.min.time())

    # Ending on a future date, or today at a future time. The events should be
    # published, and should be chronological.
    # We limit to four events, one large event and one set of three events.
    events = (Event.objects(Q(end_date__gte=today))
                   .filter(published=True)
                   .order_by('start_date', 'start_time')
                   .limit(ONE_LARGE_AND_TRIPLE))

    # sort published posts chronologically back in time
    all_blog_posts = (BlogPost.objects(published=True)
                              .order_by('-date_published'))
    latest_blog_post = all_blog_posts[0] if all_blog_posts else None

    return render_template('index.html',
                           events=events,
                           blog_post=latest_blog_post)
Ejemplo n.º 3
0
def delete(post_id):
    """Delete an existing blog post.

    **Route:** ``/admin/posts/delete/<post_id>``

    **Methods:** ``POST``

    :param str post_id: The ID of the post to edit.
    """
    object_id = ObjectId(post_id)
    if BlogPost.objects(id=object_id).count() == 1:
        post = BlogPost.objects().with_id(object_id)
        post.delete()
    else:
        flash('Invalid event id', ERROR_FLASH)
    return redirect(url_for('.index'))
Ejemplo n.º 4
0
def delete(post_id):
    """Delete an existing blog post.

    **Route:** ``/admin/posts/delete/<post_id>``

    **Methods:** ``POST``

    :param str post_id: The ID of the post to edit.
    """
    object_id = ObjectId(post_id)
    if BlogPost.objects(id=object_id).count() == 1:
        post = BlogPost.objects().with_id(object_id)
        post.delete()
    else:
        flash('Invalid event id', ERROR_FLASH)
    return redirect(url_for('.index'))
Ejemplo n.º 5
0
def index():
    """View the ADI homepage.

    **Route:** ``/``

    **Methods:** ``GET``
    """
    this_moment = datetime.now().time()
    # cast date.today() to a datetime
    today = datetime.combine(date.today(), datetime.min.time())

    # Ending on a future date, or today at a future time. The events should be
    # published, and should be chronological.
    # We limit to four events, one large event and one set of three events.
    events = (Event.objects(Q(end_date__gte=today))
                            # |
                            # Q(end_date=today, end_time__gt=this_moment)) #
                   .filter(published=True)
                   .order_by('start_date', 'start_time')
                   .limit(ONE_LARGE_AND_TRIPLE))

    # sort published posts chronologically back in time
    all_blog_posts = (BlogPost.objects(published=True)
                              .order_by('-date_published'))
    latest_blog_post = all_blog_posts[0] if all_blog_posts else None

    return render_template('index.html',
                           events=events,
                           blog_post=latest_blog_post)
Ejemplo n.º 6
0
def preview(slug):
    if BlogPost.objects(slug=slug).count() != 1:
        abort(404)
    post = BlogPost.objects().get(slug=slug)
    if not post.date_published:
        post.date_published = datetime.now()  # just used as placeholder to
        # display in preview. Does not get saved to db.

    recent_posts = BlogPost.objects(published=True,
                                    id__ne=post.id,
                                    featured_image__ne=None)\
        .order_by('-date_published')[:3]

    related_posts = post.get_related_posts()[:3]
    return render_template('blog/preview.html',
                           post=post,
                           recent_posts=recent_posts,
                           related_posts=related_posts)
Ejemplo n.º 7
0
def index():
    """View all of the blog posts.

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

    **Methods:** ``GET``
    """
    all_posts = BlogPost.objects().order_by('published', '-date_published')
    return render_template('eventum_posts/posts.html', posts=all_posts)
Ejemplo n.º 8
0
def index():
    """View all of the blog posts.

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

    **Methods:** ``GET``
    """
    all_posts = BlogPost.objects().order_by('published', '-date_published')
    return render_template('eventum_posts/posts.html', posts=all_posts)
Ejemplo n.º 9
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)
Ejemplo n.º 10
0
    def run(self):
        """Run the generation.  Uses the configurations passed to
        func:`__init__`.
        """

        # Setup: db connection, superuser, and printer.
        connect(config['MONGODB_SETTINGS']['DB'])
        try:
            superuser = User.objects().get(gplus_id='super')
        except DoesNotExist:
            print ('Failed to get superuser.  Try running:\n'
                   '\texport GOOGLE_AUTH_ENABLED=TRUE')
        printer = ProgressPrinter(self.quiet)

        # Images
        if self.should_gen_images:
            if self.wipe:
                self.warn('Image')
                print CLIColor.warning('Wiping Image database.')
                Image.drop_collection()
            create_images(12, superuser, printer)

        # Blog posts
        if self.should_gen_posts:
            if self.wipe:
                self.warn('BlogPost')
                print CLIColor.warning('Wiping BlogPost database.')
                BlogPost.drop_collection()
            create_posts(10, superuser, printer)

        # Events and event series
        if self.should_gen_events:
            if self.wipe:
                self.warn('Event and EventSeries')
                print CLIColor.warning('Wiping Event database.')
                Event.drop_collection()
                print CLIColor.warning('Wiping EventSeries database.')
                EventSeries.drop_collection()
            create_events(superuser, printer)
Ejemplo n.º 11
0
def post(slug):
    """View an individual blog post.

    **Route:** ``/blog/post/<slug>``

    **Methods:** ``GET``
    """
    if BlogPost.objects(slug=slug).count() != 1:
        abort(404)
    post = BlogPost.objects().get(slug=slug)

    recent_posts = BlogPost.objects(published=True,
                                    id__ne=post.id,
                                    featured_image__ne=None).order_by(
                                        '-date_published')[:3]

    related_posts = post.get_related_posts()[:3]
    if not post.published:
        abort(404)
    return render_template('blog/post.html',
                           post=post,
                           recent_posts=recent_posts,
                           related_posts=related_posts)
Ejemplo n.º 12
0
def same_tag(tag):
    """View blog posts with the same tag
    **Route:** ``/blog/tag/<tag>``

    **Methods:** ``GET``
    """

    tag = Tag.objects().get(tagname=tag)
    blog_posts = list(BlogPost.objects(post_tags=tag, published=True)
                      .order_by('-date_published')[:10])
    previous_index = None
    next_index = 1
    return render_template('blog/blog.html',
                           posts=blog_posts,
                           previous_index=previous_index,
                           next_index=next_index)
Ejemplo n.º 13
0
def index():
    """View recent blog posts.

    **Route:** ``/blog``

    **Methods:** ``GET``
    """
    blog_posts = BlogPost.objects(published=True).order_by('-date_published')

    # Hide the next button if there are <= 10 posts.
    next_index = 1 if len(blog_posts) > 10 else None
    previous_index = None

    return render_template('blog/blog.html',
                           posts=list(blog_posts[:10]),
                           previous_index=previous_index,
                           next_index=next_index)
Ejemplo n.º 14
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))
        images = [Image.objects().get(filename=fn) for fn in form.images.data]
        tags = Tag.get_or_create_tags(form.tags.data)
        post = BlogPost(title=form.title.data,
                        slug=form.slug.data,
                        images=images,
                        markdown_content=form.body.data,
                        author=author,
                        posted_by=g.user,
                        post_tags=tags)
        post.save()

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

        if form.preview.data is True:
            return redirect(url_for('blog.preview', slug=post.slug))

        return redirect(url_for('.index'))
    images = Image.objects()
    return render_template('eventum_posts/edit.html',
                           user=g.user,
                           form=form,
                           images=images,
                           upload_form=upload_form)
Ejemplo n.º 15
0
def index():
    """The homepage of Eventum. Shows the latest blog posts and events.

    **Route:** ``/admin/home``

    **Methods:** ``GET``
    """
    today = date.today()
    last_sunday = datetime.combine(
        today - timedelta(days=(today.isoweekday() % 7)),
        datetime.min.time())
    next_sunday = last_sunday + timedelta(days=7)

    this_week = (Event.objects(start_date__gt=last_sunday,
                               start_date__lt=next_sunday)
                 .order_by('start_date'))
    posts = BlogPost.objects().order_by('published', '-date_published')[:5]

    return render_template('eventum_home.html',
                           this_week=this_week,
                           recent_posts=posts)
Ejemplo n.º 16
0
def index():
    """The homepage of Eventum. Shows the latest blog posts and events.

    **Route:** ``/admin/home``

    **Methods:** ``GET``
    """
    today = date.today()
    last_sunday = datetime.combine(
        today - timedelta(days=(today.isoweekday() % 7)),
        datetime.min.time())
    next_sunday = last_sunday + timedelta(days=7)

    this_week = (Event.objects(start_date__gt=last_sunday,
                               start_date__lt=next_sunday)
                 .order_by('start_date'))
    posts = BlogPost.objects().order_by('published', '-date_published')[:5]

    return render_template('eventum_home.html',
                           this_week=this_week,
                           recent_posts=posts)
Ejemplo n.º 17
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))
        images = [Image.objects().get(filename=fn) for fn in form.images.data]
        tags = Tag.get_or_create_tags(form.tags.data)
        post = BlogPost(title=form.title.data,
                        slug=form.slug.data,
                        images=images,
                        markdown_content=form.body.data,
                        author=author,
                        posted_by=g.user, post_tags=tags)
        post.save()

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

        if form.preview.data is True:
            return redirect(url_for('blog.preview', slug=post.slug))

        return redirect(url_for('.index'))
    images = Image.objects()
    return render_template('eventum_posts/edit.html', user=g.user, form=form,
                           images=images, upload_form=upload_form)
Ejemplo n.º 18
0
def edit(post_id):
    """Edit an existing blog post.

    **Route:** ``/admin/posts/edit/<post_id>``

    **Methods:** ``GET, POST``

    :param str post_id: The ID of the post to edit.
    """
    try:
        object_id = ObjectId(post_id)
    except InvalidId:
        return abort(404)
    try:
        post = BlogPost.objects().with_id(object_id)
    except (DoesNotExist, ValidationError):
        flash('Cannot find blog post with id {}.'.format(post_id), ERROR_FLASH)
        return redirect(url_for('.index'))

    if request.method == '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.default = str(g.user.id)

        if form.validate_on_submit():
            post.title = form.title.data
            post.author = User.objects.get(id=ObjectId(form.author.data))
            post.slug = form.slug.data
            post.markdown_content = form.body.data
            post.images = [
                Image.objects().get(filename=fn) for fn in form.images.data
            ]

            post.post_tags = Tag.get_or_create_tags(form.tags.data)
            if form.featured_image.data:
                post.featured_image = Image.objects().get(
                    filename=form.featured_image.data)
            else:
                post.featured_image = None
            post.save()

            if post.published != form.published.data:
                if form.published.data:
                    post.publish()
                    flash('Blogpost published', MESSAGE_FLASH)
                else:
                    post.unpublish()
                    flash('Blogpost unpublished', MESSAGE_FLASH)

            if form.preview.data is True:
                return redirect(url_for('blog.preview', slug=post.slug))

    upload_form = UploadImageForm()
    feat_img = post.featured_image.filename if post.featured_image else None
    form = CreateBlogPostForm(request.form,
                              title=post.title,
                              slug=post.slug,
                              published=post.published,
                              body=post.markdown_content,
                              images=[image.filename for image in post.images],
                              author=str(post.author.id),
                              featured_image=feat_img, tags=post.post_tags)
    form.author.choices = [
        (str(u.id), u.name + " (You)" if u == g.user else u.name)
        for u in User.objects()
    ]
    form.author.default = str(g.user.id)
    images = [image for image in Image.objects() if image not in post.images]

    return render_template('eventum_posts/edit.html',
                           user=g.user,
                           form=form,
                           post=post,
                           images=images,
                           upload_form=upload_form)
Ejemplo n.º 19
0
def edit(post_id):
    """Edit an existing blog post.

    **Route:** ``/admin/posts/edit/<post_id>``

    **Methods:** ``GET, POST``

    :param str post_id: The ID of the post to edit.
    """
    try:
        object_id = ObjectId(post_id)
    except InvalidId:
        return abort(404)
    try:
        post = BlogPost.objects().with_id(object_id)
    except (DoesNotExist, ValidationError):
        flash('Cannot find blog post with id {}.'.format(post_id), ERROR_FLASH)
        return redirect(url_for('.index'))

    if request.method == '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.default = str(g.user.id)

        if form.validate_on_submit():
            post.title = form.title.data
            post.author = User.objects.get(id=ObjectId(form.author.data))
            post.slug = form.slug.data
            post.markdown_content = form.body.data
            post.images = [
                Image.objects().get(filename=fn) for fn in form.images.data
            ]

            post.post_tags = Tag.get_or_create_tags(form.tags.data)
            if form.featured_image.data:
                post.featured_image = Image.objects().get(
                    filename=form.featured_image.data)
            else:
                post.featured_image = None
            post.save()

            if post.published != form.published.data:
                if form.published.data:
                    post.publish()
                    flash('Blogpost published', MESSAGE_FLASH)
                else:
                    post.unpublish()
                    flash('Blogpost unpublished', MESSAGE_FLASH)

            if form.preview.data is True:
                return redirect(url_for('blog.preview', slug=post.slug))

    upload_form = UploadImageForm()
    feat_img = post.featured_image.filename if post.featured_image else None
    form = CreateBlogPostForm(request.form,
                              title=post.title,
                              slug=post.slug,
                              published=post.published,
                              body=post.markdown_content,
                              images=[image.filename for image in post.images],
                              author=str(post.author.id),
                              featured_image=feat_img,
                              tags=post.post_tags)
    form.author.choices = [(str(u.id),
                            u.name + " (You)" if u == g.user else u.name)
                           for u in User.objects()]
    form.author.default = str(g.user.id)
    images = [image for image in Image.objects() if image not in post.images]

    return render_template('eventum_posts/edit.html',
                           user=g.user,
                           form=form,
                           post=post,
                           images=images,
                           upload_form=upload_form)