Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dev #204

Merged
merged 9 commits into from
Feb 1, 2013
180 changes: 98 additions & 82 deletions elections/management/commands/elections_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,80 +2,15 @@
from django.core.management.base import BaseCommand, CommandError
from django.contrib.auth.models import User
from elections.models import Election, Candidate, PersonalData, Category, Question, Answer, BackgroundCategory,\
Background, Link, BackgroundCandidate
Background, Link, BackgroundCandidate, PersonalDataCandidate
import csv
from django.core.urlresolvers import reverse

class Loader(object):
def __init__(self,username, lines, styles):
self.user = User.objects.get(username=username)
self.lines = lines
self.styles = styles

def getElection(self, line):
election_name = line[0].decode('utf-8').strip()
election, created = Election.objects.get_or_create(name=election_name, owner=self.user)

if(created):
election.custom_style = self.styles
election.save()
embedded_url = reverse('election_detail_embeded',kwargs={'username': election.owner.username,'slug': election.slug})
[category.delete() for category in election.category_set.all()]
[personal_data.delete() for personal_data in election.personaldata_set.all()]
[background_category.delete() for background_category in election.backgroundcategory_set.all()]

otros = BackgroundCategory.objects.create(name=u"Otros", election=election)
reparos = Background.objects.create(name=u"Aclaraciones al cuestionario", category=otros)

parser = QuestionsParser(election)
parser.createQuestions(self.lines)
return election


def getCandidate(self, line):
candidate_name = line[1].decode('utf-8').strip()
election = self.getElection(line)
candidate, created = Candidate.objects.get_or_create(name=candidate_name, election=election, has_answered=False)
partido = line[2].decode('utf-8').strip()
personal_data, created_personal_data = PersonalData.objects.get_or_create(label=u"Partido", election=election)
candidate.add_personal_data(personal_data, partido)
pacto = line[3].decode('utf-8').strip()
personal_data, created_personal_data = PersonalData.objects.get_or_create(label=u"Pacto", election=election)
candidate.add_personal_data(personal_data, pacto)
reeleccion = line[4].decode('utf-8').strip()
personal_data, created_personal_data = PersonalData.objects.get_or_create(label=u"¿Va a reelección?", election=election)
candidate.add_personal_data(personal_data, reeleccion)
agnos = line[5].decode('utf-8').strip()
personal_data, created_personal_data = PersonalData.objects.get_or_create(label=u"Número de años que ha sido alcalde", election=election)
candidate.add_personal_data(personal_data, agnos)
periodos = line[6].decode('utf-8').strip()
personal_data, created_personal_data = PersonalData.objects.get_or_create(label=u"Períodos como alcalde", election=election)
candidate.add_personal_data(personal_data, periodos)
postulaciones_previas = line[7].decode('utf-8').strip()
personal_data, created_personal_data = PersonalData.objects.get_or_create(label=u"Elecciones anteriores", election=election)
candidate.add_personal_data(personal_data, postulaciones_previas)

reparos = election.backgroundcategory_set.all()[0].background_set.all()[0]

sin_reparos = BackgroundCandidate.objects.create(candidate=candidate, background=reparos, value=u"Sin aclaraciones")


facebook_address = line[8].decode('utf-8').strip()
if facebook_address:
facebook = Link.objects.create(name=candidate.name, url=facebook_address, candidate=candidate)


twitter_username = line[9].decode('utf-8').strip()
if twitter_username:
twitter = Link.objects.create(name=u'@'+twitter_username, url=u"https://twitter.com/"+twitter_username, candidate=candidate)


return candidate


class QuestionsParser(object):
def __init__(self, election):
self.election = election
self.election.backgroundcategory_set.all().delete()
self.election.personaldata_set.all().delete()

