<commit_before>from django.db import models
from django.core.urlresolvers import reverse
from django.utils.text import slugify

# Create your models here.
class Filer(models.Model):
    FILER_TYPE_OPTIONS =(
        ('pac', 'Political Action Committee'),
        ('cand', 'Candidate'),
    )
    # straight out of the filer tables.
    filer_id = models.IntegerField()
    status = models.CharField(max_length=255, null=True)
    filer_type = models.CharField(max_length=10L, choices=FILER_TYPE_OPTIONS)
    
    ## fields updated by other tables
    xref_filer_id = models.CharField(max_length=32L, null=True)
    name = models.CharField(max_length=255L, null=True)

    def __unicode__(self):
        return self.name

    def get_absolute_url(self):
        return reverse('filer_detail', args=[str(self.slug), str(self.pk)])

    def _create_slug(self):
        return slugify(self.name)

    slug = property(_create_slug)


class Committee(models.Model):
    '''
        If a Candidate controls the committee, the filer is associated with the Candidate Filer record, not the committee Filer record
        But the committee Filer record can still be accessed using filer_id_raw
        So candidate filers potentially link to multiple committes, and committee filers that are not candidate controlled link back to one, committee filer
        If there's a better way I'm open to suggestions
    '''
    CMTE_TYPE_OPTIONS = (
        ('ccrc', 'Candidate Controlled Recipient Committee'),
        ('ncrc', 'Non-Candidate Controlled Recipient Committee'),
    )
    filer = models.ForeignKey(Filer)
    filer_id_raw = models.IntegerField()
    name = models.CharField(max_length=255, null=True)
    committee_type = models.CharField(max_length=4, choices=CMTE_TYPE_OPTIONS)

    def __unicode__(self):
        return self.name

    def get_absolute_url(self):
        return reverse('committee_detail', args=[str(self.pk)])

class Cycle(models.Model):
    name = models.IntegerField()

class Filing(models.Model):
    cycle = models.ForeignKey(Cycle)
    committee = models.ForeignKey(Committee)
    filing_id = models.IntegerField()<commit_msg>added total_contribution model method to committee and filer models<commit_after>from django.db import models
from django.core.urlresolvers import reverse
from django.utils.text import slugify
from django.db.models import Sum

# Create your models here.
class Filer(models.Model):
    FILER_TYPE_OPTIONS =(
        ('pac', 'Political Action Committee'),
        ('cand', 'Candidate'),
    )
    # straight out of the filer tables.
    filer_id = models.IntegerField()
    status = models.CharField(max_length=255, null=True)
    filer_type = models.CharField(max_length=10L, choices=FILER_TYPE_OPTIONS)
    
    ## fields updated by other tables
    xref_filer_id = models.CharField(max_length=32L, null=True)
    name = models.CharField(max_length=255L, null=True)

    def __unicode__(self):
        return self.name

    def get_absolute_url(self):
        return reverse('filer_detail', args=[str(self.slug), str(self.pk)])

    def _create_slug(self):
        return slugify(self.name)

    slug = property(_create_slug)

    def _total_contributions(self):

        qs = Filing.objects.filter(committee__filer=self)
        total = Summary.objects.filter(filing__in=qs).aggregate(tot=Sum('total_contribs'))['tot']
        return total

    total_contributions = property(_total_contributions) 


class Committee(models.Model):
    '''
        If a Candidate controls the committee, the filer is associated with the Candidate Filer record, not the committee Filer record
        But the committee Filer record can still be accessed using filer_id_raw
        So candidate filers potentially link to multiple committes, and committee filers that are not candidate controlled link back to one, committee filer
        If there's a better way I'm open to suggestions
    '''
    CMTE_TYPE_OPTIONS = (
        ('ccrc', 'Candidate Controlled Recipient Committee'),
        ('ncrc', 'Non-Candidate Controlled Recipient Committee'),
    )
    filer = models.ForeignKey(Filer)
    filer_id_raw = models.IntegerField()
    name = models.CharField(max_length=255, null=True)
    committee_type = models.CharField(max_length=4, choices=CMTE_TYPE_OPTIONS)

    def __unicode__(self):
        return self.name

    def get_absolute_url(self):
        return reverse('committee_detail', args=[str(self.pk)])

    def _total_contributions(self):
        qs = Filing.objects.filter(committee=self)
        total = Summary.objects.filter(filing__in=qs).aggregate(tot=Sum('total_contribs'))['tot']
        return total

    total_contributions = property(_total_contributions) 


class Cycle(models.Model):
    name = models.IntegerField()

class Filing(models.Model):
    cycle = models.ForeignKey(Cycle)
    committee = models.ForeignKey(Committee)
    filing_id = models.IntegerField()
{'fraction_non_alphanumeric': 0.0938642828118651, 'fraction_numerical': 0.005688744412840309, 'mean_word_length': 3.774975751697381, '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': '5255338', 'n_tokens_mistral': 1501, 'n_tokens_neox': 1442, 'n_words': 375}