<s> from __future__ import unicode_literals <EOL> from django . contrib import auth <EOL> from django . contrib . contenttypes . models import ContentType <EOL> from django . db . models import Q <EOL> from django . utils import six <EOL> import django_filters <EOL> from django_filters . filterset import FilterSetMetaclass <EOL> from rest_framework . filters import BaseFilterBackend , DjangoFilterBackend <EOL> import taggit <EOL> from nodeconductor . core import filters as core_filters <EOL> from nodeconductor . core import models as core_models <EOL> from nodeconductor . core . filters import BaseExternalFilter <EOL> from nodeconductor . logging . filters import ExternalAlertFilterBackend <EOL> from nodeconductor . structure import models <EOL> from nodeconductor . structure import serializers <EOL> from nodeconductor . structure import SupportedServices <EOL> from nodeconductor . structure . managers import filter_queryset_for_user <EOL> User = auth . get_user_model ( ) <EOL> class ScopeTypeFilterBackend ( DjangoFilterBackend ) : <EOL> """<STR_LIT>""" <EOL> scope_field = '<STR_LIT>' <EOL> scope_param = '<STR_LIT>' <EOL> scope_models = ( <EOL> models . Resource , <EOL> models . Service , <EOL> models . ServiceProjectLink , <EOL> models . Project , <EOL> models . Customer ) <EOL> @ classmethod <EOL> def get_scope_type ( cls , obj ) : <EOL> field = getattr ( obj , cls . scope_field ) <EOL> for model in cls . scope_models : <EOL> if isinstance ( field , model ) : <EOL> return model . _meta . model_name <EOL> @ classmethod <EOL> def get_scope_models ( cls , types ) : <EOL> for model in cls . scope_models : <EOL> if model . _meta . model_name in types : <EOL> try : <EOL> for submodel in model . get_all_models ( ) : <EOL> yield submodel <EOL> except AttributeError : <EOL> yield model <EOL> @ classmethod <EOL> def get_scope_content_types ( cls , types ) : <EOL> return ContentType . objects . get_for_models ( * cls . get_scope_models ( types ) ) . values ( ) <EOL> @ classmethod <EOL> def get_ct_field ( cls , obj_or_cls ) : <EOL> return next ( field . ct_field for field in obj_or_cls . _meta . virtual_fields if field . name == cls . scope_field ) <EOL> def filter_queryset ( self , request , queryset , view ) : <EOL> if self . scope_param in request . query_params : <EOL> content_types = self . get_scope_content_types ( request . query_params . getlist ( self . scope_param ) ) <EOL> return queryset . filter ( ** { '<STR_LIT>' % self . get_ct_field ( queryset . model ) : content_types } ) <EOL> return queryset <EOL> class GenericRoleFilter ( BaseFilterBackend ) : <EOL> def filter_queryset ( self , request , queryset , view ) : <EOL> return filter_queryset_for_user ( queryset , request . user ) <EOL> class CustomerFilter ( django_filters . FilterSet ) : <EOL> name = django_filters . CharFilter ( <EOL> lookup_type = '<STR_LIT>' , <EOL> ) <EOL> native_name = django_filters . CharFilter ( <EOL> lookup_type = '<STR_LIT>' , <EOL> ) <EOL> abbreviation = django_filters . CharFilter ( <EOL> lookup_type = '<STR_LIT>' , <EOL> ) <EOL> contact_details = django_filters . CharFilter ( <EOL> lookup_type = '<STR_LIT>' , <EOL> ) <EOL> class Meta ( object ) : <EOL> model = models . Customer <EOL> fields = [ <EOL> '<STR_LIT:name>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ] <EOL> order_by = [ <EOL> '<STR_LIT:name>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ] <EOL> class ProjectFilter ( django_filters . FilterSet ) : <EOL> customer = django_filters . CharFilter ( <EOL> name = '<STR_LIT>' , <EOL> distinct = True , <EOL> ) <EOL> customer_name = django_filters . CharFilter ( <EOL> name = '<STR_LIT>' , <EOL> distinct = True , <EOL> lookup_type = '<STR_LIT>' <EOL> ) <EOL> customer_native_name = django_filters . CharFilter ( <EOL> name = '<STR_LIT>' , <EOL> distinct = True , <EOL> lookup_type = '<STR_LIT>' <EOL> ) <EOL> customer_abbreviation = django_filters . CharFilter ( <EOL> name = '<STR_LIT>' , <EOL> distinct = True , <EOL> lookup_type = '<STR_LIT>' <EOL> ) <EOL> project_group = django_filters . CharFilter ( <EOL> name = '<STR_LIT>' , <EOL> distinct = True , <EOL> ) <EOL> project_group_name = django_filters . CharFilter ( <EOL> name = '<STR_LIT>' , <EOL> distinct = True , <EOL> lookup_type = '<STR_LIT>' <EOL> ) <EOL> name = django_filters . CharFilter ( lookup_type = '<STR_LIT>' ) <EOL> description = django_filters . CharFilter ( lookup_type = '<STR_LIT>' ) <EOL> class Meta ( object ) : <EOL> model = models . Project <EOL> fields = [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT:name>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT:description>' , <EOL> '<STR_LIT>' , <EOL> ] <EOL> order_by = [ <EOL> '<STR_LIT:name>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ] <EOL> order_by_mapping = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } <EOL> class ProjectGroupFilter ( django_filters . FilterSet ) : <EOL> customer = django_filters . CharFilter ( <EOL> name = '<STR_LIT>' , <EOL> distinct = True , <EOL> ) <EOL> customer_name = django_filters . CharFilter ( <EOL> name = '<STR_LIT>' , <EOL> distinct = True , <EOL> lookup_type = '<STR_LIT>' , <EOL> ) <EOL> customer_native_name = django_filters . CharFilter ( <EOL> name = '<STR_LIT>' , <EOL> distinct = True , <EOL> lookup_type = '<STR_LIT>' , <EOL> ) <EOL> customer_abbreviation = django_filters . CharFilter ( <EOL> name = '<STR_LIT>' , <EOL> distinct = True , <EOL> lookup_type = '<STR_LIT>' , <EOL> ) <EOL> name = django_filters . CharFilter ( lookup_type = '<STR_LIT>' ) <EOL> class Meta ( object ) : <EOL> model = models . ProjectGroup <EOL> fields = [ <EOL> '<STR_LIT:name>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ] <EOL> order_by = [ <EOL> '<STR_LIT:name>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ] <EOL> order_by_mapping = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } <EOL> class ProjectGroupMembershipFilter ( django_filters . FilterSet ) : <EOL> project_group = django_filters . CharFilter ( <EOL> name = '<STR_LIT>' , <EOL> ) <EOL> project_group_name = django_filters . CharFilter ( <EOL> name = '<STR_LIT>' , <EOL> lookup_type = '<STR_LIT>' , <EOL> ) <EOL> project = django_filters . CharFilter ( <EOL> name = '<STR_LIT>' , <EOL> ) <EOL> project_name = django_filters . CharFilter ( <EOL> name = '<STR_LIT>' , <EOL> lookup_type = '<STR_LIT>' , <EOL> ) <EOL> class Meta ( object ) : <EOL> model = models . ProjectGroup . projects . through <EOL> fields = [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ] <EOL> class UserFilter ( django_filters . FilterSet ) : <EOL> project_group = django_filters . CharFilter ( <EOL> name = '<STR_LIT>' , <EOL> distinct = True , <EOL> lookup_type = '<STR_LIT>' , <EOL> ) <EOL> project = django_filters . CharFilter ( <EOL> name = '<STR_LIT>' , <EOL> distinct = True , <EOL> lookup_type = '<STR_LIT>' , <EOL> ) <EOL> full_name = django_filters . CharFilter ( lookup_type = '<STR_LIT>' ) <EOL> username = django_filters . CharFilter ( ) <EOL> native_name = django_filters . CharFilter ( lookup_type = '<STR_LIT>' ) <EOL> job_title = django_filters . CharFilter ( lookup_type = '<STR_LIT>' ) <EOL> email = django_filters . CharFilter ( lookup_type = '<STR_LIT>' ) <EOL> is_active = django_filters . BooleanFilter ( ) <EOL> class Meta ( object ) : <EOL> model = User <EOL> fields = [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT:email>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT:description>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT:username>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ] <EOL> order_by = [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT:email>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT:description>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT:username>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ] <EOL> class ProjectPermissionFilter ( django_filters . FilterSet ) : <EOL> project = django_filters . CharFilter ( <EOL> name = '<STR_LIT>' , <EOL> ) <EOL> project_url = core_filters . URLFilter ( <EOL> view_name = '<STR_LIT>' , <EOL> name = '<STR_LIT>' , <EOL> ) <EOL> user_url = core_filters . URLFilter ( <EOL> view_name = '<STR_LIT>' , <EOL> name = '<STR_LIT>' , <EOL> ) <EOL> username = django_filters . CharFilter ( <EOL> name = '<STR_LIT>' , <EOL> lookup_type = '<STR_LIT>' , <EOL> ) <EOL> full_name = django_filters . CharFilter ( <EOL> name = '<STR_LIT>' , <EOL> lookup_type = '<STR_LIT>' , <EOL> ) <EOL> native_name = django_filters . CharFilter ( <EOL> name = '<STR_LIT>' , <EOL> lookup_type = '<STR_LIT>' , <EOL> ) <EOL> role = core_filters . MappedChoiceFilter ( <EOL> name = '<STR_LIT>' , <EOL> choices = ( <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( models . ProjectRole . ADMINISTRATOR , '<STR_LIT>' ) , <EOL> ( models . ProjectRole . MANAGER , '<STR_LIT>' ) , <EOL> ) , <EOL> choice_mappings = { <EOL> '<STR_LIT>' : models . ProjectRole . ADMINISTRATOR , <EOL> '<STR_LIT>' : models . ProjectRole . MANAGER , <EOL> } , <EOL> ) <EOL> class Meta ( object ) : <EOL> model = User . groups . through <EOL> fields = [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT:username>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ] <EOL> order_by = [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ] <EOL> class ProjectGroupPermissionFilter ( django_filters . FilterSet ) : <EOL> project_group = django_filters . CharFilter ( <EOL> name = '<STR_LIT>' , <EOL> ) <EOL> project_group_url = core_filters . URLFilter ( <EOL> view_name = '<STR_LIT>' , <EOL> name = '<STR_LIT>' , <EOL> ) <EOL> user_url = core_filters . URLFilter ( <EOL> view_name = '<STR_LIT>' , <EOL> name = '<STR_LIT>' , <EOL> ) <EOL> username = django_filters . CharFilter ( <EOL> name = '<STR_LIT>' , <EOL> lookup_type = '<STR_LIT>' , <EOL> ) <EOL> full_name = django_filters . CharFilter ( <EOL> name = '<STR_LIT>' , <EOL> lookup_type = '<STR_LIT>' , <EOL> ) <EOL> native_name = django_filters . CharFilter ( <EOL> name = '<STR_LIT>' , <EOL> lookup_type = '<STR_LIT>' , <EOL> ) <EOL> role = core_filters . MappedChoiceFilter ( <EOL> name = '<STR_LIT>' , <EOL> choices = ( <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( models . ProjectGroupRole . MANAGER , '<STR_LIT>' ) , <EOL> ) , <EOL> choice_mappings = { <EOL> '<STR_LIT>' : models . ProjectGroupRole . MANAGER , <EOL> } , <EOL> ) <EOL> class Meta ( object ) : <EOL> model = User . groups . through <EOL> fields = [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT:username>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ] <EOL> order_by = [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ] <EOL> class CustomerPermissionFilter ( django_filters . FilterSet ) : <EOL> customer = django_filters . CharFilter ( <EOL> name = '<STR_LIT>' , <EOL> ) <EOL> customer_url = core_filters . URLFilter ( <EOL> view_name = '<STR_LIT>' , <EOL> name = '<STR_LIT>' , <EOL> ) <EOL> user_url = core_filters . URLFilter ( <EOL> view_name = '<STR_LIT>' , <EOL> name = '<STR_LIT>' , <EOL> ) <EOL> username = django_filters . CharFilter ( <EOL> name = '<STR_LIT>' , <EOL> lookup_type = '<STR_LIT>' , <EOL> ) <EOL> full_name = django_filters . CharFilter ( <EOL> name = '<STR_LIT>' , <EOL> lookup_type = '<STR_LIT>' , <EOL> ) <EOL> native_name = django_filters . CharFilter ( <EOL> name = '<STR_LIT>' , <EOL> lookup_type = '<STR_LIT>' , <EOL> ) <EOL> role = core_filters . MappedChoiceFilter ( <EOL> name = '<STR_LIT>' , <EOL> choices = ( <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( models . CustomerRole . OWNER , '<STR_LIT>' ) , <EOL> ) , <EOL> choice_mappings = { <EOL> '<STR_LIT>' : models . CustomerRole . OWNER , <EOL> } , <EOL> ) <EOL> class Meta ( object ) : <EOL> model = User . groups . through <EOL> fields = [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT:username>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ] <EOL> order_by = [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ] <EOL> class SshKeyFilter ( django_filters . FilterSet ) : <EOL> uuid = django_filters . CharFilter ( ) <EOL> user_uuid = django_filters . CharFilter ( <EOL> name = '<STR_LIT>' <EOL> ) <EOL> name = django_filters . CharFilter ( lookup_type = '<STR_LIT>' ) <EOL> class Meta ( object ) : <EOL> model = core_models . SshPublicKey <EOL> fields = [ <EOL> '<STR_LIT:name>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' <EOL> ] <EOL> order_by = [ <EOL> '<STR_LIT:name>' , <EOL> '<STR_LIT>' , <EOL> ] <EOL> class ServiceSettingsFilter ( django_filters . FilterSet ) : <EOL> name = django_filters . CharFilter ( lookup_type = '<STR_LIT>' ) <EOL> type = core_filters . MappedChoiceFilter ( <EOL> choices = SupportedServices . Types . get_direct_filter_mapping ( ) , <EOL> choice_mappings = SupportedServices . Types . get_reverse_filter_mapping ( ) <EOL> ) <EOL> state = core_filters . SynchronizationStateFilter ( ) <EOL> class Meta ( object ) : <EOL> model = models . ServiceSettings <EOL> fields = ( '<STR_LIT:name>' , '<STR_LIT:type>' , '<STR_LIT:state>' ) <EOL> class BaseServiceFilter ( django_filters . FilterSet ) : <EOL> customer = django_filters . CharFilter ( name = '<STR_LIT>' ) <EOL> name = django_filters . CharFilter ( lookup_type = '<STR_LIT>' ) <EOL> project = core_filters . URLFilter ( view_name = '<STR_LIT>' , name = '<STR_LIT>' , distinct = True ) <EOL> project_uuid = django_filters . CharFilter ( name = '<STR_LIT>' , distinct = True ) <EOL> settings = core_filters . URLFilter ( view_name = '<STR_LIT>' , name = '<STR_LIT>' , distinct = True ) <EOL> shared = django_filters . BooleanFilter ( name = '<STR_LIT>' , distinct = True ) <EOL> type = core_filters . MappedChoiceFilter ( <EOL> name = '<STR_LIT>' , <EOL> choices = SupportedServices . Types . get_direct_filter_mapping ( ) , <EOL> choice_mappings = SupportedServices . Types . get_reverse_filter_mapping ( ) <EOL> ) <EOL> tag = django_filters . ModelMultipleChoiceFilter ( <EOL> name = '<STR_LIT>' , <EOL> to_field_name = '<STR_LIT:name>' , <EOL> lookup_type = '<STR_LIT>' , <EOL> queryset = taggit . models . Tag . objects . all ( ) , <EOL> ) <EOL> rtag = django_filters . ModelMultipleChoiceFilter ( <EOL> name = '<STR_LIT>' , <EOL> to_field_name = '<STR_LIT:name>' , <EOL> queryset = taggit . models . Tag . objects . all ( ) , <EOL> conjoined = True , <EOL> ) <EOL> class Meta ( object ) : <EOL> model = models . Service <EOL> fields = ( '<STR_LIT:name>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:type>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> class BaseServiceProjectLinkFilter ( django_filters . FilterSet ) : <EOL> service_uuid = django_filters . CharFilter ( name = '<STR_LIT>' ) <EOL> customer_uuid = django_filters . CharFilter ( name = '<STR_LIT>' ) <EOL> project_uuid = django_filters . CharFilter ( name = '<STR_LIT>' ) <EOL> project = core_filters . URLFilter ( view_name = '<STR_LIT>' , name = '<STR_LIT>' ) <EOL> class Meta ( object ) : <EOL> model = models . ServiceProjectLink <EOL> class ResourceFilterMetaclass ( FilterSetMetaclass ) : <EOL> """<STR_LIT>""" <EOL> def __new__ ( cls , name , bases , args ) : <EOL> resource_filter = super ( ResourceFilterMetaclass , cls ) . __new__ ( cls , name , bases , args ) <EOL> SupportedServices . register_resource_filter ( args [ '<STR_LIT:Meta>' ] . model , resource_filter ) <EOL> return resource_filter <EOL> class BaseResourceFilter ( six . with_metaclass ( ResourceFilterMetaclass , <EOL> django_filters . FilterSet ) ) : <EOL> customer = django_filters . CharFilter ( name = '<STR_LIT>' ) <EOL> customer_uuid = django_filters . CharFilter ( name = '<STR_LIT>' ) <EOL> customer_name = django_filters . CharFilter ( <EOL> name = '<STR_LIT>' , lookup_type = '<STR_LIT>' ) <EOL> customer_native_name = django_filters . CharFilter ( <EOL> name = '<STR_LIT>' , lookup_type = '<STR_LIT>' ) <EOL> customer_abbreviation = django_filters . CharFilter ( <EOL> name = '<STR_LIT>' , lookup_type = '<STR_LIT>' ) <EOL> project = django_filters . CharFilter ( name = '<STR_LIT>' ) <EOL> project_uuid = django_filters . CharFilter ( name = '<STR_LIT>' ) <EOL> project_name = django_filters . CharFilter ( name = '<STR_LIT>' , lookup_type = '<STR_LIT>' ) <EOL> project_group = django_filters . CharFilter ( name = '<STR_LIT>' ) <EOL> project_group_uuid = django_filters . CharFilter ( name = '<STR_LIT>' ) <EOL> project_group_name = django_filters . CharFilter ( <EOL> name = '<STR_LIT>' , lookup_type = '<STR_LIT>' ) <EOL> service_uuid = django_filters . CharFilter ( name = '<STR_LIT>' ) <EOL> service_name = django_filters . CharFilter ( name = '<STR_LIT>' , lookup_type = '<STR_LIT>' ) <EOL> name = django_filters . CharFilter ( lookup_type = '<STR_LIT>' ) <EOL> description = django_filters . CharFilter ( lookup_type = '<STR_LIT>' ) <EOL> state = core_filters . MappedMultipleChoiceFilter ( <EOL> choices = [ ( representation , representation ) for db_value , representation in models . Resource . States . CHOICES ] , <EOL> choice_mappings = { representation : db_value for db_value , representation in models . Resource . States . CHOICES } , <EOL> ) <EOL> uuid = django_filters . CharFilter ( lookup_type = '<STR_LIT>' ) <EOL> tag = django_filters . ModelMultipleChoiceFilter ( <EOL> name = '<STR_LIT>' , <EOL> to_field_name = '<STR_LIT:name>' , <EOL> lookup_type = '<STR_LIT>' , <EOL> queryset = taggit . models . Tag . objects . all ( ) , <EOL> ) <EOL> rtag = django_filters . ModelMultipleChoiceFilter ( <EOL> name = '<STR_LIT>' , <EOL> to_field_name = '<STR_LIT:name>' , <EOL> queryset = taggit . models . Tag . objects . all ( ) , <EOL> conjoined = True , <EOL> ) <EOL> strict = False <EOL> class Meta ( object ) : <EOL> model = models . Resource <EOL> fields = ( <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT:name>' , '<STR_LIT:description>' , '<STR_LIT:state>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> ) <EOL> order_by = [ <EOL> '<STR_LIT:name>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT:state>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ] <EOL> order_by_mapping = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } <EOL> class BaseResourceStateFilter ( BaseResourceFilter ) : <EOL> state = core_filters . MappedMultipleChoiceFilter ( <EOL> choices = [ ( representation , representation ) for db_value , representation in core_models . StateMixin . States . CHOICES ] , <EOL> choice_mappings = { representation : db_value for db_value , representation in core_models . StateMixin . States . CHOICES } , <EOL> ) <EOL> class Meta ( BaseResourceFilter . Meta ) : <EOL> model = models . ResourceMixin <EOL> class TagsFilter ( BaseFilterBackend ) : <EOL> """<STR_LIT>""" <EOL> def filter_queryset ( self , request , queryset , view ) : <EOL> self . db_field = getattr ( view , '<STR_LIT>' , '<STR_LIT>' ) <EOL> self . request_field = getattr ( view , '<STR_LIT>' , '<STR_LIT>' ) <EOL> queryset = self . _filter ( request , queryset ) <EOL> queryset = self . _order ( request , queryset ) <EOL> return queryset <EOL> def _filter ( self , request , queryset ) : <EOL> for key in request . query_params . keys ( ) : <EOL> item_name = self . _get_item_name ( key ) <EOL> if item_name : <EOL> value = request . query_params . get ( key ) <EOL> filter_kwargs = { <EOL> self . db_field + '<STR_LIT>' : item_name , <EOL> self . db_field + '<STR_LIT>' : value , <EOL> } <EOL> queryset = queryset . filter ( ** filter_kwargs ) <EOL> return queryset <EOL> def _order ( self , request , queryset ) : <EOL> order_by = request . query_params . get ( '<STR_LIT:o>' ) <EOL> item_name = self . _get_item_name ( order_by ) <EOL> if item_name : <EOL> filter_kwargs = { self . db_field + '<STR_LIT>' : item_name } <EOL> queryset = queryset . filter ( ** filter_kwargs ) . order_by ( self . db_field + '<STR_LIT>' ) <EOL> return queryset <EOL> def _get_item_name ( self , key ) : <EOL> prefix = self . request_field + '<STR_LIT>' <EOL> if key and key . startswith ( prefix ) : <EOL> return key [ len ( prefix ) : ] <EOL> class StartTimeFilter ( BaseFilterBackend ) : <EOL> """<STR_LIT>""" <EOL> def filter_queryset ( self , request , queryset , view ) : <EOL> order = request . query_params . get ( '<STR_LIT:o>' , None ) <EOL> if order == '<STR_LIT>' : <EOL> queryset = queryset . extra ( select = { <EOL> '<STR_LIT>' : '<STR_LIT>' } ) . order_by ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> elif order == '<STR_LIT>' : <EOL> queryset = queryset . extra ( select = { <EOL> '<STR_LIT>' : '<STR_LIT>' } ) . order_by ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> return queryset <EOL> class BaseServicePropertyFilter ( django_filters . FilterSet ) : <EOL> name = django_filters . CharFilter ( <EOL> name = '<STR_LIT:name>' , <EOL> lookup_type = '<STR_LIT>' , <EOL> ) <EOL> class Meta ( object ) : <EOL> model = models . BaseServiceProperty <EOL> fields = ( '<STR_LIT:name>' , ) <EOL> class ServicePropertySettingsFilter ( BaseServicePropertyFilter ) : <EOL> settings_uuid = django_filters . CharFilter ( name = '<STR_LIT>' ) <EOL> class Meta ( BaseServicePropertyFilter . Meta ) : <EOL> fields = BaseServicePropertyFilter . Meta . fields + ( '<STR_LIT>' , ) <EOL> class AggregateFilter ( BaseExternalFilter ) : <EOL> """<STR_LIT>""" <EOL> def filter ( self , request , queryset , view ) : <EOL> if '<STR_LIT>' not in request . query_params : <EOL> return queryset <EOL> serializer = serializers . AggregateSerializer ( data = request . query_params ) <EOL> serializer . is_valid ( raise_exception = True ) <EOL> aggregates = serializer . get_aggregates ( request . user ) <EOL> projects = serializer . get_projects ( request . user ) <EOL> querysets = [ aggregates , projects ] <EOL> aggregates_ids = list ( aggregates . values_list ( '<STR_LIT:id>' , flat = True ) ) <EOL> query = { serializer . data [ '<STR_LIT>' ] + '<STR_LIT>' : aggregates_ids } <EOL> all_models = ( models . Resource . get_all_models ( ) + <EOL> models . Service . get_all_models ( ) + <EOL> models . ServiceProjectLink . get_all_models ( ) ) <EOL> for model in all_models : <EOL> qs = model . objects . filter ( ** query ) . all ( ) <EOL> querysets . append ( filter_queryset_for_user ( qs , request . user ) ) <EOL> aggregate_query = Q ( ) <EOL> for qs in querysets : <EOL> content_type = ContentType . objects . get_for_model ( qs . model ) <EOL> ids = qs . values_list ( '<STR_LIT:id>' , flat = True ) <EOL> aggregate_query |= Q ( content_type = content_type , object_id__in = ids ) <EOL> return queryset . filter ( aggregate_query ) <EOL> ExternalAlertFilterBackend . register ( AggregateFilter ( ) ) <EOL> class ResourceSummaryFilterBackend ( core_filters . DjangoMappingFilterBackend ) : <EOL> """<STR_LIT>""" <EOL> def filter_queryset ( self , request , queryset , view ) : <EOL> queryset = self . filter ( request , queryset , view ) <EOL> queryset = self . order ( request , queryset , view ) <EOL> return queryset <EOL> def filter ( self , request , queryset , view ) : <EOL> """<STR_LIT>""" <EOL> summary_queryset = queryset <EOL> filtered_querysets = [ ] <EOL> for resource_queryset in summary_queryset . querysets : <EOL> try : <EOL> filter_class = SupportedServices . get_resource_filter ( resource_queryset . model ) <EOL> except KeyError : <EOL> filter_class = BaseResourceFilter <EOL> resource_queryset = filter_class ( request . query_params , queryset = resource_queryset ) . qs <EOL> filtered_querysets . append ( resource_queryset ) <EOL> summary_queryset . querysets = filtered_querysets <EOL> return summary_queryset <EOL> def order ( self , request , queryset , view ) : <EOL> """<STR_LIT>""" <EOL> ordering = self . get_valid_ordering ( request , BaseResourceFilter ) <EOL> if ordering : <EOL> queryset = queryset . order_by ( ordering ) <EOL> return queryset </s>
