<commit_before>from django.conf import settings
from django.utils.translation import ugettext_lazy as _
from rest_framework import serializers
from nodeconductor.core import serializers as core_serializers

from . import models


class InvoiceItemSerializer(serializers.HyperlinkedModelSerializer):
    tax = serializers.DecimalField(max_digits=15, decimal_places=7)
    total = serializers.DecimalField(max_digits=15, decimal_places=7)

    class Meta(object):
        model = models.InvoiceItem
        fields = ('name', 'price', 'tax', 'total', 'unit_price', 'unit',
                  'start', 'end', 'usage_days', 'product_code',
                  'article_code', 'project_name', 'project_uuid',)


class OpenStackItemSerializer(InvoiceItemSerializer):
    tenant_name = serializers.ReadOnlyField(source='get_tenant_name')
    tenant_uuid = serializers.ReadOnlyField(source='get_tenant_uuid')
    template_name = serializers.ReadOnlyField(source='get_template_name')
    template_uuid = serializers.ReadOnlyField(source='get_template_uuid')
    template_category = serializers.ReadOnlyField(source='get_template_category')

    class Meta(InvoiceItemSerializer.Meta):
        model = models.OpenStackItem
        fields = InvoiceItemSerializer.Meta.fields + ('package', 'tenant_name', 'tenant_uuid',
                                                      'template_name', 'template_uuid', 'template_category')
        extra_kwargs = {
            'package': {'lookup_field': 'uuid', 'view_name': 'openstack-package-detail'},
        }


class OfferingItemSerializer(InvoiceItemSerializer):
    offering_type = serializers.ReadOnlyField(source='get_offering_type')

    class Meta(InvoiceItemSerializer.Meta):
        model = models.OfferingItem
        fields = InvoiceItemSerializer.Meta.fields + ('offering', 'offering_type')
        extra_kwargs = {
            'offering': {'lookup_field': 'uuid', 'view_name': 'support-offering-detail'},
        }


class InvoiceSerializer(serializers.HyperlinkedModelSerializer):
    price = serializers.DecimalField(max_digits=15, decimal_places=7)
    tax = serializers.DecimalField(max_digits=15, decimal_places=7)
    total = serializers.DecimalField(max_digits=15, decimal_places=7)
    openstack_items = OpenStackItemSerializer(many=True)
    offering_items = OfferingItemSerializer(many=True)
    issuer_details = serializers.SerializerMethodField()<commit_msg>Allow to specify list of fields for invoice [WAL-1086] Ut allows to reduce amount of queries.<commit_after>from django.conf import settings
from django.utils.translation import ugettext_lazy as _
from rest_framework import serializers
from nodeconductor.core import serializers as core_serializers

from . import models


class InvoiceItemSerializer(serializers.HyperlinkedModelSerializer):
    tax = serializers.DecimalField(max_digits=15, decimal_places=7)
    total = serializers.DecimalField(max_digits=15, decimal_places=7)

    class Meta(object):
        model = models.InvoiceItem
        fields = ('name', 'price', 'tax', 'total', 'unit_price', 'unit',
                  'start', 'end', 'usage_days', 'product_code',
                  'article_code', 'project_name', 'project_uuid',)


class OpenStackItemSerializer(InvoiceItemSerializer):
    tenant_name = serializers.ReadOnlyField(source='get_tenant_name')
    tenant_uuid = serializers.ReadOnlyField(source='get_tenant_uuid')
    template_name = serializers.ReadOnlyField(source='get_template_name')
    template_uuid = serializers.ReadOnlyField(source='get_template_uuid')
    template_category = serializers.ReadOnlyField(source='get_template_category')

    class Meta(InvoiceItemSerializer.Meta):
        model = models.OpenStackItem
        fields = InvoiceItemSerializer.Meta.fields + ('package', 'tenant_name', 'tenant_uuid',
                                                      'template_name', 'template_uuid', 'template_category')
        extra_kwargs = {
            'package': {'lookup_field': 'uuid', 'view_name': 'openstack-package-detail'},
        }


class OfferingItemSerializer(InvoiceItemSerializer):
    offering_type = serializers.ReadOnlyField(source='get_offering_type')

    class Meta(InvoiceItemSerializer.Meta):
        model = models.OfferingItem
        fields = InvoiceItemSerializer.Meta.fields + ('offering', 'offering_type')
        extra_kwargs = {
            'offering': {'lookup_field': 'uuid', 'view_name': 'support-offering-detail'},
        }


class InvoiceSerializer(core_serializers.RestrictedSerializerMixin,
                        serializers.HyperlinkedModelSerializer):
    price = serializers.DecimalField(max_digits=15, decimal_places=7)
    tax = serializers.DecimalField(max_digits=15, decimal_places=7)
    total = serializers.DecimalField(max_digits=15, decimal_places=7)
    openstack_items = OpenStackItemSerializer(many=True)
    offering_items = OfferingItemSerializer(many=True)
    issuer_details = serializers.SerializerMethodField()
{'fraction_non_alphanumeric': 0.10100603621730382, 'fraction_numerical': 0.006841046277665996, 'mean_word_length': 4.288297872340426, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3399355', 'n_tokens_mistral': 1390, 'n_tokens_neox': 1360, 'n_words': 241}