<commit_before>from rest_framework import serializers

from astrobin.utils import get_client_country_code
from astrobin_apps_equipment.api.serializers.brand_listing_serializer import BrandListingSerializer
from astrobin_apps_equipment.api.serializers.item_listing_serializer import ItemListingSerializer
from astrobin_apps_equipment.models.equipment_item import EquipmentItemReviewerDecision
from astrobin_apps_equipment.services import EquipmentItemService, EquipmentService
from astrobin_apps_premium.services.premium_service import PremiumService
from astrobin_apps_users.services import UserService
from common.constants import GroupName


class EquipmentItemSerializer(serializers.ModelSerializer):
    brand_name = serializers.SerializerMethodField(read_only=True)
    variants = serializers.SerializerMethodField(read_only=True)
    listings = serializers.SerializerMethodField(read_only=True)

    def get_brand_name(self, item):
        if item.brand:
            return item.brand.name

        return ""

    def get_variants(self, item):
        user = None
        request = self.context.get("request")
        if request and hasattr(request, "user"):
            user = request.user

        ModelClass = self.__class__.Meta.model

        variants = ModelClass.objects.filter(variant_of__pk=item.pk)

        if user and user.is_authenticated:
            if not UserService(user).is_in_group(GroupName.EQUIPMENT_MODERATORS):
                variants = variants.filter(EquipmentItemService.non_moderator_queryset(user))
        else:
            variants = variants.filter(
                reviewer_decision=EquipmentItemReviewerDecision.APPROVED,
                brand__isnull=False,
            )

        return self.__class__(variants, many=True).data

    def get_listings(self, item):
        request = self.context.get("request")

        if request is None:
            return dict()

        country_code = get_client_country_code(request)

        valid_user_subscription = PremiumService(request.user).get_valid_usersubscription()
        allow_full_retailer_integration = PremiumService.allow_full_retailer_integration(valid_user_subscription, None)

        item_listings = EquipmentService.equipment_item_listings(item, country_code)
        brand_listings = EquipmentService.equipment_brand_listings_by_item(item, country_code)
        return dict(
            brand_listings=BrandListingSerializer(brand_listings, many=True).data,
            item_listings=ItemListingSerializer(item_listings, many=True).data,
            allow_full_retailer_integration=allow_full_retailer_integration,
        )

    class Meta:
        fields = [
            'klass',
            'created_by',
            'reviewed_by',
            'reviewed_timestamp',
            'reviewer_decision',
            'reviewer_rejection_reason',
            'reviewer_comment',
            'created',
            'updated',
            'brand',
            'brand_name',
            'name',
            'website',
            'image',
            'variant_of'
        ]
        read_only_fields = ['image']
        abstract = True

    def validate(self, attrs):
        user = self.context['request'].user
        EquipmentItemService.validate(user, attrs)
        return super().validate(attrs)

    def create(self, validated_data):
        user = self.context['request'].user
        validated_data['created_by'] = user
        return super(EquipmentItemSerializer, self).create(validated_data)
<commit_msg>(equipment) Make variants inherit website and image from base if they don't have them (#2935)<commit_after>from rest_framework import serializers

from astrobin.utils import get_client_country_code
from astrobin_apps_equipment.api.serializers.brand_listing_serializer import BrandListingSerializer
from astrobin_apps_equipment.api.serializers.item_listing_serializer import ItemListingSerializer
from astrobin_apps_equipment.models.equipment_item import EquipmentItem, EquipmentItemReviewerDecision
from astrobin_apps_equipment.services import EquipmentItemService, EquipmentService
from astrobin_apps_premium.services.premium_service import PremiumService
from astrobin_apps_users.services import UserService
from common.constants import GroupName


class EquipmentItemSerializer(serializers.ModelSerializer):
    brand_name = serializers.SerializerMethodField(read_only=True)
    variants = serializers.SerializerMethodField(read_only=True)
    listings = serializers.SerializerMethodField(read_only=True)

    def get_brand_name(self, item):
        if item.brand:
            return item.brand.name

        return ""

    def get_variants(self, item):
        user = None
        request = self.context.get("request")
        if request and hasattr(request, "user"):
            user = request.user

        ModelClass = self.__class__.Meta.model

        variants = ModelClass.objects.filter(variant_of__pk=item.pk)

        if user and user.is_authenticated:
            if not UserService(user).is_in_group(GroupName.EQUIPMENT_MODERATORS):
                variants = variants.filter(EquipmentItemService.non_moderator_queryset(user))
        else:
            variants = variants.filter(
                reviewer_decision=EquipmentItemReviewerDecision.APPROVED,
                brand__isnull=False,
            )

        return self.__class__(variants, many=True).data

    def get_listings(self, item):
        request = self.context.get("request")

        if request is None:
            return dict()

        country_code = get_client_country_code(request)

        valid_user_subscription = PremiumService(request.user).get_valid_usersubscription()
        allow_full_retailer_integration = PremiumService.allow_full_retailer_integration(valid_user_subscription, None)

        item_listings = EquipmentService.equipment_item_listings(item, country_code)
        brand_listings = EquipmentService.equipment_brand_listings_by_item(item, country_code)
        return dict(
            brand_listings=BrandListingSerializer(brand_listings, many=True).data,
            item_listings=ItemListingSerializer(item_listings, many=True).data,
            allow_full_retailer_integration=allow_full_retailer_integration,
        )

    def to_representation(self, item: EquipmentItem):
        ret = super().to_representation(item)

        if item.variant_of:
            if not item.website:
                ret['website'] = item.variant_of.website
            if not item.image:
                ret['image'] = item.variant_of.image.url if item.variant_of.image else None

        return ret

    class Meta:
        fields = [
            'klass',
            'created_by',
            'reviewed_by',
            'reviewed_timestamp',
            'reviewer_decision',
            'reviewer_rejection_reason',
            'reviewer_comment',
            'created',
            'updated',
            'brand',
            'brand_name',
            'name',
            'website',
            'image',
            'variant_of'
        ]
        read_only_fields = ['image']
        abstract = True

    def validate(self, attrs):
        user = self.context['request'].user
        EquipmentItemService.validate(user, attrs)
        return super().validate(attrs)

    def create(self, validated_data):
        user = self.context['request'].user
        validated_data['created_by'] = user
        return super(EquipmentItemSerializer, self).create(validated_data)

{'fraction_non_alphanumeric': 0.07578187650360867, 'fraction_numerical': 0.0005346164127238706, 'mean_word_length': 3.0037453183520597, '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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '829701', 'n_tokens_mistral': 2103, 'n_tokens_neox': 2003, 'n_words': 359}