-
Notifications
You must be signed in to change notification settings - Fork 0
chore: improve query performance #31
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,15 +1,25 @@ | ||||||||||||||||||||||||||
| from common.pagination.base_pagination import BasePagination | ||||||||||||||||||||||||||
| from django.db.models import Prefetch | ||||||||||||||||||||||||||
| from django_filters.rest_framework import DjangoFilterBackend | ||||||||||||||||||||||||||
| from rest_framework.filters import OrderingFilter | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| from apps.custom_email.models import OrganizationEmail | ||||||||||||||||||||||||||
| from apps.custom_email.serializers import OrganizationEmailSerializer | ||||||||||||||||||||||||||
| from apps.organization_setting.models import OrganizationTheme | ||||||||||||||||||||||||||
|
Comment on lines
+2
to
+8
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since
Suggested change
|
||||||||||||||||||||||||||
| from utils.views import OrganizationListAPIView | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| class ListCustomEmailView(OrganizationListAPIView): | ||||||||||||||||||||||||||
| serializer_class = OrganizationEmailSerializer | ||||||||||||||||||||||||||
| queryset = OrganizationEmail.objects.all() | ||||||||||||||||||||||||||
| queryset = OrganizationEmail.objects.select_related( | ||||||||||||||||||||||||||
| "organization", | ||||||||||||||||||||||||||
| "organization__organization_settings", | ||||||||||||||||||||||||||
| ).prefetch_related( | ||||||||||||||||||||||||||
| Prefetch( | ||||||||||||||||||||||||||
| "organization__organization_settings__themes", | ||||||||||||||||||||||||||
| queryset=OrganizationTheme.objects.all(), | ||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||
|
Comment on lines
+14
to
+22
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Using queryset = OrganizationEmail.objects.select_related(
"organization",
"organization__organization_settings",
).prefetch_related(
"organization__organization_settings__themes"
) |
||||||||||||||||||||||||||
| organization_field = "organization" | ||||||||||||||||||||||||||
| pagination_class = BasePagination | ||||||||||||||||||||||||||
| filterset_fields = ["email_type"] | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -108,22 +108,26 @@ | |||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| def create_default_policies(organization): | ||||||||||||||||||||||||||||||||||||||
| organization_policies = [] | ||||||||||||||||||||||||||||||||||||||
| for policy in default_policies: | ||||||||||||||||||||||||||||||||||||||
| organization_policy = OrganizationPolicy(**policy, organization=organization) | ||||||||||||||||||||||||||||||||||||||
| organization_policy.save() | ||||||||||||||||||||||||||||||||||||||
| organization_policies.append(organization_policy.pk) | ||||||||||||||||||||||||||||||||||||||
| organization_policies = [ | ||||||||||||||||||||||||||||||||||||||
| OrganizationPolicy(**policy, organization=organization) | ||||||||||||||||||||||||||||||||||||||
| for policy in default_policies | ||||||||||||||||||||||||||||||||||||||
| ] | ||||||||||||||||||||||||||||||||||||||
| OrganizationPolicy.objects.bulk_create(organization_policies) | ||||||||||||||||||||||||||||||||||||||
| return organization_policies | ||||||||||||||||||||||||||||||||||||||
|
Comment on lines
110
to
116
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Django's
Suggested change
|
||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| def create_default_organization_role_by_policy_tag(name, tag, organization): | ||||||||||||||||||||||||||||||||||||||
| policies = OrganizationPolicy.objects.filter( | ||||||||||||||||||||||||||||||||||||||
| tags__icontains=tag, organization=organization | ||||||||||||||||||||||||||||||||||||||
| ).all() | ||||||||||||||||||||||||||||||||||||||
| def create_default_organization_role_by_policy_tag( | ||||||||||||||||||||||||||||||||||||||
| name, tag, organization, policies=None | ||||||||||||||||||||||||||||||||||||||
| ): | ||||||||||||||||||||||||||||||||||||||
| if policies is None: | ||||||||||||||||||||||||||||||||||||||
| policies = OrganizationPolicy.objects.filter( | ||||||||||||||||||||||||||||||||||||||
| tags__contains=[tag], organization=organization | ||||||||||||||||||||||||||||||||||||||
| ).all() | ||||||||||||||||||||||||||||||||||||||
| else: | ||||||||||||||||||||||||||||||||||||||
| policies = [policy for policy in policies if tag in policy.tags] | ||||||||||||||||||||||||||||||||||||||
| organization_role = OrganizationRole(name=name, organization=organization) | ||||||||||||||||||||||||||||||||||||||
| organization_role.save() | ||||||||||||||||||||||||||||||||||||||
| organization_role.policies.set([policy.pk for policy in policies]) | ||||||||||||||||||||||||||||||||||||||
| organization_role.save() | ||||||||||||||||||||||||||||||||||||||
| return organization_role | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Accessing Django's private
_prefetched_objects_cacheattribute is fragile and can break in future Django updates. Instead, you can use the publicsetting.themes.all()API. If thethemesrelation has been prefetched, Django will automatically use the prefetched cache without hitting the database. If it hasn't been prefetched, it will perform a query to fetch the themes. Since an organization setting typically has very few themes (e.g., light and dark), fetching all of them is highly efficient and much more maintainable.