From 72713badff2a24556e9cb2089fca4ba4b3a07fc5 Mon Sep 17 00:00:00 2001 From: Dmitriy Kuzmenko Date: Fri, 25 Oct 2019 11:04:40 +0300 Subject: [PATCH 1/7] 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/7] 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 8cf2cebe85083d23cbafd604063653bac75bcddc Mon Sep 17 00:00:00 2001 From: Anatoly Date: Tue, 29 Oct 2019 14:01:27 +0300 Subject: [PATCH 3/7] added BaseModelAdminMixin --- apps/establishment/admin.py | 5 +++-- apps/news/admin.py | 3 ++- apps/review/admin.py | 3 ++- apps/utils/admin.py | 36 ++++++++++++++++++++++++++++++++++++ 4 files changed, 43 insertions(+), 4 deletions(-) create mode 100644 apps/utils/admin.py diff --git a/apps/establishment/admin.py b/apps/establishment/admin.py index 4b845c60..38016a18 100644 --- a/apps/establishment/admin.py +++ b/apps/establishment/admin.py @@ -4,6 +4,7 @@ from django.contrib.contenttypes.admin import GenericTabularInline from django.utils.translation import gettext_lazy as _ from comment.models import Comment +from utils.admin import BaseModelAdminMixin from establishment import models from main.models import Award from review import models as review_models @@ -57,7 +58,7 @@ class EstablishmentAdmin(admin.ModelAdmin): @admin.register(models.Position) -class PositionAdmin(admin.ModelAdmin): +class PositionAdmin(BaseModelAdminMixin): """Position admin.""" @@ -68,7 +69,7 @@ class PlateInline(admin.TabularInline): @admin.register(models.Menu) -class MenuAdmin(admin.ModelAdmin): +class MenuAdmin(BaseModelAdminMixin): """Menu admin.""" list_display = ['id', 'category_translated'] inlines = [ diff --git a/apps/news/admin.py b/apps/news/admin.py index 91fb284c..a6ca3279 100644 --- a/apps/news/admin.py +++ b/apps/news/admin.py @@ -3,6 +3,7 @@ from django.conf import settings from news import models from .tasks import send_email_with_news +from utils.admin import BaseModelAdminMixin @admin.register(models.NewsType) @@ -24,7 +25,7 @@ send_email_action.short_description = "Send the selected news by email" @admin.register(models.News) -class NewsAdmin(admin.ModelAdmin): +class NewsAdmin(BaseModelAdminMixin): """News admin.""" raw_id_fields = ('address',) actions = [send_email_action] diff --git a/apps/review/admin.py b/apps/review/admin.py index 5820f557..03c2419a 100644 --- a/apps/review/admin.py +++ b/apps/review/admin.py @@ -1,8 +1,9 @@ """Admin page for app Review""" from . import models from django.contrib import admin +from utils.admin import BaseModelAdminMixin @admin.register(models.Review) -class ReviewAdminModel(admin.ModelAdmin): +class ReviewAdminModel(BaseModelAdminMixin): """Admin model for model Review.""" diff --git a/apps/utils/admin.py b/apps/utils/admin.py new file mode 100644 index 00000000..43680e20 --- /dev/null +++ b/apps/utils/admin.py @@ -0,0 +1,36 @@ +"""Mixins for admin models.""" +from django.contrib import admin +from django.db.models import ForeignKey + + +class BaseModelAdminMixin(admin.ModelAdmin): + """ + Class that overridden ModelAdmin and adds to readonly_fields attr + persisted fields like created_by, modified_by. + """ + + _PERSISTENT_READ_ONLY_FIELDS = ['created_by', 'modified_by'] + + def _get_fk_field_names(self, fields: iter): + """ + Return an iterable object which contains FK model fields. + :param fields: iterable + :return: iterable + """ + foreign_key_fields = [] + for field in fields: + if isinstance(field, ForeignKey): + foreign_key_fields.append(field.name) + return foreign_key_fields + + def get_readonly_fields(self, request, obj=None): + """ + Hook for specifying custom readonly fields. + """ + _readonly_fields = list(self.readonly_fields) + fk_field_names = self._get_fk_field_names(self.model._meta.fields) + + for field_name in fk_field_names: + if field_name in self._PERSISTENT_READ_ONLY_FIELDS: + _readonly_fields.append(field_name) + return tuple(_readonly_fields) From 97f75dfc0546b07834eddfdb431fccc8c80c4eb3 Mon Sep 17 00:00:00 2001 From: Dmitriy Kuzmenko Date: Tue, 29 Oct 2019 14:32:51 +0300 Subject: [PATCH 4/7] add filter accounts for existing users --- apps/account/transfer_data.py | 8 ++++++-- apps/utils/models.py | 3 +++ 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/apps/account/transfer_data.py b/apps/account/transfer_data.py index 91f9051f..c39e0ac1 100644 --- a/apps/account/transfer_data.py +++ b/apps/account/transfer_data.py @@ -8,8 +8,12 @@ def transfer_user(): # queryset = Profiles.objects.all() # queryset = queryset.annotate(nickname=F("account__nickname")) # queryset = queryset.annotate(email=F("account__email")) - - queryset = Accounts.objects.filter(confirmed_at__isnull=False) + stop_list = ['cyril@tomatic.net', + 'cyril2@tomatic.net', + 'd.sadykova@id-east.ru', + 'd.sadykova@octopod.ru', + 'n.yurchenko@id-east.ru'] + queryset = Accounts.objects.filter(confirmed_at__isnull=False).exclude(email__in=stop_list) serialized_data = UserSerializer(data=list(queryset.values()), many=True) diff --git a/apps/utils/models.py b/apps/utils/models.py index fb1de17c..ae6e15b0 100644 --- a/apps/utils/models.py +++ b/apps/utils/models.py @@ -331,3 +331,6 @@ class GMTokenGenerator(PasswordResetTokenGenerator): used. """ return self.get_fields(user, timestamp) + + +timezone.datetime.now().date().isoformat() \ No newline at end of file From 631ffd265a38fbec0af6bd53620861a8b3a9d044 Mon Sep 17 00:00:00 2001 From: Dmitriy Kuzmenko Date: Tue, 29 Oct 2019 14:46:44 +0300 Subject: [PATCH 5/7] add mixin for admin panel --- apps/establishment/admin.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apps/establishment/admin.py b/apps/establishment/admin.py index 38016a18..735ccc4b 100644 --- a/apps/establishment/admin.py +++ b/apps/establishment/admin.py @@ -48,7 +48,7 @@ class CommentInline(GenericTabularInline): @admin.register(models.Establishment) -class EstablishmentAdmin(admin.ModelAdmin): +class EstablishmentAdmin(BaseModelAdminMixin): """Establishment admin.""" list_display = ['id', '__str__', 'image_tag', ] inlines = [ From 30ba313f54b7fe770d07997d5157a7c5a460a248 Mon Sep 17 00:00:00 2001 From: Dmitriy Kuzmenko Date: Tue, 29 Oct 2019 14:47:57 +0300 Subject: [PATCH 6/7] add filter accounts for existing users --- apps/establishment/admin.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/apps/establishment/admin.py b/apps/establishment/admin.py index 735ccc4b..c9a7d33e 100644 --- a/apps/establishment/admin.py +++ b/apps/establishment/admin.py @@ -11,12 +11,12 @@ from review import models as review_models @admin.register(models.EstablishmentType) -class EstablishmentTypeAdmin(admin.ModelAdmin): +class EstablishmentTypeAdmin(BaseModelAdminMixin): """EstablishmentType admin.""" @admin.register(models.EstablishmentSubType) -class EstablishmentSubTypeAdmin(admin.ModelAdmin): +class EstablishmentSubTypeAdmin(BaseModelAdminMixin): """EstablishmentSubType admin.""" From 03bf648ef9f08b05f757704dcab9f31db73e40fa Mon Sep 17 00:00:00 2001 From: Dmitriy Kuzmenko Date: Tue, 29 Oct 2019 15:08:54 +0300 Subject: [PATCH 7/7] add change admin for establishments --- apps/establishment/admin.py | 1 + 1 file changed, 1 insertion(+) diff --git a/apps/establishment/admin.py b/apps/establishment/admin.py index c9a7d33e..0397c407 100644 --- a/apps/establishment/admin.py +++ b/apps/establishment/admin.py @@ -55,6 +55,7 @@ class EstablishmentAdmin(BaseModelAdminMixin): AwardInline, ContactPhoneInline, ContactEmailInline, ReviewInline, CommentInline] raw_id_fields = ('address',) + fields = ['old_id', 'name'] @admin.register(models.Position)