def index(request): article = Article(title='abc', content='1111') category = Category(name="最新文章") category.save() article.category = category article.save() return HttpResponse("success")
def setUp(self): category = Category() category.name = "Category" category.slug = "category" category.save() all_categories = Category.objects.all() self.assertEqual(len(all_categories), 1) category_from_database = all_categories[0] self.assertEqual(category_from_database, category)
def test_notempty_categorymodels(self, name=None): first_user = User.objects.create_user( username='******', email='*****@*****.**', password='******', ) test_category = Category(user_id=first_user.uuid) if name is not None: test_category.name = 'aaa' test_category.save() categories = Category.objects.all() self.assertEqual(categories.count(), 1)
def setUp(self): self.OLD_TEMPLATES = settings.TEMPLATES settings.TEMPLATES = (('desktop', ('default',),),) category = Category(name = "t", slug = "t") category.save() article = Article() article.category = category article.published = True article.title = "Article title" article.slug = "article" article.save() self.article = article self.browser = webdriver.Firefox() self.browser.implicitly_wait(5)
def test_categorymodel_assert(self): first_user = User.objects.create_user( username='******', email='*****@*****.**', password='******', ) name = 'aaa' c = Category( name=name, user_id=first_user.uuid, ) c.save() self.assertEqual(c.name, name) categories = Category.objects.all() self.assertEqual(categories.count(), 1)
def test_increase_viewCount(self): author = User(username='******', password='******') author.save() category = Category(name='分类3') category.save() article = Article(title='test3', body='test3', author=author, category=category) article.save() self.assertIs(article.viewCount, 0) # 判断初始化浏览量是否是0 url = reverse('article:articleDetailView', args=(article.id, )) self.client.get(url) # 浏览一次这个新闻 viewCountArticle = Article.objects.get(id=article.id) self.assertIs(viewCountArticle.viewCount, 1) # 判断浏览量是否是1
def test_categorymodels_relation(self): first_user = User.objects.create_user( username='******', email='*****@*****.**', password='******', ) name = 'category' c = Category( name=name, user_id=first_user.uuid, ) c.save() title = 'title' discript = 'discript' text = 'text' a = Article( title='title', discript='discript', text='text', user_id=first_user.uuid, category_id=c.id, ) a.save() aa = Article( title='2個目', discript='説明', text='テキスト', user_id=first_user.uuid, category_id=c.id, ) aa.save() tag = Tag( name='tag', user_id=first_user.uuid, ) tag.save() a.tag.add(tag) aa.tag.add(tag) tags = a.tag.all() sv = tag.article_set.all() articles = Article.objects.all() self.assertEqual(a.title, title) self.assertEqual(a.discript, discript) self.assertEqual(a.text, text) self.assertEqual(a.category_id, 2) self.assertEqual(tags.count(), 1) self.assertEqual(sv.count(), 2) self.assertEqual(articles.count(), 2)
def category(request): if request.method == 'GET': cate = Category.objects.all() return render(request, 'category.html', {'cate': cate}) if request.method == 'POST': lmmc = request.POST.get('name') cate1 = Category() cate1.lmmc = lmmc cate1.save() cate = Category.objects.all() return render(request, 'category.html', {'cate': cate})
def home(request): """ `templates` folder should be manually created within the application It will automatically load the htmls from this folder """ # Fetch every object from Post Model post_list = Post.objects.all() # paginate on the second parameter. i.e. # value of 2 will paginate every 2 posts paginator = Paginator(post_list, 1) # `page` is embedded automatically by Paginator class page = request.GET.get('page') categories = Category.list_categories() try: posts = paginator.page(page) except PageNotAnInteger: # Django.Paginator custom exception # Set post to something else posts = paginator.page(1) except EmptyPage: # Django.Paginator custom exception # load the last page posts = paginator.page(paginator.num_pages) return render(request, 'pages/home.html', {'posts': posts, 'categories': categories})
def import_article_categories(self): cols = [ 'id', 'nazov', 'ikona', ] self.cursor.execute('SELECT ' + (', '.join(cols)) + ' FROM clanky_kategorie') for category_row in self.cursor: self.logger.step_sub_progress() category_dict = self.decode_cols_to_dict(cols, category_row) category = { 'pk': category_dict['id'], 'name': category_dict['nazov'], 'slug': slugify(category_dict['nazov']), } category_object = ArticleCategory(**category) category_object.save() connections['default'].cursor().execute('SELECT setval(\'article_category_id_seq\', (SELECT MAX(id) FROM article_category) + 1);')
def test_Tagdetail(self): response = Client().get(reverse('article:tag_detail', kwargs={'pk': 8})) self.assertEqual(response.status_code, 302) self.assertRedirects(response, '/user/login/?next=/article/tag_detail/8') c = self.client c.login(username='******', password='******') response = c.get(reverse('article:tag_detail', kwargs={'pk': 8})) self.assertEqual(response.status_code, 200) self.assertContains(response, Category(id=8).name) self.assertContains(response, Article(id=7).title)
def post(self, request): send_type = request.POST.get('type', '') user = request.user if send_type == 'delete': cate_id = request.POST.get('id', '') Category.objects.get(pk=cate_id).delete() category = Category.objects.filter(User=user) return render(request, 'center/notice.html', {'category': category}) elif send_type == 'see': cate_id = request.POST.get('id', '') cate = Category.objects.get(pk=cate_id) articles = cate.Article_Category.filter(Author=user) category = Category.objects.filter(User=user) return render(request, 'center/cate.html', {'category': category, 'articles': articles}) elif send_type == 'update': old = request.POST.get('old', '') new = request.POST.get('new', '') category = Category.objects.get(pk=old) category.Category = new category.save() category = Category.objects.filter(User=user) return render(request, 'center/notice.html', {'category': category}) else: cate = request.POST.get('cate', '') category = Category() category.Category = cate category.User = user category.save() category = Category.objects.filter(User=user) return render(request, 'center/notice.html', {'category': category})
def test_related_posts(self): test_tag = Tag(name="テストタグ") test_tag.save() new_category = Category(name="テストタグ") new_category.save() article1 = Article( title="テスト記事1", pub_type=1, edit_body="テストです", category=new_category, pub_date=datetime.now(timezone.utc) ) article1.save() article1.tags.add(test_tag) article1.save() article2 = Article( title="テスト記事2", pub_type=1, edit_body="テストです", category=new_category, pub_date=datetime.now(timezone.utc) ) article2.save() article2.tags.add(test_tag) article2.save() article3 = Article( title="テスト記事3", pub_type=1, edit_body="テストです", category=new_category, pub_date=datetime.now(timezone.utc) ) article3.save() article3.tags.add(test_tag) article3.save() assert(article1.related_posts())
def test_article_create_and_change(self): new_category = Category(name="テストタグ") new_category.save() new_article = Article( title="テスト記事", pub_type=1, edit_body="テストです", category=new_category, pub_date=datetime.now(timezone.utc) ) new_article.save() self.assertTrue(new_article.is_need_celery_change) #記事本文変更 print("===記事本文変更===\n") new_article.edit_body = "ああああ" new_article.save() self.assertFalse(new_article.is_need_celery_change) #公開時間変更 print("===公開時間変更==\n") new_article.pub_date = datetime.now(timezone.utc) new_article.save() self.assertTrue(new_article.is_need_celery_change)
def test_ArticleDeleteView(self): response = Client().get('/article/delete/9') self.assertEqual(response.status_code, 302) self.assertRedirects(response, '/user/login/?next=/article/delete/9') c = self.client c.login(username='******', password='******') u = User.objects.get(username='******') create = Article.objects.create( title='delete_only', discript='testonly', text='only', user_id=u.uuid, category=Category(id=5), ) response = c.delete('/article/delete/9') all = Article.objects.all().count() self.assertEqual(all, 2)
def archive(request, category): """ View for rendering page for rendering categories `templates` folder should be manually created within the application It will automatically load the htmls from this folder Argument: category (str): Category slug """ # Fetch posts based on the category cat = Category.objects.get(slug=category) # __ is special in django. its a direct call in model attributes post_list = Post.objects.filter(category__pk=cat.id) # paginate on the second parameter. i.e. # value of 2 will paginate every 2 posts paginator = Paginator(post_list, 1) # `page` is embedded automatically by Paginator class page = request.GET.get('page') categories = Category.list_categories() try: posts = paginator.page(page) except PageNotAnInteger: # Django.Paginator custom exception # Set post to something else posts = paginator.page(1) except EmptyPage: # Django.Paginator custom exception # load the last page posts = paginator.page(paginator.num_pages) return render(request, 'pages/archive.html', {'posts': posts, 'categories': categories})
def get_context_data(self, **kwargs): # 方法一 context = super(CommonMixin, self).get_context_data(**kwargs) context.update(Category.get_top()) context.update(SidePane.get_show()) return context
def category_add(request): untitle = Category.objects.get_untitle() category = Category(name=untitle) category.save() return redirect('/category/%s/modify/' % category.id)