<commit_before>from django.db import models
from datetime import datetime
from django.core.exceptions import ImproperlyConfigured
import pytz
from django.conf import settings
from django.db.models import Q
import inspect
import importlib
import operator
from functools import reduce

if not (hasattr(settings, 'OCD_CITY_COUNCIL_ID') or hasattr(settings, 'OCD_CITY_COUNCIL_NAME')):
    raise ImproperlyConfigured(
        'You must define a OCD_CITY_COUNCIL_ID or OCD_CITY_COUNCIL_NAME in settings.py')

if not hasattr(settings, 'CITY_COUNCIL_NAME'):
    raise ImproperlyConfigured(
        'You must define a CITY_COUNCIL_NAME in settings.py')

MANUAL_HEADSHOTS = settings.MANUAL_HEADSHOTS if hasattr(
    settings, 'MANUAL_HEADSHOTS') else {}

app_timezone = pytz.timezone(settings.TIME_ZONE)

bill_document_choices = (
    ('A', 'Attachment'),
    ('V', 'Version'),
)


def override_relation(base_model):

    models_module = '{0}.models'.format(settings.APP_NAME)
    app_models = importlib.import_module(models_module)

    overridden = base_model
    for name, member in inspect.getmembers(app_models):

        try:
            if issubclass(member, base_model.__class__):
                module = __import__(models_module, globals(), locals(), [name])

                overridden = getattr(module, name)()

                attrs = [
                    f.attname for f in base_model.__class__._meta.fields] + ['_state']
                for attr in attrs:
                    setattr(overridden, attr, getattr(base_model, attr))

        except TypeError:
            pass

    return overridden


class Person(models.Model):
    ocd_id = models.CharField(max_length=100, unique=True, null=True)
    name = models.CharField(max_length=100)
    headshot = models.CharField(max_length=255, blank=True)
    source_url = models.CharField(max_length=255, blank=True)
    source_note = models.CharField(max_length=255, blank=True)
    website_url = models.CharField(max_length=255, blank=True)
    email = models.CharField(max_length=255, blank=True)
    slug = models.CharField(max_length=255, unique=True, null=True)
    updated_at = models.DateTimeField(auto_now=True)

    def __str__(self):
        return self.name

    @property
    def latest_council_membership(self):
        if hasattr(settings, 'OCD_CITY_COUNCIL_ID'):
            filter_kwarg = {'_organization__ocd_id': settings.OCD_CITY_COUNCIL_ID}
        else:
            filter_kwarg = {'_organization__name': settings.OCD_CITY_COUNCIL_NAME}

        city_council_memberships = self.memberships.filter(**filter_kwarg)

        if city_council_memberships.count():
            return city_council_memberships.order_by('-start_date', '-end_date').first()

        return None

    @property
    def current_council_seat(self):
        m = self.latest_council_membership
        if m and m.post:
            if self == m.post.current_member.person:
                return m.post.label
        return ''

    @property
    def latest_council_seat(self):
        m = self.latest_council_membership
        if m:
            return m.post.label
        return ''

    @property
    def is_speaker(self):
        return True if self.memberships.filter(role='Speaker').first() else False

    @property
    def headshot_url(self):
        if self.slug in MANUAL_HEADSHOTS:
            return '/static/images/' + MANUAL_HEADSHOTS[self.slug]['image']
        elif self.headshot:
            return '/static/images/' + self.ocd_id + ".jpg"
        else:
            return '/static/images/headshot_placeholder.png'

    @property
    def headshot_source(self):
        if self.slug in MANUAL_HEADSHOTS:
            return MANUAL_HEADSHOTS[self.slug]['source']
        elif self.headshot:
            return settings.CITY_VOCAB['SOURCE']
        else:
            return None

    @property
    def link_html(self):
        # if person is a city council member
        if self.ocd_id and self.slug:
            return '<a href="/person/' + self.slug + '/" title="More on ' + self.name + '">' + self.name + '</a>'
        # otherwise, don't make a link
        else:
            return self.name

    @property
    def primary_sponsorships(self):
        return self.sponsorships.filter(is_primary=True)

    @property
    def non_council_memberships(self):
        exclude_kwarg = {'_organization__ocd_id': settings.OCD_CITY_COUNCIL_ID}
        return self.memberships.exclude(**exclude_kwarg).order_by('role')


class Bill(models.Model):
    ocd_id = models.CharField(max_length=100, unique=True)
    ocd_created_at = models.DateTimeField(default=None)
    ocd_updated_at = models.DateTimeField(default=None)
    description = models.TextField()
    identifier = models.CharField(max_length=50)<commit_msg>Secondary sort of person memberships by org name.<commit_after>from django.db import models
