From c11ba16e4e3219bc394892ae97c6794ae2218851 Mon Sep 17 00:00:00 2001 From: fit2bot <68588906+fit2bot@users.noreply.github.com> Date: Mon, 14 Aug 2023 18:37:28 +0800 Subject: [PATCH] =?UTF-8?q?perf:=20oidc=20=E6=9B=BF=E6=8D=A2=E5=8E=9F?= =?UTF-8?q?=E6=9C=89=E7=9A=84is=5Fajax=E6=96=B9=E6=B3=95=EF=BC=8C=E4=BC=98?= =?UTF-8?q?=E5=8C=96accountbackupexecution=20=E8=BF=81=E7=A7=BB=E6=96=87?= =?UTF-8?q?=E4=BB=B6=20(#11274)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: feng <1304903146@qq.com> --- .../0013_account_backup_recipients.py | 32 +++++++++++++++---- .../backends/oidc/middleware.py | 16 ++++++---- 2 files changed, 35 insertions(+), 13 deletions(-) diff --git a/apps/accounts/migrations/0013_account_backup_recipients.py b/apps/accounts/migrations/0013_account_backup_recipients.py index ac9956b76..6fe03e349 100644 --- a/apps/accounts/migrations/0013_account_backup_recipients.py +++ b/apps/accounts/migrations/0013_account_backup_recipients.py @@ -1,8 +1,9 @@ # Generated by Django 4.1.10 on 2023-08-03 08:28 - from django.conf import settings from django.db import migrations, models +import common.db.encoder + def migrate_recipients(apps, schema_editor): account_backup_model = apps.get_model('accounts', 'AccountBackupAutomation') @@ -13,13 +14,22 @@ def migrate_recipients(apps, schema_editor): continue account_backup.recipients_part_one.set(recipients) - execution_bojs = [] + objs = [] for execution in execution_model.objects.all(): snapshot = execution.snapshot recipients = snapshot.pop('recipients', {}) snapshot.update({'recipients_part_one': recipients, 'recipients_part_two': {}}) - execution_bojs.append(execution) - execution_model.objects.bulk_update(execution_bojs, ['snapshot']) + objs.append(execution) + execution_model.objects.bulk_update(objs, ['snapshot']) + + +def migrate_snapshot(apps, schema_editor): + model = apps.get_model('accounts', 'AccountBackupExecution') + objs = [] + for execution in model.objects.all(): + execution.snapshot = execution.plan_snapshot + objs.append(execution) + model.objects.bulk_update(objs, ['snapshot']) class Migration(migrations.Migration): @@ -45,12 +55,20 @@ class Migration(migrations.Migration): to=settings.AUTH_USER_MODEL, verbose_name='Recipient part two' ), ), - migrations.RenameField( + migrations.AddField( model_name='accountbackupexecution', - old_name='plan_snapshot', - new_name='snapshot', + name='snapshot', + field=models.JSONField( + default=dict, encoder=common.db.encoder.ModelJSONFieldEncoder, + null=True, blank=True, verbose_name='Account backup snapshot' + ), ), + migrations.RunPython(migrate_snapshot), migrations.RunPython(migrate_recipients), + migrations.RemoveField( + model_name='accountbackupexecution', + name='plan_snapshot', + ), migrations.RemoveField( model_name='accountbackupautomation', name='recipients', diff --git a/apps/authentication/backends/oidc/middleware.py b/apps/authentication/backends/oidc/middleware.py index c2ad33637..4481951e4 100644 --- a/apps/authentication/backends/oidc/middleware.py +++ b/apps/authentication/backends/oidc/middleware.py @@ -1,15 +1,14 @@ import time + import requests import requests.exceptions - -from django.core.exceptions import MiddlewareNotUsed from django.conf import settings from django.contrib import auth +from django.core.exceptions import MiddlewareNotUsed + from common.utils import get_logger - -from .utils import validate_and_return_id_token from .decorator import ssl_verification - +from .utils import validate_and_return_id_token logger = get_logger(__file__) @@ -25,11 +24,16 @@ class OIDCRefreshIDTokenMiddleware: def __call__(self, request): # Refreshes tokens only in the applicable cases. - if request.method == 'GET' and not request.is_ajax() and request.user.is_authenticated and settings.AUTH_OPENID: + if request.method == 'GET' and not self.is_ajax(request) and \ + request.user.is_authenticated and settings.AUTH_OPENID: self.refresh_token(request) response = self.get_response(request) return response + @staticmethod + def is_ajax(request): + return request.META.get('HTTP_X_REQUESTED_WITH') == 'XMLHttpRequest' + @ssl_verification def refresh_token(self, request): """ Refreshes the token of the current user. """