mirror of https://github.com/jumpserver/jumpserver
perf: oidc 替换原有的is_ajax方法,优化accountbackupexecution 迁移文件 (#11274)
Co-authored-by: feng <1304903146@qq.com>pull/11277/head
parent
efe57b3ebe
commit
c11ba16e4e
|
@ -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',
|
||||
|
|
|
@ -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. """
|
||||
|
|
Loading…
Reference in New Issue