def delete(post_id): 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') pass return redirect(url_for('.index'))
def delete(post_id): 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') # print "Invalid event id" pass return redirect(url_for('.index'))
def post(slug): 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] if not post.published: abort(404) return render_template('blog/post.html', post=post, recent_posts=recent_posts)
def post(slug): 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] if not post.published: abort(404) return render_template('blog/post.html', post=post, recent_posts=recent_posts)
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'))
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)
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)
def index(): """View the ADI homepage. **Route:** ``/`` **Methods:** ``GET`` """ this_moment = datetime.now().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__gt=date.today()) | Q(end_date=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)
def index(): blog_posts = list(BlogPost.objects(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)
def index(): blog_posts = list( BlogPost.objects(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)
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('admin/posts/preview.html', post=post, recent_posts=recent_posts, related_posts=related_posts)
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('admin/posts/posts.html', posts=all_posts)
def set_published_status(post_id, status): object_id = ObjectId(post_id) if BlogPost.objects(id=object_id).count() == 1: post = BlogPost.objects().with_id(object_id) if status != post.published: post.published = status # TODO Actually publish/unpublish the post here if post.published: flash('Blogpost published') else: flash('Blogpost unpublished') post.save() else: flash("The blog post had not been published. No changes made.") else: flash('Invalid post id') # print "Invalid post id" pass return redirect(url_for('.index'))
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] if not post.published: abort(404) return render_template('blog/post.html', post=post, recent_posts=recent_posts)
def edit(post_id): 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 %s.' % post_id) 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(): was_published = post.published should_be_published = form.published.data 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] if form.featured_image.data: post.featured_image = Image.objects().get(filename=form.featured_image.data) else: post.featured_image = None post.save() if was_published != should_be_published: if was_published: set_published_status(post.id, False) else: set_published_status(post.id, True) return redirect(url_for('.index')) upload_form = UploadImageForm() featured_image = 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=featured_image) 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('admin/posts/edit.html', user=g.user, form=form, post=post, images=images, upload_form=upload_form)
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)
def index(): all_events = (Event.objects( Q(published=True, end_date__gt=date.today()) | Q(published=True, end_date=date.today(), end_time__gt=datetime.now().time()))) events = all_events.order_by('start_date', 'start_time')[:4] all_blog_posts = BlogPost.objects( published=True).order_by('-date_published') blog_post = all_blog_posts[0] if all_blog_posts else None return render_template('index.html', events=events, blog_post=blog_post)
def same_tag(tag): """View blog posts with the same tag **Route:** ``/blog/tag/<tag>`` **Methods:** ``GET`` """ blog_posts = list(BlogPost.objects(tags=tag).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)
def index(): 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("admin/home.html", this_week=this_week, recent_posts=posts)
def index(): """View recent blog posts. **Route:** ``/blog`` **Methods:** ``GET`` """ blog_posts = list(BlogPost.objects(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)
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)
def blog_archive(index): index = int(index) if index <= 0: return redirect(url_for('.index')) blog_posts = BlogPost.objects(published=True).order_by('-date_published') 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)
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)
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("admin/home.html", this_week=this_week, recent_posts=posts)
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)
def blog_archive(index): index = int(index) if index <= 0: return redirect(url_for('.index')) blog_posts = BlogPost.objects(published=True).order_by('-date_published') 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)
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("admin/home.html", this_week=this_week, recent_posts=posts)
def index(): """View the ADI homepage. **Route:** ``/`` **Methods:** ``GET`` """ all_events = (Event.objects( Q(published=True, end_date__gt=date.today()) | Q(published=True, end_date=date.today(), end_time__gt=datetime.now().time()))) events = all_events.order_by('start_date', 'start_time')[:4] all_blog_posts = BlogPost.objects( published=True).order_by('-date_published') blog_post = all_blog_posts[0] if all_blog_posts else None return render_template('index.html', events=events, blog_post=blog_post)
def blog_archive(index): """View older blog posts. **Route:** ``/blog/<index>`` **Methods:** ``GET`` """ index = int(index) if index <= 0: return redirect(url_for('.index')) blog_posts = BlogPost.objects(published=True).order_by('-date_published') 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)
def index(): """View the client homepage. **Route:** ``/`` **Methods:** ``GET`` """ all_events = (Event.objects( Q(published=True, end_date__gt=date.today()) | Q(published=True, end_date=date.today(), end_time__gt=datetime.now().time()))) events = all_events.order_by('start_date', 'start_time')[:4] all_blog_posts = BlogPost.objects(published=True).order_by('-date_published') blog_post = all_blog_posts[0] if all_blog_posts else None return render_template('index.html', events=events, blog_post=blog_post)
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)) 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.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') else: post.unpublish() flash('Blogpost unpublished') return redirect(url_for('.index')) upload_form = UploadImageForm() featured_image = 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=featured_image, tags=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('admin/posts/edit.html', user=g.user, form=form, post=post, images=images, upload_form=upload_form, tags=post.tags)
def view_all_posts(): return str(BlogPost.objects())
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('.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('admin/posts/edit.html', user=g.user, form=form, post=post, images=images, upload_form=upload_form)
def index(): all_posts = BlogPost.objects().order_by('published', '-date_published') return render_template('admin/posts/posts.html', posts=all_posts)
def dad(): for post in BlogPost.objects(): post.delete() return "son"