def new_thread(request, forumSlug): try: forum = SubForum.objects.get(slug=forumSlug) except SubForum.DoesNotExist: raise Http404("Sub-forum doesn't exist.") if request.method == 'POST': form = ThreadForm(request.POST) if form.is_valid(): title = form.cleaned_data['title'] content = form.cleaned_data['content'] theThread = Thread(sub_forum=forum, user=request.user, title=title, slug='', text=content) utilities.unique_slugify(theThread, title) theThread.save() return HttpResponseRedirect(theThread.get_url()) else: form = ThreadForm() context = {'forumSlug': forumSlug, 'form': form} return render(request, 'new/new_thread.html', context)
def newthread(request, cat_id): if not request.user.is_authenticated(): return redirect(reverse(index)) else: try: cat = Category.objects.get(pk=cat_id) except Category.DoesNotExist: return redirect(reverse(ticket)) if request.method == 'POST': tform = ThreadForm(request.POST, prefix="thd") pform = PostForm(request.POST, prefix="pst") if tform.is_valid() and pform.is_valid(): date = datetime.datetime.now() newt = tform.save(commit=False) newt.rank = 0 newt.parent = cat newt.date = date newt.save() newp = pform.save(commit=False) newp.author = request.user newp.date = date newp.parent = newt newp.save() url = ''.join(('/agora/', str(cat_id), '/', str(newt.id), '/')) return redirect(url, request) else: tform = ThreadForm(prefix="thd") pform = PostForm(prefix="pst") return render(request, 'newthread.html', locals())
class ForumPage(Page): parent_page_types = ['home.HomePage'] subpage_types = ['ForumThreadPage'] slug = 'forum' form = None threads = None def serve(self, request, *args, **kwargs): self.form = ThreadForm(request.POST or None) if request.method == 'POST' and self.form.is_valid(): try: data = self.form.cleaned_data thread = Thread( subject=data['subject'], content=data['content'], author=request.user ) thread.save() messages.success(request, 'Your thread has been created') self.form = ThreadForm(None) except: messages.error(request, 'An error occurred during thread creation. Please try again later') self.threads = Thread.objects.all() return super().serve(request)
def test_item_description_is_required(self): """ test description field is required """ form = ThreadForm({'description': ''}) self.assertFalse(form.is_valid()) self.assertIn('description', form.errors.keys()) self.assertEqual(form.errors['description'][0], 'This field is required.')
def newThread(request): if request.method == 'POST': # If the form has been submitted... form = ThreadForm(request.POST) # A form bound to the POST data if 'login' in request.session: usr = User.objects.get(login= request.session['login']) else: return HttpResponseRedirect('/error/2/') if form.is_valid(): # All validation rules pass thread = Thread( title= form.cleaned_data['title'], content = form.cleaned_data['content'], category = form.cleaned_data['category'], user = usr, response = [], rating = [] ) thread.save() return HttpResponseRedirect('/thread/' + str(thread.id)) # Redirect after POST else: form = ThreadForm() # An unbound form categories = Category.objects.all() return render(request, 'newThread.html', { 'form': form, 'categories' : categories, })
def new_thread_view(request, category_id): category = get_object_or_404(Category, pk=category_id) doge_user = DogeUser.objects.get(user=request.user) form = ThreadForm(data=request.POST) if form.is_valid(): new_thread = form.create_thread(doge_user, category) return thread_redirect(new_thread.id) return HttpResponseRedirect(reverse('index'))
def forum(request, slug): """ Displays a list of threads within a forum. Threads are sorted by their sticky flag, followed by their most recent post. """ f = get_object_or_404(Forum, slug=slug) # If the user is not authorized to view the thread, then redirect if f.is_private and request.user.is_staff != True: return HttpResponseRedirect('/forum') # Process new thread form if data was sent if request.method == 'POST': if not request.user.is_authenticated(): return HttpResponseServerError() thread_form = ThreadForm(request.POST) if thread_form.is_valid(): new_thread = thread_form.save(commit=False) new_thread.forum = f new_thread.save() Post.objects.create(thread=new_thread, author=request.user, body=thread_form.cleaned_data['body'], time=datetime.now()) if (thread_form.cleaned_data['subscribe'] == True): Subscription.objects.create(author=request.user, thread=new_thread) return HttpResponseRedirect(new_thread.get_absolute_url()) else: thread_form = ThreadForm() # Pagination t = f.thread_set.all() paginator = Paginator(t, settings.FORUM_PAGINATE) try: page = int(request.GET.get('page', 1)) except: page = 1 try: threads = paginator.page(page) except (EmptyPage, InvalidPage): threads = paginator.page(paginator.num_pages) return j2shim.r2r( 'forum/thread_list.html', { 'forum': f, 'threads': threads.object_list, 'page_range': paginator.page_range, 'page': page, 'thread_form': thread_form }, request)
def forum(request, slug): """ Displays a list of threads within a forum. Threads are sorted by their sticky flag, followed by their most recent post. """ f = get_object_or_404(Forum, slug=slug) # If the user is not authorized to view the thread, then redirect if f.is_private and request.user.is_staff != True: return HttpResponseRedirect('/forum') # Process new thread form if data was sent if request.method == 'POST': if not request.user.is_authenticated(): return HttpResponseServerError() thread_form = ThreadForm(request.POST) if thread_form.is_valid(): new_thread = thread_form.save(commit = False) new_thread.forum = f new_thread.save() Post.objects.create(thread=new_thread, author=request.user, body=thread_form.cleaned_data['body'], time=datetime.now()) if (thread_form.cleaned_data['subscribe'] == True): Subscription.objects.create(author=request.user, thread=new_thread) return HttpResponseRedirect(new_thread.get_absolute_url()) else: thread_form = ThreadForm() # Pagination t = f.thread_set.all() paginator = Paginator(t, settings.FORUM_PAGINATE) try: page = int(request.GET.get('page', 1)) except: page = 1 try: threads = paginator.page(page) except (EmptyPage, InvalidPage): threads = paginator.page(paginator.num_pages) return j2shim.r2r('forum/thread_list.html', { 'forum': f, 'threads': threads.object_list, 'page_range': paginator.page_range, 'page': page, 'thread_form': thread_form }, request)
def index(request): if request.POST and request.user.is_authenticated(): form = ThreadForm(request.POST) if form.is_valid(): form.save(request.user) return HttpResponseRedirect(reverse('forum_index')) else: form = ThreadForm() return render_to_response(request, 'forum/index.html', {'threads': Thread.objects.all().order_by('-last_comment_date'), 'user': request.user.is_authenticated() and request.user or None, 'form': form, })
def new_thread(request, cat): if (request.method == "POST"): form = ThreadForm(request.POST) if form.is_valid(): obj = form.save(commit=False) obj.author = request.user obj.category = cat obj.save() return HttpResponseRedirect('/forum/cat-'+cat) else: error = True else: form = ThreadForm() return (render_to_response("form.html", RequestContext(request, {'form':form, 'title':"Create new thread"})))
def thread_dir(request, forum_id): thread_list = Thread.objects.filter( forum = forum_id ) forum_info = Forum.objects.get( id = forum_id ) if request.method == 'POST': # If the form has been submitted... form = ThreadForm(request.POST) # A form bound to the POST data if form.is_valid(): # All validation rules pass forum = Thread( forum = forum_info ) form = ThreadForm(request.POST, instance = forum ) form.save() return HttpResponseRedirect(reverse('forum.views.thread_dir', args=(forum_id,))) else: form = ThreadForm() # An unbound form return render(request, "thread.html", { 'thread_list' : thread_list, 'forum_info' : forum_info, 'form': form} )
def editThread(request, id): categories = Category.objects.all() if 'userId' in request.session: try: usr = request.session['userId'] us = User.objects.get(id=usr) thrd = Thread.objects.get(id=id) except User.DoesNotExist or Thread.DoesNotExist: return HttpResponseRedirect('/error/1/') else: return HttpResponse('/error/2/') form = ThreadForm(request.POST or None, instance=thrd) if form.is_valid(): form.save() return HttpResponseRedirect('/thread/' + id) else: return render_to_response('editThread.html', RequestContext(request, {'form': form, 'categories': categories}))
def new_thread(request, cat): if (request.method == "POST"): form = ThreadForm(request.POST) if form.is_valid(): obj = form.save(commit=False) obj.author = request.user obj.category = cat obj.save() return HttpResponseRedirect('/forum/cat-' + cat) else: error = True else: form = ThreadForm() return (render_to_response( "form.html", RequestContext(request, { 'form': form, 'title': "Create new thread" })))
def forum(request): last_five = Thread.objects.all().order_by('-id')[:5] if request.method == 'POST': form = ThreadForm(request.POST) if form.is_valid(): creator = form.cleaned_data['name'] text = form.cleaned_data['subject'] Thread(text=text, creator=creator).save() messages.success(request, 'Thanks for getting involved, dude') return redirect(reverse('forum:forum')) else: form = ThreadForm() cform = CommentForm() return render(request, 'forum/forum.html', { 'last_five': last_five, 'form': form, 'cform': cform })
def edit_thread(request, threadSlug): try: theThread = Thread.objects.get(slug=threadSlug) except Thread.DoesNotExist: raise Http404("Thread doesn't exist.") if request.user != theThread.user and not request.user.has_moderator_rights( ): return HttpResponseForbidden("Not your thread (and not a moderator).") if request.method == 'POST': form = ThreadForm(request.POST) if form.is_valid(): title = form.cleaned_data['title'] content = form.cleaned_data['content'] theThread.title = title theThread.text = content theThread.was_edited = True theThread.date_edited = timezone.localtime(timezone.now()) theThread.edited_by = request.user utilities.unique_slugify(theThread, title) theThread.save() return HttpResponseRedirect(theThread.get_url()) else: form = ThreadForm(initial={ 'title': theThread.title, 'content': theThread.text }) context = {'form': form, 'thread': theThread} return render(request, 'edit/edit_thread.html', context)
def new_thread(request): if request.method == "POST": thread_form = ThreadForm(request.POST) post_form = PostForm(request.POST) if thread_form.is_valid() and post_form.is_valid(): thread = thread_form.save() post = post_form.save(False) post.thread = thread post.save() # else: # thread_form = ThreadForm() # post_form = PostForm() # # # args = {} # args.update(csrf(request)) # args['thread_form'] = thread_form # args['post_form'] = post_form return render(request, 'index.html', locals())
def thread(request, forum, thread=None): instance = None if thread: instance = get_object_or_404(Thread, slug=thread, forum__slug=forum, forum__site=settings.SITE_ID) f_instance = get_object_or_404(Forum, slug=forum, site=settings.SITE_ID) def can_post(forum, user): if forum.only_staff_posts: return user.is_authenticated() and user.is_staff if forum.only_upgraders: return user.is_authenticated() and (user.is_staff or (hasattr(user, 'userprofile') and getattr(user.userprofile, 'is_upgraded', False))) return user.is_authenticated() if not can_post(f_instance, request.user): return HttpResponseForbidden() if not Forum.objects.has_access(f_instance, request.user): return HttpResponseForbidden() if not request.user.is_authenticated(): if instance and instance.comment and instance.comment.user != request.user: return HttpResponseForbidden() form = ThreadForm(request.POST or None, instance=instance, user=request.user, forum=f_instance) # If previewing, render preview and form. if "preview" in request.POST: if form.is_valid(): data = form.cleaned_data else: data = form.data return render( request, 'forum/previewthread.html', { 'form': form, 'thread': Thread( title=data.get('title') or form.initial.get('title') or '', forum=f_instance), 'forum': f_instance, 'instance': instance, 'comment': data.get('body') or form.initial.get('body') or '', }) if request.method == "POST" and form.is_valid(): if not thread: 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_instance.title, expiry)) return HttpResponseRedirect(post_url) instance = form.save() if not thread: Thread.nonrel_objects.push_to_list('%s-latest-comments' % f_instance.slug, instance, trim=30) thread_created.send(sender=Thread, instance=instance, author=request.user) return HttpResponseRedirect(instance.get_absolute_url()) if hasattr(form, 'cleaned_data'): preview_comment = form.cleaned_data.get('body') else: preview_comment = form.data.get('body') or form.initial.get('body') or '' preview_instance = None if not instance: if hasattr(form, 'cleaned_data'): title = form.cleaned_data.get('title') or '' else: title = form.data.get('title') or form.initial.get('title') or '' preview_instance = Thread( title=title, forum=f_instance) return render( request, 'forum/previewthread.html', { 'form': form, 'thread': preview_instance or instance, 'forum': f_instance, 'comment': preview_comment or '', })
def test_item_topic_is_required(self): """ test topic field is required """ form = ThreadForm({'topic': ''}) self.assertFalse(form.is_valid()) self.assertIn('topic', form.errors.keys()) self.assertEqual(form.errors['topic'][0], 'This field is required.')
def new_thread(request, subject_id, poll): subject = get_object_or_404(Subject, pk=subject_id) is_a_poll = False if poll == "poll": is_a_poll = True poll_subject_formset = formset_factory(PollSubjectForm, extra=3) if request.method == "POST": thread_form = ThreadForm(request.POST) post_form = PostForm(request.POST) if is_a_poll: poll_subject_formset = poll_subject_formset(request.POST) poll_form = PollForm(request.POST) if thread_form.is_valid() and post_form.is_valid() and poll_form.is_valid() \ and poll_subject_formset.is_valid(): thread = thread_form.save(False) thread.subject = subject thread.user = request.user thread.save() post = post_form.save(False) post.user = request.user post.thread = thread post.save() poll = poll_form.save(False) poll.thread = thread poll.save() for subject_form in poll_subject_formset: subject = subject_form.save(False) subject.poll = poll subject.save() messages.success(request, "You have created a new poll") else: if thread_form.is_valid() and post_form.is_valid(): thread = thread_form.save(False) thread.subject = subject thread.user = request.user thread.save() post = post_form.save(False) post.user = request.user post.thread = thread post.save() messages.success(request, "You have created a new topic") return redirect(reverse('forum_thread', args={thread.pk})) else: thread_form = ThreadForm() post_form = PostForm(request.POST) args = { 'thread_form': thread_form, 'post_form': post_form, 'subject': subject } args.update(csrf(request)) if is_a_poll: poll_form = PollForm() # poll_subject_formset = poll_subject_formset() args['poll_form'] = poll_form args['poll_subject_formset'] = poll_subject_formset return render(request, 'forum/topic_poll_form.html', args) else: return render(request, 'forum/topic_form.html', args)