<commit_before>from datetime import date
from django.db import transaction
from rest_framework import serializers

from common.consts import (
    FUNCTIONAL_RESPONSIBILITY_CHOICES,
)
from partner.models import (
    Partner,
    PartnerProfile,
    PartnerMailingAddress,
    PartnerHeadOrganization,
    PartnerAuditAssessment,
    PartnerReporting,
    PartnerMandateMission,
    PartnerFunding,
    PartnerOtherInfo,
    PartnerInternalControl,
    PartnerMember,
    PartnerBudget,
)

from partner.serializers import (
    PartnerSerializer,
    PartnerProfileSerializer,
    PartnerHeadOrganizationRegisterSerializer,
    PartnerMemberSerializer,
)
from .models import User


class RegisterSimpleAccountSerializer(serializers.ModelSerializer):

    date_joined = serializers.DateTimeField(required=False, read_only=True)
    username = serializers.CharField(required=False, read_only=True)

    class Meta:
        model = User
        fields = (
            'id',
            'first_name',
            'last_name',
            'username',
            'email',
            'password',
            'date_joined',
        )


class PartnerRegistrationSerializer(serializers.Serializer):

    user = RegisterSimpleAccountSerializer()
    partner = PartnerSerializer()
    partner_profile = PartnerProfileSerializer()
    partner_head_organization = PartnerHeadOrganizationRegisterSerializer()
    partner_member = PartnerMemberSerializer()

    @transaction.atomic
    def create(self, validated_data):
        validated_data['user']['username'] = validated_data['user']['email']
        self.user = User.objects.create(**validated_data['user'])
        self.user.set_password(validated_data['user']['password'])
        self.user.save()

        self.partner = Partner.objects.create(**validated_data['partner'])

        partner_profile = validated_data['partner_profile']
        partner_profile['partner_id'] = self.partner.id
        self.partner_profile = PartnerProfile.objects.create(**partner_profile)

        partner_head_orge = validated_data['partner_head_organization']
        partner_head_orge['partner_id'] = self.partner.id
        self.partner_head_organization = PartnerHeadOrganization.objects.create(**partner_head_orge)

        PartnerMailingAddress.objects.create(partner=self.partner)
        PartnerAuditAssessment.objects.create(partner=self.partner)
        PartnerReporting.objects.create(partner=self.partner)
        PartnerMandateMission.objects.create(partner=self.partner)
        PartnerFunding.objects.create(partner=self.partner)
        PartnerOtherInfo.objects.create(partner=self.partner)
        responsibilities = []
        for responsibility in list(FUNCTIONAL_RESPONSIBILITY_CHOICES._db_values):
            responsibilities.append(
                PartnerInternalControl(partner=self.partner, functional_responsibility=responsibility)
            )
        PartnerInternalControl.objects.bulk_create(responsibilities)

        budgets = []
        for year in [date.today().year, date.today().year-1, date.today().year-2]:
            budgets.append(PartnerBudget(partner=self.partner, year=year))
        PartnerBudget.objects.bulk_create(budgets)

        partner_member = validated_data['partner_member']
        partner_member['partner_id'] = self.partner.id
        partner_member['user_id'] = self.user.id
        self.partner_member = PartnerMember.objects.create(**validated_data['partner_member'])

        user_data = RegisterSimpleAccountSerializer(instance=self.user).data
        user_data.pop('password')
        self.instance_json = {
            "partner": PartnerSerializer(instance=self.partner).data,
            "user": user_data,
            "partner_profile": PartnerProfileSerializer(instance=self.partner_profile).data,
            "partner_head_organization": PartnerHeadOrganizationRegisterSerializer(self.partner_head_organization).data,
            "partner_member": PartnerMemberSerializer(instance=self.partner_member).data,
        }
        return self.instance_json


class UserSerializer(serializers.ModelSerializer):

    name = serializers.CharField(source='get_fullname')

    class Meta:
        model = User
        fields = ('id', 'name', 'email',)


class IDUserSerializer(serializers.ModelSerializer):

    class Meta:
        model = User
        fields = ('id', )

<commit_msg>#350 - created partner memer given as active admin instead of invited reader<commit_after>from datetime import date
from django.db import transaction
from rest_framework import serializers

