perf: oidc 替换原有的is_ajax方法,优化accountbackupexecution 迁移文件 (#11274)

Co-authored-by: feng <1304903146@qq.com>
pull/11277/head
fit2bot 2023-08-14 18:37:28 +08:00 committed by GitHub
parent efe57b3ebe
commit c11ba16e4e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 35 additions and 13 deletions

View File

@ -1,8 +1,9 @@
# Generated by Django 4.1.10 on 2023-08-03 08:28 # Generated by Django 4.1.10 on 2023-08-03 08:28
from django.conf import settings from django.conf import settings
from django.db import migrations, models from django.db import migrations, models
import common.db.encoder
def migrate_recipients(apps, schema_editor): def migrate_recipients(apps, schema_editor):
account_backup_model = apps.get_model('accounts', 'AccountBackupAutomation') account_backup_model = apps.get_model('accounts', 'AccountBackupAutomation')
@ -13,13 +14,22 @@ def migrate_recipients(apps, schema_editor):
continue continue
account_backup.recipients_part_one.set(recipients) account_backup.recipients_part_one.set(recipients)
execution_bojs = [] objs = []
for execution in execution_model.objects.all(): for execution in execution_model.objects.all():
snapshot = execution.snapshot snapshot = execution.snapshot
recipients = snapshot.pop('recipients', {}) recipients = snapshot.pop('recipients', {})
snapshot.update({'recipients_part_one': recipients, 'recipients_part_two': {}}) snapshot.update({'recipients_part_one': recipients, 'recipients_part_two': {}})
execution_bojs.append(execution) objs.append(execution)
execution_model.objects.bulk_update(execution_bojs, ['snapshot']) 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): class Migration(migrations.Migration):
@ -45,12 +55,20 @@ class Migration(migrations.Migration):
to=settings.AUTH_USER_MODEL, verbose_name='Recipient part two' to=settings.AUTH_USER_MODEL, verbose_name='Recipient part two'
), ),
), ),
migrations.RenameField( migrations.AddField(
model_name='accountbackupexecution', model_name='accountbackupexecution',
old_name='plan_snapshot', name='snapshot',
new_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.RunPython(migrate_recipients),
migrations.RemoveField(
model_name='accountbackupexecution',
name='plan_snapshot',
),
migrations.RemoveField( migrations.RemoveField(
model_name='accountbackupautomation', model_name='accountbackupautomation',
name='recipients', name='recipients',

View File

@ -1,15 +1,14 @@
import time import time
import requests import requests
import requests.exceptions import requests.exceptions
from django.core.exceptions import MiddlewareNotUsed
from django.conf import settings from django.conf import settings
from django.contrib import auth from django.contrib import auth
from django.core.exceptions import MiddlewareNotUsed
from common.utils import get_logger from common.utils import get_logger
from .utils import validate_and_return_id_token
from .decorator import ssl_verification from .decorator import ssl_verification
from .utils import validate_and_return_id_token
logger = get_logger(__file__) logger = get_logger(__file__)
@ -25,11 +24,16 @@ class OIDCRefreshIDTokenMiddleware:
def __call__(self, request): def __call__(self, request):
# Refreshes tokens only in the applicable cases. # 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) self.refresh_token(request)
response = self.get_response(request) response = self.get_response(request)
return response return response
@staticmethod
def is_ajax(request):
return request.META.get('HTTP_X_REQUESTED_WITH') == 'XMLHttpRequest'
@ssl_verification @ssl_verification
def refresh_token(self, request): def refresh_token(self, request):
""" Refreshes the token of the current user. """ """ Refreshes the token of the current user. """