def heartbeats(request): if not request.user.is_authenticated: return HttpResponseRedirect('/login') if request.method == 'POST': is_search = False if request.POST.has_key('search'): is_search = True words = request.POST['search'] search_title = BlogPost.objects.filter(title__contains=words) search_body = BlogPost.objects.filter(body__contains=words) posts = search_body | search_title posts = list(posts) relation_people = [] for post in posts: b = User.objects.get(username=post.author) a = Profile.objects.get(user=b) relation_people.extend([a]) posts = zip(posts, relation_people) return render(request, 'heartbeats.html', { 'posts': posts, 'is_search': is_search, 'word': words }) if request.POST.has_key('body'): bp = BlogPost() bp.title = request.POST['title'] bp.body = request.POST['body'] bp.author = request.user.username bp.timestamp = datetime.now() bp.save() posts = BlogPost.objects.all() posts = list(posts) relation_people = [] for post in posts: b = User.objects.get(username=post.author) a = Profile.objects.get(user=b) relation_people.extend([a]) posts = zip(posts, relation_people) return render( request, 'heartbeats.html', { 'posts': posts, 'is_search': is_search, 'relation_people': relation_people }) else: posts = BlogPost.objects.all() posts = list(posts) relation_people = [] for post in posts: b = User.objects.get(username=post.author) a = Profile.objects.get(user=b) relation_people.extend([a]) posts = zip(posts, relation_people) return render(request, 'heartbeats.html', { 'posts': posts, 'relation_people': relation_people })
def test_save_and_retrieve_blog_posts(self): self.assertEqual(BlogPost.objects.all().count(), 0) blog_post = BlogPost() blog_post.title = "A post" blog_post.date = datetime.datetime(1939, 9, 1, 5, 0, 0) blog_post.body = "Blah blah blah" blog_post.visible = False blog_post.save() self.assertEqual(BlogPost.objects.all().count(), 1) retrieved_post = BlogPost.objects.first() self.assertEqual(retrieved_post, blog_post)
def publish(request, id): context = {} user = request.user if not user.is_authenticated: return redirect("login") notes = get_object_or_404(Notes, pk=id) if notes.author != user: return HttpResponse("You are not the author of that post.") blog_post = BlogPost() blog_post.title = notes.title blog_post.body = notes.body blog_post.author = request.user blog_post.save() return redirect("/")
def load_education_blog_posts(): """ Transferring blog posts from EdCentral CSV for Education Policy Program """ education_blog_mapping = edcentral_blog_mapping() for post in education_blog_mapping: if post['title'] == 'title': pass else: print(post['title']) post_parent = get_program('5') parent_blog_homepage = get_content_homepage( post_parent, ProgramBlogPostsPage, 'EdCentral', ) ed_blog_post_slug = post['slug'] print(ed_blog_post_slug) new_blog_post = BlogPost.objects.filter( slug=ed_blog_post_slug).first() if not new_blog_post and ed_blog_post_slug: new_blog_post = BlogPost( search_description='', seo_title='', depth=5, show_in_menus=False, slug=ed_blog_post_slug, title=post['title'], date=post['real_date'], body=json.dumps([{ 'type': 'paragraph', 'value': post['content'] }]), story_excerpt=get_summary(post['excerpt']), ) parent_blog_homepage.add_child(instance=new_blog_post) new_blog_post.save() print( "-------------------ADDED NEW EDCENTRAL POST----------------------" ) get_education_authors(new_blog_post, post['author']) connect_subprograms_to_post( new_blog_post, clean_subprograms_for_ed(post['categories'])) elif new_blog_post and ed_blog_post_slug: new_blog_post.search_description = '' new_blog_post.seo_title = '' new_blog_post.depth = 5 new_blog_post.show_in_menus = False new_blog_post.slug = ed_blog_post_slug new_blog_post.title = post['title'] new_blog_post.date = post['real_date'] new_blog_post.body = json.dumps([{ 'type': 'paragraph', 'value': post['content'] }]) new_blog_post.story_excerpt = get_summary(post['excerpt']) get_education_authors(new_blog_post, post['author']) connect_subprograms_to_post( new_blog_post, clean_subprograms_for_ed(post['categories'])) print( "-------------------UPDATED EXISTING EDCENTRAL POST----------------------" ) new_blog_post.save()