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 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 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 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 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 index(): form = HelloForm() if form.validate_on_submit(): name = form.name.data body = form.body.data message = Message(body=body, name=name) db.session.add(message) db.session.commit() flash('Your message have been sent to the world!') return redirect(url_for('index')) messages = Message.query.order_by(Message.timestamp.desc()).all() return render_template('index.html', form=form, messages=messages)
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)
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 forge(count): """Generate fake messages.""" from faker import Faker db.drop_all() db.create_all() fake = Faker() click.echo('Working...') for i in range(count): message = Message( name=fake.name(), body=fake.sentence(), timestamp=fake.date_time_this_year() ) db.session.add(message) db.session.commit() click.echo('Created %d fake messages.' % count)
def create_new_messages(): new_messages = set() id_counter = 1 for message in UNCOMBINED_MESSAGES: new_messages.add( Message(id=id_counter, parent_msg_id=id_counter, author=message.id_member, text=message.body, date_created=message.postertime, subject=message.subject, topic=message.id_topic, board=message.id_board)) id_counter += 1 teacher_message_marker = r'(?<=\[color=blue]).+?(?=\[/color])|' \ r'(?<=\[color=navy]).+?(?=\[/color])|' \ r'(?<=\[color=green]).+?(?=\[/color])' for message in COMBINED_MESSAGES: student_begins = not bool( re.match(r'\[color=blue]|\[color=navy]|\[color=green]', message.body)) student_messages = re.split(teacher_message_marker, message.body) for i in range(len(student_messages)): if re.match(r'\[/color]', student_messages[i]): student_messages[i] = student_messages[i][8:] if re.search(r'\[color=blue]$|\[color=navy]$', student_messages[i]): student_messages[i] = student_messages[i][:-12] if re.search(r'\[color=green]$', student_messages[i]): student_messages[i] = student_messages[i][:-13] teacher_messages = re.findall(teacher_message_marker, message.body) splitted_messages = list() last_index = 0 for i in range(min(len(teacher_messages), len(student_messages))): if student_begins: splitted_messages.append(student_messages[i]) splitted_messages.append(teacher_messages[i]) else: splitted_messages.append(teacher_messages[i]) splitted_messages.append(student_messages[i]) last_index = i if len(student_messages) > last_index + 1: splitted_messages.extend(student_messages[last_index + 1:]) if len(teacher_messages) > last_index + 1: splitted_messages.extend(teacher_messages[last_index + 1:]) while '' in splitted_messages: splitted_messages.remove('') for i in range(len(splitted_messages)): if splitted_messages[ i] in teacher_messages and message.modifiedname != '': id_member = TashkentMember.objects.get( realname=message.modifiedname) else: id_member = message.id_member new_messages.add( Message(id=id_counter, parent_msg_id=id_counter - i, author=id_member, text=splitted_messages[i], date_created=message.postertime + i, subject=message.subject, topic=message.id_topic, board=message.id_board)) id_counter += 1 Message.objects.bulk_create(new_messages)