def addArticle(request, template_name): act_type = 'publish' try: if request.GET['act_type']: act_type = request.GET['act_type'] except MultiValueDictKeyError: pass if request.method == 'POST': form = articlesForm(request.POST) if form.is_valid(): t = form.save(request,act_type) form.save_m2m() if act_type == 'publish': return HttpResponseRedirect(reverse('getarticlelist')) elif act_type == 'save': return HttpResponseRedirect(reverse('editarticle',args=[t.id])) else: form = articlesForm() vt = loader.get_template(template_name) c = RequestContext( request, { 'form':form, 'form_url':reverse('addarticle'), 'page_title':_('Add Article'), 'tags':tags.objects.order_by('-actionLatest')[0:35], 'tslug':None } ) return HttpResponse(vt.render(c))
def editArticle(request,tid,template_name): act_type = 'publish' try: if request.GET['act_type']: act_type = request.GET['act_type'] except MultiValueDictKeyError: pass try: tarticles = articles.objects.get(id=tid) if tarticles.author == request.user or request.user.get_user_role() == 'admin': if request.method == 'POST': form = articlesForm(request.POST,instance=tarticles) if form.is_valid(): t = form.save(request,act_type) form.save_m2m() if act_type == 'publish': return HttpResponseRedirect(reverse('getarticlelist')) elif act_type == 'save': return HttpResponseRedirect(reverse('editarticle',args=[t.id])) else: taglist = tarticles.tags.values_list('name',flat=True) tag_init = ','.join(taglist) if tag_init: tag_init += ',' form = articlesForm(instance=tarticles,initial={'tags':tag_init}) vt = loader.get_template(template_name) c = RequestContext( request, { 'form':form, 'form_url':reverse('editarticle',args=[tid]), 'page_title':_('Edit Article'), 'tags':tags.objects.all()[0:35], 'tslug':tarticles.slug } ) return HttpResponse(vt.render(c)) return render_to_response( 'error.html', {'message':_('You have no perm to edit this article.')}, context_instance=RequestContext(request) ) except articles.DoesNotExist: return render_to_response( 'error.html', {'message':_('Article does not exist.')}, context_instance=RequestContext(request) )