from datetime import datetime
from django.core.exceptions import ImproperlyConfigured
import pytz
from django.conf import settings
from django.db.models import Q
import inspect
import importlib
import operator
from functools import reduce

if not (hasattr(settings, 'OCD_CITY_COUNCIL_ID') or hasattr(settings, 'OCD_CITY_COUNCIL_NAME')):
    raise ImproperlyConfigured(
        'You must define a OCD_CITY_COUNCIL_ID or OCD_CITY_COUNCIL_NAME in settings.py')

if not hasattr(settings, 'CITY_COUNCIL_NAME'):
    raise ImproperlyConfigured(
        'You must define a CITY_COUNCIL_NAME in settings.py')

MANUAL_HEADSHOTS = settings.MANUAL_HEADSHOTS if hasattr(
    settings, 'MANUAL_HEADSHOTS') else {}

app_timezone = pytz.timezone(settings.TIME_ZONE)

bill_document_choices = (
    ('A', 'Attachment'),
    ('V', 'Version'),
)


def override_relation(base_model):

    models_module = '{0}.models'.format(settings.APP_NAME)
    app_models = importlib.import_module(models_module)

    overridden = base_model
    for name, member in inspect.getmembers(app_models):

        try:
            if issubclass(member, base_model.__class__):
                module = __import__(models_module, globals(), locals(), [name])

                overridden = getattr(module, name)()

                attrs = [
                    f.attname for f in base_model.__class__._meta.fields] + ['_state']
                for attr in attrs:
                    setattr(overridden, attr, getattr(base_model, attr))

        except TypeError:
            pass

    return overridden


class Person(models.Model):
    ocd_id = models.CharField(max_length=100, unique=True, null=True)
    name = models.CharField(max_length=100)
    headshot = models.CharField(max_length=255, blank=True)
    source_url = models.CharField(max_length=255, blank=True)
    source_note = models.CharField(max_length=255, blank=True)
    website_url = models.CharField(max_length=255, blank=True)
    email = models.CharField(max_length=255, blank=True)
    slug = models.CharField(max_length=255, unique=True, null=True)
    updated_at = models.DateTimeField(auto_now=True)

    def __str__(self):
        return self.name

    @property
    def latest_council_membership(self):
        if hasattr(settings, 'OCD_CITY_COUNCIL_ID'):
            filter_kwarg = {'_organization__ocd_id': settings.OCD_CITY_COUNCIL_ID}
        else:
            filter_kwarg = {'_organization__name': settings.OCD_CITY_COUNCIL_NAME}

        city_council_memberships = self.memberships.filter(**filter_kwarg)

        if city_council_memberships.count():
            return city_council_memberships.order_by('-start_date', '-end_date').first()

        return None

    @property
    def current_council_seat(self):
        m = self.latest_council_membership
        if m and m.post:
            if self == m.post.current_member.person:
                return m.post.label
        return ''

    @property
    def latest_council_seat(self):
        m = self.latest_council_membership
        if m:
            return m.post.label
        return ''

    @property
    def is_speaker(self):
        return True if self.memberships.filter(role='Speaker').first() else False

    @property
    def headshot_url(self):
        if self.slug in MANUAL_HEADSHOTS:
            return '/static/images/' + MANUAL_HEADSHOTS[self.slug]['image']
        elif self.headshot:
            return '/static/images/' + self.ocd_id + ".jpg"
        else:
            return '/static/images/headshot_placeholder.png'

    @property
    def headshot_source(self):
        if self.slug in MANUAL_HEADSHOTS:
            return MANUAL_HEADSHOTS[self.slug]['source']
        elif self.headshot:
            return settings.CITY_VOCAB['SOURCE']
        else:
            return None

    @property
    def link_html(self):
        # if person is a city council member
        if self.ocd_id and self.slug:
            return '<a href="/person/' + self.slug + '/" title="More on ' + self.name + '">' + self.name + '</a>'
        # otherwise, don't make a link
        else:
            return self.name

    @property
    def primary_sponsorships(self):
        return self.sponsorships.filter(is_primary=True)

    @property
    def non_council_memberships(self):
        exclude_kwarg = {'_organization__ocd_id': settings.OCD_CITY_COUNCIL_ID}
        return self.memberships.exclude(**exclude_kwarg).order_by('role', '_organization__name')


class Bill(models.Model):
    ocd_id = models.CharField(max_length=100, unique=True)
    ocd_created_at = models.DateTimeField(default=None)
    ocd_updated_at = models.DateTimeField(default=None)
    description = models.TextField()
    identifier = models.CharField(max_length=50)
{'fraction_non_alphanumeric': 0.09786124152321335, 'fraction_numerical': 0.006259780907668232, 'mean_word_length': 3.00752508361204, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, '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': '488611', 'n_tokens_mistral': 3132, 'n_tokens_neox': 2972, 'n_words': 613}