Commit 1af40b6c authored by ibuler's avatar ibuler

[Update] 支持etag

parent 8ed3bb85
...@@ -35,12 +35,12 @@ class CommandFilterRuleSerializer(BulkOrgResourceModelSerializer): ...@@ -35,12 +35,12 @@ class CommandFilterRuleSerializer(BulkOrgResourceModelSerializer):
fields = '__all__' fields = '__all__'
list_serializer_class = AdaptedBulkListSerializer list_serializer_class = AdaptedBulkListSerializer
def validate_content(self, content): # def validate_content(self, content):
tp = self.initial_data.get("type") # tp = self.initial_data.get("type")
if tp == CommandFilterRule.TYPE_REGEX: # if tp == CommandFilterRule.TYPE_REGEX:
return content # return content
if self.invalid_pattern.search(content): # if self.invalid_pattern.search(content):
invalid_char = self.invalid_pattern.pattern.replace('\\', '') # invalid_char = self.invalid_pattern.pattern.replace('\\', '')
msg = _("Content should not be contain: {}").format(invalid_char) # msg = _("Content should not be contain: {}").format(invalid_char)
raise serializers.ValidationError(msg) # raise serializers.ValidationError(msg)
return content # return content
...@@ -6,6 +6,8 @@ from django.core.cache import cache ...@@ -6,6 +6,8 @@ from django.core.cache import cache
from django.db.models import Q from django.db.models import Q
from django.conf import settings from django.conf import settings
from rest_framework.views import Response from rest_framework.views import Response
from django.utils.decorators import method_decorator
from django.views.decorators.http import condition
from django.utils.translation import ugettext as _ from django.utils.translation import ugettext as _
from common.utils import get_logger from common.utils import get_logger
...@@ -14,7 +16,7 @@ from ..utils import ( ...@@ -14,7 +16,7 @@ from ..utils import (
AssetPermissionUtil AssetPermissionUtil
) )
from .. import const from .. import const
from ..hands import Asset, Node, SystemUser, Label from ..hands import Asset, Node, SystemUser
from .. import serializers from .. import serializers
logger = get_logger(__name__) logger = get_logger(__name__)
...@@ -22,6 +24,17 @@ logger = get_logger(__name__) ...@@ -22,6 +24,17 @@ logger = get_logger(__name__)
__all__ = ['UserPermissionCacheMixin', 'GrantAssetsMixin', 'NodesWithUngroupMixin'] __all__ = ['UserPermissionCacheMixin', 'GrantAssetsMixin', 'NodesWithUngroupMixin']
def get_etag(request, *args, **kwargs):
cache_policy = request.GET.get("cache_policy")
if cache_policy != '1':
return None
view = request.parser_context.get("view")
if not view:
return None
etag = view.get_meta_cache_id()
return etag
class UserPermissionCacheMixin: class UserPermissionCacheMixin:
cache_policy = '0' cache_policy = '0'
RESP_CACHE_KEY = '_PERMISSION_RESPONSE_CACHE_V2_{}' RESP_CACHE_KEY = '_PERMISSION_RESPONSE_CACHE_V2_{}'
...@@ -96,6 +109,7 @@ class UserPermissionCacheMixin: ...@@ -96,6 +109,7 @@ class UserPermissionCacheMixin:
cache.set(key, response.data, self.CACHE_TIME) cache.set(key, response.data, self.CACHE_TIME)
logger.debug("Set response to cache: {}".format(key)) logger.debug("Set response to cache: {}".format(key))
@method_decorator(condition(etag_func=get_etag))
def get(self, request, *args, **kwargs): def get(self, request, *args, **kwargs):
self.cache_policy = request.GET.get('cache_policy', '0') self.cache_policy = request.GET.get('cache_policy', '0')
......
...@@ -3,6 +3,7 @@ ...@@ -3,6 +3,7 @@
import uuid import uuid
from django.shortcuts import get_object_or_404 from django.shortcuts import get_object_or_404
from rest_framework.views import APIView, Response from rest_framework.views import APIView, Response
from rest_framework.generics import ( from rest_framework.generics import (
ListAPIView, get_object_or_404, RetrieveAPIView ListAPIView, get_object_or_404, RetrieveAPIView
) )
......
...@@ -223,7 +223,7 @@ function formSubmit(props) { ...@@ -223,7 +223,7 @@ function formSubmit(props) {
return return
} }
$.each(errors, function (k, v) { $.each(errors, function (k, v) {
var fieldRef = props.form.find('input[name="' + k + '"]'); var fieldRef = props.form.find('[name="' + k + '"]');
var formGroupRef = fieldRef.parents('.form-group'); var formGroupRef = fieldRef.parents('.form-group');
var parentRef = fieldRef.parent(); var parentRef = fieldRef.parent();
var helpBlockRef = parentRef.children('.help-block.error'); var helpBlockRef = parentRef.children('.help-block.error');
...@@ -237,7 +237,7 @@ function formSubmit(props) { ...@@ -237,7 +237,7 @@ function formSubmit(props) {
helpBlockRef.html(help_msg); helpBlockRef.html(help_msg);
} else { } else {
$.each(v, function (kk, vv) { $.each(v, function (kk, vv) {
if (typeof errors === "object") { if (typeof vv === "object") {
$.each(vv, function (kkk, vvv) { $.each(vv, function (kkk, vvv) {
noneFieldErrorMsg += " " + vvv + '<br/>'; noneFieldErrorMsg += " " + vvv + '<br/>';
}) })
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment