from django.db import models
from django.dispatch import receiver
from django.core.urlresolvers import reverse
from django.template.defaultfilters import slugify

from candidates.models import PopItPerson, person_added

class CachedCount(models.Model):
    """
    Fairly generic model for storing counts of various sorts.

    The object_id is used for linking through to the relevant URL.
    """

    count_type = models.CharField(blank=False, max_length=100, db_index=True)
    name = models.CharField(blank=False, max_length=100)
    count = models.IntegerField(blank=False, null=False)
    object_id = models.CharField(blank=True, max_length=100)
    election = models.CharField(blank=True, null=True, max_length=512)

    class Meta:
        ordering = ['-count', 'name']

    @classmethod
    def total_2015(cls):
        print cls.objects.get(name="total_2015")
        return cls.objects.get(name="total_2015").count

    @property
    def object_url(self):
        if self.count_type == "constituency":
            return reverse('constituency', kwargs={
                # FIXME: for the moment, just return the the URL for
                # the 2015 election, but really 'election' should be
                # another field in CachedCount, and 'constituency'
                # should be migrated to 'post'.
                'election': '2015',
                'mapit_area_id': self.object_id,
                'ignored_slug': slugify(self.name)
            })

    @classmethod
    def increment_count(cls, count_type, object_id):
        """
        Increments the count of the object with the type of `count_type` and
        the id of `object_id`.  If this object does not exist, do nothing.
        """
        filters = {
            'count_type': count_type,
            'object_id': object_id,
        }

        cls.objects.filter(**filters).update(count=models.F('count') + 1)


@receiver(person_added, sender=PopItPerson)
def person_added_handler(sender, **kwargs):
    """
    Called when the `person_added` signal is sent, mainly from
    `candidates.update.create_person`.
    """

    data = kwargs['data']

    # constituency
    constituency_url = data['standing_in'].get('2015', {}).get('mapit_url')
    constituency_id = constituency_url.split('/')[-1]
    CachedCount.increment_count('constituency', constituency_id)

    # party
    party_id = data['party_memberships'].get('2015', {}).get('id')
    CachedCount.increment_count('party', party_id)

@@ -769,24 +769,408 @@
 ', 'name'%5D%0A%0A
+    def __repr__(self):%0A        fmt = '%3CCachedCount: election=%7Be%7D count_type=%7Bct%7D, name=%7Bn%7D, count=%7Bc%7D, object_id=%7Bo%7D%3E'%0A        return fmt.format(%0A            e=repr(self.election),%0A            ct=repr(self.count_type),%0A            n=repr(self.name),%0A            c=repr(self.count),%0A            o=repr(self.object_id),%0A        )%0A%0A    def __unicode__(self):%0A        return repr(self)%0A%0A
     @classme

Add a helful __repr__ and __unicode__ for CachedCount