Skip to content

Commit

Permalink
Add new proxies, methods and properties for getting OCD metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
gordonje committed Aug 11, 2017
1 parent d252dd7 commit f40747e
Show file tree
Hide file tree
Showing 13 changed files with 222 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"""
import datetime
from calaccess_processed.models import (
OCDRunoffProxy,
OCDCandidateContestProxy,
OCDCandidacyProxy,
ScrapedCandidateProxy,
)
Expand All @@ -29,7 +29,7 @@ def handle(self, *args, **options):
# connect runoffs to their previously undecided contests
if self.verbosity > 2:
self.log(' Linking runoffs to previous contests')
OCDRunoffProxy.objects.set_parents()
OCDCandidateContestProxy.objects.set_parents()

self.success("Done!")

Expand Down Expand Up @@ -84,8 +84,8 @@ def load(self):
# He was on the ballot in the general but records and a phone
# interview with the candidate show he did not run in any primary.
if (
contest.name == 'STATE SENATE 15 (NO PARTY PREFERENCE)'
and contest.election.date == datetime.date(2008, 6, 3)
contest.name == 'STATE SENATE 15 (NO PARTY PREFERENCE)' and
contest.election.date == datetime.date(2008, 6, 3)
):
if self.verbosity > 2:
self.log("Deleting blacklisted {}".format(contest))
Expand Down
12 changes: 10 additions & 2 deletions calaccess_processed/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,14 +92,18 @@
ScrapedIncumbentElectionProxy,
ScrapedPropositionProxy,
ScrapedPropositionElectionProxy,
OCDCandidateContestProxy,
OCDCandidacyProxy,
OCDDivisionProxy,
OCDElectionProxy,
OCDMembershipProxy,
OCDOrganizationProxy,
OCDOrganizationIdentifierProxy,
OCDOrganizationNameProxy,
OCDPartyProxy,
OCDPersonProxy,
OCDPersonNameProxy,
OCDPostProxy,
OCDRunoffProxy,
)


Expand Down Expand Up @@ -187,12 +191,16 @@
'ScrapedIncumbentElectionProxy',
'ScrapedPropositionProxy',
'ScrapedPropositionElectionProxy',
'OCDCandidateContestProxy',
'OCDCandidacyProxy',
'OCDDivisionProxy',
'OCDElectionProxy',
'OCDMembershipProxy',
'OCDOrganizationProxy',
'OCDOrganizationIdentifierProxy',
'OCDOrganizationNameProxy',
'OCDPartyProxy',
'OCDPersonProxy',
'OCDPersonNameProxy',
'OCDPostProxy',
'OCDRunoffProxy',
)
12 changes: 10 additions & 2 deletions calaccess_processed/models/proxies/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,18 @@
ScrapedPropositionElectionProxy
)
from .opencivicdata import (
OCDCandidateContestProxy,
OCDCandidacyProxy,
OCDDivisionProxy,
OCDElectionProxy,
OCDMembershipProxy,
OCDOrganizationProxy,
OCDOrganizationIdentifierProxy,
OCDOrganizationNameProxy,
OCDPartyProxy,
OCDPersonProxy,
OCDPersonNameProxy,
OCDPostProxy,
OCDRunoffProxy,
)


Expand All @@ -32,12 +36,16 @@
'ScrapedIncumbentElectionProxy',
'ScrapedPropositionProxy',
'ScrapedPropositionElectionProxy',
'OCDCandidateContestProxy',
'OCDCandidacyProxy',
'OCDDivisionProxy',
'OCDElectionProxy',
'OCDMembershipProxy',
'OCDOrganizationProxy',
'OCDOrganizationIdentifierProxy',
'OCDOrganizationNameProxy',
'OCDPartyProxy',
'OCDPersonProxy',
'OCDPersonNameProxy',
'OCDPostProxy',
'OCDRunoffProxy',
)
17 changes: 13 additions & 4 deletions calaccess_processed/models/proxies/opencivicdata/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,32 @@
"""
Proxy models for augmenting our source data tables with methods useful for processing.
"""
from .candidatecontests import OCDCandidateContestProxy
from .candidacies import OCDCandidacyProxy
from .divisions import OCDDivisionProxy
from .elections import OCDElectionProxy
from .organizations import OCDOrganizationProxy
from .organizations import (
OCDMembershipProxy,
OCDOrganizationProxy,
OCDOrganizationIdentifierProxy,
OCDOrganizationNameProxy,
)
from .parties import OCDPartyProxy
from .people import OCDPersonProxy
from .people import OCDPersonProxy, OCDPersonNameProxy
from .posts import OCDPostProxy
from .candidatecontests import OCDRunoffProxy


__all__ = (
'OCDCandidateContestProxy',
'OCDCandidacyProxy',
'OCDDivisionProxy',
'OCDElectionProxy',
'OCDMembershipProxy',
'OCDOrganizationProxy',
'OCDOrganizationIdentifierProxy',
'OCDOrganizationNameProxy',
'OCDPartyProxy',
'OCDPersonProxy',
'OCDPersonNameProxy',
'OCDPostProxy',
'OCDRunoffProxy',
)
40 changes: 40 additions & 0 deletions calaccess_processed/models/proxies/opencivicdata/base.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Base class for proxies to OCD models.
"""
import textwrap


class OCDProxyModelMixin(object):
"""
Properties and methods shared by all OCD proxy models.
"""
@property
def model(self):
"""
Returns the model class that is being proxied.
"""
return self.__class__.__bases__[0]

