From 72713badff2a24556e9cb2089fca4ba4b3a07fc5 Mon Sep 17 00:00:00 2001 From: Dmitriy Kuzmenko Date: Fri, 25 Oct 2019 11:04:40 +0300 Subject: [PATCH 1/5] remove print from tests --- apps/account/tests/tests_back.py | 21 ++++++++----- apps/account/tests/tests_common.py | 12 ++++---- .../tests/tests_authorization.py | 8 ++--- apps/comment/tests.py | 13 ++++---- apps/establishment/tests.py | 30 +++++++++++-------- apps/favorites/tests.py | 3 -- apps/location/tests.py | 12 ++------ apps/notification/tests.py | 8 ++--- apps/recipe/tests.py | 2 -- apps/utils/tests/tests_translated.py | 1 - 10 files changed, 55 insertions(+), 55 deletions(-) diff --git a/apps/account/tests/tests_back.py b/apps/account/tests/tests_back.py index 8adc6b35..d0178159 100644 --- a/apps/account/tests/tests_back.py +++ b/apps/account/tests/tests_back.py @@ -1,10 +1,13 @@ -from rest_framework.test import APITestCase -from rest_framework import status -from authorization.tests.tests_authorization import get_tokens_for_user -from django.urls import reverse from http.cookies import SimpleCookie + +from django.urls import reverse +from rest_framework import status +from rest_framework.test import APITestCase + +from account.models import Role, User +from authorization.tests.tests_authorization import get_tokens_for_user from location.models import Country -from account.models import Role, User, UserRole + class RoleTests(APITestCase): def setUp(self): @@ -65,9 +68,11 @@ class UserRoleTests(APITestCase): ) self.role.save() - self.user_test = User.objects.create_user(username='test', - email='testemail@mail.com', - password='passwordtest') + self.user_test = User.objects.create_user( + username='test', + email='testemail@mail.com', + password='passwordtest' + ) def test_user_role_post(self): url = reverse('back:account:user-role-list-create') diff --git a/apps/account/tests/tests_common.py b/apps/account/tests/tests_common.py index dea807c4..67dfacda 100644 --- a/apps/account/tests/tests_common.py +++ b/apps/account/tests/tests_common.py @@ -1,9 +1,11 @@ -from rest_framework.test import APITestCase -from rest_framework import status -from authorization.tests.tests_authorization import get_tokens_for_user from http.cookies import SimpleCookie -from account.models import User + from django.urls import reverse +from rest_framework import status +from rest_framework.test import APITestCase + +from account.models import User +from authorization.tests.tests_authorization import get_tokens_for_user class AccountUserTests(APITestCase): @@ -62,7 +64,7 @@ class AccountChangePasswordTests(APITestCase): self.assertEqual(response.status_code, status.HTTP_200_OK) -class AccountChangePasswordTests(APITestCase): +class AccountConfirmEmail(APITestCase): def setUp(self): self.data = get_tokens_for_user() diff --git a/apps/authorization/tests/tests_authorization.py b/apps/authorization/tests/tests_authorization.py index a6a49ea5..cc0881f5 100644 --- a/apps/authorization/tests/tests_authorization.py +++ b/apps/authorization/tests/tests_authorization.py @@ -1,7 +1,7 @@ -from rest_framework.test import APITestCase -from account.models import User from django.urls import reverse -# Create your tests here. +from rest_framework.test import APITestCase + +from account.models import User def get_tokens_for_user( @@ -28,7 +28,7 @@ class AuthorizationTests(APITestCase): self.password = data["password"] def LoginTests(self): - data ={ + data = { 'username_or_email': self.username, 'password': self.password, 'remember': True diff --git a/apps/comment/tests.py b/apps/comment/tests.py index 9b060f4e..bd22214a 100644 --- a/apps/comment/tests.py +++ b/apps/comment/tests.py @@ -1,11 +1,13 @@ -from utils.tests.tests_permissions import BasePermissionTests -from rest_framework import status -from authorization.tests.tests_authorization import get_tokens_for_user -from django.urls import reverse -from django.contrib.contenttypes.models import ContentType from http.cookies import SimpleCookie + +from django.contrib.contenttypes.models import ContentType +from django.urls import reverse +from rest_framework import status + from account.models import Role, User, UserRole +from authorization.tests.tests_authorization import get_tokens_for_user from comment.models import Comment +from utils.tests.tests_permissions import BasePermissionTests class CommentModeratorPermissionTests(BasePermissionTests): @@ -40,7 +42,6 @@ class CommentModeratorPermissionTests(BasePermissionTests): self.comment.save() self.url = reverse('back:comment:comment-crud', kwargs={"id": self.comment.id}) - def test_put_moderator(self): tokens = User.create_jwt_tokens(self.moderator) self.client.cookies = SimpleCookie( diff --git a/apps/establishment/tests.py b/apps/establishment/tests.py index 43aa62c6..cc9ed152 100644 --- a/apps/establishment/tests.py +++ b/apps/establishment/tests.py @@ -20,13 +20,14 @@ class BaseTestCase(APITestCase): self.newsletter = True self.user = User.objects.create_user( username=self.username, email=self.email, password=self.password) - #get tokkens - tokkens = User.create_jwt_tokens(self.user) + # get tokens + tokens = User.create_jwt_tokens(self.user) self.client.cookies = SimpleCookie( - {'access_token': tokkens.get('access_token'), - 'refresh_token': tokkens.get('refresh_token')}) + {'access_token': tokens.get('access_token'), + 'refresh_token': tokens.get('refresh_token')}) - self.establishment_type = EstablishmentType.objects.create(name="Test establishment type") + self.establishment_type = EstablishmentType.objects.create( + name="Test establishment type") # Create lang object self.lang = Language.objects.get( @@ -42,12 +43,15 @@ class BaseTestCase(APITestCase): country=self.country_ru) self.region.save() - self.city = City.objects.create(name='Mosocow', code='01', - region=self.region, country=self.country_ru) + self.city = City.objects.create( + name='Mosocow', code='01', + region=self.region, + country=self.country_ru) self.city.save() - self.address = Address.objects.create(city=self.city, street_name_1='Krasnaya', - number=2, postal_code='010100') + self.address = Address.objects.create( + city=self.city, street_name_1='Krasnaya', + number=2, postal_code='010100') self.address.save() self.role = Role.objects.create(role=Role.ESTABLISHMENT_MANAGER) @@ -63,8 +67,9 @@ class BaseTestCase(APITestCase): self.establishment.save() - self.user_role = UserRole.objects.create(user=self.user, role=self.role, - establishment=self.establishment) + self.user_role = UserRole.objects.create( + user=self.user, role=self.role, + establishment=self.establishment) self.user_role.save() @@ -128,12 +133,11 @@ class EmployeeTests(BaseTestCase): self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT) -# Class to test childs class ChildTestCase(BaseTestCase): def setUp(self): super().setUp() -# Test childs + class EmailTests(ChildTestCase): def setUp(self): super().setUp() diff --git a/apps/favorites/tests.py b/apps/favorites/tests.py index cebd43c2..5f0a2fcd 100644 --- a/apps/favorites/tests.py +++ b/apps/favorites/tests.py @@ -61,9 +61,6 @@ class BaseTestCase(APITestCase): description={"en-GB": "description of test establishment"}, establishment_type=self.test_establishment_type, is_publish=True) - # value for GenericRelation(reverse side) field must be iterable - # value for GenericRelation(reverse side) field must be assigned through - # "set" method of field self.test_establishment.favorites.set([self.test_favorites]) diff --git a/apps/location/tests.py b/apps/location/tests.py index cb574036..4e192831 100644 --- a/apps/location/tests.py +++ b/apps/location/tests.py @@ -19,15 +19,10 @@ class BaseTestCase(APITestCase): self.user = User.objects.create_user( username=self.username, email=self.email, password=self.password) - # get tokens - - # self.user.is_superuser = True - # self.user.save() - - tokkens = User.create_jwt_tokens(self.user) + tokens = User.create_jwt_tokens(self.user) self.client.cookies = SimpleCookie( - {'access_token': tokkens.get('access_token'), - 'refresh_token': tokkens.get('refresh_token')}) + {'access_token': tokens.get('access_token'), + 'refresh_token': tokens.get('refresh_token')}) self.lang = Language.objects.get( title='Russia', @@ -102,7 +97,6 @@ class RegionTests(BaseTestCase): user_role.save() - def test_region_CRUD(self): response = self.client.get('/api/back/location/regions/', format='json') self.assertEqual(response.status_code, status.HTTP_200_OK) diff --git a/apps/notification/tests.py b/apps/notification/tests.py index d78c7fca..87264435 100644 --- a/apps/notification/tests.py +++ b/apps/notification/tests.py @@ -15,10 +15,10 @@ class BaseTestCase(APITestCase): self.password = 'sedragurdaredips19' self.email = 'sedragurda@desoz.com' self.user = User.objects.create_user(username=self.username, email=self.email, password=self.password) - # get tokkens - tokkens = User.create_jwt_tokens(self.user) - self.client.cookies = SimpleCookie({'access_token': tokkens.get('access_token'), - 'refresh_token': tokkens.get('refresh_token')}) + # get tokens + tokens = User.create_jwt_tokens(self.user) + self.client.cookies = SimpleCookie({'access_token': tokens.get('access_token'), + 'refresh_token': tokens.get('refresh_token')}) class NotificationAnonSubscribeTestCase(APITestCase): diff --git a/apps/recipe/tests.py b/apps/recipe/tests.py index d0a17a36..68359be0 100644 --- a/apps/recipe/tests.py +++ b/apps/recipe/tests.py @@ -27,7 +27,5 @@ class BaseTestCase(APITestCase): self.assertEqual(response.status_code, status.HTTP_200_OK) def test_recipe_detail(self): - print(self.test_recipe.id) response = self.client.get(f"/api/web/recipes/{self.test_recipe.id}/") - print(response.json()) self.assertEqual(response.status_code, status.HTTP_200_OK) diff --git a/apps/utils/tests/tests_translated.py b/apps/utils/tests/tests_translated.py index 77b67d8a..4569ecf2 100644 --- a/apps/utils/tests/tests_translated.py +++ b/apps/utils/tests/tests_translated.py @@ -62,7 +62,6 @@ class TranslateFieldTests(BaseTestCase): response = self.client.get(f"/api/web/news/slug/{self.news_item.slug}/", format='json') self.assertEqual(response.status_code, status.HTTP_200_OK) news_data = response.json() - print(news_data) self.assertIn("title_translated", news_data) self.assertIn("Test news item", news_data['title_translated']) From 91f6503a26fe1b22d7b9c60bd5d68822f2c03995 Mon Sep 17 00:00:00 2001 From: Semyon Yekhmenin Date: Fri, 25 Oct 2019 10:54:01 +0000 Subject: [PATCH 2/5] Added country name to site settings --- apps/main/serializers.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/apps/main/serializers.py b/apps/main/serializers.py index a0af662b..f939c448 100644 --- a/apps/main/serializers.py +++ b/apps/main/serializers.py @@ -63,6 +63,8 @@ class SiteSettingsSerializer(serializers.ModelSerializer): # todo: remove this country_code = serializers.CharField(source='subdomain', read_only=True) + country_name = serializers.CharField(source='country.name_translated', read_only=True) + class Meta: """Meta class.""" @@ -78,7 +80,8 @@ class SiteSettingsSerializer(serializers.ModelSerializer): 'config', 'ad_config', 'published_features', - 'currency' + 'currency', + 'country_name' ) From 7f23f0e891455324d1832a44e0053f3ffdadb489 Mon Sep 17 00:00:00 2001 From: Kuroshini Date: Wed, 16 Oct 2019 14:11:07 +0300 Subject: [PATCH 3/5] Establishment each hour reindexing --- apps/establishment/models.py | 5 +++++ apps/establishment/tasks.py | 12 ++++++++++-- apps/search_indexes/documents/establishment.py | 2 +- project/settings/base.py | 10 +++++++++- requirements/base.txt | 3 ++- 5 files changed, 27 insertions(+), 5 deletions(-) diff --git a/apps/establishment/models.py b/apps/establishment/models.py index 304ea2a6..4a6a95e4 100644 --- a/apps/establishment/models.py +++ b/apps/establishment/models.py @@ -120,6 +120,11 @@ class EstablishmentQuerySet(models.QuerySet): def with_type_related(self): return self.prefetch_related('establishment_subtypes') + def with_es_related(self): + """Return qs with related for ES indexing objects.""" + return self.select_related('address', 'establishment_type', 'address__city', 'address__city__country').\ + prefetch_related('tags', 'schedule') + def search(self, value, locale=None): """Search text in JSON fields.""" if locale is not None: diff --git a/apps/establishment/tasks.py b/apps/establishment/tasks.py index cf23a7e6..978f29ef 100644 --- a/apps/establishment/tasks.py +++ b/apps/establishment/tasks.py @@ -1,10 +1,13 @@ """Establishment app tasks.""" import logging + from celery import shared_task +from django.core import management +from django_elasticsearch_dsl.management.commands import search_index + from establishment import models from location.models import Country - logger = logging.getLogger(__name__) @@ -12,10 +15,15 @@ logger = logging.getLogger(__name__) def recalculate_price_levels_by_country(country_id): try: country = Country.objects.get(pk=country_id) - except Country.DoesNotExist as ex: + except Country.DoesNotExist as _: logger.error(f'ESTABLISHMENT. Country does not exist. ID {country_id}') else: qs = models.Establishment.objects.filter(address__city__country=country) for establishment in qs: establishment.recalculate_price_level(low_price=country.low_price, high_price=country.high_price) + +@shared_task +def rebuild_establishment_indices(): + management.call_command(search_index.Command(), action='rebuild', models=[models.Establishment.__name__], + force=True) diff --git a/apps/search_indexes/documents/establishment.py b/apps/search_indexes/documents/establishment.py index 5d858321..665fc85d 100644 --- a/apps/search_indexes/documents/establishment.py +++ b/apps/search_indexes/documents/establishment.py @@ -106,4 +106,4 @@ class EstablishmentDocument(Document): ) def get_queryset(self): - return super().get_queryset().published() + return super().get_queryset().published().with_es_related() diff --git a/project/settings/base.py b/project/settings/base.py index f9524f94..8a65c1bb 100644 --- a/project/settings/base.py +++ b/project/settings/base.py @@ -13,6 +13,7 @@ https://docs.djangoproject.com/en/2.2/ref/settings/ import os import sys from datetime import timedelta +from celery.schedules import crontab # Build paths inside the project like this: os.path.join(BASE_DIR, ...) BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) @@ -98,7 +99,8 @@ EXTERNAL_APPS = [ 'timezone_field', 'storages', 'sorl.thumbnail', - 'timezonefinder' + 'timezonefinder', + 'django_celery_beat', ] @@ -324,6 +326,12 @@ CELERY_ACCEPT_CONTENT = ['application/json'] CELERY_TASK_SERIALIZER = 'json' CELERY_RESULT_SERIALIZER = 'json' CELERY_TIMEZONE = TIME_ZONE +CELERY_BEAT_SCHEDULE = { + 'send-summary-every-hour': { + 'task': 'establishment.tasks.rebuild_establishment_indices', + 'schedule': crontab(hour=1), + }, +} # Django FCM (Firebase push notifications) FCM_DJANGO_SETTINGS = { diff --git a/requirements/base.txt b/requirements/base.txt index c95055de..19c29b7d 100644 --- a/requirements/base.txt +++ b/requirements/base.txt @@ -44,4 +44,5 @@ sorl-thumbnail==12.5.0 # temp solution redis==3.2.0 amqp>=2.4.0 -celery==4.3.0rc2 \ No newline at end of file +celery==4.3.0rc2 +django-celery-beat==1.5.0 \ No newline at end of file From dd0b5951328fa09ea47ab1e221950b28b082110e Mon Sep 17 00:00:00 2001 From: Kuroshini Date: Wed, 16 Oct 2019 14:11:07 +0300 Subject: [PATCH 4/5] Review fixes --- apps/establishment/tasks.py | 4 +++- project/settings/base.py | 8 -------- requirements/base.txt | 3 +-- 3 files changed, 4 insertions(+), 11 deletions(-) diff --git a/apps/establishment/tasks.py b/apps/establishment/tasks.py index 978f29ef..b7a39d2d 100644 --- a/apps/establishment/tasks.py +++ b/apps/establishment/tasks.py @@ -2,6 +2,8 @@ import logging from celery import shared_task +from celery.schedules import crontab +from celery.task import periodic_task from django.core import management from django_elasticsearch_dsl.management.commands import search_index @@ -23,7 +25,7 @@ def recalculate_price_levels_by_country(country_id): establishment.recalculate_price_level(low_price=country.low_price, high_price=country.high_price) -@shared_task +@periodic_task(run_every=crontab(minute=60)) def rebuild_establishment_indices(): management.call_command(search_index.Command(), action='rebuild', models=[models.Establishment.__name__], force=True) diff --git a/project/settings/base.py b/project/settings/base.py index 8a65c1bb..a8334839 100644 --- a/project/settings/base.py +++ b/project/settings/base.py @@ -13,7 +13,6 @@ https://docs.djangoproject.com/en/2.2/ref/settings/ import os import sys from datetime import timedelta -from celery.schedules import crontab # Build paths inside the project like this: os.path.join(BASE_DIR, ...) BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) @@ -100,7 +99,6 @@ EXTERNAL_APPS = [ 'storages', 'sorl.thumbnail', 'timezonefinder', - 'django_celery_beat', ] @@ -326,12 +324,6 @@ CELERY_ACCEPT_CONTENT = ['application/json'] CELERY_TASK_SERIALIZER = 'json' CELERY_RESULT_SERIALIZER = 'json' CELERY_TIMEZONE = TIME_ZONE -CELERY_BEAT_SCHEDULE = { - 'send-summary-every-hour': { - 'task': 'establishment.tasks.rebuild_establishment_indices', - 'schedule': crontab(hour=1), - }, -} # Django FCM (Firebase push notifications) FCM_DJANGO_SETTINGS = { diff --git a/requirements/base.txt b/requirements/base.txt index 19c29b7d..c95055de 100644 --- a/requirements/base.txt +++ b/requirements/base.txt @@ -44,5 +44,4 @@ sorl-thumbnail==12.5.0 # temp solution redis==3.2.0 amqp>=2.4.0 -celery==4.3.0rc2 -django-celery-beat==1.5.0 \ No newline at end of file +celery==4.3.0rc2 \ No newline at end of file From 4eaa600fc1baee60176e9a37937670420d331e1a Mon Sep 17 00:00:00 2001 From: Kuroshini Date: Wed, 16 Oct 2019 14:11:07 +0300 Subject: [PATCH 5/5] Fix all tests --- apps/establishment/tasks.py | 2 +- apps/utils/tests/tests_translated.py | 8 ++++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/apps/establishment/tasks.py b/apps/establishment/tasks.py index b7a39d2d..fdc10933 100644 --- a/apps/establishment/tasks.py +++ b/apps/establishment/tasks.py @@ -25,7 +25,7 @@ def recalculate_price_levels_by_country(country_id): establishment.recalculate_price_level(low_price=country.low_price, high_price=country.high_price) -@periodic_task(run_every=crontab(minute=60)) +@periodic_task(run_every=crontab(minute=59)) def rebuild_establishment_indices(): management.call_command(search_index.Command(), action='rebuild', models=[models.Establishment.__name__], force=True) diff --git a/apps/utils/tests/tests_translated.py b/apps/utils/tests/tests_translated.py index 4569ecf2..557c8b5d 100644 --- a/apps/utils/tests/tests_translated.py +++ b/apps/utils/tests/tests_translated.py @@ -9,6 +9,7 @@ from account.models import User from news.models import News, NewsType from establishment.models import Establishment, EstablishmentType, Employee +from location.models import Country class BaseTestCase(APITestCase): @@ -39,7 +40,13 @@ class TranslateFieldTests(BaseTestCase): self.news_type = NewsType.objects.create(name="Test news type") self.news_type.save() + + self.country_ru = Country.objects.get( + name={"en-GB": "Russian"} + ) + self.news_item = News.objects.create( + id=8, created_by=self.user, modified_by=self.user, title={ @@ -52,6 +59,7 @@ class TranslateFieldTests(BaseTestCase): news_type=self.news_type, slug='test', state=News.PUBLISHED, + country=self.country_ru, ) self.news_item.save()