def test_form_validation_for_too_long_titles(self): form = ArticleForm(data={'title': 'An unexpected failure—it’s actually in the tests for our final view, view_list. Because we’ve changed the way errors are displayed in all templates, we’re no longer showing the error that we manually pass into the template.', 'text': 'bla bla', 'author': self.user}) self.assertFalse(form.is_valid()) self.assertEqual( form.errors['title'], [TITLE_LENGTH_ERROR] )
def post(self, request): """ :param request: :return: """ error_message = None response_article = Article.objects.filter( pk=request.POST.get("id_article_answered")) if len(response_article) > 0: article_with_data = Article(user=request.user, article_answered=response_article[0]) else: article_with_data = Article(user=request.user) article_form = ArticleForm(request.POST, request.FILES, instance=article_with_data) if article_form.is_valid(): new_article = article_form.save() return HttpResponseRedirect( reverse('article_details', args=[new_article.user.username, new_article.pk])) context = {'form': article_form, 'message': error_message} return render(request, 'articles/create.html', context)
def new_article(request): is_login = True user = request.user if request.method == 'POST': form = ArticleForm(request.POST) if form.is_valid(): article = Article.objects.create( author=request.user, title=form.cleaned_data['title'], short_text=form.cleaned_data['short_text'], text=form.cleaned_data['text'], internal=form.cleaned_data['internal']) article.save() request.session['new_article'] = json.dumps(True) request.session['last_created_article'] = json.dumps(article.id) return redirect(reverse('articles:new_article_success')) form = ArticleForm() context = { "header": { "title": "Создать новую статью" }, "form": form, "is_login": is_login, "user": user } template = loader.get_template('articles/newarticle.html') return HttpResponse(template.render(context, request))
def post(self, request): article = Article() article.author = request.user form = ArticleForm(request.POST, request.FILES, instance=article) if form.is_valid(): article = form.save() return redirect('user_articles_detail.html', self.id)
def test_article_form_with_valid_data(self): form = ArticleForm( data={ 'titre': "Un titre", 'description': 'Une description', 'date_pub': timezone.now(), 'auteur': "Nom de l'auteur" }) self.assertTrue(form.is_valid()) self.assertEquals(len(form.cleaned_data), 4)
def article_submit(request): if request.method == 'POST': form = ArticleForm(request.POST) if form.is_valid(): # Preview article if request.POST.get('action') == u'Vista previa': article = form.create_article(request.user) form = ArticlePreviewForm(request.POST) return render_to_response('articles/article_preview.html', { 'form' : form, 'article' : article }, context_instance=RequestContext(request)) # Edit article if request.POST.get('action') == u'Editar': return render_to_response('articles/article_submit.html', { 'form' : form }, context_instance=RequestContext(request)) # Submit article if request.POST.get('action') == u'Enviar noticia': article = form.save(request.user) from django.core.mail import send_mail from django.conf import settings from_email = '"%s" <%s>' % (request.user.get_full_name(), request.user.email) subject = u'Nueva noticia' message = render_to_string('articles/new_article_email.txt', { 'article': article, }) send_mail(subject, message, from_email, [a[1] for a in settings.MANAGERS]) return HttpResponseRedirect('/noticias/submit/sent/') else: form = ArticleForm() return render_to_response('articles/article_submit.html', { 'form' : form }, context_instance=RequestContext(request))
def create_article(request): if request.method == 'POST': form = ArticleForm(request.POST) if form.is_valid(): form.save() return HttpResponseRedirect('/articles/all') else: form = ArticleForm() args = {} args.update(csrf(request)) args['form'] = form return render_to_response('create_article.html', args)
def post(self, request, block_id): self.init_data(block_id) form = ArticleForm(request.POST) if form.is_valid(): # article = Article(block=block, title=form.cleaned_data['title'], # content=form.cleaned_data['content'],status=0) article = form.save(commit=False) article.block = self.block article.status = 0 article.save() return redirect('/article/list/%s' % self.block_id) else: return render(request, self.template_name, {'b': self.block, 'form': form})
def add(request): if request.method == "POST": # save article article_form = ArticleForm(request.POST) if article_form.is_valid(): article = article_form.save(commit=False) article.user = request.user article.save() return redirect(reverse_lazy('article_item', args=[article.id])) else: article_form = ArticleForm() return render(request, 'article/add.html', {'form': article_form})
def post(self, request, *args, **kwargs): """save the form to the Article model database""" if not request.user.is_authenticated: return redirect("login") form = ArticleForm(request.POST, request.FILES) if form.is_valid(): article = form.save(commit=False) article.author = get_object_or_404(User, pk=request.user.id) article.save() return redirect(article.get_absolute_url()) context = {"form": form} return render(request, self.template_name, context)
def add_article(request): if request.method == 'POST': form = ArticleForm(request.POST) if form.is_valid(): article = Article() article.create_user = request.user article.title = form.cleaned_data.get('title') article.content = form.cleaned_data.get('content').replace('\n', '<br>') node_name = form.cleaned_data.get('node') article.add_node(node_name) article.save() return index(request) else: form = ArticleForm() return render(request,'add_article.html', {'form': form})
def get(self, request): form = ArticleForm() context = {'form': form} return render(request, 'new_article.html', context)
def add_article(request): if request.method == 'POST': form = ArticleForm(request.POST) if form.is_valid(): article = Article() article.create_user = request.user article.title = form.cleaned_data.get('title') article.content = form.cleaned_data.get('content').replace( '\n', '<br>') node_name = form.cleaned_data.get('node') article.add_node(node_name) article.save() return index(request) else: form = ArticleForm() return render(request, 'add_article.html', {'form': form})
def update(request, article_id): if request.method == 'GET': return redirect(reverse_lazy('article_item', args=[article_id])) category_id = request.POST.get('id', '') info = get_object_or_404(Articles, id=category_id) # article = CategoryForm(request.POST, instance=info) article = ArticleForm(request.POST, instance=info) if article.is_valid(): article.save() return redirect(reverse_lazy('article_item', args=[article_id])) return render(request, 'article/edit.html', {'form': article, 'article_id': article_id})
def list_articles(request): if request.method == "POST": form = ArticleForm(request.POST) if form.is_valid(): article = Articles( title=form.cleaned_data['title'], markdown_text=form.cleaned_data['markdown_text']) article.save() messages.success(request, "Your article add.") return HttpResponseRedirect("/articles/all/") else: form = ArticleForm() articles = Articles.objects.all() return render(request, "articles/list_articles.html", { 'form': form, "articles": articles, })
def test_articleform_transforms_rawcontent_to_fields_to_allow_saving(self): raw_content = self.sample_meta + self.sample_content form = ArticleForm(data={'author': self.user.pk, 'raw_content': raw_content}) self.assertTrue(form.is_valid()) # First, we check that the form has parsed the markdown data = form.cleaned_data self.assertIn('full_rendered_content', data) self.assertEqual(markdown(raw_content), data['full_rendered_content']) # Now the splits: # The first is the title self.assertEqual('Header title', data['title']) # Then the punchline self.assertEqual('blockquote punchline', data['punchline']) # The introduction should keep the formatting, on the other hand. self.assertEqual('Paragraph introduction <strong>markup</strong>.', data['description']) # At this point, we should have the actual body of the article left as rendered_html. self.assertHTMLEqual(markdown(self.sample_content), data['rendered_html'])
def edit(request, id): tags = '' if id: article = get_object_or_404(Article, pk=id) for tag in article.get_tags(): tags = u'{0} {1}'.format(tags, tag.tag) tags = tags.strip() else: article = Article(create_user=request.user) if request.POST: form = ArticleForm(request.POST, instance=article) if form.is_valid(): form.save() return redirect('/articles/') else: form = ArticleForm(instance=article, initial={'tags': tags}) return render(request, 'articles/edit.html', {'form': form})
def list_articles(request): if request.method == "POST": form = ArticleForm(request.POST) if form.is_valid(): article = Articles( title=form.cleaned_data['title'], markdown_text=form.cleaned_data['markdown_text'] ) article.save() messages.success(request, "Your article add.") return HttpResponseRedirect("/articles/all/") else: form = ArticleForm() articles = Articles.objects.all() return render(request, "articles/list_articles.html", { 'form': form, "articles": articles, })
def write(request): if request.method == 'POST': form = ArticleForm(request.POST) if form.is_valid(): article = Article() article.create_user = request.user article.title = form.cleaned_data.get('title') article.content = form.cleaned_data.get('content') status = form.cleaned_data.get('status') if status in [Article.PUBLISHED, Article.DRAFT]: article.status = form.cleaned_data.get('status') article.save() tags = form.cleaned_data.get('tags') article.create_tags(tags) return redirect('/articles/') else: form = ArticleForm() return render(request, 'articles/write.html', {'form': form})
def test_form_validation_for_blank_inputs(self): form = ArticleForm(data={'title': '', 'text': '', 'categories': '', 'city': '', 'author': self.user}) self.assertFalse(form.is_valid()) self.assertEqual( form.errors['title'], [TITLE_EMPTY_ERROR] ) self.assertEqual( form.errors['text'], [TEXT_EMPTY_ERROR] ) self.assertEqual( form.errors['categories'], [NO_CATEGORY_ERROR] ) self.assertEqual( form.errors['city'], [NO_CITY_ERROR] )
def create_article(request, block_id): block_id = int(block_id) block = Block.objects.get(id=block_id) if request.method == "GET": return render(request, 'create_article.html', {'b': block}) else: form = ArticleForm(request.POST) if form.is_valid(): # article = Article(block=block, title=form.cleaned_data['title'], # content=form.cleaned_data['content'],status=0) article = form.save(commit=False) article.block = block article.status = 0 article.save() return redirect('/article/list/%s'%block_id) else: return render(request, 'create_article.html', {'b': block, 'form': form})
def get(self, request): """ Presenta el formulario para crear un artículo :param request: objeto HttpRequest con los datos de la petición :return: objeto HttpResponse con los datos de la respuesta """ message = None article_form = ArticleForm() context = {'form': article_form, 'message': message} return render(request, 'articles/article_creation.html', context)
def write(request): if request.method == 'POST': form = ArticleForm(request.POST) if form.is_valid(): form.save() return redirect('articles') else: form = ArticleForm() return render(request, 'articles/write.html', {'form': form})
def article_form(): article_form = ArticleForm() article_form.title = 'title' article_form.description = 'description' article_form.updated_at = 'updated_at' article_form.category = category() article_form.published = 'published' return article_form
def get(self, request, pk): article = get_articles(pk=pk)[0] if article: if request.user != article.author and not request.user.is_staff: context = {'message': 'You don\'t have permission for that'} return render(request, self.template_name, context) form = ArticleForm(instance=article) context = {'form': form, 'message': 'Edit you article'} else: context = get_context_from_articles(article) return render(request, self.template_name, context)
def get(self, request, *args, **kwargs): """ Render the creation view for Article """ if not request.user.is_authenticated: return redirect("login") form = ArticleForm() context = {"form": form} return render(request, self.template_name, context)
def get(self, request, pk): """ :param request: :return: """ post = Article.objects.all().filter(pk=pk) article_form = ArticleForm(instance=post[0]) context = {'form': article_form, 'message': ""} return render(request, 'articles/create.html', context)
def edit(request, id): article = get_object_or_404(Article, id=id) if request.method == 'POST': form = ArticleForm(request.POST, instance=article) if form.is_valid(): form.save() return redirect('articles') else: form = ArticleForm(instance=article) return render(request, 'articles/edit.html', {'form': form})
def article_submit(request): if request.method == "POST": form = ArticleForm(request.POST) if form.is_valid(): # Preview article if request.POST.get("action") == u"Vista previa": article = form.create_article(request.user) form = ArticlePreviewForm(request.POST) return render_to_response( "articles/article_preview.html", {"form": form, "article": article}, context_instance=RequestContext(request), ) # Edit article if request.POST.get("action") == u"Editar": return render_to_response( "articles/article_submit.html", {"form": form}, context_instance=RequestContext(request) ) # Submit article if request.POST.get("action") == u"Enviar noticia": article = form.save(request.user) from django.core.mail import send_mail from django.conf import settings from_email = '"%s" <%s>' % (request.user.get_full_name(), request.user.email) subject = u"Nueva noticia" message = render_to_string("articles/new_article_email.txt", {"article": article}) send_mail(subject, message, from_email, [a[1] for a in settings.MANAGERS]) return HttpResponseRedirect("/noticias/submit/sent/") else: form = ArticleForm() return render_to_response("articles/article_submit.html", {"form": form}, context_instance=RequestContext(request))
def articles_update(request, slug): article = get_object_or_404(Article, slug=slug) if request.method == "POST": form = ArticleForm(request.POST, instance=article) if form.is_valid(): article = form.save(commit=False) article.slug = slugify(article.title) form.save() return redirect('articles:list') else: form = ArticleForm(instance=article) return render(request, "articles/articles_new.html", {'form': form})
def test_articleform_transforms_rawcontent_to_fields_to_allow_saving(self): raw_content = self.sample_meta + self.sample_content form = ArticleForm(data={ 'author': self.user.pk, 'raw_content': raw_content }) self.assertTrue(form.is_valid()) # First, we check that the form has parsed the markdown data = form.cleaned_data self.assertIn('full_rendered_content', data) self.assertEqual(markdown(raw_content), data['full_rendered_content']) # Now the splits: # The first is the title self.assertEqual('Header title', data['title']) # Then the punchline self.assertEqual('blockquote punchline', data['punchline']) # The introduction should keep the formatting, on the other hand. self.assertEqual('Paragraph introduction <strong>markup</strong>.', data['description']) # At this point, we should have the actual body of the article left as rendered_html. self.assertHTMLEqual(markdown(self.sample_content), data['rendered_html'])
def submit_article_page(request): form = ArticleForm() if request.method == 'POST': form = ArticleForm(request.POST, request.FILES) if form.is_valid(): form.save() return redirect('articles:list') context = { 'form':form, } return render(request, 'articles/submit.html', context)
def post(self, request, pk): """ :param request: :return: """ error_message = None post = Article.objects.all().filter(pk=pk) article_form = ArticleForm(request.POST, request.FILES, instance=post[0]) if article_form.is_valid(): update_article = article_form.save() return HttpResponseRedirect( reverse('article_details', args=[update_article.user.username, update_article.pk])) context = {'form': article_form, 'message': error_message} return render(request, 'articles/create.html', context)
def post(self, request, *args, **kwargs): """ Update the Article object with the new form details """ article = get_object_or_404(Article, slug=kwargs.get("slug")) if not request.user.is_authenticated: return redirect("login") if not request.user == article.author: return redirect("articles:article-list", "nieuw") form = ArticleForm(request.POST or None, request.FILES, instance=article) if form.is_valid(): form.save() return redirect(article.get_absolute_url()) context = { "form": form, } return render(request, self.template_name, context)
def update(request, pk): article = get_object_or_404(Article, pk=pk) if request.method == 'POST': form = ArticleForm(request.POST, request.FILES, instance=article) if form.is_valid(): form.save() return redirect('articles:detail', article.pk) else: form = ArticleForm(instance=article) context = { 'form': form, } return render(request, 'articles/form.html', context)
def change_article(request, id): changed = False is_login = True user = request.user if request.method == 'POST': form = ArticleForm(request.POST) if form.is_valid(): id = request.POST["id_article"] article = get_object_or_404(Article, id=id) article.title = form.cleaned_data["title"] article.short_text = form.cleaned_data["short_text"] article.text = form.cleaned_data["text"] article.internal = form.cleaned_data["internal"] article.save() changed = True if changed: article = article else: article = get_object_or_404(Article, id=id) title = u"Изменение статьи {title}".format(title=article.title) form = ArticleForm(instance=article) context = { "header": { "title": title, }, "article": article, "form": form, "changed": changed, "is_login": is_login, "user": user } template = loader.get_template('articles/changearticle.html') return HttpResponse(template.render(context, request))
def add_article(request): if request.method == 'POST': form = ArticleForm(request.POST, request.FILES) if form.is_valid(): form.save() return HttpResponseRedirect(reverse('ArticleList', args=[])) else: form = ArticleForm() template_name = 'articles/new.html' context = { 'form': form, } return render(request, template_name, context)
def create(request): if request.method == 'POST': form = ArticleForm(request.POST, request.FILES) if form.is_valid(): article = form.save() return redirect('articles:detail', article.pk) else: form = ArticleForm() context = { 'form': form, } return render(request, 'articles/form.html', context)
def create(request): if request.POST: form = ArticleForm(request.POST) if form.is_valid(): form.save() return HttpResponseRedirect('/data/all') else: form = ArticleForm() args = {} args.update(csrf(request)) args['form'] = form return render_to_response('create_article.html', args)
def get(self, request, *args, **kwargs): """ Render the edit view for an object, form filled in with existing data """ article = get_object_or_404(Article, slug=kwargs.get("slug")) if not request.user.is_authenticated: return redirect("login") if not request.user == article.author: return redirect("articles:article-list", "nieuw") form = ArticleForm(instance=article) context = { "form": form, } return render(request, self.template_name, context)
def add_article(request, template="article/add-article.html"): data = {} try: aid = request.REQUEST.get('aid') article = Article.objects.get(id = aid) data['article'] = article form = ArticleForm(instance = article) except: article = False form = ArticleForm() if request.POST: if article:form = ArticleForm(request.POST,instance = article) else:form = ArticleForm(request.POST) if form.is_valid(): savearticleform = form.save(commit=False) savearticleform.slug = slugify(savearticleform.title) savearticleform.save() cover_image_id = request.POST.get('cover_image',False) if cover_image_id: cover_image = ArticleImage.objects.get(id = int(cover_image_id)) savearticleform.images.add(cover_image) photo_ids = request.POST.getlist('images',[]) is_featured = request.POST.get('is_featured') if is_featured == 'True': savearticleform.is_featured = True else: savearticleform.is_featured = False if photo_ids: for photoid in photo_ids: image = ArticleImage.objects.get(id = int(photoid)) savearticleform.images.add(image) savearticleform.save() return HttpResponseRedirect(reverse('list_articles')) data['form'] = form return TemplateResponse(request, template, data)
def test_form_item_input_has_placeholder(self): form = ArticleForm() self.assertIn('placeholder="Enter a descriptive title"', form.as_p())