<commit_before>import copy
from .base import (BaseModel, SourceMixin, LinkMixin, ContactDetailMixin, OtherNameMixin,
                   IdentifierMixin)
from .schemas.post import schema as post_schema
from .schemas.person import schema as person_schema
from .schemas.membership import schema as membership_schema
from .schemas.organization import schema as org_schema
from ..utils import _make_pseudo_id

# a copy of the org schema without sources
org_schema_no_sources = copy.deepcopy(org_schema)
org_schema_no_sources['properties'].pop('sources')


class Post(BaseModel, LinkMixin, ContactDetailMixin):
    """
    A popolo-style Post
    """

    _type = 'post'
    _schema = post_schema

    def __init__(self, *, label, role, organization_id=None, chamber=None,
                 division_id=None, start_date='', end_date=''):
        super(Post, self).__init__()
        self.label = label
        self.role = role
        self.organization_id = pseudo_organization(organization_id, chamber)
        self.division_id = division_id
        self.start_date = start_date
        self.end_date = end_date

    def __str__(self):
        return self.label


class Membership(BaseModel, ContactDetailMixin, LinkMixin):
    """
    A popolo-style Membership.
    """

    _type = 'membership'
    _schema = membership_schema

    def __init__(self, *, person_id, organization_id, post_id=None, role='', label='',
                 start_date='', end_date='', on_behalf_of_id=None,
                 person_name=''
                 ):
        """
        Constructor for the Membership object.

        We require a person ID and organization ID, as required by the
        popolo spec. Additional arguments may be given, which match those
        defined by popolo.
        """
        super(Membership, self).__init__()
        self.person_id = person_id
        self.person_name = person_name
        self.organization_id = organization_id
        self.post_id = post_id
        self.start_date = start_date
        self.end_date = end_date
        self.role = role
        self.label = label
        self.on_behalf_of_id = on_behalf_of_id

    def __str__(self):
        return self.person_id + ' membership in ' + self.organization_id


class Person(BaseModel, SourceMixin, ContactDetailMixin, LinkMixin, IdentifierMixin,
             OtherNameMixin):
    """
    Details for a Person in Popolo format.
    """

    _type = 'person'
    _schema = person_schema

    def __init__(self, name, *, birth_date='', death_date='', biography='', summary='', image='',
                 gender='', national_identity='',
                 # specialty fields
                 district=None, party=None, primary_org='', role='member',
                 start_date='', end_date='', primary_org_name=None):
        super(Person, self).__init__()
        self.name = name
        self.birth_date = birth_date
        self.death_date = death_date
        self.biography = biography
        self.summary = summary
        self.image = image
        self.gender = gender
        self.national_identity = national_identity
        if primary_org:
            self.add_term(role, primary_org, district=district,
                          start_date=start_date, end_date=end_date,
                          org_name=primary_org_name)
        if party:
            self.add_party(party)

    def add_membership(self, name_or_org, role='member', **kwargs):
        """
            add a membership in an organization and return the membership
            object in case there are more details to add
        """
        if isinstance(name_or_org, Organization):
            membership = Membership(person_id=self._id,
                                    person_name=self.name,
                                    organization_id=name_or_org._id,
                                    role=role, **kwargs)
        else:
            membership = Membership(person_id=self._id,
                                    person_name=self.name,
                                    organization_id=_make_pseudo_id(name=name_or_org),
                                    role=role, **kwargs)
        self._related.append(membership)
        return membership

    def add_party(self, party, **kwargs):
        membership = Membership(
            person_id=self._id,
            person_name=self.name,
            organization_id=_make_pseudo_id(classification="party", name=party),
            role='member', **kwargs)
        self._related.append(membership)

    def add_term(self, role, org_classification, *, district=None,
                 start_date='', end_date='', label='', org_name=None,
                 appointment=False):
        if org_name:
            org_id = _make_pseudo_id(classification=org_classification,
                                     name=org_name)
        else:
            org_id = _make_pseudo_id(classification=org_classification)

        if district:
            post_id = _make_pseudo_id(label=district,
                                      role=role,
                                      organization__classification=org_classification)
        elif appointment:<commit_msg>make role optional in Person constructor when used w/ primary_org<commit_after>import copy
from .base import (BaseModel, SourceMixin, LinkMixin, ContactDetailMixin, OtherNameMixin,
                   IdentifierMixin)
