def test_delete_new(self): category = Category() category.name = 'Экономика' category.description = 'Новости экономики' category.slug = 'economy' category.save() news = News() news.title = 'Моя первая новость' news.announcement = 'Анонс первой новости ++' news.text = 'Текст первой новости!!!' news.pub_date = timezone.now() news.category = category news.save() all_news = News.objects.all() self.assertEquals(len(all_news), 1) self.client.login(username='******', password='******') response = self.client.post('/admin/news/news/' + str(news.id) + '/delete/', { 'post':'yes' }, follow=True ) self.assertContains(response, 'успешно удален', status_code=200) all_news = News.objects.all() self.assertEquals(len(all_news), 0)
def test_edit_news(self): category = Category() category.name = 'Экономика' category.description = 'Новости экономики' category.slug = 'economy' category.save() news = News() news.title = 'Моя первая новость' news.announcement = 'Анонс первой новости ++' news.text = 'Текст первой новости!!!' news.pub_date = timezone.now() news.category = category news.save() self.client.login(username='******', password='******') response = self.client.post('/admin/news/news/' + str(news.id) + '/', { 'title': 'Моя вторая новость', 'announcement': 'Анонс второй новости', 'text': 'Текст второй новости', 'pub_date_0': '2014-10-15', 'pub_date_1': '16:07:00', 'category': str(category.id) }, follow=True) self.assertContains(response, 'успешно изменен', status_code=200) all_news = News.objects.all() self.assertEquals(len(all_news), 1) only_news = all_news[0] self.assertEquals(only_news.title, 'Моя вторая новость') self.assertEquals(only_news.announcement, 'Анонс второй новости') self.assertEquals(only_news.text, 'Текст второй новости')
def test_delete_new(self): category = Category() category.name = 'Экономика' category.description = 'Новости экономики' category.slug = 'economy' category.save() news = News() news.title = 'Моя первая новость' news.announcement = 'Анонс первой новости ++' news.text = 'Текст первой новости!!!' news.pub_date = timezone.now() news.category = category news.save() all_news = News.objects.all() self.assertEquals(len(all_news), 1) self.client.login(username='******', password='******') response = self.client.post('/admin/news/news/' + str(news.id) + '/delete/', {'post': 'yes'}, follow=True) self.assertContains(response, 'успешно удален', status_code=200) all_news = News.objects.all() self.assertEquals(len(all_news), 0)
def test_category_page(self): category = Category() category.name = 'Экономика' category.description = 'Новости экономики' category.slug = 'economy' category.save() news = News() news.title = 'Моя первая новость' news.announcement = 'Анонс первой новости' news.pub_date = timezone.now() news.category = category news.save() all_news = News.objects.all() self.assertEquals(len(all_news), 1) only_news = all_news[0] self.assertEquals(only_news, news) category_url = news.category.get_absolute_url() response = self.client.get(category_url) self.assertContains(response, news.category.name, status_code=200) #проверяем атрибуты self.assertEquals(only_news.title, 'Моя первая новость') self.assertEquals(only_news.announcement, 'Анонс первой новости') self.assertEquals(only_news.pub_date.day, news.pub_date.day) self.assertEquals(only_news.pub_date.year, news.pub_date.year)
def test_create_news(self): category = Category() category.name = 'Экономика' category.description = 'Новости экономики' category.slug = 'economy' category.save() self.client.login(username='******', password='******') response = self.client.get('/admin/news/news/add/') self.assertEquals(response.status_code, 200) response = self.client.post('/admin/news/news/add/', { 'title': 'Моя первая новость', 'announcement': 'Анонс первой новости', 'text': 'Плюс текст первой новости', 'pub_date_0': '2014-10-15', 'pub_date_1': '15:35:00', 'category': str(category.id) }, follow=True) self.assertContains(response, 'успешно добавлен', status_code=200) all_news = News.objects.all() self.assertEquals(len(all_news), 1)
def test_create_news(self): category = Category() category.name = 'Экономика' category.description = 'Новости экономики' category.slug = 'economy' category.save() self.client.login(username='******', password='******') response = self.client.get('/admin/news/news/add/') self.assertEquals(response.status_code, 200) response = self.client.post('/admin/news/news/add/', { 'title': 'Моя первая новость', 'announcement': 'Анонс первой новости', 'text': 'Плюс текст первой новости', 'pub_date_0': '2014-10-15', 'pub_date_1': '15:35:00', 'category': str(category.id) }, follow=True ) self.assertContains(response, 'успешно добавлен', status_code=200) all_news = News.objects.all() self.assertEquals(len(all_news), 1)
def test_create_category(self): category = Category() category.name = 'Экономика' category.description = 'Новости экономики' category.slug = 'economy' category.save() all_categories = Category.objects.all() self.assertEquals(len(all_categories), 1) only_category = all_categories[0] self.assertEquals(only_category, category) self.assertEquals(only_category.name, 'Экономика') self.assertEquals(only_category.description, 'Новости экономики')
def add_category(): list_category = ['Экономика', 'Туризм', 'Спорт', 'СНГ', 'Силовые структуры', 'Россия', 'Происшествия', 'Политика', 'Нацпроекты', 'Наука и техника', 'Мир', 'Культура', 'Интернет и СМИ', 'Власть', 'Авто', 'Digital', ''] category_in_to_base = [] for c in Category.objects.all(): category_in_to_base.append(c.name) for category_name in list_category: if category_name not in category_in_to_base: category = Category() category.name = category_name category.save() else: print('Категория существует в базе.')
def test_delete_category(self): category = Category() category.name = 'Экономика' category.description = 'Новости экономики' category.slug = 'economy' category.save() self.client.login(username='******', password='******') response = self.client.post('/admin/news/category/' + str(category.id) + '/delete/', {'post': 'yes'}, follow=True) self.assertContains(response, 'успешно удален', status_code=200) all_categories = Category.objects.all() self.assertEquals(len(all_categories), 0)
def test_delete_category(self): category = Category() category.name = 'Экономика' category.description = 'Новости экономики' category.slug = 'economy' category.save() self.client.login(username='******', password='******') response = self.client.post('/admin/news/category/' + str(category.id) + '/delete/', { 'post': 'yes' }, follow=True ) self.assertContains(response, 'успешно удален', status_code=200) all_categories = Category.objects.all() self.assertEquals(len(all_categories), 0)
def test_create_news(self): category = Category() category.name = 'Экономика' category.description = 'Новости экономики' category.slug = 'economy' category.save() #создаем новость и заполняем поля news = News() #устанавливаем атрибуты news.title = 'Моя первая новость' news.announcement = 'Анонс первой новости ++' news.text = 'Текст первой новости!!!' news.pub_date = timezone.now() news.category = category #сохраняем в бд news.save() #проверяем что новость доавбилась в базу all_news = News.objects.all() self.assertEquals(len(all_news), 1) only_news = all_news[0] self.assertEquals(only_news, news) #проверяем атрибуты self.assertEquals(only_news.title, 'Моя первая новость') self.assertEquals(only_news.announcement, 'Анонс первой новости ++') self.assertEquals(only_news.text, 'Текст первой новости!!!') self.assertEquals(only_news.pub_date.day, news.pub_date.day) self.assertEquals(only_news.pub_date.month, news.pub_date.month) self.assertEquals(only_news.pub_date.year, news.pub_date.year) self.assertEquals(only_news.pub_date.hour, news.pub_date.hour) self.assertEquals(only_news.pub_date.minute, news.pub_date.minute) self.assertEquals(only_news.pub_date.second, news.pub_date.second) self.assertEquals(only_news.category.name, 'Экономика') self.assertEquals(only_news.category.description, 'Новости экономики')
def test_edit_category(self): category = Category() category.name = 'Экономика' category.description = 'Новости экономики' category.slug = 'economy' category.save() self.client.login(username='******', password='******') response = self.client.post('/admin/news/category/' + str(category.id) + '/', { 'name': 'Политика', 'description': 'Новости политики', 'slug': 'politics' }, follow=True) self.assertContains(response, 'успешно изменен', status_code=200) all_categories = Category.objects.all() self.assertEquals(len(all_categories), 1) only_category = all_categories[0] self.assertEquals(only_category.name, 'Политика') self.assertEquals(only_category.description, 'Новости политики')
def test_edit_category(self): category = Category() category.name = 'Экономика' category.description = 'Новости экономики' category.slug = 'economy' category.save() self.client.login(username='******', password='******') response = self.client.post('/admin/news/category/' + str(category.id) + '/', { 'name': 'Политика', 'description': 'Новости политики', 'slug': 'politics' }, follow=True ) self.assertContains(response, 'успешно изменен', status_code=200) all_categories = Category.objects.all() self.assertEquals(len(all_categories), 1) only_category = all_categories[0] self.assertEquals(only_category.name, 'Политика') self.assertEquals(only_category.description, 'Новости политики')
def parser3(): url = 'https://www.gazeta.ru/export/rss/first.xml' r = requests.get(url) soup = BeautifulSoup(r.text, 'html.parser') item_xml = soup.find_all('item')[0] get_title = item_xml.find('title').text get_category = item_xml.find('category').text get_link = item_xml.find('guid').text icon = 'https://www.gazeta.ru/i/gazeta_og_image.jpg' list_category = [] list_title = [] for c in Category.objects.all(): list_category.append(c.name) if get_category in list_category: print(get_category, " Категория существует") elif get_category not in list_category: new_category = Category() new_category.name = get_category new_category.save() print("Добавлена новая категория ", get_category) for t in News.objects.all(): list_title.append(t.title) if get_title in list_title: print("новость существует в базе") elif get_title not in list_title: print("Добавление новой новости") new_news = News() new_news.title = get_title new_news.category_id = Category.objects.get(name=get_category).pk new_news.url_source = get_link new_news.icon = icon new_news.save()
def test_edit_news(self): category = Category() category.name = 'Экономика' category.description = 'Новости экономики' category.slug = 'economy' category.save() news = News() news.title = 'Моя первая новость' news.announcement = 'Анонс первой новости ++' news.text = 'Текст первой новости!!!' news.pub_date = timezone.now() news.category = category news.save() self.client.login(username='******', password='******') response = self.client.post('/admin/news/news/' + str(news.id) + '/', { 'title': 'Моя вторая новость', 'announcement': 'Анонс второй новости', 'text': 'Текст второй новости', 'pub_date_0': '2014-10-15', 'pub_date_1': '16:07:00', 'category': str(category.id) }, follow=True ) self.assertContains(response, 'успешно изменен', status_code=200) all_news = News.objects.all() self.assertEquals(len(all_news), 1) only_news = all_news[0] self.assertEquals(only_news.title, 'Моя вторая новость') self.assertEquals(only_news.announcement, 'Анонс второй новости') self.assertEquals(only_news.text, 'Текст второй новости')
def test_news_page(self): category = Category() category.name = 'Экономика' category.description = 'Новости экономики' category.slug = 'economy' category.save() news = News() news.title = 'Моя первая новость' news.text = 'Текст первой новости' news.pub_date = timezone.now() news.category = category news.save() all_news = News.objects.all() self.assertEquals(len(all_news), 1) response = self.client.get('/news/' + str(news.id) + '/') self.assertContains(response, news.title, status_code=200) self.assertContains(response, news.text) self.assertContains(response, news.pub_date.year) # выдает на англ и сравнивает с русским - не отрабатывает self.assertContains(response, news.pub_date.strftime('%b')) self.assertContains(response, news.pub_date.day) self.assertContains(response, news.category.name)
def test_does_slug_field_work(self): from news.models import Category cat = Category(name='how do i create a slug in django') cat.save() self.assertEqual(cat.slug,'how-do-i-create-a-slug-in-django')
def editView(request, id): q = Category.objects.all() obj = Category(name="smileyz") obj.save() context = {"object": obj} return render(request,'edit.html', context)