@property
def doc(self):
"""
Returns doc string of the model corresponding to the ProcessedDataFile.
"""
if self.model.__doc__.startswith(self.model._meta.object_name):
return ''
return textwrap.dedent(self.model.__doc__).strip()

@property
def klass_group():
"""
Return the model's group.
"""
return "CCDC"

def get_field_list(self):
"""
Return all the fields on the model as a list.
"""
return self.model._meta.fields
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from django.db.models.functions import Cast
from opencivicdata.core.models import Membership
from opencivicdata.elections.models import Candidacy
from .base import OCDProxyModelMixin


class OCDCandidacyQuerySet(models.QuerySet):
Expand Down Expand Up @@ -156,7 +157,7 @@ def get_or_create_from_calaccess(
return candidacy, candidacy_created


class OCDCandidacyProxy(Candidacy):
class OCDCandidacyProxy(Candidacy, OCDProxyModelMixin):
"""
A proxy on the OCD Candidacy model with helper methods.
"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,18 @@
from __future__ import unicode_literals
from django.db import models
from opencivicdata.elections.models import CandidateContest
from .base import OCDProxyModelMixin


class OCDRunoffManager(models.Manager):
class OCDCandidateContestManager(models.Manager):
"""
Custom helpers for the OCD CandidateContest model that limit it to runoffs.
"""
def get_queryset(self):
"""
Filters down to state senate divisions.
"""
return super(OCDRunoffManager, self).get_queryset().filter(name__contains='RUNOFF')
return super(OCDCandidateContestManager, self).get_queryset().filter(name__contains='RUNOFF')

def set_parents(self):
"""
Expand All @@ -29,11 +30,11 @@ def set_parents(self):
obj.save()


class OCDRunoffProxy(CandidateContest):
class OCDCandidateContestProxy(CandidateContest, OCDProxyModelMixin):
"""
A proxy on the OCD CandidateContest model with helper methods and limited to runoffs.
"""
objects = OCDRunoffManager()
objects = OCDCandidateContestManager()

class Meta:
"""
Expand Down
11 changes: 10 additions & 1 deletion calaccess_processed/models/proxies/opencivicdata/divisions.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from __future__ import unicode_literals
from django.db import models
from opencivicdata.core.models import Division
from .base import OCDProxyModelMixin


class OCDAssemblyDivisionManager(models.Manager):
Expand Down Expand Up @@ -47,7 +48,7 @@ def california(self):
return self.get_queryset().get(id='ocd-division/country:us/state:ca')


class OCDDivisionProxy(Division):
class OCDDivisionProxy(Division, OCDProxyModelMixin):
"""
A proxy on the OCD Division model with helper methods.
"""
Expand All @@ -60,3 +61,11 @@ class Meta:
Make this a proxy model.
"""
proxy = True

@property
def doc(self):
"""
Returns the preferred description for the proxied model.
"""
return "A political geography such as a county or legislative district, \
which may have multiple boundaries over its lifetime."
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from django.utils.text import get_text_list
from .organizations import OCDOrganizationProxy
from opencivicdata.elections.models import Election
from .base import OCDProxyModelMixin


class OCDPartisanPrimaryManager(models.Manager):
Expand Down Expand Up @@ -54,7 +55,7 @@ def create_from_calaccess(self, name, dt, election_id=None, election_type=None):
return obj


class OCDElectionProxy(Election):
class OCDElectionProxy(Election, OCDProxyModelMixin):
"""
A proxy for the Election model in opencivicdata app.
"""
Expand Down
75 changes: 73 additions & 2 deletions calaccess_processed/models/proxies/opencivicdata/organizations.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,13 @@
"""
from __future__ import unicode_literals
from django.db import models
from opencivicdata.core.models import Organization
from opencivicdata.core.models import (
Membership,
Organization,
OrganizationIdentifier,
OrganizationName,
)
from .base import OCDProxyModelMixin


class OCDOrganizationManager(models.Manager):
Expand Down Expand Up @@ -69,7 +75,7 @@ def board_of_equalization(self):
)[0]


class OCDOrganizationProxy(Organization):
class OCDOrganizationProxy(Organization, OCDProxyModelMixin):
"""
A proxy on the OCD Organization model with helper methods.
"""
Expand All @@ -80,3 +86,68 @@ class Meta:
Make this a proxy model.
"""
proxy = True

@property
def doc(self):
"""
Returns the preferred description for the proxied model.
"""
return "A group of people, typically in a legislative or rule-making context."


class OCDOrganizationIdentifierProxy(OrganizationIdentifier, OCDProxyModelMixin):
"""
A proxy on the OCD OrganizationIdentifier model with helper methods.
"""

class Meta:
"""
Make this a proxy model.
"""
proxy = True

@property
def doc(self):
"""
Returns the preferred description for the proxied model.
"""
return "Upstream identifiers of the organization, if any exist."


class OCDOrganizationNameProxy(OrganizationName, OCDProxyModelMixin):
"""
A proxy on the OCD OrganizationName model with helper methods.
"""

class Meta:
"""
Make this a proxy model.
"""
proxy = True

@property
def doc(self):
"""
Returns the preferred description for the proxied model.
"""
return "Alternate or former names for the organization."


class OCDMembershipProxy(Membership, OCDProxyModelMixin):
"""
A proxy on the OCD Membership model with helper methods.
"""

class Meta:
"""
Make this a proxy model.
"""
proxy = True

@property
def doc(self):
"""
Returns the preferred description for the proxied model.
"""
return "A relationship between a Person and an Organization, possibly \
including a Post."

0 comments on commit f40747e

Please sign in to comment.