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 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 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 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 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 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 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 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 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 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 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 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 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_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 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 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(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 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 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 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 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 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 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 update_article(request, slug=None): article = get_object_or_404(Article, slug=slug) if request.method == 'POST': form = ArticleForm(request.POST, request.FILES, instance=article) if form.is_valid(): form.save() return HttpResponseRedirect( reverse('ArticleDetail', kwargs={ 'slug': slug, })) else: form = ArticleForm(instance=article) template_name = 'articles/update.html' context = { 'form': form, } return render(request, template_name, context)
def post(self, request): """ Presenta el formulario para crear un artículo, valida el formulario y crea el artículo si todo ha ido bien :param request: objeto HttpRequest con los datos de la petición :return: objeto HttpResponse con los datos de la respuesta """ message = None article_user = Article(author=request.user) article_form = ArticleForm(request.POST, instance=article_user) if article_form.is_valid(): article = article_form.save() article_form = ArticleForm() message = 'Artículo creado correctamente.Puedes ver tu artículo haciendo click <a href="{0}">aquí</a>'.format( reverse('article_detail', args=[article.author.username, article.pk])) context = {'form': article_form, 'message': message} return render(request, 'articles/article_creation.html', context)
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 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 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 articles_new(request): if request.method == 'POST': form = ArticleForm(request.POST) if form.is_valid(): article = form.save(commit=False) topic = form.cleaned_data['topic'] title = form.cleaned_data['title'] content = form.cleaned_data['content'] article.topic = topic article.title = title article.slug = slugify(title) article.content = content article.save() return redirect('articles:list') else: form = ArticleForm() 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 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 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 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 test_article_form_with_no_data(self): form = ArticleForm({}) self.assertFalse(form.is_valid()) self.assertEquals(len(form.errors), 4)
def post(self, request, pk): article = get_articles(pk=pk)[0] form = ArticleForm(request.POST, instance=article) if form.is_valid(): create_or_update_article(form=form, request=request) return redirect(article.get_absolute_url())
def post(self, request): form = ArticleForm(request.POST) if form.is_valid(): create_or_update_article(form=form, request=request) return redirect('allArticlesByUser', slug_username=request.user)