def reply(request, thread_id): p = get_object_or_404(Thread, pk=thread_id) try: text = request.POST['text'] if text and len(text) > 5 or len(text) > 2000: # Check the captcha length a = request.POST['a'] b = request.POST['b'] value = request.POST['captcha'] if int(a) + int(b) == int(value or 0): date = timezone.now() post = Post(thread=p, post_text=text, pub_date=date) post.save() p.thread_count += 1 p.save() else: return render(request, 'board/thread.html', { 'thread': p, 'error_message': "Bad reply length", }) except (KeyError): return render(request, 'board/thread.html', { 'thread': p, 'error_message': "Reply can't be empty", }) else: return HttpResponseRedirect(reverse('thread', args=(p.id, )))
def __init__(self, body, created, topic, author): p = Post() p.body = body p.created = created p.topic_id = topic p.author_id = author p.save()
def test_saving_and_retrieving_post(self): first_post = Post() first_post.board = self.default_board first_post.title = 'first post of title' first_post.content = 'first post of content' first_post.ip = '127.0.0.1' first_post.save() second_post = Post() second_post.board = self.default_board second_post.title = 'second post of title' second_post.content = 'second post of content' second_post.ip = '127.0.0.1' second_post.save() saved_posts = Post.objects.all() self.assertEqual(saved_posts.count(), 2) first_saved_post = saved_posts[0] second_saved_post = saved_posts[1] self.assertEqual(first_saved_post.title, 'first post of title') self.assertEqual(first_saved_post.content, 'first post of content') self.assertEqual(first_saved_post.ip, '127.0.0.1') self.assertEqual(second_saved_post.title, 'second post of title') self.assertEqual(second_saved_post.content, 'second post of content')
def reply(request, thread_id): p = get_object_or_404(Thread, pk=thread_id) try: text = request.POST['text'] if text and len(text) > 5 or len(text) > 2000: # Check the captcha length a = request.POST['a'] b = request.POST['b'] value = request.POST['captcha'] if int(a) + int(b) == int(value or 0): date = timezone.now() post = Post(thread=p, post_text=text, pub_date=date) post.save() p.thread_count += 1 p.save() else: return render(request, 'board/thread.html', { 'thread': p, 'error_message': "Bad reply length", }) except (KeyError): return render(request, 'board/thread.html', { 'thread': p, 'error_message': "Reply can't be empty", }) else: return HttpResponseRedirect(reverse('thread', args=(p.id,)))
def test_post_creation_combinations(self): """Make sure that creating posts in different combinations doesn't break anything.""" i1 = Image(name="1", photo=bytearray("1", "utf-8"), uuid=uuid.uuid4()) i2 = Image(name="2", photo=bytearray("2", "utf-8")) i3 = Image(name="3", photo=bytearray("3", "utf-8")) i4 = Image(name="2", photo=bytearray("2", "utf-8")) i5 = Image(name="3", photo=bytearray("3", "utf-8")) i1.save() i2.save() i3.save() i4.save() i5.save() b1 = Board(title="foo", description="bar", bg=i1) b2 = Board(title="green eggs", description="and ham") b1.save() b2.save() # just make sure that none of these throw an exception when creating p1 = Post(associated_board=b1, message="post1") p2 = Post(associated_board=b1, name="author2", photo=i2) p3 = Post(associated_board=b1, photo=i3) p4 = Post(associated_board=b1, name="author4", message="post4", photo=i4) p5 = Post(associated_board=b1, message="post5", photo=i5) p1.save() p2.save() p3.save() p4.save() p5.save()
def test_cannot_save_empty_content_post(self): post = Post() post.board = self.default_board post.title = 'This is a title' post.content = '' with self.assertRaises(ValidationError): post.save() post.full_clean()
def show_board(request): if request.method == "POST": p = Post() p.text = request.POST["text"] p.pub_date = datetime.now() p.save() return HttpResponseRedirect(reverse("show_board")) else: posts = Post.objects.order_by("-pub_date") return render(request, "index.html", {"title": "Hi there", "posts": posts})
def test_post_save(self): post = Post() post.user = User.objects.get(pk=1) post.title = '제목' post.content = '내용' post.tags = 'abc' post.status = 'd' post.save() saved_post = Post.objects.first() self.assertEqual(saved_post, post)
def add_posts(self, board, amount): """Return a list of posts that are added to a board.""" posts = [] for i in range(amount): p = Post(associated_board=board, message='hi', name=str(i), photo=Image(name=f'photo{i}', photo=bytearray('hi', 'utf-8')).save(), created_at=timezone.now() + timezone.timedelta(seconds=i)) p.save() posts.append(p) return posts
def test_is_delete_change_to_True_after_delete_post(self): delete_post = Post() delete_post.board = self.default_board delete_post.title = 'post of title' delete_post.content = 'post of content' delete_post.ip = '192.168.0.1' delete_post.save() self.assertEqual(delete_post.is_deleted, False) self.client.post(reverse('board:delete_post', args=[delete_post.id])) delete_post.refresh_from_db() self.assertEqual(delete_post.is_deleted, True)
def test_board_post_connections(self): """Make sure that posts to a board are properly associated and posts can be found.""" i1 = Image(name="1", photo=bytearray("1", "utf-8")) i2 = Image(name="2", photo=bytearray("2", "utf-8")) i3 = Image(name="3", photo=bytearray("3", "utf-8")) i1.save() i2.save() i3.save() b1 = Board(title="foo", description="bar", bg=i1) b2 = Board(title="foo2", description="bar", bg=i2) b1.save() b2.save() p1 = Post(associated_board=b1, name="a", message="b") p2 = Post(associated_board=b1, name="a", photo=i3) p3 = Post(associated_board=b2, name="a", message="b") p4 = Post(associated_board=b1, name="a", message="b") p1.save() p2.save() p3.save() p4.save() self.assertQuerysetEqual(b1.post_set.all(), [p1, p2, p4], ordered=False) self.assertQuerysetEqual(b2.post_set.all(), [p3], ordered=False)
def create_thread(request): if request.method == 'POST': title = request.POST['title'] text = request.POST['text'] name = request.POST['name'] image = request.FILES['image'] board = request.POST['board'] board_letter = Boards.objects.get(pk=board) if len(text) > 5 and len(text) < 2000: # Check captcha was correct a = request.POST['a'] b = request.POST['b'] value = request.POST['captcha'] if int(a) + int(b) == int(value or 0): #print (image.name) #thumbnail = create_thumbnail(image) thread = Post(board_id=board, post_title=title, post_text=text, post_name=name, post_image=image) thread.save() return HttpResponseRedirect(reverse('index'))
def reply(request, thread_id): thread = get_object_or_404(Thread, pk=thread_id) if request.method == 'POST': text = request.POST['text'] if text and len(text) > 5 and len(text) < 2000: # Check captcha a = request.POST['a'] b = request.POST['b'] value = request.POST['captcha'] if int(a) + int(b) == int(value or 0): post = Post(thread=thread, post_text=text) post.save() # Check if thread is at bump limit if thread.thread_count > 300: thread.last_bumped.auto_now = False thread.thread_count += 1 thread.save() return HttpResponseRedirect(reverse('thread', args=(thread.id,)))
def reply(request, thread_id): thread = get_object_or_404(Thread, pk=thread_id) if request.method == 'POST': text = request.POST['text'] if text and len(text) > 5 and len(text) < 2000: # Check captcha a = request.POST['a'] b = request.POST['b'] value = request.POST['captcha'] if int(a) + int(b) == int(value or 0): post = Post(thread=thread, post_text=text) post.save() # Check if thread is at bump limit if thread.thread_count > 300: thread.last_bumped.auto_now = False thread.thread_count += 1 thread.save() return HttpResponseRedirect(reverse('thread', args=(thread.id, )))
def test_board_post_image_connections(self): """Make sure that boards and posts and their connections to an image does not throw errors.""" b1 = Board(title="board1", description="bar") b1.save() i2 = Image(name="foo", photo=bytearray("bar", "utf-8")) i3 = Image(name="ham", photo=bytearray("sam", "utf-8")) i2.save() i3.save() b2 = Board(title="board2", description="foo", bg=i2) b2.save() self.assertIs(b2.bg, i2) self.assertIs(i2.board, b2) p1 = Post(associated_board=b2, name="p1", message="m1") p2 = Post(associated_board=b2, name="p1", message="m1", photo=Image(name="spam", photo=bytearray("eggs", "utf-8")).save()) p3 = Post(associated_board=b2, name="p1", message="m1", photo=i3) p1.save() p2.save() p3.save() self.assertIs(p3.photo, i3) self.assertIs(i3.post, p3) self.assertIs(p2.photo.name, "spam")
def new_post(request): if request.method == 'POST': form = PostForm(request.POST) if form.is_valid(): post = Post() post.title = request.POST.get('title') post.description = request.POST.get('description') post.detailed_description = request.POST.get( 'detailed_description') post.tags = request.POST.getlist('tags')[0].split(' ') post.author = request.user.get_full_name().split(" ")[0] post.publication_date = datetime.date.today() post.email = request.POST.get('email') post.save() return HttpResponseRedirect('/board/') else: form = PostForm() form.fields['author'].initial = request.user.username return render(request, 'form.html', {'form': form})
def setUpTestData(cls): # create user user = User.objects.create(name=name, email=email, password=password) # get token when login request = RequestFactory().post('/login/', data=json.dumps({ 'email': email, 'password': password }), content_type='application/json') global token token = json.loads(login(request=request).content)['token'] # create a post post = Post() post.author = user post.content = "測試用的發文" post.save() # get post_id for post_item in Post.objects.filter(content="測試用的發文"): global post_id post_id = post_item.id
def convert_post(self, wpost, first_post=False): """Converts single post.""" slug = wpost.section_slug if slug in self.bad_sections: raise ConvertError("Section {} does not exist".format(slug)) post = Post() post.data = "" for f in self.fields: setattr(post, f, getattr(wpost, f)) # add video to the post if wpost.video: try: post.message += u" {}".format(wpost.video) except UnicodeEncodeError: raise ConvertError("Cannot add video to the post {}".format(wpost.id)) if first_post: post.is_op_post = True thread = Thread() thread.section_id = self.section_map[slug] for f in ("is_closed", "is_pinned"): setattr(thread, f, getattr(wpost, f)) else: tid = self.thread_map.get((slug, wpost.parent)) try: thread = Thread.objects.get(id=tid) except Thread.DoesNotExist: raise ConvertError("Thread does not exist") thread.bump = wpost.date thread.save(rebuild_cache=False) if first_post: self.thread_map[(slug, wpost.pid)] = thread.id post.thread = thread if wpost.image: to = os.path.join(settings.WAKABA_PATH, slug) extension = wpost.image.split(".").pop() type_id = self.filetype_map.get(extension) if not type_id: raise ConvertError("Type {} does not exist".format(extension)) try: f = DjangoFile(open(os.path.join(to, wpost.image))) except IOError: pass else: file = File( file=f, hash=wpost.image_md5, type_id=type_id, image_width=wpost.image_width, image_height=wpost.image_height, ) if wpost.thumb: try: thumb = DjangoFile(open(os.path.join(to, wpost.thumb))) except IOError: pass else: file.thumb = thumb if file.file: file.save() post.file = file post.save() thread.save()
def post(request): p = Post(txt=request.POST["post"], published_at=datetime.datetime.now()) p.save() return HttpResponseRedirect("/")
def new_post_process(t, request, data): post = Post(topic = t, user = request.user, message = data['message']) post.save()
def new_topic_process(category, request, data): t = Topic(name = data['topic_name'], desc = data['topic_desc'], category = category, user = request.user) t.save() post = Post(topic = t, user = request.user, message = data['post_message']) post.save()
def convert_post(self, wpost, first_post=False): """Converts single post.""" slug = wpost.section_slug if slug in self.bad_sections: raise ConvertError("Section {} does not exist".format(slug)) post = Post() post.data = "" for f in self.fields: setattr(post, f, getattr(wpost, f)) # add video to the post if wpost.video: try: post.message += u" {}".format(wpost.video) except UnicodeEncodeError: raise ConvertError("Cannot add video to the post {}".format( wpost.id)) if first_post: post.is_op_post = True thread = Thread() thread.section_id = self.section_map[slug] for f in ("is_closed", "is_pinned"): setattr(thread, f, getattr(wpost, f)) else: tid = self.thread_map.get((slug, wpost.parent)) try: thread = Thread.objects.get(id=tid) except Thread.DoesNotExist: raise ConvertError("Thread does not exist") thread.bump = wpost.date thread.save(rebuild_cache=False) if first_post: self.thread_map[(slug, wpost.pid)] = thread.id post.thread = thread if wpost.image: to = os.path.join(settings.WAKABA_PATH, slug) extension = wpost.image.split(".").pop() type_id = self.filetype_map.get(extension) if not type_id: raise ConvertError("Type {} does not exist".format(extension)) try: f = DjangoFile(open(os.path.join(to, wpost.image))) except IOError: pass else: file = File(file=f, hash=wpost.image_md5, type_id=type_id, image_width=wpost.image_width, image_height=wpost.image_height) if wpost.thumb: try: thumb = DjangoFile(open(os.path.join(to, wpost.thumb))) except IOError: pass else: file.thumb = thumb if file.file: file.save() post.file = file post.save() thread.save()
def test_created_posts_have_timestamps(self): # p = G(Post, fill_nullable_fields=False) p = Post(user=self.user) p.save() self.assertEqual(int(time.mktime(p.created.timetuple())), p.timestamp)
def post(request): p = Post(txt=request.POST["post"], published_at=datetime.datetime.now() ) p.save() return HttpResponseRedirect("/")