def test_all_post_feed(self): # Create the category category = Category() category.name = 'python' category.description = 'The Python programming language' category.save() # Create the tag tag = Tag() tag.name = 'python' tag.description = 'The Python programming language' tag.save() # Create the author author = User.objects.create_user('testuser', '*****@*****.**', 'password') author.save() # Create the site site = Site() site.name = 'example.com' site.domain = 'example.com' site.save() # Create a post post = Post() post.title = 'My first post' post.text = 'This is my first blog post' post.slug = 'my-first-post' post.pub_date = timezone.now() post.author = author post.site = site post.category = category # Save it post.save() # Add the tag post.tags.add(tag) post.save() # Check we can find it all_posts = Post.objects.all() self.assertEquals(len(all_posts), 1) only_post = all_posts[0] self.assertEquals(only_post, post) # Fetch the feed response = self.client.get('/feeds/posts/') self.assertEquals(response.status_code, 200) # Parse the feed feed = feedparser.parse(response.content) # Check length self.assertEquals(len(feed.entries), 1) # Check post retrieved is the correct one feed_post = feed.entries[0] self.assertEquals(feed_post.title, post.title) self.assertEquals(feed_post.description, post.text)
def addpost(request): setting = Setting.objects.get(pk=1) last_posts = Post.objects.filter(status=True).order_by('-id')[:4] if request.method == 'POST': form = PostForm(request.POST, request.FILES) if form.is_valid(): current_user = request.user data = Post() data.user_id = current_user.id data.category = form.cleaned_data['category'] data.title = form.cleaned_data['title'] data.keywords = form.cleaned_data['keywords'] data.description = form.cleaned_data['description'] data.image = form.cleaned_data['image'] data.slug = form.cleaned_data['slug'] data.content = form.cleaned_data['content'] data.status = 'False' data.save() messages.success(request, 'İçeriğiniz başarıyla kaydedildi.') return HttpResponseRedirect('/user/posts') else: messages.error(request, 'Lütfen hatalı alanları kontrol ediniz.<br>'+ str(form.errors)) return HttpResponseRedirect('/user/posts') else: category = Category.objects.all() form = PostForm() context = {'setting': setting, 'category': category, 'form': form, 'last_posts': last_posts, } return render(request, 'user_addpost.html', context)
def test_create_post(self): # Create the post post = Post() category = Category(id=1) # Set the attributes post.title = 'My first post' post.text = 'This is my first blog post' post.pub_date = timezone.now() category.title = 'My category' post.category = category # Save it post.save() # Check we can find it all_posts = Post.objects.all() self.assertEquals(len(all_posts), 1) only_post = all_posts[0] self.assertEquals(only_post, post) # Check attributes self.assertEquals(only_post.title, 'My first post') self.assertEquals(only_post.text, 'This is my first blog post') self.assertEquals(only_post.pub_date.day, post.pub_date.day)
def post(self, request, format=None): serializer = PublishSerializer(data=request.data) if not serializer.is_valid(): print(serializer.errors) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) category = serializer.data.get('category') category = Category.objects.get(pk=category) if category is None: return Response(status=status.HTTP_400_BAD_REQUEST) tags = serializer.data.get('tags') with transaction.atomic(): post = Post() post.title = serializer.data.get('title') post.body = serializer.data.get('body') post.category = category post.author = request.user post.save() for tag in tags: tag = Tag.objects.get(pk=tag) post.tags.add(tag) return Response(status=status.HTTP_201_CREATED)
def test_creating_a_new_comment_and_saving_it_to_the_database(self): # start by create one Category and one post category = Category() category.name = "First" category.slug = "first" category.save() # create new post post = Post() post.category = category post.title = "First post" post.content = "Content" post.visable = True post.save() # create one comment comment = Comment() comment.name = "John" comment.content = "This is cool" comment.post = post # check save comment.save() # now check we can find it in the database all_comment_in_database = Comment.objects.all() self.assertEquals(len(all_comment_in_database), 1) only_comment_in_database = all_comment_in_database[0] self.assertEquals(only_comment_in_database, comment) # and check that it's saved its two attributes: name and content self.assertEquals(only_comment_in_database.name, comment.name) self.assertEquals(only_comment_in_database.content, comment.content)
def test_creating_a_new_post_and_saving_it_to_the_database(self): # start by create one Category category = Category() category.name = "First" category.slug = "first" category.save() # create new post post = Post() post.category = category post.title = "First post" post.content = "Content" post.visable = True # check we can save it post.save() # check slug self.assertEquals(post.slug, "first-post") # now check we can find it in the database all_post_in_database = Post.objects.all() self.assertEquals(len(all_post_in_database), 1) only_post_in_database = all_post_in_database[0] self.assertEquals(only_post_in_database, post) # check that it's saved all attributes: category, title, content, visable and generate slug self.assertEquals(only_post_in_database.category, category) self.assertEquals(only_post_in_database.title, post.title) self.assertEquals(only_post_in_database.content, post.content) self.assertEquals(only_post_in_database.visable, post.visable) self.assertEquals(only_post_in_database.slug, post.slug)
def test_category_page(self): # Create the category category = Category() category.name = 'python' category.description = 'The Python programming language' category.save() # Create the author author = User.objects.create_user('testuser', '*****@*****.**', 'password') author.save() # Create the site site = Site() site.name = 'example.com' site.domain = 'example.com' site.save() # Create the post post = Post() post.title = 'My first post' post.text = 'This is [my first blog post](http://127.0.0.1:8000/)' post.slug = 'my-first-post' post.pub_date = timezone.now() post.author = author post.site = site post.category = category post.save() # Check new post saved all_posts = Post.objects.all() self.assertEquals(len(all_posts), 1) only_post = all_posts[0] self.assertEquals(only_post, post) # Get the category URL category_url = post.category.get_absolute_url() # Fetch the category response = self.client.get(category_url) self.assertEquals(response.status_code, 200) # Check the category name is in the response self.assertTrue(post.category.name in response.content) # Check the post text is in the response self.assertTrue( markdown.markdown(post.text).encode() in response.content) # Check the post date is in the response self.assertTrue(str(post.pub_date.year) in response.content) self.assertTrue( _date(post.pub_date, "F").encode('utf-8') in response.content) self.assertTrue(str(post.pub_date.day) in response.content) # Check the link is marked up properly self.assertTrue( '<a href="http://127.0.0.1:8000/">my first blog post</a>' in response.content)
def test_delete_post(self): # Create the category category = Category() category.name = 'python' category.description = 'The Python programming language' category.save() # Create the tag tag = Tag() tag.name = 'python' tag.description = 'The Python programming language' tag.save() # Create the author author = User.objects.create_user('testuser', '*****@*****.**', 'password') author.save() # Create the site site = Site() site.name = 'example.com' site.domain = 'example.com' site.save() # Create the post post = Post() post.title = 'My first post' post.text = 'This is my first blog post' post.slug = 'my-first-post' post.pub_date = timezone.now() post.site = site post.author = author post.category = category post.save() post.tags.add(tag) post.save() # Check new post saved all_posts = Post.objects.all() self.assertEquals(len(all_posts), 1) # Log in self.client.login(username='******', password="******") # Delete the post response = self.client.post('/admin/blogengine/post/1/delete/', { 'post': 'yes' }, follow=True) self.assertEquals(response.status_code, 200) # Check deleted successfully self.assertTrue('deleted successfully' in response.content) # Check post deleted all_posts = Post.objects.all() self.assertEquals(len(all_posts), 0)
def test_category_page(self): # Create the category category = Category() category.name = 'python' category.description = 'The Python programming language' category.save() # Create the author author = User.objects.create_user('testuser', '*****@*****.**', 'password') author.save() # Create the site site = Site() site.name = 'example.com' site.domain = 'example.com' site.save() # Create the post post = Post() post.title = 'My first post' post.text = 'This is [my first blog post](http://127.0.0.1:8000/)' post.slug = 'my-first-post' post.pub_date = timezone.now() post.author = author post.site = site post.category = category post.save() # Check new post saved all_posts = Post.objects.all() self.assertEquals(len(all_posts), 1) only_post = all_posts[0] self.assertEquals(only_post, post) # Get the category URL category_url = post.category.get_absolute_url() # Fetch the category response = self.client.get(category_url) self.assertEquals(response.status_code, 200) # Check the category name is in the response self.assertTrue(post.category.name in response.content) # Check the post text is in the response self.assertTrue(markdown.markdown(post.text) in response.content) # Check the post date is in the response self.assertTrue(str(post.pub_date.year) in response.content) self.assertTrue(post.pub_date.strftime('%b') in response.content) self.assertTrue(str(post.pub_date.day) in response.content) # Check the link is marked up properly self.assertTrue('<a href="http://127.0.0.1:8000/">my first blog post</a>' in response.content)
def test_index(self): category = Category() category.name = 'python' category.description = 'The python language' category.save() # Create the tag tag = Tag() tag.name = 'perl' tag.description = 'The Perl programming language' tag.save() author = User.objects.create_user('testuser', '*****@*****.**', 'password') author.save() # site = Site() # site.name = 'nunfuxgvn.com' # site.domain = 'nunfuxgvn.com' # site.save() post = Post() post.title = 'First Post' post.content = 'My [first post](http://127.0.0.1:8000/) -yay' post.slug = 'first-post' post.pub_date = timezone.now() post.author = author # post.site = site post.category = category post.save() post.tags.add(tag) all_posts = Post.objects.all() self.assertEquals(len(all_posts), 1) # Fetch test_index response = self.client.get('/') self.assertEquals(response.status_code, 200) self.assertTrue(post.title in response.content) self.assertTrue(markdown.markdown(post.content) in response.content) self.assertTrue(post.category.name in response.content) post_tag = all_posts[0].tags.all()[0] self.assertTrue(post_tag.name in response.content) self.assertTrue(str(post.pub_date.year) in response.content) self.assertTrue(post.pub_date.strftime('%b') in response.content) self.assertTrue(str(post.pub_date.day) in response.content) self.assertTrue('<a href="http://127.0.0.1:8000/">first post</a>' in response.content)
def post_create(request): if request.method == 'POST': form = PostForm(request.POST) if form.is_valid(): post = Post() post.author = request.user post.category = form.cleaned_data['category'] post.name = form.cleaned_data['name'] post.content = form.cleaned_data['content'] post.status = form.cleaned_data['status'] post.save() return redirect('blog.home') else: form = PostForm() return render(request, 'blog/post_create.html', {'form': form})
def test_create_post(self): category = Category(name='test') category.save() user = User.objects.last() new_post = Post() new_post.user = user new_post.category = category new_post.title = 'Title test' new_post.content = "is TDD good?" new_post.save() # self.assertIsNotNone(new_post.pk) # post = Post.objects.get(pk=new_post.pk) exists = Post.objects.filter(pk=new_post.pk).exists() self.assertTrue(exists)
def test_client_create_post(self): category = Category(name='test') category.save() c = Client() p = Post() p.user = self.user p.category = category p.title = 'qqqq' p.content = 'zzzz' p.save() url = reverse('blog:detail', kwargs={'pk': p.pk}) res = c.get(url) self.assertEqual(res.status_code, 200)
def test_create_post(self): # Create the category category = Category() category.name = 'python' category.description = 'The Python programming language' category.save() # Create the tag tag = Tag() tag.name = 'python' tag.description = 'The Python programming language' tag.save() # Create the author author = User.objects.create_user('testuser', '*****@*****.**', 'password') author.save() # Create the site site = Site() site.name = 'example.com' site.domain = 'example.com' site.save() # Create the post post = Post() # Set the attributes post.title = 'My first post' post.text = 'This is my first blog post' post.slug = 'my-first-post' post.pub_date = timezone.now() post.author = author post.site = site post.category = category # Save it post.save() # Add the tag post.tags.add(tag) post.save() # Check we can find it all_posts = Post.objects.all() self.assertEquals(len(all_posts), 1) only_post = all_posts[0] self.assertEquals(only_post, post) # Check attributes self.assertEquals(only_post.title, 'My first post') self.assertEquals(only_post.text, 'This is my first blog post') self.assertEquals(only_post.slug, 'my-first-post') self.assertEquals(only_post.site.name, 'example.com') self.assertEquals(only_post.site.domain, 'example.com') self.assertEquals(only_post.pub_date.day, post.pub_date.day) self.assertEquals(only_post.pub_date.month, post.pub_date.month) self.assertEquals(only_post.pub_date.year, post.pub_date.year) self.assertEquals(only_post.pub_date.hour, post.pub_date.hour) self.assertEquals(only_post.pub_date.minute, post.pub_date.minute) self.assertEquals(only_post.pub_date.second, post.pub_date.second) self.assertEquals(only_post.author.username, 'testuser') self.assertEquals(only_post.author.email, '*****@*****.**') self.assertEquals(only_post.category.name, 'python') self.assertEquals(only_post.category.description, 'The Python programming language') # Check tags post_tags = only_post.tags.all() self.assertEquals(len(post_tags), 1) only_post_tag = post_tags[0] self.assertEquals(only_post_tag, tag) self.assertEquals(only_post_tag.name, 'python') self.assertEquals(only_post_tag.description, 'The Python programming language')
def parse_xml(): et = ET.ElementTree(file='/home/jasper/Downloads/jasper.xml') root = et.getroot() user = User.objects.get(username='******') count = 1 alias_re = re.compile(r'^[a-z|0-9|\-]+$') for channel in root.findall('channel'): for item in channel.findall('item'): title = item.find('title') post_name = item.find('{http://wordpress.org/export/1.2/}post_name') pubDate = item.find('pubDate') description = item.find('description') content = item.find('{http://purl.org/rss/1.0/modules/content/}encoded') categories = item.findall('category') tags = [] category = Category() for ca in categories: if ca.attrib['domain'] == 'post_tag': tags.append(ca.text) else: try: category = Category.objects.get(name=ca.text) except Category.DoesNotExist: category.name = ca.text if alias_re.match(ca.attrib['nicename']): category.alias = ca.attrib['nicename'] else: category.alias = category.name print category.alias category.save() try: Post.objects.get(title=title.text) print title.text, 'already exist' continue except Post.DoesNotExist: pass post = Post() post.title = title.text post.category = category if alias_re.match(post_name.text): post.alias = post_name.text else: post.alias = title.text print post.alias post.content = content.text post.content_html = content.text if not description.text: post.summary = content.text[:140] else: post.summary = description.text post.is_old = True post.tags = ','.join(tags) create_time = pubDate.text[:-6] tf = '%a, %d %b %Y %H:%M:%S' post.old_pub_time = datetime.strptime(create_time, tf) print post.old_pub_time post.author = user try: post.save() print count, post count += 1 except Exception, e: print e
def teste_create_post(self): # Create the tag tag = Tag() tag.name = 'python' tag.description = 'The Python programming language' tag.save() # Create the category category = Category() category.name = 'python' category.description = 'The Python programming language' category.save() # Create the author author = User.objects.create_user('testuser', '*****@*****.**', 'password') author.save() # Create the site site = Site() site.name = 'example.com' site.domain = 'example.com' site.save() # cria o post post = Post() post.title = 'Meu primeiro post' post.text = 'Este é meu primeiro post do blog' post.slug = "my-first-post" post.pub_date = timezone.now() post.author = author post.site = site post.category = category # salva o post no banco de dados post.save() # Add the tag post.tags.add(tag) post.save() # verifica se conseguimos encontrá-lo all_posts = Post.objects.all() self.assertEquals(len(all_posts), 1) only_post = all_posts[0] self.assertEqual(only_post, post) # verifica os atributos self.assertEqual(only_post.title, 'Meu primeiro post') self.assertEqual(only_post.text, u'Este é meu primeiro post do blog') self.assertEqual(only_post.slug, 'my-first-post') self.assertEquals(only_post.site.name, 'example.com') self.assertEquals(only_post.site.domain, 'example.com') self.assertEqual(only_post.pub_date.day, post.pub_date.day) self.assertEqual(only_post.pub_date.month, post.pub_date.month) self.assertEqual(only_post.pub_date.year, post.pub_date.year) self.assertEqual(only_post.pub_date.hour, post.pub_date.hour) self.assertEqual(only_post.pub_date.minute, post.pub_date.minute) self.assertEqual(only_post.pub_date.second, post.pub_date.second) self.assertEquals(only_post.author.username, 'testuser') self.assertEquals(only_post.author.email, '*****@*****.**') self.assertEquals(only_post.category.name, 'python') self.assertEquals(only_post.category.description, 'The Python programming language') # Check tags post_tags = only_post.tags.all() self.assertEquals(len(post_tags), 1) only_post_tag = post_tags[0] self.assertEquals(only_post_tag, tag) self.assertEquals(only_post_tag.name, 'python') self.assertEquals(only_post_tag.description, 'The Python programming language')