def edit_article(request, article_id=0): if request.method == 'POST': form = EditArticleForm(request.POST, request.FILES) if form.is_valid(): author = request.user.author date = timezone.now() title = form.cleaned_data['title'] is_beta = form.cleaned_data['is_beta'] text = form.cleaned_data['text'] category = form.cleaned_data['category'] image = form.cleaned_data['image'] if article_id == 0: e = Event(name=title, pub_date=date, category=category, image=None) a = Article(event=e, author=author, is_beta=is_beta, text=text) else: a = get_object_or_404(Article, pk=article_id) if image is None: image = a.event.image e = a.event if author != a.author and author not in a.modifiers.all(): a.modifiers.add(author) a.is_beta = is_beta a.text = text e.name = title e.pub_date = date e.category = category e.image = image a.event = e e.save() a.save() return HttpResponseRedirect(a.get_absolute_url()) else: if article_id == 0: form = EditArticleForm() else: a = get_object_or_404(Article, pk=article_id) form = EditArticleForm(initial={'title':a.event.name, 'image':a.event.image, 'category':a.event.category, 'is_beta':a.is_beta, 'text':a.text}) if article_id == 0: send_to = '/article/new/' else : send_to = '/article/edit/' + article_id + '/' return render(request, 'article/edit.html', locals())
def edit_article(request, article_id=0): if request.method == 'POST': form = EditArticleForm(request.POST, request.FILES) formset=formset_factory( EditPartForm )( request.POST ) if form.is_valid(): author = request.user.member date = timezone.now() title = form.cleaned_data['title'] is_beta = form.cleaned_data['is_beta'] category = form.cleaned_data['category'] image = form.cleaned_data['image'] is_pinned = form.cleaned_data['is_pinned'] introduction = form.cleaned_data['introduction'] if article_id == 0: e = Event(name=title, pub_date=date, category=category, image=image, is_pinned=is_pinned) e.save() a = Article(event=e, author=author, is_beta=is_beta, introduction=introduction) a.save() if formset.is_valid(): for p in formset: text = p.cleaned_data['text'] title= p.cleaned_data['title'] part=Part(text=text, title=title, article=a) part.save() idx = a.pk else: a = get_object_or_404(Article, pk=article_id) if image is None: image = a.event.image e = a.event a.introduction = introduction a.is_beta = is_beta e.is_pinned = is_pinned e.name = title e.pub_date = date e.category = category e.image = image a.event = e if formset.is_valid(): for p in a.parts.all(): p.delete() for p in formset: a.parts.create(title=p.cleaned_data['title'], text=p.cleaned_data['text']) e.save() a.save() return HttpResponseRedirect(a.get_absolute_url()) else: if article_id == 0: form = EditArticleForm() parts = formset_factory(EditPartForm, extra=1)() else: a = get_object_or_404(Article, pk=article_id) p_list = [] no_parts = True for i in a.parts.all(): p_list.append({'title': i.title,'text':i.text}) no_parts = False if no_parts: parts = formset_factory(EditPartForm, extra=0)(initial=p_list) else: parts = formset_factory(EditPartForm, extra=0)(initial=p_list) form = EditArticleForm(initial={'title':a.event.name, 'image':a.event.image, 'category':a.event.category, 'is_beta':a.is_beta, 'introduction':a.introduction,}) if article_id == 0: send_to = '/article/new/' else : send_to = '/article/' + article_id + '/edit/' return render(request, 'article/edit.html', locals())