def newthread(request, forum): """ 新建帖子 """ f = get_object_or_404(Forum, slug=forum) if not Forum.objects.has_access(f, request.user.groups.all()): return Http404 if request.method == 'POST': form = CreateThreadForm(request.POST) if form.is_valid(): t = Thread( forum=f, title=form.cleaned_data['title'], ) t.save() p = Post( thread=t, author=request.user, body=form.cleaned_data['body'], time=datetime.now(), ) p.save() return HttpResponseRedirect(t.get_absolute_url()) else: form = CreateThreadForm() return render_to_response('forum/newthread.html', RequestContext(request, {'form': form, 'forum': f, }))
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 HttpResponseRedirect("%s?next=%s" % (LOGIN_URL, request.path)) 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(request.POST) if form.is_valid(): t = Thread(forum=f, title=form.cleaned_data["title"]) t.save() p = Post(thread=t, author=request.user, body=form.cleaned_data["body"], time=datetime.now()) p.save() return HttpResponseRedirect(t.get_absolute_url()) else: form = CreateThreadForm() return render_to_response("forum/newthread.html", RequestContext(request, {"form": form, "forum": f}))
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) t = Thread( forum=f, title=request.POST.get('title'), ) t.save() p = Post( thread=t, author=request.user, body=request.POST.get('body'), time=datetime.now(), ) p.save() if request.POST.get('subscribe',False): s = Subscription( author=request.user, thread=t ) s.save() return HttpResponseRedirect(t.get_absolute_url())
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 HttpResponseRedirect('%s?next=%s' % (LOGIN_URL, request.path)) f = get_object_or_404(Forum, slug=forum) if not Forum.objects.has_access(f, request.user): return HttpResponseForbidden() if request.method == 'POST': form = CreateThreadForm(request.POST) if form.is_valid(): t = Thread( forum=f, title=form.cleaned_data['title'], ) t.save() Post = comments.get_model() ct = ContentType.objects.get_for_model(Thread) p = Post( content_type=ct, object_pk=t.pk, user=request.user, comment=form.cleaned_data['body'], submit_date=datetime.now(), site=Site.objects.get_current(), ) p.save() t.latest_post = p t.comment = p t.save() """ undecided if form.cleaned_data.get('subscribe', False): s = Subscription( author=request.user, thread=t ) s.save() """ return HttpResponseRedirect(t.get_absolute_url()) else: form = CreateThreadForm() return render_to_response('forum/newthread.html', RequestContext(request, { 'form': form, 'forum': f, }))
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 HttpResponseRedirect('%s?next=%s' % (LOGIN_URL, request.path)) 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(request.POST) formset = AttachFileFormset(request.POST, request.FILES) if form.is_valid() and formset.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() formset.instance = p formset.save() if form.cleaned_data.get('subscribe', False): s = Subscription(author=request.user, thread=t) s.save() return HttpResponseRedirect(t.get_absolute_url()) else: form = CreateThreadForm() formset = AttachFileFormset() return render_to_response( 'forum/newthread.html', RequestContext(request, { 'form': form, 'formset': formset, 'forum': f, 'active': 7, }))
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(request.POST) if form.is_valid(): t = Thread( forum=f, 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() return HttpResponseRedirect(t.get_absolute_url()) else: form = CreateThreadForm() return render_to_response('forum/newthread.html', RequestContext(request, { 'form': form, 'forum': f, }))
def newthread(request, forum): """Post a new thread. 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. @param forum: forum slug to create new thread for. @type forum: string @return: a view to post a new thread @rtype: Django response """ if not request.user.is_authenticated(): return HttpResponseRedirect("%s?next=%s" % (reverse("user_signin"), request.path)) f = get_object_or_404(Forum, slug=forum) if not Forum.objects.has_access(f, request.user.groups.all()): return HttpResponseForbidden() preview = False if request.method == "POST": form = CreateThreadForm(request.POST) if form.is_valid(): if request.POST.has_key("preview"): preview = {"title": form.cleaned_data["title"], "body": form.cleaned_data["body"]} else: t = Thread(forum=f, 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() return HttpResponseRedirect(t.get_absolute_url()) else: form = CreateThreadForm() return render_to_response( "forum/thread_new.html", RequestContext(request, {"form": form, "forum": f, "preview": preview, "section": forum}), )
def newthread(request, forum): """ 新建帖子 """ f = get_object_or_404(Forum, slug=forum) if not Forum.objects.has_access(f, request.user.groups.all()): return Http404 if request.method == 'POST': form = CreateThreadForm(request.POST) if form.is_valid(): t = Thread( forum=f, title=form.cleaned_data['title'], ) t.save() p = Post( thread=t, author=request.user, body=form.cleaned_data['body'], time=datetime.now(), ) p.save() return HttpResponseRedirect(t.get_absolute_url()) else: form = CreateThreadForm() return render_to_response( 'forum/newthread.html', RequestContext(request, { 'form': form, 'forum': f, }))
def previewthread(request, forum): """ Renders a preview of the new post and gives the user the option to modify it before posting. Only allows a user to post if they're logged in. """ if not request.user.is_authenticated(): return HttpResponseRedirect('%s?next=%s' % (LOGIN_URL, request.path)) f = get_object_or_404(Forum, slug=forum, site=settings.SITE_ID) if not Forum.objects.has_access(f, request.user): return HttpResponseForbidden() if request.method == "POST": if not can_post(f, request.user): return HttpResponseForbidden cache = get_forum_cache() key = make_cache_forum_key(request.user, forum, settings.SITE_ID) if cache and forum in FORUM_FLOOD_CONTROL: if cache.get(key): post_title, post_url, expiry = cache.get(key) expiry = timeuntil(datetime.fromtimestamp(expiry)) messages.error(request, "You can't post a thread in the forum %s for %s." % (f.title, expiry)) return HttpResponseRedirect(post_url) form = CreateThreadForm(request.POST) if form.is_valid(): t = Thread( forum=f, title=form.cleaned_data['title'], ) Post = comments.get_model() ct = ContentType.objects.get_for_model(Thread) # If previewing, render preview and form. if "preview" in request.POST: return render_to_response('forum/previewthread.html', RequestContext(request, { 'form': form, 'forum': f, 'thread': t, 'comment': form.cleaned_data['body'], 'user': request.user, })) # No preview means we're ready to save the post. else: t.save() p = Post( content_type=ct, object_pk=t.pk, user=request.user, comment=form.cleaned_data['body'], submit_date=datetime.now(), site=Site.objects.get_current(), ) p.save() t.latest_post = p t.comment = p t.save() Thread.nonrel_objects.push_to_list('%s-latest-comments' % t.forum.slug, t, trim=30) thread_created.send(sender=Thread, instance=t, author=request.user) if cache and forum in FORUM_FLOOD_CONTROL: cache.set(key, (t.title, t.get_absolute_url(), get_forum_expire_datetime(forum)), FORUM_FLOOD_CONTROL.get(forum, FORUM_POST_EXPIRE_IN)) return HttpResponseRedirect(t.get_absolute_url()) else: form = CreateThreadForm() return render_to_response('forum/newthread.html', RequestContext(request, { 'form': form, 'forum': f, }))
def newthread(request, forum): """Post a new thread. 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. @param forum: forum slug to create new thread for. @type forum: string @return: a view to post a new thread @rtype: Django response """ if not request.user.is_authenticated(): return HttpResponseRedirect('%s?next=%s' % (reverse('user_signin'), request.path)) f = get_object_or_404(Forum, slug=forum) if not Forum.objects.has_access(f, request.user.groups.all()): return HttpResponseForbidden() preview = False if request.method == 'POST': form = CreateThreadForm(request.POST) if form.is_valid(): if request.POST.has_key('preview'): preview = { 'title': form.cleaned_data['title'], 'body': form.cleaned_data['body'] } else: t = Thread( forum=f, 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() return HttpResponseRedirect(t.get_absolute_url()) else: form = CreateThreadForm() return render_to_response( 'forum/thread_new.html', RequestContext(request, { 'form': form, 'forum': f, 'preview': preview, 'section': forum, }))
def previewthread(request, forum): """ Renders a preview of the new post and gives the user the option to modify it before posting. If called without a POST, redirects to newthread. Only allows a user to post if they're logged in. """ if not request.user.is_authenticated(): return HttpResponseRedirect('%s?next=%s' % (LOGIN_URL, request.path)) f = get_object_or_404(Forum, slug=forum) if not Forum.objects.has_access(f, request.user): return HttpResponseForbidden() if request.method == "POST": form = CreateThreadForm(request.POST) if form.is_valid(): t = Thread( forum=f, title=form.cleaned_data['title'], ) Post = comments.get_model() ct = ContentType.objects.get_for_model(Thread) # If previewing, render preview and form. if "preview" in request.POST: return render_to_response('forum/previewthread.html', RequestContext(request, { 'form': form, 'forum': f, 'thread': t, 'comment': form.cleaned_data['body'], 'user': request.user, })) # No preview means we're ready to save the post. else: t.save() p = Post( content_type=ct, object_pk=t.pk, user=request.user, comment=form.cleaned_data['body'], submit_date=datetime.now(), site=Site.objects.get_current(), ) p.save() t.latest_post = p t.comment = p t.save() thread_created.send(sender=Thread, instance=t, author=request.user) return HttpResponseRedirect(t.get_absolute_url()) else: form = CreateThreadForm() return render_to_response('forum/newthread.html', RequestContext(request, { 'form': form, 'forum': f, }))
def merge_thread(request, thread_id): """Merge the posts of two threads into one single thread. The posts are ordered chronologically in the new thread, the old threads are locked, and a notification post is created in all three threads. """ thread = get_object_or_404(Thread, pk=thread_id) new_title_plain = thread.title_plain new_title_html = thread.title_html other_thread = False if request.method == 'POST' and "cancel" not in request.POST: other_thread = get_object_or_404(Thread, pk=request.POST['other-thread-id']) # try --- fix new_title_plain = request.POST['new-thread-title'] new_title_html = prettify_title(new_title_plain) if request.method == 'POST' and "merge" in request.POST: if len(new_title_plain) > MAX_THREAD_TITLE_LENGTH: messages.error(request, long_title_error % MAX_THREAD_TITLE_LENGTH) elif request.method == 'POST' and "confirm" in request.POST: now = datetime.now() # UTC? user = request.user t = Thread(title_plain=new_title_plain, title_html=new_title_html, creation_date=now, author=user, category=thread.category, latest_reply_date=now) t.save() # Update posts in two threads to point to new thread t thread.post_set.all().update(thread=t.id) other_thread.post_set.all().update(thread=t.id) # Make post notification in ALL threads # Do not append a redundant full stop if new_title_plain[-1] not in set([".!?"]) \ and new_title_plain[-3:-1] not in set(['."', '!"', '?"', ".'", "!'", "?'"]): end = "." else: end = "" message = "(*%s* was merged with *%s* by *%s* into *[%s](%s)*%s)" % \ (thread.title_html, other_thread.title_html, user.username, new_title_html, t.get_absolute_url(), end) html = sanitized_smartdown(message) p1 = Post(creation_date=now, author=user, thread=t, content_plain=message, content_html=html) p2 = Post(creation_date=now, author=user, thread=thread, content_plain=message, content_html=html) p3 = Post(creation_date=now, author=user, thread=other_thread, content_plain=message, content_html=html) p1.save() p2.save() p3.save() # Lock original threads thread.is_locked = True other_thread.is_locked = True thread.save() other_thread.save() return HttpResponseRedirect(reverse('forum.views.thread', args=(t.id,))) return render(request, 'merge.html', {'full_url' : request.build_absolute_uri(), 'current_site': Site.objects.get_current(), 'thread' : thread, 'other_thread': other_thread, 'new_title' : new_title_plain})
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, }))