# Path: elections/models.py
# class ElectionEvent(models.Model):
#     """An event that is going to happen in an election"""
#     event_code = models.CharField(primary_key=True, max_length=20)
#     state = models.CharField(max_length=2)
#     state_name = models.CharField(max_length=32)
#     event_date = models.DateField()
#     description = models.CharField(blank=True, max_length=255)
#     checksum = models.CharField(max_length=32)
#     
#     def calculate_checksum(self):
#         """
#         Calculate the MD5 checksum for the record
#         """
#         import hashlib
#         checksum = hashlib.md5()
#         checksum.update(self.event_code)
#         checksum.update(self.state or '')
#         checksum.update(self.state_name or '')
#         checksum.update(self.event_date.isoformat() or '')
#         checksum.update(self.description or '')
#         return checksum.hexdigest()
#     
#     def save(self, *args, **kwargs):
#         """
#         Add the checksum
#         """
#         self.checksum = self.calculate_checksum()
#         super(ElectionEvent, self).save(*args, **kwargs)
#     
#     class Meta:
#         ordering = ['event_date',]
# 
#     def __unicode__(self):
#         return u" ".join([self.state, self.event_date.isoformat(), self.description])


import time
import datetime
import hashlib
        import csv
from django.core.management.base import LabelCommand, CommandError
from elections.models import ElectionEvent


class Command(LabelCommand):
    args = '[file1 file2 ...]'
    help = 'Imports election events'

    def handle_label(self, label, **options):
        events = csv.reader(open(label, 'rb'), delimiter='|')
        for row in events:
            row[3] = datetime.datetime.strptime(row[3], "%Y-%m-%d").date()
            checksum = hashlib.md5()
            
            for item in row[:-1]:
                if item:
                    checksum.update(str(item))
                else:
                    checksum.update('')
            try:
                evt = ElectionEvent.objects.get(event_code=row[0])
                if evt.checksum != checksum.hexdigest():
                    evt.state = row[1]
                    evt.state_name = row[2]
                    evt.event_date = row[3]
                    evt.description = row[4]
                    print 'Updating %s' % (" ".join([row[0], row[4],]))
                    evt.save()
                else:
                    print "Skipping %s. No change." % row[0]
            except ElectionEvent.DoesNotExist:
                print 'Adding %s' % (" ".join([row[0], row[4],]))

# Path: elections/models.py
# class ElectionEvent(models.Model):
#     """An event that is going to happen in an election"""
#     event_code = models.CharField(primary_key=True, max_length=20)
#     state = models.CharField(max_length=2)
#     state_name = models.CharField(max_length=32)
#     event_date = models.DateField()
#     description = models.CharField(blank=True, max_length=255)
#     checksum = models.CharField(max_length=32)
#     
#     def calculate_checksum(self):
#         """
#         Calculate the MD5 checksum for the record
#         """
#         import hashlib
#         checksum = hashlib.md5()
#         checksum.update(self.event_code)
#         checksum.update(self.state or '')
#         checksum.update(self.state_name or '')
#         checksum.update(self.event_date.isoformat() or '')
#         checksum.update(self.description or '')
#         return checksum.hexdigest()
#     
#     def save(self, *args, **kwargs):
#         """
#         Add the checksum
#         """
#         self.checksum = self.calculate_checksum()
#         super(ElectionEvent, self).save(*args, **kwargs)
#     
#     class Meta:
#         ordering = ['event_date',]
# 
#     def __unicode__(self):
#         return u" ".join([self.state, self.event_date.isoformat(), self.description])


# Path: elections/management/commands/import_calendar.py
import time
import datetime
import hashlib
        import csv
from django.core.management.base import LabelCommand, CommandError
from elections.models import ElectionEvent



class Command(LabelCommand):
    args = '[file1 file2 ...]'
    help = 'Imports election events'

    def handle_label(self, label, **options):
        events = csv.reader(open(label, 'rb'), delimiter='|')
        for row in events:
            row[3] = datetime.datetime.strptime(row[3], "%Y-%m-%d").date()
            checksum = hashlib.md5()
            
            for item in row[:-1]:
                if item:
                    checksum.update(str(item))
                else:
                    checksum.update('')
            try:
                evt = ElectionEvent.objects.get(event_code=row[0])
                if evt.checksum != checksum.hexdigest():
                    evt.state = row[1]
                    evt.state_name = row[2]
                    evt.event_date = row[3]
                    evt.description = row[4]
                    print 'Updating %s' % (" ".join([row[0], row[4],]))
                    evt.save()
                else:
                    print "Skipping %s. No change." % row[0]
            except ElectionEvent.DoesNotExist:
                print 'Adding %s' % (" ".join([row[0], row[4],]))

                evt = ElectionEvent.objects.create(