1
from models import RegionData, EVSData, VSData
2
from swevote.models import Region, EarlyVotingStation, VotingStation
3
from django.db.models.signals import post_save
1
from swevote.utils import slugify
2
from models import RegionData, EVSData, VSData, LocalData
3
from utils import distance
4
from swevote.models import Region, EarlyVotingStation, VotingStation, VotingLocal, LocalTypes
5
from django.db.models.signals import post_save, post_syncdb
6
8
def create_data_for_region(sender, instance, **kwargs):
34
38
if len(VSData.objects.filter(votingstation=instance)) == 0:
35
39
data = VSData(votingstation=instance)
39
post_save.connect(create_data_for_region, sender=Region)
40
post_save.connect(create_data_for_evs, sender=EarlyVotingStation)
41
41
post_save.connect(create_data_for_vs, sender=VotingStation)
44
# Utilities for migration 0001 -> 0002
46
REMOVAL_PATTERN = r'[ s,.:;-_]'
48
def _find_existing_local(station):
49
for local in VotingLocal.objects.filter(municipality=station.municipality):
50
if re.sub(REMOVAL_PATTERN, '', local.name.lower()) == re.sub(REMOVAL_PATTERN, '', station.local.lower()):
51
if distance(local,station) < 25:
53
if station.address1 != "" and local.address1 == station.address1 and distance(local,station) < 300:
57
def _generate_slug(name, municipality):
61
slug = slugify(name + ' ' + str(count))
65
if VotingLocal.objects.filter(municipality=municipality,
66
slug=slug).count() == 0:
72
def copy_bookings(localdata, vsdata):
75
localdata.ballots += vsdata.ballots
76
#localdata.ballots_locked = vsdata.ballots_locked
78
if vsdata.distributor:
79
if localdata.distributor and \
80
localdata.distributor.email != \
81
vsdata.distributor.email:
82
raise Exception("%s already has distributor!" % localdata.votinglocal.name)
83
localdata.distributor = vsdata.distributor
86
if vsdata.first_watch:
87
if localdata.first_watch and \
88
localdata.first_watch.email != \
89
vsdata.first_watch.email:
90
raise Exception("%s already has first_watch!" % localdata.votinglocal.name)
91
localdata.first_watch = vsdata.first_watch
94
if vsdata.second_watch:
95
if localdata.second_watch and \
96
localdata.second_watch.email != \
97
vsdata.second_watch.email:
98
raise Exception("%s already has second_watch!" % localdata.votinglocal.name)
99
localdata.second_watch = vsdata.second_watch
102
if vsdata.third_watch:
103
if localdata.third_watch and \
104
localdata.third_watch.email != \
105
vsdata.third_watch.email:
106
raise Exception("%s already has third_watch!" % localdata.votinglocal.name)
107
localdata.third_watch = vsdata.third_watch
113
def generate_locals():
114
if VotingLocal.objects.count() > 0:
115
print "VotingLocal already existing. Remove all and try again. Exiting."
118
print "Calculating VotingLocal (this might take a while)..."
119
for station in VotingStation.objects.order_by('municipality'):
120
local = _find_existing_local(station)
122
local.voters += station.voters
126
localtype=LocalTypes.ELECTION,
127
municipality=station.municipality,
128
region=station.region,
129
name=station.local.strip(),
130
slug=_generate_slug(station.local, station.municipality),
131
address1=station.address1.strip(),
132
address2=station.address2.strip(),
133
address3=station.address3.strip(),
134
city=station.city.strip(),
135
voters=station.voters,
140
local.localdata = LocalData(votinglocal=local)
141
local.localdata.save()
143
station.votinglocal = local
146
print "found %d locals" % VotingLocal.objects.count()
149
def merge_bookings():
150
for station in VotingStation.objects.all():
151
copy_bookings(station.votinglocal.data, station.vsdata)
153
def postsync_votinglocal_handler(sender, app, created_models,
154
verbosity, interactive, **kwargs):
155
if LocalData in created_models:
156
if VotingLocal.objects.count() == 0:
160
post_syncdb.connect(postsync_votinglocal_handler)