def test_was_created_recently_with_seconds_before_article(self): # 若文章创建时间为1分钟内,返回True ——————它才是算是最近发布的! author = User(username="******",password="******") author.save() seconds_before_article = ArticlePost(author=author,title="test1",body="test1", created=timezone.now()-datetime.timedelta(seconds=45)) self.assertIs(seconds_before_article.was_created_recently(),True)
def test_was_created_recently_with_days_before_article(self): # 若文章创建时间为5天,返回False author = User(username="******",password="******") author.save() days_before_article = ArticlePost(author=author,title="test3",body="test3", created=timezone.now()-datetime.timedelta(days=5)) self.assertIs(days_before_article.was_created_recently(),False)
def test_was_created_recently_with_hours_before_article(self): # 若创建文章的时间为3hours前,返回False author = User(username="******",password="******") author.save() hours_before_article = ArticlePost(author=author,title="test2",body="test2", created=timezone.now()-datetime.timedelta(hours=3)) self.assertIs(hours_before_article.was_created_recently(),False)
def test_was_created_recently_with_hours_before_article(self): author = User(username='******', password='******') author.save() hours_before_article = ArticlePost(author=author, title='test2', body='test2', created=timezone.now() - datetime.timedelta(hours=3)) self.assertIs(hours_before_article.was_created_recently(), False)
def test_was_created_recently_with_seconds_before_article(self): author = User(username='******', password='******') author.save() seconds_before_article = ArticlePost(author=author, title='test1', body='test1', created=timezone.now() - datetime.timedelta(seconds=45)) self.assertIs(seconds_before_article.was_created_recently(), True)
def test_was_created_recently_with_days_before_article(self): author = User(username='******', password='******') author.save() months_before_article = ArticlePost(author=author, title='test3', body='test3', created=timezone.now() - datetime.timedelta(days=5)) self.assertIs(months_before_article.was_created_recently(), False)
def test_was_created_recently_with_future_article(self): # 若文章创建时间为未来30天,返回False author = User(username="******",password="******") author.save() # 创建1篇:未来30后发布的文章(30天后的时间获取方式"created=...") future_article = ArticlePost(author=author,title="test",body="test", created=timezone.now()+datetime.timedelta(days=30)) # python单元测试库的"断言"方法:作用,检测方法内2个参数是否"一致",不一致则抛异常提示! self.assertIs(future_article.was_created_recently(),False)
def test_was_created_recently_with_future_article(self): # 若文章创建时间为未来,返回 False author = User(username='******', password='******') author.save() future_article = ArticlePost(author=author, title='test', body='test', created=timezone.now() + datetime.timedelta(days=30)) self.assertIs(future_article.was_created_recently(), False)
def test_was_created_recently_with_future_article(self): author = User(username='******', password='******') author.save() future_article = ArticlePost(author=author, title='test', body='test', created=timezone.now() + datetime.timedelta(days=30)) # 检测方法内的两个参数是否完全一致,如果不是则抛出异常 self.assertIs(future_article.was_created_recently(), False)
def test_increase_views_but_not_change_updated_field(self): # 请求详情视图时,不改变updated字段! author = User(username="******",password="******") author.save() article = ArticlePost(author=author,title="test5",body="test5") article.save() sleep(0.5) url = reverse("article:article_detail",args=(article.id,)) response = self.client.get(url) viewed_article = ArticlePost.objects.get(id=article.id) self.assertIs(viewed_article.updated - viewed_article.created < timezone.timedelta(seconds=0.1),True)
def test_increase_views(self): # 请求详情视图时,阅读量+1("0->1") author = User(username="******",password="******") author.save() article = ArticlePost(author=author,title="test4",body="test4") article.save() self.assertIs(article.total_views,0) # 新文章=0 url = reverse("article:article_detail",args=(article.id,)) response = self.client.get(url) viewed_article = ArticlePost.objects.get(id=article.id) self.assertIs(viewed_article.total_views,1) # 访问了=1
def test_increase_view(self): author = User(username='******', password='******') author.save() article = ArticlePost( author=author, title='test4', body='test4', ) article.save() self.assertIs(article.total_views, 0) url = reverse('article:article_detail', kwargs={'pid': article.id}) response = self.client.get(url) new_article = ArticlePost.objects.get(id=article.id) self.assertIs(new_article.total_views, 1)
def test_increase_views_but_not_change_updated_field(self): # 请求详情视图时,不改变 updated 字段 author = User(username='******', password='******') author.save() article = ArticlePost( author=author, title='test5', body='test5', ) article.save() sleep(0.5) url = reverse('article:article_detail', kwargs={'pid': article.id}) self.client.get(url) new_article = ArticlePost.objects.get(id=article.id) self.assertIs(new_article.updated - new_article.created < timezone.timedelta(seconds=0.1), True)
def test_increase_views(self): author = User(username='******', password='******') author.save() article = ArticlePost( author=author, title='test4', body='test4', ) article.save() self.assertIs(article.total_views, 0) url = reverse('article:detail_view', args=(article.id, )) response = self.client.get(url) viewed_article = ArticlePost.objects.get(id=article.id) self.assertIs(viewed_article.total_views, 1)
def test_increase_views_but_not_change_updated_field(self): # 请求详情视图时,不改变 updated 字段 author = User(username='******', password='******') author.save() article = ArticlePost( author=author, title='test5', body='test5', ) article.save() url = reverse('article:detail_view', args=(article.id, )) response = self.client.get(url) viewed_article = ArticlePost.objects.get(id=article.id) self.assertIs( viewed_article.updated - viewed_article.created < timezone.timedelta(seconds=0.1), True)
def test_increase_veiws(self): # 请求详情视图是偶,阅读量+1 author = User(username='******', password='******') author.save() article = ArticlePost( author=author, title='test4', body='test4', ) article.save() self.assertIs(article.total_views, 0) url = reverse('article:article_detail', args=(article.pk, )) response = self.client.get(url) viewed_article = ArticlePost.objects.get(pk=article.pk) self.assertIs(viewed_article.total_views, 1)
def test_increase_views_but_not_change_updated_field(self): author = User(username='******', password='******') author.save() article = ArticlePost( author=author, title='test5', body='test5', ) article.save() sleep(0.5) url = reverse('article:article_detail', args=(article.id, )) # 向视图发起请求并获得了响应 response = self.client.get(url) # 从数据库中取出更新后的数据,并用断言语句来判断代码是否符合预期了 viewed_article = ArticlePost.objects.get(id=article.id) self.assertIs( viewed_article.updated - viewed_article.created < timezone.timedelta(seconds=0.1), True)
def test_increase_views_but_changge_update_field(self): # 请求详情视图时候,不改变update 字段 author = User(username='******', password='******') author.save() article = ArticlePost( author=author, title='test5', body='test5', ) article.save() sleep(0.5) url = reverse('article:article_detail', args=(article.pk, )) response = self.client.get(url) viewed_article = ArticlePost.objects.get(pk=article.pk) self.assertIs( viewed_article.update_time - viewed_article.created_time < timezone.timedelta(seconds=0.1), True)
def article_post(request): sections = SectionForum.objects.all() if request.method == "POST": article_post_form = ArticlePostForm(data=request.POST) if article_post_form.is_valid(): cd = article_post_form.cleaned_data #try: new_article = ArticlePost() new_article.author = request.user new_article.title = cd.get('title') new_article.ueditor_body = cd.get('content') new_article.section_belong_fk = SectionForum.objects.get( name=request.POST.get('secsb')) new_article.save() # 更新用户动态 new_action = common_member_action_log() new_action.uid = request.user new_action.pid = new_article new_action.action = 'post' new_action.save() common_member.objects.filter(id=request.user.id).update( posts=request.user.posts + 1) #url = reverse('') url = reverse('article_detail', kwargs={ 'pid': new_article.pid, 'slug': new_article.slug }) #print(url) return HttpResponseRedirect(url) #return HttpResponse('1') #except: #return HttpResponse("2") else: return HttpResponse("3") else: article_post_form = ArticlePostForm() context = {'sections': sections} return render(request, "x_fatie_demo.html", { "article_post_form": article_post_form, "sections": sections })
def test_was_created_recently_with_future_article(self): # 若文章创建时间为未来,返回 False author = User(username='******', password='******') author.save() future_article = ArticlePost(author=author, title='test', body='test', created=timezone.now() + datetime.timedelta(days=30)) def test_was_created_recently_with_seconds_before_article(self): # 若文章创建时间为 1 分钟内,返回 True author = User(username='******', password='******') author.save() seconds_before_article = ArticlePost( author=author, title='test1', body='test1', created=timezone.now() - datetime.timedelta(seconds=45)) self.assertIs(seconds_before_article.was_created_recently(), True) def test_was_created_recently_with_hours_before_article(self): # 若文章创建时间为几小时前,返回 False author = User(username='******', password='******') author.save() hours_before_article = ArticlePost(author=author, title='test2', body='test2', created=timezone.now() - datetime.timedelta(hours=3)) self.assertIs(hours_before_article.was_created_recently(), False) def test_was_created_recently_with_days_before_article(self): # 若文章创建时间为几天前,返回 False author = User(username='******', password='******') author.save() months_before_article = ArticlePost(author=author, title='test3', body='test3', created=timezone.now() - datetime.timedelta(days=5)) self.assertIs(months_before_article.was_created_recently(), False)
def section_all(request, section_slug): section = SectionForum.objects.filter(slug=section_slug).first() posts = ArticlePost.objects.filter( section_belong_fk=section).order_by('-pub_date')[0:2 * Num_per_page] post_num = ArticlePost.objects.filter(section_belong_fk=section).count() if request.method == "POST": post_form = FastPostForm(data=request.POST) if post_form.is_valid(): cd = post_form.cleaned_data #try: new_article = ArticlePost() new_article.author = request.user new_article.title = cd.get('title') new_article.ueditor_body = cd.get('content') new_article.section_belong_fk = section new_article.save() # 更新用户动态 new_action = common_member_action_log() new_action.uid = request.user new_action.pid = new_article new_action.action = 'post' new_action.save() common_member.objects.filter(id=request.user.id).update( posts=request.user.posts + 1) form = FastPostForm() context = { 'section': section, 'post_num': post_num, 'posts': posts, 'form': form, } return render(request, 'x_bankuai_demo.html', context)
def article_post(request): if request.method == "POST": article_post_form = ArticlePostForm(data=request.POST) if article_post_form.is_valid(): cd = article_post_form.cleaned_data #try: new_article = ArticlePost() new_article.author = request.user new_article.title = cd.get('title') new_article.ueditor_body = cd.get('content') new_article.save() # 更新用户动态 new_action = common_member_action_log() new_action.uid = request.user new_action.pid = new_article new_action.action = 'post' new_action.save() #url = reverse('') url = reverse('article_detail', kwargs={ 'pid': new_article.pid, 'slug': new_article.slug }) #print(url) return HttpResponseRedirect(url) #return HttpResponse('1') #except: #return HttpResponse("2") else: return HttpResponse("3") else: article_post_form = ArticlePostForm() return render(request, "x_fatie_demo.html", {"article_post_form": article_post_form})
def main(): query = client.search(index="chinadaily", body={"query": { "match_all": {} }}, scroll='5m', size=100) results = query['hits']['hits'] # es查询出的结果第一页 total = query['hits']['total'] # es查询出的结果总量 scroll_id = query['_scroll_id'] # 游标用于输出es查询出的所有结果 for i in range(0, int(total / 100) + 1): # scroll参数必须指定否则会报错 query_scroll = client.scroll(scroll_id=scroll_id, scroll='5m')['hits']['hits'] results += query_scroll # Article.objects.all().delete() for result in results: print('saving article: %s' % result["_source"]["title"]) article = ArticlePost() article.title = result["_source"]["title"] article.content = result["_source"]["content"], article.save()
def articlePost(request): if request.method == 'POST': try: post = request.POST['post'] user = request.user title = request.POST['title'] articlePost = ArticlePost(title=title, user=user, post=post) articlePost.image_title1 = request.POST['image_title1'] articlePost.image_title2 = request.POST['image_title2'] articlePost.image_title3 = request.POST['image_title3'] articlePost.image_title4 = request.POST['image_title4'] if request.FILES.get('image1') != None: articlePost.image2 = request.FILES['image1'] if request.FILES.get('image2') != None: articlePost.image2 = request.FILES['image2'] if request.FILES.get('image3') != None: articlePost.image3 = request.FILES['image3'] if request.FILES.get('image4') != None: articlePost.image4 = request.FILES['image4'] articlePost.save() messages.success(request, 'article posted') return redirect("home") except: messages.error(request, 'somrthing went wrong try again') return redirect('article_post') else: return render(request, 'article/articlePost.html')