from .schemas.post import schema as post_schema
from .schemas.person import schema as person_schema
from .schemas.membership import schema as membership_schema
from .schemas.organization import schema as org_schema
from ..utils import _make_pseudo_id

# a copy of the org schema without sources
org_schema_no_sources = copy.deepcopy(org_schema)
org_schema_no_sources['properties'].pop('sources')


class Post(BaseModel, LinkMixin, ContactDetailMixin):
    """
    A popolo-style Post
    """

    _type = 'post'
    _schema = post_schema

    def __init__(self, *, label, role, organization_id=None, chamber=None,
                 division_id=None, start_date='', end_date=''):
        super(Post, self).__init__()
        self.label = label
        self.role = role
        self.organization_id = pseudo_organization(organization_id, chamber)
        self.division_id = division_id
        self.start_date = start_date
        self.end_date = end_date

    def __str__(self):
        return self.label


class Membership(BaseModel, ContactDetailMixin, LinkMixin):
    """
    A popolo-style Membership.
    """

    _type = 'membership'
    _schema = membership_schema

    def __init__(self, *, person_id, organization_id, post_id=None, role='', label='',
                 start_date='', end_date='', on_behalf_of_id=None,
                 person_name=''
                 ):
        """
        Constructor for the Membership object.

        We require a person ID and organization ID, as required by the
        popolo spec. Additional arguments may be given, which match those
        defined by popolo.
        """
        super(Membership, self).__init__()
        self.person_id = person_id
        self.person_name = person_name
        self.organization_id = organization_id
        self.post_id = post_id
        self.start_date = start_date
        self.end_date = end_date
        self.role = role
        self.label = label
        self.on_behalf_of_id = on_behalf_of_id

    def __str__(self):
        return self.person_id + ' membership in ' + self.organization_id


class Person(BaseModel, SourceMixin, ContactDetailMixin, LinkMixin, IdentifierMixin,
             OtherNameMixin):
    """
    Details for a Person in Popolo format.
    """

    _type = 'person'
    _schema = person_schema

    def __init__(self, name, *, birth_date='', death_date='', biography='', summary='', image='',
                 gender='', national_identity='',
                 # specialty fields
                 district=None, party=None, primary_org='', role='',
                 start_date='', end_date='', primary_org_name=None):
        super(Person, self).__init__()
        self.name = name
        self.birth_date = birth_date
        self.death_date = death_date
        self.biography = biography
        self.summary = summary
        self.image = image
        self.gender = gender
        self.national_identity = national_identity
        if primary_org:
            self.add_term(role, primary_org, district=district,
                          start_date=start_date, end_date=end_date,
                          org_name=primary_org_name)
        if party:
            self.add_party(party)

    def add_membership(self, name_or_org, role='member', **kwargs):
        """
            add a membership in an organization and return the membership
            object in case there are more details to add
        """
        if isinstance(name_or_org, Organization):
            membership = Membership(person_id=self._id,
                                    person_name=self.name,
                                    organization_id=name_or_org._id,
                                    role=role, **kwargs)
        else:
            membership = Membership(person_id=self._id,
                                    person_name=self.name,
                                    organization_id=_make_pseudo_id(name=name_or_org),
                                    role=role, **kwargs)
        self._related.append(membership)
        return membership

    def add_party(self, party, **kwargs):
        membership = Membership(
            person_id=self._id,
            person_name=self.name,
            organization_id=_make_pseudo_id(classification="party", name=party),
            role='member', **kwargs)
        self._related.append(membership)

    def add_term(self, role, org_classification, *, district=None,
                 start_date='', end_date='', label='', org_name=None,
                 appointment=False):
        if org_name:
            org_id = _make_pseudo_id(classification=org_classification,
                                     name=org_name)
        else:
            org_id = _make_pseudo_id(classification=org_classification)

        if district:
            if role:
                post_id = _make_pseudo_id(label=district,
                                          role=role,
                                          organization__classification=org_classification)
            else:
                post_id = _make_pseudo_id(label=district,
                                          organization__classification=org_classification)
        elif appointment:
{'fraction_non_alphanumeric': 0.08504814045686238, 'fraction_numerical': 0.0, 'mean_word_length': 2.071014492753623, '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': 22, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2186788', 'n_tokens_mistral': 2885, 'n_tokens_neox': 2783, 'n_words': 646}