def post(request, category, success_url=None, form_class=NewsForm, template_name='news/news_post.html', extra_context=None): c = get_object_or_404(Category.objects.all(), slug=category) if request.method == 'POST': form = form_class(data=request.POST, files=request.FILES) if form.is_valid(): n = News( title =form.cleaned_data['title'], slug =form.cleaned_data['slug'], deliverer =form.cleaned_data['deliverer'], category =form.cleaned_data['category'], source =form.cleaned_data['source'], content =form.cleaned_data['content'], summary =form.cleaned_data['summary'], ) n.save() kwargs = {} kwargs['title'] = _("%s Posted A new News: %s") % (self.deliverer.username,self.title) kwargs['content'] = _("%s Posted A new News,go and view it now <a href='%s'> %s </a> " ) % (self.deliverer.username,self.get_absolute_url(),self.title) kwargs['slug'] = "news%d-%s" % (self.id,datetime.datetime.now().strftime("%Y%m%d%H%M%S")) latest_news_created.send(sender=self.__class__,**kwargs) for attachedfilefield in form.files: #attachment_file = form.files[attachedfilefield] #attach = Attachment() #attach.attached_by = request.user #attach.file = attachment_file #attach.content_type = ContentType.objects.get_for_model(n) #attach.object_id = n.id #attach.title = attachment_file.name #attach.summary = n.title #attach.file.save(attachment_file.name, attachment_file, save=False) #attach.save() attachment_file = form.files[attachedfilefield] attach = Attachment() attach.handle_uploaded_attachment( n, attachment_file, attached_by = request.user, title = attachment_file.name, summary = n.title ) if attachedfilefield == u"file": if not n.pic: n.pic = attach.file_url() n.save() return HttpResponseRedirect(n.get_absolute_url()) #return HttpResponseRedirect(success_url or reverse('news-index')) else: form = form_class(initial={'category':c.id, 'deliverer':request.user.id}) #form = form_class() if extra_context is None: extra_context = {} context = RequestContext(request) for key, value in extra_context.items(): context[key] = callable(value) and value() or value return render_to_response(template_name, { 'form': form, 'category':c, }, context_instance=context)
def post(request, category, success_url=None, form_class=NewsForm, template_name='news/news_post.html', extra_context=None): if request.method == 'POST': form = form_class(data=request.POST, files=request.FILES) if form.is_valid(): n = News( title=form.cleaned_data['title'], slug=form.cleaned_data['slug'], deliverer=form.cleaned_data['deliverer'], category=form.cleaned_data['category'], source=form.cleaned_data['source'], content=form.cleaned_data['content'], summary=form.cleaned_data['summary'], ) n.save() kwargs = {} kwargs['title'] = _("%s Posted A new News: %s") % ( self.deliverer.username, self.title) kwargs['content'] = _( "%s Posted A new News,go and view it now <a href='%s'> %s </a> " ) % (self.deliverer.username, self.get_absolute_url(), self.title) kwargs['slug'] = "news%d-%s" % ( self.id, datetime.datetime.now().strftime("%Y%m%d%H%M%S")) latest_news_created.send(sender=self.__class__, **kwargs) for attachedfilefield in form.files: #attachment_file = form.files[attachedfilefield] #attach = Attachment() #attach.attached_by = request.user #attach.file = attachment_file #attach.content_type = ContentType.objects.get_for_model(n) #attach.object_id = n.id #attach.title = attachment_file.name #attach.summary = n.title #attach.file.save(attachment_file.name, attachment_file, save=False) #attach.save() attachment_file = form.files[attachedfilefield] attach = Attachment() attach.handle_uploaded_attachment(n, attachment_file, attached_by=request.user, title=attachment_file.name, summary=n.title) if attachedfilefield == u"file": if not n.pic: n.pic = attach.file_url() n.save() return HttpResponseRedirect(n.get_absolute_url()) #return HttpResponseRedirect(success_url or reverse('news-index')) else: c = get_object_or_404(Category, slug=category) form = form_class(initial={'category': c.id, 'title': 'ssss'}) #form = form_class() if extra_context is None: extra_context = {} context = RequestContext(request) for key, value in extra_context.items(): context[key] = callable(value) and value() or value return render_to_response(template_name, {'form': form}, context_instance=context)