From 061c96d198bf4470b6dcafaf3424ad673ccb5988 Mon Sep 17 00:00:00 2001 From: Anatoly Date: Wed, 5 Feb 2020 17:58:56 +0300 Subject: [PATCH] refactored a little, added Gin indexes to `account.User` model. --- .../migrations/0036_auto_20200205_1457.py | 34 +++++++++++++++++++ apps/account/models.py | 8 +++++ apps/news/models.py | 5 +-- apps/report/models.py | 4 +++ 4 files changed, 49 insertions(+), 2 deletions(-) create mode 100644 apps/account/migrations/0036_auto_20200205_1457.py diff --git a/apps/account/migrations/0036_auto_20200205_1457.py b/apps/account/migrations/0036_auto_20200205_1457.py new file mode 100644 index 00000000..94691d14 --- /dev/null +++ b/apps/account/migrations/0036_auto_20200205_1457.py @@ -0,0 +1,34 @@ +# Generated by Django 2.2.7 on 2020-02-05 14:57 + +import django.contrib.postgres.indexes +from django.db import migrations + + +class Migration(migrations.Migration): + + dependencies = [ + ('account', '0035_userrole_for_team'), + ] + + operations = [ + migrations.AddIndex( + model_name='user', + index=django.contrib.postgres.indexes.GinIndex(fields=['username'], name='account_use_usernam_20b96d_gin'), + ), + migrations.AddIndex( + model_name='user', + index=django.contrib.postgres.indexes.GinIndex(fields=['first_name'], name='account_use_first_n_472c42_gin'), + ), + migrations.AddIndex( + model_name='user', + index=django.contrib.postgres.indexes.GinIndex(fields=['last_name'], name='account_use_last_na_595e99_gin'), + ), + migrations.AddIndex( + model_name='user', + index=django.contrib.postgres.indexes.GinIndex(fields=['email'], name='account_use_email_2f1668_gin'), + ), + migrations.AddIndex( + model_name='user', + index=django.contrib.postgres.indexes.GinIndex(fields=['phone'], name='account_use_phone_9593fb_gin'), + ), + ] diff --git a/apps/account/models.py b/apps/account/models.py index 76513e5e..bc6ca299 100644 --- a/apps/account/models.py +++ b/apps/account/models.py @@ -5,6 +5,7 @@ from typing import List from django.conf import settings from django.contrib.auth.models import AbstractUser, UserManager as BaseUserManager +from django.contrib.postgres.indexes import GinIndex from django.contrib.postgres.search import TrigramSimilarity from django.core.mail import send_mail from django.db import models @@ -289,6 +290,13 @@ class User(PhoneModelMixin, AbstractUser): """Meta class.""" verbose_name = _('User') verbose_name_plural = _('Users') + indexes = [ + GinIndex(fields=['username']), + GinIndex(fields=['first_name']), + GinIndex(fields=['last_name']), + GinIndex(fields=['email']), + GinIndex(fields=['phone']), + ] def __str__(self): """String method.""" diff --git a/apps/news/models.py b/apps/news/models.py index a792fc18..e840f9c5 100644 --- a/apps/news/models.py +++ b/apps/news/models.py @@ -19,7 +19,8 @@ from rest_framework.reverse import reverse from main.models import Carousel from rating.models import Rating from utils.models import ( - BaseAttributes, FavoritesMixin, GalleryMixin, HasTagsMixin, IntermediateGalleryModelMixin, ProjectBaseMixin, + BaseAttributes, FavoritesMixin, GalleryMixin, HasTagsMixin, IntermediateGalleryModelMixin, + ProjectBaseMixin, TJSONField, TranslatedFieldsMixin, TypeDefaultImageMixin, ) from utils.querysets import TranslationQuerysetMixin @@ -252,7 +253,7 @@ class NewsQuerySet(TranslationQuerysetMixin): ).filter(relevance__gte=0.3).order_by('-relevance') def available_news(self, user, country_code: str): - """Return QuerySet with news that user has an access.""" + """Return QuerySet with news that user has access.""" return self.filter(site__country__code=country_code) if not user.is_superuser else self diff --git a/apps/report/models.py b/apps/report/models.py index ef3d197e..b89e992d 100644 --- a/apps/report/models.py +++ b/apps/report/models.py @@ -68,6 +68,10 @@ class Report(ProjectBaseMixin): verbose_name = _('Report') verbose_name_plural = _('Reports') + def __str__(self): + """Implement `str` dunder method.""" + return f'{self.id}: {self.get_category_display()} ({self.url})' + def get_body_email_message(self): """Prepare the body of the email message""" return {