<s> from __future__ import unicode_literals <EOL> from django . contrib . contenttypes . models import ContentType <EOL> from django . db . models import Q <EOL> import django_filters <EOL> from rest_framework import filters <EOL> from nodeconductor . core import filters as core_filters <EOL> from nodeconductor . cost_tracking import models , serializers <EOL> from nodeconductor . structure import models as structure_models , SupportedServices <EOL> from nodeconductor . structure . models import Resource <EOL> class PriceEstimateFilter ( django_filters . FilterSet ) : <EOL> is_manually_input = django_filters . BooleanFilter ( ) <EOL> class Meta : <EOL> model = models . PriceEstimate <EOL> fields = [ <EOL> '<STR_LIT>' , <EOL> ] <EOL> class PriceEstimateScopeFilterBackend ( core_filters . GenericKeyFilterBackend ) : <EOL> def get_related_models ( self ) : <EOL> return models . PriceEstimate . get_estimated_models ( ) <EOL> def get_field_name ( self ) : <EOL> return '<STR_LIT>' <EOL> class AdditionalPriceEstimateFilterBackend ( filters . BaseFilterBackend ) : <EOL> def filter_queryset ( self , request , queryset , view ) : <EOL> if '<STR_LIT:date>' in request . query_params : <EOL> date_serializer = serializers . PriceEstimateDateFilterSerializer ( <EOL> data = { '<STR_LIT>' : request . query_params . getlist ( '<STR_LIT:date>' ) } ) <EOL> date_serializer . is_valid ( raise_exception = True ) <EOL> query = Q ( ) <EOL> for year , month in date_serializer . validated_data [ '<STR_LIT>' ] : <EOL> query |= Q ( year = year , month = month ) <EOL> queryset = queryset . filter ( query ) <EOL> date_range_serializer = serializers . PriceEstimateDateRangeFilterSerializer ( data = request . query_params ) <EOL> date_range_serializer . is_valid ( raise_exception = True ) <EOL> if '<STR_LIT:start>' in date_range_serializer . validated_data : <EOL> year , month = date_range_serializer . validated_data [ '<STR_LIT:start>' ] <EOL> queryset = queryset . filter ( Q ( year__gt = year ) | Q ( year = year , month__gte = month ) ) <EOL> if '<STR_LIT:end>' in date_range_serializer . validated_data : <EOL> year , month = date_range_serializer . validated_data [ '<STR_LIT:end>' ] <EOL> queryset = queryset . filter ( Q ( year__lt = year ) | Q ( year = year , month__lte = month ) ) <EOL> if '<STR_LIT>' in request . query_params : <EOL> customer_uuid = request . query_params [ '<STR_LIT>' ] <EOL> qs = Q ( ) <EOL> for model in models . PriceEstimate . get_estimated_models ( ) : <EOL> content_type = ContentType . objects . get_for_model ( model ) <EOL> if model == structure_models . Customer : <EOL> query = { '<STR_LIT>' : customer_uuid } <EOL> else : <EOL> query = { model . Permissions . customer_path + '<STR_LIT>' : customer_uuid } <EOL> ids = model . objects . filter ( ** query ) . values_list ( '<STR_LIT>' , flat = True ) <EOL> qs |= Q ( content_type = content_type , object_id__in = ids ) <EOL> queryset = queryset . filter ( qs ) <EOL> return queryset <EOL> class PriceListItemServiceFilterBackend ( core_filters . GenericKeyFilterBackend ) : <EOL> def get_related_models ( self ) : <EOL> return structure_models . Service . get_all_models ( ) <EOL> def get_field_name ( self ) : <EOL> return '<STR_LIT>' <EOL> class ResourceTypeFilter ( django_filters . CharFilter ) : <EOL> def filter ( self , qs , value ) : <EOL> if value : <EOL> resource_models = SupportedServices . get_resource_models ( ) <EOL> try : <EOL> model = resource_models [ value ] <EOL> ct = ContentType . objects . get_for_model ( model ) <EOL> return super ( ResourceTypeFilter , self ) . filter ( qs , ct ) <EOL> except ( ContentType . DoesNotExist , KeyError ) : <EOL> return qs . none ( ) <EOL> return qs <EOL> class DefaultPriceListItemFilter ( django_filters . FilterSet ) : <EOL> resource_content_type = core_filters . ContentTypeFilter ( ) <EOL> resource_type = ResourceTypeFilter ( name = '<STR_LIT>' ) <EOL> class Meta : <EOL> model = models . DefaultPriceListItem <EOL> fields = [ <EOL> '<STR_LIT:key>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ] </s>
