def admin_edit_post(request, slug, year, month, day): post = Post.query( ndb.AND( Post.slug == slug, Post.date_published == datetime.datetime( int(year), int(month), int(day) ) ) ).get() form = PostForm(initial = post.to_dict()) if request.method == "POST": form = PostForm(request.POST) if form.is_valid(): post.title = form.cleaned_data['title'] post.brief = form.cleaned_data['brief'] post.content = form.cleaned_data['content'] post.is_active = form.cleaned_data['is_active'] post.comments_enabled = form.cleaned_data['comments_enabled'] post.put() return redirect(post.get_absolute_url()) else: form r = render( request, 'admin/edit.html', { 'post': post, 'form': form } ) return r
def tags(request): """ Get exhaustive list of Tags for published Posts :return: tags [LIST] list of Post tags """ # Grab all published Posts post_query = Post.query(ancestor=get_blog_key()).filter(Post.published == True) # Remove duplicates tags = list(set(flatten_list([post.tags for post in post_query.iter()]))) return json_response(tags)
def post_index(request): posts = Post.query( Post.is_active == True ).order( -Post.created_at ).fetch() r = render( request, 'post/index.html', { 'posts': posts } ) return r
def pages(request, tags=None): """ Get number of pages of Blog Posts :param tags [LIST] of tags to filter on [optional] :return: pages [INT] number of pages """ # Grab all published Posts post_query = Post.query().filter(Post.published == True) # Apply Tag filter if tags: post_query = post_query.filter(Post.tags.IN(tags)) return HttpResponse(page_count(post_query.count(), POST_PAGE_SIZE))
def admin_delete_post(request, slug, year, month, day): post = Post.query( ndb.AND( Post.slug == slug, Post.date_published == datetime.datetime( int(year), int(month), int(day) ) ) ).get() post.key.delete() return redirect('/')
def posts(request, post_id=None, tags=None, page=None, titles_only=False, published_only=False): """ Get Published Blog Posts :param tags [LIST] of tags to filter on [optional] :param page [INT] page number of Posts to return [optional] :param titles_only [BOOLEAN] return Post titles and stamps only [optional] :param published_only [BOOLEAN] return published Posts only [optional] :param post_id [LONG] Post identifier [optional] :return: posts [LIST] List of Posts """ # ID filter (if we get an ID parameter lets assume the user wants all the info on that Post) if post_id: post = Post.get_by_id(post_id, parent=get_blog_key()) iterator = [post] if post else [] else: # If no ID specified, get all Posts ordered by stamp for our Blog post_query = Post.query(ancestor=get_blog_key()).order(-Post.stamp) # Published filter if published_only: post_query = post_query.filter(Post.published == True) # Tag filter if tags: post_query = post_query.filter(Post.tags.IN(tags)) # Page Filter if page is not None: iterator = post_query.fetch(POST_PAGE_SIZE, offset=page * POST_PAGE_SIZE) else: iterator = post_query.fetch() # Preview or full Post if titles_only: response = json_response([post.preview() for post in iterator]) else: response = json_response([post.dictionary() for post in iterator]) return response
def post_view(request, slug, year, month, day): post = Post.query( ndb.AND( Post.slug == slug, Post.date_published == datetime.datetime( int(year), int(month), int(day) ), Post.is_active == True, ) ).get() if post is None: raise Http404 r = render( request, 'post/view.html', { 'post': post, 'disqus_shortname': settings.DISQUS_SHORTNAME } ) return r
def clean_posts(): # Remove posts if we have exceeded the limit for expired_post in Post.query().order(-Post.stamp).fetch()[MAX_NUMBER_OF_POSTS:]: expired_post.key.delete()
def test_post_get(self): """we can get it """ res = self.c.get('/post/{0}'.format(self.post_id)) self.assertEqual(Post.query(ancestor=Blog.get_key(self.blog_id)).get().title, res.context_data['post'].title)