from django.db import models
from django.contrib.postgres.fields import ArrayField


class AreaType(models.Model):
    name = models.CharField(max_length=128)
    source = models.CharField(max_length=128, blank=True,
                              help_text="e.g MapIt")

    def __unicode__(self):
        return self.name


class Election(models.Model):
    slug = models.CharField(max_length=128)
    for_post_role = models.CharField(max_length=128)
    winner_membership_role = models.CharField(max_length=128, blank=True)
    candidate_membership_role = models.CharField(max_length=128)
    election_date = models.DateField()
    candidacy_start_date = models.DateField()
    name = models.CharField(max_length=128)
    current = models.BooleanField()
    use_for_candidate_suggestions = models.BooleanField(default=False)
    party_membership_start_date = models.DateField()
    party_membership_end_date = models.DateField()
    area_types = models.ManyToManyField(AreaType)
    area_generation = models.CharField(max_length=128, blank=True)
    organization_id = models.CharField(max_length=128)
    organization_name = models.CharField(max_length=128, blank=True)
    post_id_format = models.CharField(max_length=128)
    party_lists_in_use = models.BooleanField(default=False)
    default_party_list_members_to_show = models.IntegerField(default=0)
    show_official_documents = models.BooleanField(default=False)
    ocd_division = models.CharField(max_length=250, blank=True)

    description = models.CharField(max_length=500, blank=True)

    def __unicode__(self):
        return self.name

@@ -38,49 +38,769 @@
 ngo.
-contrib.postgres.fields import ArrayField
+shortcuts import get_object_or_404%0Afrom collections import defaultdict%0A%0Aclass ElectionQuerySet(models.QuerySet):%0A    def current(self):%0A        return self.filter(%0A            current=True%0A        )%0A%0A    def get_by_slug(self, election):%0A        return get_object_or_404(self, slug=election)%0A%0A    def by_date(self):%0A        return self.order_by(%0A            'election_date'%0A        )%0A%0Aclass ElectionManager(models.Manager):%0A    def elections_for_area_generations(self):%0A        generations = defaultdict(list)%0A        for election in self.current():%0A            for area_type in election.area_types.all():%0A                area_tuple = (area_type.name, election.area_generation)%0A                generations%5Barea_tuple%5D.append(election)%0A%0A        return generations
 %0A%0A%0Ac
@@ -2261,24 +2261,89 @@
 lank=True)%0A%0A
+    objects = ElectionManager.from_queryset(ElectionQuerySet)()%0A%0A
     def __un

Add a Manager to the election model