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)
def newthread(request, forum): """ Rudimentary post function - this should probably use newforms, although not sure how that goes when we're updating two models. Only allows a user to post if they're logged in. """ if not request.user.is_authenticated(): return HttpResponseServerError() f = get_object_or_404(Forum, slug=forum) if not Forum.objects.has_access(f, request.user.groups.all()): return HttpResponseForbidden() if request.method == 'POST': form = CreateThreadForm(data=request.POST, files=request.FILES) #return HttpResponseRedirect('/POST'+str(request.FILES['file'])) #return HttpResponseRedirect('/POST'+str(form.is_valid())) if form.is_valid(): t = Thread( forum=f, author=request.user, title=form.cleaned_data['title'], ) t.save() p = Post( thread=t, author=request.user, body=form.cleaned_data['body'], time=datetime.now(), ) p.save() if form.cleaned_data.get('subscribe', False): s = Subscription( author=request.user, thread=t ) s.save() for attachedfilefield in form.files: #file_path = '%s%s' % (settings.MEDIA_ROOT, form.files[attachedfilefield]) attachment_file = form.files[attachedfilefield] attach=Attachment() attach.handle_uploaded_attachment(p, attachment_file, attached_by = request.user, title = attachment_file.name, summary = t.title ) return HttpResponseRedirect(t.get_absolute_url()) else: form = CreateThreadForm() return render_to_response('forum/newthread.html', RequestContext(request, { 'form': form, 'forum': f, }))
def edit(request, thread): """ If a thread isn't closed, and the user is logged in, post a reply to a thread. Note we don't have "nested" replies at this stage. """ if not request.user.is_authenticated(): return HttpResponseServerError() t = get_object_or_404(Thread, pk=thread) if t.closed: return HttpResponseServerError() if not Forum.objects.has_access(t.forum, request.user.groups.all()): return HttpResponseForbidden() if request.method == "POST": form = ReplyForm(data=request.POST, files=request.FILES) if form.is_valid(): body = form.cleaned_data['body'] p = Post( thread=t, author=request.user, body=body, time=datetime.now(), ) p.save() sub = Subscription.objects.filter(thread=t, author=request.user) if form.cleaned_data.get('subscribe',False): if not sub: s = Subscription( author=request.user, thread=t ) s.save() else: if sub: sub.delete() # Subscriptions are updated now send mail to all the authors subscribed in # this thread. mail_subject = '' try: mail_subject = settings.FORUM_MAIL_PREFIX except AttributeError: mail_subject = '[Forum]' mail_from = '' try: mail_from = settings.FORUM_MAIL_FROM except AttributeError: mail_from = settings.DEFAULT_FROM_EMAIL mail_tpl = loader.get_template('forum/notify.txt') c = Context({ 'body': wordwrap(striptags(body), 72), 'site' : Site.objects.get_current(), 'thread': t, }) email = EmailMessage( subject=mail_subject+' '+striptags(t.title), body= mail_tpl.render(c), from_email=mail_from, to=[mail_from], bcc=[s.author.email for s in t.subscription_set.all()],) email.send(fail_silently=True) for attachedfilefield in form.files: #file_path = '%s%s' % (settings.MEDIA_ROOT, form.files[attachedfilefield]) attachment_file = form.files[attachedfilefield] attach=Attachment() attach.handle_uploaded_attachment(p, attachment_file, request.user, attachment_file.name, t.title ) return HttpResponseRedirect(p.get_absolute_url()) else: post_id = request.GET.get('pid', 0) if post_id ==0: return HttpResponseServerError(_("The Post Do Not Exist")) try: p = Post.objects.all().get(id=post_id) except Post.DoesNotExist: raise Http404(_("Not Found")) body=p.body initial = {'subscribe': True, 'body':body} form = ReplyForm(initial=initial) return render_to_response('forum/edit.html', RequestContext(request, { 'form': form, 'post': p }))
def reply(request, thread): """ If a thread isn't closed, and the user is logged in, post a reply to a thread. Note we don't have "nested" replies at this stage. """ if not request.user.is_authenticated(): return HttpResponseServerError() t = get_object_or_404(Thread, pk=thread) if t.closed: return HttpResponseServerError() if not Forum.objects.has_access(t.forum, request.user.groups.all()): return HttpResponseForbidden() if request.method == "POST": form = ReplyForm(data=request.POST, files=request.FILES) if form.is_valid(): body = form.cleaned_data['body'] p = Post( thread=t, author=request.user, body=body, time=datetime.now(), ) p.save() sub = Subscription.objects.filter(thread=t, author=request.user) if form.cleaned_data.get('subscribe',False): if not sub: s = Subscription( author=request.user, thread=t ) s.save() else: if sub: sub.delete() # Subscriptions are updated now send mail to all the authors subscribed in # this thread. mail_subject = '' try: mail_subject = settings.FORUM_MAIL_PREFIX except AttributeError: mail_subject = '[Forum]' mail_from = '' try: mail_from = settings.FORUM_MAIL_FROM except AttributeError: mail_from = settings.DEFAULT_FROM_EMAIL mail_tpl = loader.get_template('forum/notify.txt') c = Context({ 'body': wordwrap(striptags(body), 72), 'site' : Site.objects.get_current(), 'thread': t, }) email = EmailMessage( subject=mail_subject+' '+striptags(t.title), body= mail_tpl.render(c), from_email=mail_from, to=[mail_from], bcc=[s.author.email for s in t.subscription_set.all()],) email.send(fail_silently=True) ## data={'title': t.title, ## 'summary': t.title, ## 'attached_timestamp':datetime.now() ## } ## attachment_form=AttachmentForm(data=data,files=form.files) ## a=attachment_form.errors ## content_type =ContentType.objects.get_for_model(Post) ## object_id = p.id ## ffs=form.files ## debug() ## if attachment_form.is_valid(): ## attachment = attachment_form.save(commit=False) ## attachment.content_type = content_type ## attachment.object_id = object_id ## attachment.attached_by = request.user ## attachment.save() # for attachedfilefield in form.files: # #file_path = '%s%s' % (settings.MEDIA_ROOT, form.files[attachedfilefield]) # attachment_file = form.files[attachedfilefield] # file_path =os.path.join(ATTACHMENT_DIR, randomfilename(attachment_file.name)) # (mimetype, encoding) = mimetypes.guess_type(file_path) # # try: # mime_type = mimetype # except: # mime_type = 'text/plain' # # attach=Attachment( # content_type =ContentType.objects.get_for_model(Post), # object_id = p.id, # title = attachment_file.name, # summary = t.title, # attached_by = request.user, # ) # attach.save_uploaded_file(attachment_file) # attach.save() for attachedfilefield in form.files: #file_path = '%s%s' % (settings.MEDIA_ROOT, form.files[attachedfilefield]) attachment_file = form.files[attachedfilefield] attach=Attachment() attach.handle_uploaded_attachment(p, attachment_file, request.user, attachment_file.name, t.title ) return HttpResponseRedirect(p.get_absolute_url()) else: form = ReplyForm() return render_to_response('forum/reply.html', RequestContext(request, { 'form': form, 'forum': t.forum, 'thread': t, }))