def createQuestions(self, lines):
for line in lines:
Expand All @@ -88,6 +23,92 @@ def createQuestions(self, lines):
if line[0]=="answer":
answer = line[1].decode('utf-8').strip()
self.answer = Answer.objects.create(caption=answer, question=self.question)
if line[0]=="background history category":
background_category_name = line[1].decode('utf-8').strip()
self.background_category = BackgroundCategory.objects.create(name=background_category_name, election=self.election)
if line[0]=="background history":
background_history_name = line[1].decode('utf-8').strip()
self.background_history = Background.objects.create(name=background_history_name, category=self.background_category)
if line[0]=="personal data":
personal_data_label = line[1].decode('utf-8').strip()
self.personal_data = PersonalData.objects.create(label=personal_data_label, election=self.election)

class AnswersLoader(object):
def __init__(self,username, candidates, questions, styles):
self.user = User.objects.get(username=username)
self.lines = candidates
self.styles = styles
self.questions = questions
self.get_definitions()

def get_definitions(self):
line0 = self.lines[0]
line1 = self.lines[1]
self.definitions = {}
if len(line0) > 2:
for i in range(2, len(line0)):
definition = {"label":line1[i], "type":line0[i]}
self.definitions[i] = definition


def get_election(self, line, questions):
election_name = line[0].decode('utf-8').strip()
election, created = Election.objects.get_or_create(name=election_name, owner=self.user)

if(created):
election.custom_style = self.styles
election.save()
parser = QuestionsParser(election)
parser.createQuestions(questions)
return election

def assign_values(self, election, candidate, line):
for i in range(2,len(line)):
value = line[i].decode('utf-8').strip()
label = self.definitions[i]["label"]
the_type = self.definitions[i]["type"]

if(the_type == "personal data"):
try:
personal_data = PersonalData.objects.get(election=election, label=label)
PersonalDataCandidate.objects.create(candidate=candidate, personal_data=personal_data, value=value)
except :
print "personal data non existent "+label +"| value "+ value
if (the_type == "background history"):
try:

category_name = label.split(" - ")[0]
label = label.split(" - ")[1]
category = BackgroundCategory.objects.get(name=category_name, election=election)
background = Background.objects.get(name=label, category=category)
BackgroundCandidate.objects.create(candidate=candidate, background=background, value=value)
except :
print "background non existent "+label +"| value "+ value

if (the_type == "link"):
try:
if (label == "twitter"):
Link.objects.create(name=u'@'+value, url=u"https://twitter.com/"+value, candidate=candidate)
if (label == "facebook"):
Link.objects.create(name=candidate.name, url=value, candidate=candidate)
except:
print "link non existent "+label +"| value "+ value


def get_candidate(self, line, election):
candidate = Candidate.objects.create(name=line[1].decode('utf-8').strip(), election=election)
return candidate

def process(self):
amount_of_lines = len(self.lines)

for i in range(2, amount_of_lines):
line = self.lines[i]
election = self.get_election(line, self.questions)
candidate = self.get_candidate(line, election)
self.assign_values(election, candidate, line)




class Command(BaseCommand):
Expand All @@ -96,18 +117,13 @@ def handle(self, *args, **options):
username = args[0]
reader = open(args[3], 'rb')
style = reader.read()
lines_for_election_loader = csv.reader(open(args[1], 'rb'), delimiter=',')
lines_for_question_loader = []
lines_for_candidates_loader = csv.reader(open(args[1], 'rb'), delimiter=',')
candidates =[]
for line in lines_for_candidates_loader:
candidates.append(line)
questions_reader = csv.reader(open(args[2], 'rb'), delimiter=',')
for question_line in questions_reader:
lines_for_question_loader.append(question_line)
processCandidates(username, lines_for_election_loader, lines_for_question_loader,style)



def processCandidates(username, lines_for_election_loader, lines_for_question_loader, styles):
loader = Loader(username, lines_for_question_loader, styles)
for candidate_line in lines_for_election_loader:
candidate = loader.getCandidate(candidate_line)


questions = []
for line in questions_reader:
questions.append(line)
loader = AnswersLoader(username, candidates, questions, style)
loader.process()
Loading