def test_non_existant_setting(self): user = User.objects.all()[0] form = SettingsForm() bad_setting = 'doesnt_exist' assert bad_setting not in form.fields.keys() with self.assertRaises(KeyError): Setting.get_for_user(user, bad_setting)
def reply(request, document_slug, thread_id): """Reply to a thread.""" doc = get_document(document_slug, request) form = ReplyForm(request.POST) post_preview = None if form.is_valid(): thread = get_object_or_404(Thread, pk=thread_id, document=doc) if not thread.is_locked: reply_ = form.save(commit=False) reply_.thread = thread reply_.creator = request.user if 'preview' in request.POST: post_preview = reply_ else: reply_.save() statsd.incr('kbforums.reply') # Subscribe the user to the thread. if Setting.get_for_user(request.user, 'kbforums_watch_after_reply'): NewPostEvent.notify(request.user, thread) # Send notifications to thread/forum watchers. NewPostEvent(reply_).fire(exclude=reply_.creator) return HttpResponseRedirect(reply_.get_absolute_url()) return posts(request, document_slug, thread_id, form, post_preview)
def test_default_values(self): eq_(0, Setting.objects.count()) user = User.objects.get(username='******') keys = SettingsForm.base_fields.keys() for setting in keys: field = SettingsForm.base_fields[setting] eq_(field.initial, Setting.get_for_user(user, setting))
def new_thread(request, document_slug): """Start a new thread.""" doc = get_document(document_slug, request) if request.method == "GET": form = NewThreadForm() return jingo.render(request, "kbforums/new_thread.html", {"form": form, "document": doc}) form = NewThreadForm(request.POST) post_preview = None if form.is_valid(): if "preview" in request.POST: thread = Thread(creator=request.user, title=form.cleaned_data["title"]) post_preview = Post(thread=thread, creator=request.user, content=form.cleaned_data["content"]) else: thread = doc.thread_set.create(creator=request.user, title=form.cleaned_data["title"]) thread.save() statsd.incr("kbforums.thread") post = thread.new_post(creator=request.user, content=form.cleaned_data["content"]) post.save() # Send notifications to forum watchers. NewThreadEvent(post).fire(exclude=post.creator) # Add notification automatically if needed. if Setting.get_for_user(request.user, "kbforums_watch_new_thread"): NewPostEvent.notify(request.user, thread) return HttpResponseRedirect(reverse("wiki.discuss.posts", args=[document_slug, thread.id])) return jingo.render( request, "kbforums/new_thread.html", {"form": form, "document": doc, "post_preview": post_preview} )
def test_private_message_sends_email(self, get_current): """ With the setting enabled and receiving a private message should send and email. """ get_current.return_value.domain = 'testserver' to = User.objects.all()[1] s, c = Setting.objects.get_or_create(user=to, name='email_private_messages') s.value = True s.save() # User has setting, and should recieve notification email. assert Setting.get_for_user(to, 'email_private_messages') self.client.login(username='******', password='******') res = post(self.client, 'messages.new', { 'to': to, 'message': 'a message' }) subject = u'[SUMO] You have a new private message from [{sender}]' attrs_eq(mail.outbox[0], to=[to.email], subject=subject.format(sender='jsocol')) starts_with(mail.outbox[0].body, PRIVATE_MESSAGE_EMAIL.format(sender='jsocol'))
def test_private_message_sends_email(self, get_current): """ With the setting enabled and receiving a private message should send and email. """ get_current.return_value.domain = 'testserver' to = User.objects.all()[1] s, c = Setting.objects.get_or_create(user=to, name='email_private_messages') s.value = True s.save() # User has setting, and should recieve notification email. assert Setting.get_for_user(to, 'email_private_messages') self.client.login(username='******', password='******') res = post(self.client, 'messages.new', {'to': to, 'message': 'a message'}) subject = u'[SUMO] You have a new private message from [{sender}]' attrs_eq(mail.outbox[0], to=[to.email], subject=subject.format(sender='jsocol')) starts_with(mail.outbox[0].body, PRIVATE_MESSAGE_EMAIL.format(sender='jsocol'))
def test_view_activities(self): from users.forms import SettingsForm from users.models import Setting request = self.factory.get(reverse('lets')) request.user = self.u views.activities(request) eq_('lets', Setting.get_for_user(self.u, 'home_tab')) form = SettingsForm(data={'home_tab': 'stream'}) form.is_valid() form.save_for_user(request.user) eq_('stream', Setting.get_for_user(self.u, 'home_tab')) views.activities(request) eq_('lets', Setting.get_for_user(self.u, 'home_tab'))
def test_create_setting(self): url = reverse('users.edit_settings', locale='en-US') eq_(Setting.objects.filter(user=self.user).count(), 0) # No settings res = self.client.get(url, follow=True) eq_(200, res.status_code) res = self.client.post(url, {'forums_watch_new_thread': True}, follow=True) eq_(200, res.status_code) assert Setting.get_for_user(self.user, 'forums_watch_new_thread')
def test_create_setting(self): url = reverse('users.edit_settings', locale='en-US') eq_(Setting.objects.filter(user=self.u).count(), 0) # No settings res = self.client.get(url, follow=True) eq_(200, res.status_code) res = self.client.post(url, {'forums_watch_new_thread': True}, follow=True) eq_(200, res.status_code) assert Setting.get_for_user(self.u, 'forums_watch_new_thread')
def new_thread(request, forum_slug): """Start a new thread.""" forum = get_object_or_404(Forum, slug=forum_slug) user = request.user if not forum.allows_posting_by(user): if forum.allows_viewing_by(user): raise PermissionDenied else: raise Http404 if request.method == 'GET': form = NewThreadForm() return render(request, 'forums/new_thread.html', { 'form': form, 'forum': forum }) form = NewThreadForm(request.POST) post_preview = None if form.is_valid(): if 'preview' in request.POST: thread = Thread(creator=request.user, title=form.cleaned_data['title']) post_preview = Post(thread=thread, author=request.user, content=form.cleaned_data['content']) post_preview.author_post_count = \ post_preview.author.post_set.count() else: thread = forum.thread_set.create(creator=request.user, title=form.cleaned_data['title']) thread.save() statsd.incr('forums.thread') post = thread.new_post(author=request.user, content=form.cleaned_data['content']) post.save() NewThreadEvent(post).fire(exclude=post.author) # Add notification automatically if needed. if Setting.get_for_user(request.user, 'forums_watch_new_thread'): NewPostEvent.notify(request.user, thread) url = reverse('forums.posts', args=[forum_slug, thread.id]) return HttpResponseRedirect(urlparams(url, last=post.id)) return render(request, 'forums/new_thread.html', { 'form': form, 'forum': forum, 'post_preview': post_preview })
def send_message(to, text, sender=None): """Send a private message. :arg to: a list of Users to send the message to :arg sender: the User who is sending the message :arg text: the message text """ if sender: msg = OutboxMessage.objects.create(sender=sender, message=text) msg.to.add(*to) for user in to: im = InboxMessage.objects.create(sender=sender, to=user, message=text) if Setting.get_for_user(user, 'email_private_messages'): email_private_message(inbox_message_id=im.id) message_sent.send(sender=InboxMessage, to=to, text=text, msg_sender=sender)
def new_thread(request, forum_slug): """Start a new thread.""" forum = get_object_or_404(Forum, slug=forum_slug) user = request.user if not forum.allows_posting_by(user): if forum.allows_viewing_by(user): raise PermissionDenied else: raise Http404 if request.method == 'GET': form = NewThreadForm() return jingo.render(request, 'forums/new_thread.html', {'form': form, 'forum': forum}) form = NewThreadForm(request.POST) post_preview = None if form.is_valid(): if 'preview' in request.POST: thread = Thread(creator=request.user, title=form.cleaned_data['title']) post_preview = Post(thread=thread, author=request.user, content=form.cleaned_data['content']) post_preview.author_post_count = \ post_preview.author.post_set.count() else: thread = forum.thread_set.create(creator=request.user, title=form.cleaned_data['title']) thread.save() statsd.incr('forums.thread') post = thread.new_post(author=request.user, content=form.cleaned_data['content']) post.save() NewThreadEvent(post).fire(exclude=post.author) # Add notification automatically if needed. if Setting.get_for_user(request.user, 'forums_watch_new_thread'): NewPostEvent.notify(request.user, thread) url = reverse('forums.posts', args=[forum_slug, thread.id]) return HttpResponseRedirect(urlparams(url, last=post.id)) return jingo.render(request, 'forums/new_thread.html', {'form': form, 'forum': forum, 'post_preview': post_preview})
def activities(request, category=None, home_tab=None): search_lets = get_relevant_lets( request.user, page=request.GET.get('page'), category=request.GET.get('category')) pages = paginate(request, search_lets['ids'], lcs.LETS_PER_PAGE) data = {'lets': search_lets['results'], 'catg': CATEGORY_CHOICES, 'pages': pages, 'profile': request.user.profile} if not home_tab: from users.models import Setting home_tab = Setting.get_for_user(request.user, 'home_tab') if home_tab != 'lets': from users.forms import SettingsForm form = SettingsForm(data={'home_tab': 'lets'}) form.is_valid() form.save_for_user(request.user) return jingo.render(request, 'lets/activities.html', data)
def new_thread(request, document_slug): """Start a new thread.""" doc = get_document(document_slug, request) if request.method == 'GET': form = NewThreadForm() return render(request, 'kbforums/new_thread.html', { 'form': form, 'document': doc }) form = NewThreadForm(request.POST) post_preview = None if form.is_valid(): if 'preview' in request.POST: thread = Thread(creator=request.user, title=form.cleaned_data['title']) post_preview = Post(thread=thread, creator=request.user, content=form.cleaned_data['content']) else: thread = doc.thread_set.create(creator=request.user, title=form.cleaned_data['title']) thread.save() statsd.incr('kbforums.thread') post = thread.new_post(creator=request.user, content=form.cleaned_data['content']) post.save() # Send notifications to forum watchers. NewThreadEvent(post).fire(exclude=post.creator) # Add notification automatically if needed. if Setting.get_for_user(request.user, 'kbforums_watch_new_thread'): NewPostEvent.notify(request.user, thread) return HttpResponseRedirect( reverse('wiki.discuss.posts', args=[document_slug, thread.id])) return render(request, 'kbforums/new_thread.html', { 'form': form, 'document': doc, 'post_preview': post_preview })
def reply(request, question_id): """Post a new answer to a question.""" question = get_object_or_404(Question, pk=question_id) answer_preview = None if question.is_locked: raise PermissionDenied form = AnswerForm(request.POST) # NOJS: delete images if 'delete_images' in request.POST: for image_id in request.POST.getlist('delete_image'): ImageAttachment.objects.get(pk=image_id).delete() return answers(request, question_id=question_id, form=form) # NOJS: upload image if 'upload_image' in request.POST: upload_imageattachment(request, question) return answers(request, question_id=question_id, form=form) if form.is_valid(): answer = Answer(question=question, creator=request.user, content=form.cleaned_data['content']) if 'preview' in request.POST: answer_preview = answer else: answer.save() ct = ContentType.objects.get_for_model(answer) # Move over to the answer all of the images I added to the # reply form up_images = question.images.filter(creator=request.user) up_images.update(content_type=ct, object_id=answer.id) statsd.incr('questions.answer') if Setting.get_for_user(request.user, 'questions_watch_after_reply'): QuestionReplyEvent.notify(request.user, question) return HttpResponseRedirect(answer.get_absolute_url()) return answers(request, question_id=question_id, form=form, answer_preview=answer_preview)
def test_private_message_not_sends_email(self, get_current): """ With the setting not enabled and receiving a private message. The user should not get an email. """ get_current.return_value.domain = 'testserver' s, c = Setting.objects.get_or_create(user=self.to, name='email_private_messages') # Now user should not recieve email. s.value = False s.save() assert not Setting.get_for_user(self.to, 'email_private_messages') self.client.login(username=self.sender.username, password='******') post(self.client, 'messages.new', {'to': self.to, 'message': 'a message'}) assert not mail.outbox
def reply(request, forum_slug, thread_id): """Reply to a thread.""" forum = get_object_or_404(Forum, slug=forum_slug) user = request.user if not forum.allows_posting_by(user): if forum.allows_viewing_by(user): raise PermissionDenied else: raise Http404 form = ReplyForm(request.POST) post_preview = None if form.is_valid(): thread = get_object_or_404(Thread, pk=thread_id, forum=forum) if not thread.is_locked: reply_ = form.save(commit=False) reply_.thread = thread reply_.author = request.user if 'preview' in request.POST: post_preview = reply_ post_preview.author_post_count = \ reply_.author.post_set.count() else: reply_.save() statsd.incr('forums.reply') # Subscribe the user to the thread. if Setting.get_for_user(request.user, 'forums_watch_after_reply'): NewPostEvent.notify(request.user, thread) # Send notifications to thread/forum watchers. NewPostEvent(reply_).fire(exclude=reply_.author) return HttpResponseRedirect(thread.get_last_post_url()) return posts(request, forum_slug, thread_id, form, post_preview, is_reply=True)
def reply(request, question_id): """Post a new answer to a question.""" question = get_object_or_404(Question, pk=question_id) answer_preview = None if question.is_locked: raise PermissionDenied form = AnswerForm(request.POST) # NOJS: delete images if 'delete_images' in request.POST: for image_id in request.POST.getlist('delete_image'): ImageAttachment.objects.get(pk=image_id).delete() return answers(request, question_id, form) # NOJS: upload image if 'upload_image' in request.POST: upload_imageattachment(request, question) return answers(request, question_id, form) if form.is_valid(): answer = Answer(question=question, creator=request.user, content=form.cleaned_data['content']) if 'preview' in request.POST: answer_preview = answer else: answer.save() ct = ContentType.objects.get_for_model(answer) # Move over to the answer all of the images I added to the # reply form up_images = question.images.filter(creator=request.user) up_images.update(content_type=ct, object_id=answer.id) statsd.incr('questions.answer') if Setting.get_for_user(request.user, 'questions_watch_after_reply'): QuestionReplyEvent.notify(request.user, question) return HttpResponseRedirect(answer.get_absolute_url()) return answers(request, question_id, form, answer_preview=answer_preview)
def test_private_message_not_sends_email(self, get_current): """ With the setting not enabled and receiving a private message. The user should not get an email. """ get_current.return_value.domain = 'testserver' s, c = Setting.objects.get_or_create(user=self.to, name='email_private_messages') # Now user should not recieve email. s.value = False s.save() assert not Setting.get_for_user(self.to, 'email_private_messages') self.client.login(username=self.sender.username, password='******') post(self.client, 'messages.new', { 'to': self.to, 'message': 'a message' }) assert not mail.outbox
def test_private_message_not_sends_email(self, get_current): """ With the setting not enabled and receiving a private message. The user should not get an email. """ get_current.return_value.domain = 'testserver' to = User.objects.all()[1] s, c = Setting.objects.get_or_create(user=to, name='email_private_messages') # Now usershould not recieve email. s.value = False s.save() assert not Setting.get_for_user(to, 'email_private_messages') self.client.login(username='******', password='******') res = post(self.client, 'messages.new', {'to': to, 'message': 'a message'}) subject = u'You have a new private message from [{sender}]' assert not mail.outbox
def new_thread(request, document_slug): """Start a new thread.""" doc = get_document(document_slug, request) if request.method == 'GET': form = NewThreadForm() return render(request, 'kbforums/new_thread.html', { 'form': form, 'document': doc}) form = NewThreadForm(request.POST) post_preview = None if form.is_valid(): if 'preview' in request.POST: thread = Thread(creator=request.user, title=form.cleaned_data['title']) post_preview = Post(thread=thread, creator=request.user, content=form.cleaned_data['content']) else: thread = doc.thread_set.create(creator=request.user, title=form.cleaned_data['title']) thread.save() statsd.incr('kbforums.thread') post = thread.new_post(creator=request.user, content=form.cleaned_data['content']) post.save() # Send notifications to forum watchers. NewThreadEvent(post).fire(exclude=post.creator) # Add notification automatically if needed. if Setting.get_for_user(request.user, 'kbforums_watch_new_thread'): NewPostEvent.notify(request.user, thread) return HttpResponseRedirect( reverse('wiki.discuss.posts', args=[document_slug, thread.id])) return render(request, 'kbforums/new_thread.html', { 'form': form, 'document': doc, 'post_preview': post_preview})
def test_private_message_not_sends_email(self, get_current): """ With the setting not enabled and receiving a private message. The user should not get an email. """ get_current.return_value.domain = 'testserver' to = User.objects.all()[1] s, c = Setting.objects.get_or_create(user=to, name='email_private_messages') # Now usershould not recieve email. s.value = False s.save() assert not Setting.get_for_user(to, 'email_private_messages') self.client.login(username='******', password='******') res = post(self.client, 'messages.new', { 'to': to, 'message': 'a message' }) subject = u'You have a new private message from [{sender}]' assert not mail.outbox
def test_non_existant_setting(self): form = SettingsForm() bad_setting = 'doesnt_exist' assert bad_setting not in form.fields.keys() with self.assertRaises(KeyError): Setting.get_for_user(self.u, bad_setting)
def test_default_values(self): eq_(0, Setting.objects.count()) keys = SettingsForm.base_fields.keys() for setting in keys: field = SettingsForm.base_fields[setting] eq_(field.initial, Setting.get_for_user(self.u, setting))
def test_non_existant_setting(self): form = SettingsForm() bad_setting = "doesnt_exist" assert bad_setting not in form.fields.keys() with self.assertRaises(KeyError): Setting.get_for_user(self.u, bad_setting)