<commit_before># -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import transaction
from rest_framework import serializers

from agency.permissions import AgencyPermission
from common.consts import USER_CREATED_FLAG_CATEGORIES, INTERNAL_FLAG_CATEGORIES, FLAG_TYPES
from common.permissions import current_user_has_permission
from common.serializers import CommonFileSerializer
from agency.serializers import AgencyUserBasicSerializer
from notification.helpers import send_partner_marked_for_deletion_email
from review.models import PartnerFlag, PartnerVerification
from sanctionslist.serializers import SanctionedNameMatchSerializer


class PartnerFlagSerializer(serializers.ModelSerializer):

    submitter = AgencyUserBasicSerializer(read_only=True)
    attachment = CommonFileSerializer(required=False)
    is_valid = serializers.BooleanField(required=False)
    flag_type_display = serializers.CharField(source='get_flag_type_display', read_only=True)
    category_display = serializers.CharField(source='get_category_display', read_only=True)
    sanctions_match = SanctionedNameMatchSerializer(read_only=True)

    class Meta:
        model = PartnerFlag
        read_only_fields = (
            'submitter',
            'partner',
            'comment',
            'attachment',
            'sanctions_match',
        )
        exclude = (
            'type_history',
        )
        extra_kwargs = {
            'category': {
                'choices': USER_CREATED_FLAG_CATEGORIES
            },
            'comment': {
                'required': True
            },
            'contact_email': {
                'required': True
            },
        }

    def get_extra_kwargs(self):
        extra_kwargs = super(PartnerFlagSerializer, self).get_extra_kwargs()
        if self.instance and self.instance.category not in USER_CREATED_FLAG_CATEGORIES:
            extra_kwargs['category']['read_only'] = True
        return extra_kwargs

    def get_fields(self):
        fields = super(PartnerFlagSerializer, self).get_fields()
        request = self.context.get('request')
        if not request or not current_user_has_permission(request, agency_permissions=[
            AgencyPermission.VIEW_PROFILE_OBSERVATION_FLAG_COMMENTS<commit_msg>Make sure escalated flags are resolved through setting is_valid<commit_after># -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import transaction
from rest_framework import serializers

from agency.permissions import AgencyPermission
from common.consts import USER_CREATED_FLAG_CATEGORIES, INTERNAL_FLAG_CATEGORIES, FLAG_TYPES
from common.permissions import current_user_has_permission
from common.serializers import CommonFileSerializer
from agency.serializers import AgencyUserBasicSerializer
from notification.helpers import send_partner_marked_for_deletion_email
from review.models import PartnerFlag, PartnerVerification
from sanctionslist.serializers import SanctionedNameMatchSerializer


class PartnerFlagSerializer(serializers.ModelSerializer):

    submitter = AgencyUserBasicSerializer(read_only=True)
    attachment = CommonFileSerializer(required=False)
    is_valid = serializers.BooleanField(required=False)
    flag_type_display = serializers.CharField(source='get_flag_type_display', read_only=True)
    category_display = serializers.CharField(source='get_category_display', read_only=True)
    sanctions_match = SanctionedNameMatchSerializer(read_only=True)

    class Meta:
        model = PartnerFlag
        read_only_fields = (
            'submitter',
            'partner',
            'comment',
            'attachment',
            'sanctions_match',
        )
        exclude = (
            'type_history',
        )
        extra_kwargs = {
            'category': {
                'choices': USER_CREATED_FLAG_CATEGORIES
            },
            'comment': {
                'required': True
            },
            'contact_email': {
                'required': True
            },
        }

    def get_extra_kwargs(self):
        extra_kwargs = super(PartnerFlagSerializer, self).get_extra_kwargs()
        if self.instance and self.instance.category not in USER_CREATED_FLAG_CATEGORIES:
            extra_kwargs['category']['read_only'] = True

        if self.instance and self.instance.flag_type == FLAG_TYPES.escalated:
            # Escalated flags can only be changed through validating / invalidating
            extra_kwargs['flag_type'] = {
                'read_only': True
            }

        return extra_kwargs

    def get_fields(self):
        fields = super(PartnerFlagSerializer, self).get_fields()
        request = self.context.get('request')
        if not request or not current_user_has_permission(request, agency_permissions=[
            AgencyPermission.VIEW_PROFILE_OBSERVATION_FLAG_COMMENTS
{'fraction_non_alphanumeric': 0.07110655737704918, 'fraction_numerical': 0.0004098360655737705, 'mean_word_length': 3.1682322801024765, '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': '1448783', 'n_tokens_mistral': 1306, 'n_tokens_neox': 1278, 'n_words': 262}