def create_post(req, author): out = {} out['author'] = author = get_object_or_404(User, username=author) out['back_link'] = reverse("blog_post_list", args=[author]) if req.POST: pm = Post(author=req.user) out['form'] = form = PostForm(req.POST, instance=pm) if form.is_valid(): form.save() last_post = Post.objects.filter(author=pm.author, last_post=True) if last_post: last_post = last_post[0] last_post.last_post = False last_post.save() post_ldate = Post.objects.filter(author=pm.author, published=True).order_by('-date') if post_ldate: post_ldate = post_ldate[0] post_ldate.last_post = True post_ldate.save() return HttpResponseRedirect(req.user.get_blog_absolute_url()) else: out['form'] = PostForm() return render_to_response('blog/createPost.html', out, RequestContext(req))
def test_form_title_is_cleaned(self, sample_post_data): form = PostForm(data=sample_post_data) form.data[ 'title'] = 'why and how IMO salt & vinegar crisps with cheese are best. ' form.save(commit=False) title = form.cleaned_data['title'] assert title == 'Why and How IMO Salt and Vinegar Crisps with Cheese are Best'
def edit_post(request, post_id): logging.critical(request.method) a_post_instance = get_object_or_404(Post, id=post_id) form = PostForm(request.POST or None, instance=a_post_instance) if form.is_valid(): # All validation rules pass post = form.save() return redirect(reverse("blog_posts")) return render(request, "blog/edit_post.html", {'form': form})
def create_post(request): form = PostForm(request.POST or None) # Por qué or None? if form.is_valid(): # All validation rules pass user = request.user post = form.save(commit=False) post.author = user post.save() return redirect(reverse("blog_home")) return render(request, "blog/create_post.html", {'form': form})
def create_post(request): form = PostForm(request.POST or None) if form.is_valid(): # All validation rules pass user = User.objects.all()[0] post = form.save(commit=False) post.author = user post.save() return redirect(reverse("blog_posts")) return render(request, "blog/create_post.html", {'form': form})
def create_post_first_aproach(request): if request.method == "POST": form = PostForm(request.POST) # A form bound to the POST data if form.is_valid(): # All validation rules pass # Process the data in form.cleaned_data # ... user = User.objects.all()[0] post = form.save(commit=False) post.author = user post.save() return redirect(reverse("blog_posts")) # Redirect after POST else: form = PostForm() return render(request, "blog/create_post.html", {'form': form})
def edit_post(req, author, date, slug): redirect_to = reverse("blog_post_list", args=[author]) date = d(*time.strptime(date, '%Y-%m-%d')[:3]) today = (dt.combine(date, t.min), dt.combine(date, t.max)) post = get_object_or_404(Post.objects.filter(date__range=today), slug=slug, author__username=author) if req.user != post.author and not req.user.is_superuser: return HttpResponseRedirect(redirect_to) form = PostForm(instance=post) if req.POST: form = PostForm(req.POST, instance=post) if form.is_valid(): ps = form.save() last_post = Post.objects.filter(author=ps.author, last_post=True) if last_post: last_post = last_post[0] last_post.last_post = False last_post.save() post_ldate = Post.objects.filter(author=ps.author, published=True).order_by('-date') if post_ldate: post_ldate = post_ldate[0] post_ldate.last_post = True post_ldate.save() return HttpResponseRedirect(redirect_to) return render_to_response('blog/createPost.html', { 'form': form, 'back_link': redirect_to, 'is_edit': 1 }, RequestContext(req))
def edit_post(req, author, date, slug): redirect_to = reverse("blog_post_list", args=[author]) date = d(*time.strptime(date, '%Y-%m-%d')[:3]) today = (dt.combine(date, t.min), dt.combine(date, t.max)) post = get_object_or_404(Post.objects.filter(date__range=today), slug=slug, author__username=author) if req.user != post.author and not req.user.is_superuser: return HttpResponseRedirect(redirect_to) form = PostForm(instance = post) if req.POST: form = PostForm(req.POST, instance = post) if form.is_valid(): ps = form.save() last_post = Post.objects.filter(author=ps.author, last_post=True) if last_post: last_post = last_post[0] last_post.last_post = False last_post.save() post_ldate = Post.objects.filter(author=ps.author, published=True).order_by('-date') if post_ldate: post_ldate = post_ldate[0] post_ldate.last_post = True post_ldate.save() return HttpResponseRedirect(redirect_to) return render_to_response('blog/createPost.html', {'form': form, 'back_link': redirect_to, 'is_edit': 1}, RequestContext(req))
def test_form_still_submits_without_image(self, sample_post_data): form = PostForm(data=sample_post_data) form.data['image'] = None assert form.is_valid(), 'Should still be valid'
def test_date_fields_not_in_form(self): form = PostForm() assert 'publish_date' not in form.fields assert 'updated_date' not in form.fields
def test_empty_form_is_invalid(self): form = PostForm(data={}) assert not form.is_valid(), 'Should be invalid'
def test_form_title_requires_min_num_chars(self, sample_post_data): form = PostForm(data=sample_post_data) form.data['title'] = 'a sample title' error_returned = form.errors['title'][0] assert error_returned.startswith( 'Ensure this value has at least 40 characters (it has ')
def test_form_tests_for_all_fields(self, sample_post_data): """ Checks all fields requiring testing are in the form """ form = PostForm() for field in sample_post_data.keys(): if field != 'validity': assert field in form.fields
def test_status_field_is_required(self, sample_post_data): form = PostForm(data=sample_post_data) form.data['status'] = '' error_returned = form.errors['status'][0] assert error_returned == 'This field is required.'
def test_image_field_contains_help_text(self, sample_post_data): form = PostForm(data=sample_post_data) help_text = form.fields['image'].help_text assert help_text == 'For bests results, use an image that is 1,200px wide x 600px high'
def test_categories_field_contains_help_text(self, sample_post_data): form = PostForm(data=sample_post_data) help_text = form.fields['categories'].help_text assert help_text == 'Select more than one category by holding down Ctrl or Cmd key'
def test_title_field_contains_help_text(self, sample_post_data): form = PostForm(data=sample_post_data) help_text = form.fields['title'].help_text assert help_text == 'The length of the post must be between 40 and 60 characters'