from common.consts import (
    FUNCTIONAL_RESPONSIBILITY_CHOICES,
    MEMBER_ROLES,
    MEMBER_STATUSES,
)
from partner.models import (
    Partner,
    PartnerProfile,
    PartnerMailingAddress,
    PartnerHeadOrganization,
    PartnerAuditAssessment,
    PartnerReporting,
    PartnerMandateMission,
    PartnerFunding,
    PartnerOtherInfo,
    PartnerInternalControl,
    PartnerMember,
    PartnerBudget,
)

from partner.serializers import (
    PartnerSerializer,
    PartnerProfileSerializer,
    PartnerHeadOrganizationRegisterSerializer,
    PartnerMemberSerializer,
)
from .models import User


class RegisterSimpleAccountSerializer(serializers.ModelSerializer):

    date_joined = serializers.DateTimeField(required=False, read_only=True)
    username = serializers.CharField(required=False, read_only=True)

    class Meta:
        model = User
        fields = (
            'id',
            'first_name',
            'last_name',
            'username',
            'email',
            'password',
            'date_joined',
        )


class PartnerRegistrationSerializer(serializers.Serializer):

    user = RegisterSimpleAccountSerializer()
    partner = PartnerSerializer()
    partner_profile = PartnerProfileSerializer()
    partner_head_organization = PartnerHeadOrganizationRegisterSerializer()
    partner_member = PartnerMemberSerializer()

    @transaction.atomic
    def create(self, validated_data):
        validated_data['user']['username'] = validated_data['user']['email']
        self.user = User.objects.create(**validated_data['user'])
        self.user.set_password(validated_data['user']['password'])
        self.user.save()

        self.partner = Partner.objects.create(**validated_data['partner'])

        partner_profile = validated_data['partner_profile']
        partner_profile['partner_id'] = self.partner.id
        self.partner_profile = PartnerProfile.objects.create(**partner_profile)

        partner_head_orge = validated_data['partner_head_organization']
        partner_head_orge['partner_id'] = self.partner.id
        self.partner_head_organization = PartnerHeadOrganization.objects.create(**partner_head_orge)

        PartnerMailingAddress.objects.create(partner=self.partner)
        PartnerAuditAssessment.objects.create(partner=self.partner)
        PartnerReporting.objects.create(partner=self.partner)
        PartnerMandateMission.objects.create(partner=self.partner)
        PartnerFunding.objects.create(partner=self.partner)
        PartnerOtherInfo.objects.create(partner=self.partner)
        responsibilities = []
        for responsibility in list(FUNCTIONAL_RESPONSIBILITY_CHOICES._db_values):
            responsibilities.append(
                PartnerInternalControl(partner=self.partner, functional_responsibility=responsibility)
            )
        PartnerInternalControl.objects.bulk_create(responsibilities)

        budgets = []
        for year in [date.today().year, date.today().year-1, date.today().year-2]:
            budgets.append(PartnerBudget(partner=self.partner, year=year))
        PartnerBudget.objects.bulk_create(budgets)

        partner_member = validated_data['partner_member']
        partner_member['partner_id'] = self.partner.id
        partner_member['user_id'] = self.user.id
        partner_member['role'] = MEMBER_ROLES.admin
        partner_member['status'] = MEMBER_STATUSES.active
        self.partner_member = PartnerMember.objects.create(**validated_data['partner_member'])

        user_data = RegisterSimpleAccountSerializer(instance=self.user).data
        user_data.pop('password')
        self.instance_json = {
            "partner": PartnerSerializer(instance=self.partner).data,
            "user": user_data,
            "partner_profile": PartnerProfileSerializer(instance=self.partner_profile).data,
            "partner_head_organization": PartnerHeadOrganizationRegisterSerializer(self.partner_head_organization).data,
            "partner_member": PartnerMemberSerializer(instance=self.partner_member).data,
        }
        return self.instance_json


class UserSerializer(serializers.ModelSerializer):

    name = serializers.CharField(source='get_fullname')

    class Meta:
        model = User
        fields = ('id', 'name', 'email',)


class IDUserSerializer(serializers.ModelSerializer):

    class Meta:
        model = User
        fields = ('id', )


{'fraction_non_alphanumeric': 0.09358945722582086, 'fraction_numerical': 0.0007817735090462363, 'mean_word_length': 3.582906857727738, 'pattern_counts': {'":': 10, '<': 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': '123464', 'n_tokens_mistral': 2477, 'n_tokens_neox': 2445, 'n_words': 351}