def index(request): form = None if request.method == 'POST': if request.GET.get(SEARCH_PARAM) == "false": reqForm = MessageForm(request.POST) if request.user.is_authenticated() and reqForm.is_valid(): msg = Message(date=timezone.now(), title=reqForm.cleaned_data["title"], content=reqForm.cleaned_data["content"], author=request.user) msg.save() __clean_session(request) return redirect(reverse('board:index')) else: form = reqForm else: reqForm = SearchForm(request.POST) if reqForm.is_valid(): request.session[SEARCH_SESSION] = reqForm.cleaned_data return redirect(reverse('board:index')) if form is None: form = MessageForm() filterContent, filterAuthor = __getFilters(request) searchForm = SearchForm(initial={'search': filterContent, 'searchAuthor': filterAuthor }) dbMessages = Message.objects.filter((Q(title__icontains=filterContent) | Q(content__icontains=filterContent)) & Q(author__username__icontains=filterAuthor)).order_by('-date') messages = __get_paginated_objects(dbMessages , request.GET.get('page'), ITEMS_PER_PAGE) context = {'messages': messages, 'form': form, 'auth': request.user.is_authenticated(), 'search_form':searchForm} return render(request, 'board/index.html', context)
def submit_message(cls, request, board_id, topic_id): # DZIALA if request.method == 'POST': message_form = MessageForm(request.POST) if message_form.is_valid(): content = request.POST['content'] submitted_by = request.user topic = Topic.objects.get(pk=topic_id) submission_date = datetime.now() submitted_by = ShopUser.objects.get(user__pk=request.user.pk) new_message = Message(topic=topic, submitted_by=submitted_by, submission_date=submission_date, content=content) #new_message=Message(topic=topic, submitted_by=submitted_by, submission_date=submission_date, content=content) new_message.save() return HttpResponseRedirect("/forum/%s/%s/" % (board_id, topic_id)) else: message_form = MessageForm() topic = Topic.objects.get(pk=topic_id) board = Board.objects.get(pk=board_id) return render_to_response('new_message.html', { 'message_form': message_form, 'board': board, 'topic': topic }, context_instance=RequestContext(request))
def comment(cls, request, id): if request.method == "POST": form = MessageForm(request.POST) if form.is_valid(): content = form.cleaned_data["content"] try: topic = Topic.objects.get(board__name=str(cls.model.__name__), title=str(id)) except Exception: board = Board.objects.get(name=str(cls.model.__name__)) title = str(id) date = datetime.now() created_by = ShopUser.objects.get(user__pk=request.user.pk) topic = Topic(title=title, board=board, created_by=created_by, date=date, is_active=True) topic.save() submission_date = datetime.now() submitted_by = ShopUser.objects.get(user__pk=request.user.pk) new_message = Message(topic=topic, submitted_by=submitted_by, submission_date=submission_date, content=content) #new_message=Message(topic=topic, submitted_by=submitted_by, submission_date=submission_date, content=content) new_message.save() topic.messages.add(new_message) topic.save() return cls.show_item(request, id) return cls.show_item(request, id)
def test_deletemessage(self): # Log as admin self.client.login(username='******', password='******') user1 = ActiveUser.objects.get(username='******') user2 = ActiveUser.objects.get(username='******') # Create dummy thread thread = Thread(title='Hello World!') thread.save() msg1 = Message(author=user1, thread=thread, text='Hello 1') msg2 = Message(author=user1, thread=thread, text='Hello 2') msg1.save() msg2.save() # Add flags Flag.objects.read(user=user1, message=msg1) Flag.objects.read(user=user2, message=msg2) thread.refresh_from_db() self.assertEqual(thread.number, 2) self.assertEqual(thread.last_message, msg2) self.assertEqual(Flag.objects.get(user=user1, thread=thread).message, msg1) self.assertEqual(Flag.objects.get(user=user2, thread=thread).message, msg2) # Remove second message response = self.client.get(reverse('board_message_delete', kwargs={'message': msg2.pk}), follow=True) self.assertEqual(response.status_code, 200) self.assertEqual(Flag.objects.get(user=user1, thread=thread).message, msg1) self.assertEqual(Flag.objects.get(user=user2, thread=thread).message, msg1) thread.refresh_from_db() self.assertEqual(thread.number, 1) self.assertEqual(thread.last_message, msg1) with self.assertRaises(Message.DoesNotExist): Message.objects.get(pk=msg2.pk) # Remove first (and last) message response = self.client.get(reverse('board_message_delete', kwargs={'message': msg1.pk})) self.assertRedirects(response, reverse('board_latests')) with self.assertRaises(Flag.DoesNotExist): Flag.objects.get(user=user1, thread=thread) with self.assertRaises(Flag.DoesNotExist): Flag.objects.get(user=user2, thread=thread) with self.assertRaises(Message.DoesNotExist): Message.objects.get(pk=msg1.pk) with self.assertRaises(Thread.DoesNotExist): Thread.objects.get(pk=thread.pk)
def index(request): form = None if request.method == 'POST': if request.GET.get(SEARCH_PARAM) == "false": reqForm = MessageForm(request.POST) if request.user.is_authenticated() and reqForm.is_valid(): msg = Message(date=timezone.now(), title=reqForm.cleaned_data["title"], content=reqForm.cleaned_data["content"], author=request.user) msg.save() __clean_session(request) return redirect(reverse('board:index')) else: form = reqForm else: reqForm = SearchForm(request.POST) if reqForm.is_valid(): request.session[SEARCH_SESSION] = reqForm.cleaned_data return redirect(reverse('board:index')) if form is None: form = MessageForm() filterContent, filterAuthor = __getFilters(request) searchForm = SearchForm(initial={ 'search': filterContent, 'searchAuthor': filterAuthor }) dbMessages = Message.objects.filter( (Q(title__icontains=filterContent) | Q(content__icontains=filterContent)) & Q(author__username__icontains=filterAuthor)).order_by('-date') messages = __get_paginated_objects(dbMessages, request.GET.get('page'), ITEMS_PER_PAGE) context = { 'messages': messages, 'form': form, 'auth': request.user.is_authenticated(), 'search_form': searchForm } return render(request, 'board/index.html', context)
def submit_message(cls, request, board_id, topic_id): # DZIALA if request.method=='POST': message_form = MessageForm(request.POST) if message_form.is_valid(): content = request.POST['content'] submitted_by = request.user topic = Topic.objects.get(pk=topic_id) submission_date = datetime.now() submitted_by=ShopUser.objects.get(user__pk=request.user.pk) new_message = Message(topic=topic, submitted_by=submitted_by, submission_date=submission_date, content=content) #new_message=Message(topic=topic, submitted_by=submitted_by, submission_date=submission_date, content=content) new_message.save() return HttpResponseRedirect("/forum/%s/%s/" % (board_id, topic_id)) else: message_form = MessageForm() topic = Topic.objects.get(pk=topic_id) board=Board.objects.get(pk=board_id) return render_to_response('new_message.html', {'message_form': message_form, 'board': board, 'topic': topic}, context_instance=RequestContext(request))
class APITests(TestCase): fixtures = ['devel'] def setUp(self): user = ActiveUser.objects.get(username='******') # Create dummy thread thread = Thread(title='Hello World!') thread.save() self.msg1 = Message(author=user, thread=thread, text='Hello 1') self.msg2 = Message(author=user, thread=thread, text='Hello 2') self.msg1.save() self.msg2.save() def test_message_detail(self): response = self.client.get(reverse('board_api_message-detail', kwargs={'pk': self.msg1.pk}), format='json') self.assertEqual(response.status_code, 200) self.assertEqual(response.data['text'], 'Hello 1') self.assertEqual(response.data['author']['username'], 'admin') def test_invalid_message_detail(self): response = self.client.get(reverse('board_api_message-detail', kwargs={'pk': -1}), format='json') self.assertEqual(response.status_code, 404)