From b421b934c1c3a1be1c9cf25d52b324c3c6c35f05 Mon Sep 17 00:00:00 2001 From: Anatoly Date: Wed, 5 Feb 2020 12:16:07 +0300 Subject: [PATCH 1/7] refactored guides --- apps/collection/tasks.py | 63 +++++++++++++-------------------- project/settings/development.py | 32 ++++++++++++++++- project/settings/local.py | 10 +++--- 3 files changed, 60 insertions(+), 45 deletions(-) diff --git a/apps/collection/tasks.py b/apps/collection/tasks.py index 8efc4eec..d5bdbad8 100644 --- a/apps/collection/tasks.py +++ b/apps/collection/tasks.py @@ -9,28 +9,6 @@ logging.basicConfig(format='[%(levelname)s] %(message)s', level=logging.INFO) logger = logging.getLogger(__name__) -def get_additional_establishment_data(section_node, establishment, guide): - data = { - 'restaurant_section_node_id': section_node.id, - 'guide_id': guide.id, - 'establishment_id': establishment.id, - } - if establishment.last_published_review: - data.update({'review_id': establishment.last_published_review.id}) - return data - - -def get_additional_product_data(section_node, product, guide): - data = { - 'color_wine_section_node_id': section_node.id, - 'wine_id': product.id, - 'guide_id': guide.id, - } - if product.last_published_review: - data.update({'review_id': product.last_published_review.id}) - return data - - @shared_task def generate_establishment_guide_elements(guide_id: int, filter_set: dict): """Generate guide elements.""" @@ -38,9 +16,9 @@ def generate_establishment_guide_elements(guide_id: int, filter_set: dict): from establishment.models import Establishment guide = Guide.objects.get(id=guide_id) - guide.change_state(Guide.BUILDING) queryset_values = Establishment.objects.filter(**filter_set).values() try: + guide.change_state(Guide.BUILDING) for instance in queryset_values: populate_establishment_guide(guide_id, instance.get('id')) except Exception as e: @@ -59,8 +37,8 @@ def populate_establishment_guide(guide_id: int, establishment_id: int): from establishment.models import Establishment guide = Guide.objects.get(id=guide_id) - guide.change_state(Guide.BUILDING) try: + guide.change_state(Guide.BUILDING) establishment_qs = Establishment.objects.filter(id=establishment_id) if establishment_qs.exists(): establishment = establishment_qs.first() @@ -72,13 +50,17 @@ def populate_establishment_guide(guide_id: int, establishment_id: int): section_node, _ = GuideElement.objects.get_or_create_establishment_section_node( city_node.id, transform_into_section_name(establishment.establishment_type.index_name), - guide_id, + guide.id, ) if section_node: - GuideElement.objects.get_or_create_establishment_node( - **get_additional_establishment_data(section_node=section_node, - establishment=establishment, - guide=guide)) + params = { + 'restaurant_section_node_id': section_node.id, + 'guide_id': guide.id, + 'establishment_id': establishment.id, + } + if establishment.last_published_review: + params.update({'review_id': establishment.last_published_review.id}) + GuideElement.objects.get_or_create_establishment_node(**params) else: logger.error( f'METHOD_NAME: {generate_establishment_guide_elements.__name__}\n' @@ -108,8 +90,8 @@ def remove_establishment_guide(guide_id: int, establishment_id: int): from establishment.models import Establishment guide = Guide.objects.get(id=guide_id) - guide.change_state(Guide.REMOVING) try: + guide.change_state(Guide.REMOVING) establishment_qs = Establishment.objects.filter(id=establishment_id) if establishment_qs.exists(): establishment = establishment_qs.first() @@ -144,9 +126,9 @@ def generate_product_guide_elements(guide_id: int, filter_set: dict): from product.models import Product guide = Guide.objects.get(id=guide_id) - guide.change_state(Guide.BUILDING) queryset_values = Product.objects.filter(**filter_set).values() try: + guide.change_state(Guide.BUILDING) for instance in queryset_values: wine_id = instance.get('id') wine_qs = Product.objects.filter(id=wine_id) @@ -157,26 +139,29 @@ def generate_product_guide_elements(guide_id: int, filter_set: dict): wine_region_node, _ = GuideElement.objects.get_or_create_wine_region_node( root_node.id, wine.wine_region.id, - guide_id) + guide.id) if wine_region_node: yard_node, _ = GuideElement.objects.get_or_create_yard_node( wine.id, wine_region_node.id, - guide_id) + guide.id) if yard_node: wine_color_qs = wine.wine_colors if wine_color_qs.exists(): wine_color_section, _ = GuideElement.objects.get_or_create_color_wine_section_node( wine_color_qs.first().value, yard_node.id, - guide_id + guide.id ) if wine_color_section: - GuideElement.objects.get_or_create_wine_node( - **get_additional_product_data( - wine_color_section, - wine, - guide)) + params = { + 'color_wine_section_node_id': wine_color_section.id, + 'wine_id': wine.id, + 'guide_id': guide.id, + } + if wine.last_published_review: + params.update({'review_id': wine.last_published_review.id}) + GuideElement.objects.get_or_create_wine_node(**params) else: logger.error( f'METHOD_NAME: {generate_product_guide_elements.__name__}\n' diff --git a/project/settings/development.py b/project/settings/development.py index d5a31178..9539daee 100644 --- a/project/settings/development.py +++ b/project/settings/development.py @@ -1,6 +1,6 @@ """Development settings.""" -from .base import * from .amazon_s3 import * +from .base import * ALLOWED_HOSTS = ['gm.id-east.ru', '95.213.204.126', '0.0.0.0'] @@ -77,3 +77,33 @@ EMAIL_PORT = 587 MIDDLEWARE.append('utils.middleware.log_db_queries_per_API_request') + +LOGGING = { + 'version': 1, + 'disable_existing_loggers': False, + 'filters': { + 'require_debug_false': { + '()': 'django.utils.log.RequireDebugFalse', + }, + 'require_debug_true': { + '()': 'django.utils.log.RequireDebugTrue', + }, + }, + 'handlers': { + 'console': { + 'level': 'DEBUG', + 'filters': ['require_debug_true'], + 'class': 'logging.StreamHandler', + }, + 'null': { + 'class': 'logging.NullHandler', + }, + }, + 'loggers': { + 'django.db.backends': { + 'handlers': ['console', ], + 'level': 'ERROR', + 'propagate': False, + }, + } +} diff --git a/project/settings/local.py b/project/settings/local.py index 7278bff4..2c822c53 100644 --- a/project/settings/local.py +++ b/project/settings/local.py @@ -86,11 +86,11 @@ LOGGING = { 'py.warnings': { 'handlers': ['console'], }, - # 'django.db.backends': { - # 'handlers': ['console', ], - # 'level': 'DEBUG', - # 'propagate': False, - # }, + 'django.db.backends': { + 'handlers': ['console', ], + 'level': 'DEBUG', + 'propagate': False, + }, } } From 8e22119de9c621c4b514adf942393b0a08af0bcc Mon Sep 17 00:00:00 2001 From: Kuroshini Date: Wed, 5 Feb 2020 13:32:42 +0300 Subject: [PATCH 2/7] team --- apps/account/models.py | 1 + apps/account/urls/web.py | 2 + apps/account/views/web.py | 37 +++++++++++++++++++ .../invite_est_team_existing_user.html | 4 +- .../account/invite_est_team_new_user.html | 2 +- 5 files changed, 43 insertions(+), 3 deletions(-) diff --git a/apps/account/models.py b/apps/account/models.py index dd60086e..33a44569 100644 --- a/apps/account/models.py +++ b/apps/account/models.py @@ -388,6 +388,7 @@ class User(PhoneModelMixin, AbstractUser): 'twitter_page_url': socials.twitter_page_url if socials else '#', 'instagram_page_url': socials.instagram_page_url if socials else '#', 'facebook_page_url': socials.facebook_page_url if socials else '#', + 'contact_email': socials.contact_email if socials else '-', 'send_to': username, } diff --git a/apps/account/urls/web.py b/apps/account/urls/web.py index e590e76d..039ea3fd 100644 --- a/apps/account/urls/web.py +++ b/apps/account/urls/web.py @@ -10,6 +10,8 @@ urlpatterns_api = [ path('reset-password/', views.PasswordResetView.as_view(), name='password-reset'), path('reset-password/confirm///', views.PasswordResetConfirmView.as_view(), name='password-reset-confirm'), + path('join-establishment-team///', views.ApplyUserEstablishmentRole.as_view(), + name='join-establishment-team'), ] urlpatterns = urlpatterns_api + \ diff --git a/apps/account/views/web.py b/apps/account/views/web.py index f8853d3e..d8254fe4 100644 --- a/apps/account/views/web.py +++ b/apps/account/views/web.py @@ -72,3 +72,40 @@ class PasswordResetConfirmView(JWTGenericViewMixin, generics.GenericAPIView): access_token=tokens.get('access_token'), refresh_token=tokens.get('refresh_token')), response=Response(status=status.HTTP_200_OK)) + + +class ApplyUserEstablishmentRole(JWTGenericViewMixin, generics.GenericAPIView): + + queryset = models.User.objects.all() + permission_classes = (permissions.AllowAny,) + + def get_object(self): + """Overridden get_object method""" + queryset = self.filter_queryset(self.get_queryset()) + uidb64 = self.kwargs.get('uidb64') + + user_id = force_text(urlsafe_base64_decode(uidb64)) + token = self.kwargs.get('token') + + user = get_object_or_404(queryset, id=user_id) + + if not GMTokenGenerator(GMTokenGenerator.RESET_PASSWORD).check_token( + user, token): + raise utils_exceptions.NotValidTokenError() + + # May raise a permission denied + self.check_object_permissions(self.request, user) + + return get_object_or_404(klass=models.UserRole, pk=self.kwargs['role_id'], user=user), user + + def patch(self, request, *args, **kwargs): + instance, user = self.get_object() + instance.state = models.UserRole.VALIDATED + instance.save() + # Create tokens + tokens = user.create_jwt_tokens() + return self._put_cookies_in_response( + cookies=self._put_data_in_cookies( + access_token=tokens.get('access_token'), + refresh_token=tokens.get('refresh_token')), + response=Response(status=status.HTTP_200_OK)) diff --git a/project/templates/account/invite_est_team_existing_user.html b/project/templates/account/invite_est_team_existing_user.html index fcdca24a..a70c225b 100644 --- a/project/templates/account/invite_est_team_existing_user.html +++ b/project/templates/account/invite_est_team_existing_user.html @@ -31,7 +31,7 @@

- {% blocktrans %}Hi, you are receiving this email because you have been granted to manage the establishment %restaurant_name%. + {% blocktrans %}Hi, you are receiving this email because you have been granted to manage the establishment {{ restaurant_name }}. From now, you can manage information about this place and have it published in {{ site_name }}.{% endblocktrans %}

@@ -66,7 +66,7 @@ From now, you can manage information about this place and have it published in {
In case that you were not already a Gault&Millau user, a temporary account with your email has been temporarily created by a Gault&Millau administrator. By completing the creation process of your account, you agree to have this account permanently created. -This temporary account will be deleted after 7 days if you don't complete the registration process. Please contact %license_contact_email% if you have any questions. +This temporary account will be deleted after 7 days if you don't complete the registration process. Please contact {{ contact_email }} if you have any questions.