Skip to content

Commit

Permalink
Check_3
Browse files Browse the repository at this point in the history
  • Loading branch information
smaspb17 committed Mar 19, 2023
1 parent 1c47292 commit 6421366
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 27 deletions.
12 changes: 6 additions & 6 deletions yatube/posts/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,26 +15,26 @@ def __str__(self) -> str:

class Post(models.Model):
text = models.TextField(
verbose_name='Текст поста_v',
help_text='Введите текст поста_h',
verbose_name='Текст поста',
help_text='Введите текст поста',
)
pub_date = models.DateTimeField(
'Дата публикации_bv',
'Дата публикации',
auto_now_add=True,
)
author = models.ForeignKey(
User,
on_delete=models.CASCADE,
related_name='posts',
verbose_name='Автор публикации_v')
verbose_name='Автор публикации')
group = models.ForeignKey(
'Group',
blank=True,
null=True,
on_delete=models.SET_NULL,
related_name='posts',
verbose_name='Группа_v',
help_text='Группа, к которой будет относиться пост_h'
verbose_name='Группа',
help_text='Группа, к которой будет относиться пост'
)
image = models.ImageField(
'Картинка',
Expand Down
10 changes: 5 additions & 5 deletions yatube/posts/tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,16 @@ def test_verbose_name(self):
"""Корректный verbose_name у полей модели Post."""
def test_verbose_name_values(field):
return Post._meta.get_field(field).verbose_name
self.assertEqual(test_verbose_name_values('text'), 'Текст поста_v')
self.assertEqual(test_verbose_name_values('group'), 'Группа_v')
self.assertEqual(test_verbose_name_values('text'), 'Текст поста')
self.assertEqual(test_verbose_name_values('group'), 'Группа')
self.assertEqual(test_verbose_name_values('author'),
'Автор публикации_v')
'Автор публикации')

def test_help_text(self):
"""Корректный help_text у полей модели Post."""
def test_help_text_values(field):
return Post._meta.get_field(field).help_text
self.assertEqual(test_help_text_values('text'),
'Введите текст поста_h')
'Введите текст поста')
self.assertEqual(test_help_text_values('group'),
'Группа, к которой будет относиться пост_h')
'Группа, к которой будет относиться пост')
34 changes: 19 additions & 15 deletions yatube/posts/tests/test_urls.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from django.test import TestCase, Client
from posts.models import Post, Group, User
from django.core.cache import cache


class PostURLTest(TestCase):
Expand All @@ -14,6 +15,24 @@ def setUpClass(cls):
cls.post = Post.objects.create(author=cls.user)
cls.template_404 = 'core/404.html'

def setUp(self):
self.templates_url_names = {
'/': 'posts/index.html',
'/group/slug/': 'posts/group_list.html',
'/profile/name/': 'posts/profile.html',
f'/posts/{self.post.id}/': 'posts/post_detail.html',
f'/posts/{self.post.id}/edit/': 'posts/create_post.html',
'/create/': 'posts/create_post.html',
}
cache.clear()

def test_urls(self):
"""Соответствие шаблонов адресам"""
for address, template in self.templates_url_names.items():
with self.subTest(address=address):
response = self.authorized_client.get(address)
self.assertTemplateUsed(response, template)

def test_home_url_exists_at_desired_location(self):
"""Главная страница доступна любому пользователю."""
response = self.guest_client.get('/')
Expand All @@ -37,21 +56,6 @@ def test_urls_authorized_user(self):
response = self.authorized_client.get(url_test_adress)
self.assertEqual(response.status_code, 200)

def test_urls(self):
"""Соответствие шаблонов адресам"""
templates_url_names = {
'/': 'posts/index.html',
'/group/slug/': 'posts/group_list.html',
'/profile/name/': 'posts/profile.html',
f'/posts/{self.post.id}/': 'posts/post_detail.html',
f'/posts/{self.post.id}/edit/': 'posts/create_post.html',
'/create/': 'posts/create_post.html',
}
for address, template in templates_url_names.items():
with self.subTest(address=address):
response = self.authorized_client.get(address)
self.assertTemplateUsed(response, template)

def test_unexisting_page(self):
"""Запрос к несуществующей странице вернет ошибку 404
и при этом будет использован кастомный шаблон."""
Expand Down
2 changes: 2 additions & 0 deletions yatube/posts/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ def setUp(self):
self.user2 = User.objects.create_user(username='notfollower')
self.authorized_client2 = Client()
self.authorized_client2.force_login(self.user2)
cache.clear()

def posts_check_all_fields(self, post):
"""Корректность полей поста."""
Expand Down Expand Up @@ -220,6 +221,7 @@ def setUpClass(cls):
cls.authorized_client.force_login(cls.user)
for count in range(13):
cls.post = Post.objects.create(author=cls.user)
cache.clear()

def test_posts_if_first_page_has_ten_records(self):
"""Содержание на первой страницы 10 записей."""
Expand Down
4 changes: 3 additions & 1 deletion yatube/posts/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@
from django.shortcuts import render, get_object_or_404, redirect
from django.contrib.auth.decorators import login_required # встр. авториз
from django.core.paginator import Paginator

from django.views.decorators.cache import cache_page
from .models import Post, Group, Comment, Follow
from . forms import PostForm, CommentForm


SELECT_LIMIT = 10
CACHE_TIME_IN_SEC = 20


def paginator(request, posts):
Expand All @@ -18,6 +19,7 @@ def paginator(request, posts):
return page_obj


@cache_page(CACHE_TIME_IN_SEC, key_prefix='index_page')
def index(request):
post_list = Post.objects.all().select_related('author')
page_obj = paginator(request, post_list)
Expand Down

0 comments on commit 6421366

Please sign in to comment.