Skip to content

Instantly share code, notes, and snippets.

@lfalvarez
Created August 22, 2016 19:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lfalvarez/209623fdf0ecf16857643a0cc6a1d54c to your computer and use it in GitHub Desktop.
Save lfalvarez/209623fdf0ecf16857643a0cc6a1d54c to your computer and use it in GitHub Desktop.
mails_a_proponentes_19_agosto.py
import uuid
from django.core.urlresolvers import reverse
from django.utils.text import slugify
import csv
from django.template.loader import get_template
from django.template import Context, Template
def process_usuario_propuesta(row, post_delete_users=False):
nombre = row[0]
apellido = row[1]
nombre_completo = ''
mail = row[2]
id_propuesta = row[3]
acepta = row[4]
users = []
usuario_creado = False
propuesta_aceptada = False
es_organizacion = False
context = {}
if User.objects.filter(email=mail).exists():
user = User.objects.get(email=mail)
context['user'] = user
else:
propuesta_aceptada = True
if not apellido.strip():
es_organizacion = True
if es_organizacion:
username = nombre.strip().lower()
nombre_completo = nombre
else:
username = nombre.strip().lower() + '.' + apellido.strip().lower()
nombre_completo = nombre + ' ' + apellido
password = uuid.uuid4().hex
username = slugify(username)
user = User.objects.create(username=username, password=password)
usuario_creado = True
users.append(user.id)
context.update({'user': user,
'password': password})
user.profile.is_organization = es_organizacion
user.profile.save()
p = ProposalTemporaryData.objects.get(id=int(id_propuesta.strip()))
p.proposer = user
p.save()
texto_mail = u'Hola ' + unicode(nombre_completo) + u':\n'
texto_mail += u'¿Recuerdas que hiciste una propuesta para VotaInteligente.cl el pasado 19 de agosto?'
texto_mail += u'\nLa propuesta que creaste es como sigue:\n'
template = get_template('popular_proposal/plantillas/carta_candidato.txt')
context = Context({'area': p.area, 'preview_data':p.data})
t = template.render(context)
texto_mail += t
if propuesta_aceptada:
texto_mail += u'estamos a punto de aceptarla pero queríamos\npedirte el visto bueno para publicarla.'
texto_mail += u'Si nos respondes este mail con un "Si, publiquen mi propuesta", lo haremos.\n'
else:
texto_mail += u'tenemos algunos comentarios. Y si los quieres ver debes ingresar en la siguiente url:\n'
url = reverse('backend_citizen:temporary_data_update', kwargs={'pk':p.id})
texto_mail += u'http://votainteligente.cl' + url
if usuario_creado:
texto_mail += u'\nTe queríamos contar además que te creamos un usuario y password y son como sigue:'
texto_mail += u'\n ** usuario: ' + username
texto_mail += u'\n ** password: ' + password
url2 = reverse('password_reset')
texto_mail += u'\nPara modificar tu password debes acceder a:'
texto_mail += u'http://votainteligente.cl' + url2
if post_delete_users:
User.objects.filter(id__in=users).delete()
print texto_mail
def unicode_csv_reader(utf8_data, dialect=csv.excel, **kwargs):
csv_reader = csv.reader(utf8_data, dialect=dialect, **kwargs)
for row in csv_reader:
yield [unicode(cell, 'utf-8') for cell in row]
reader = unicode_csv_reader(open('proponentes.csv'))
for row in reader:
process_usuario_propuesta(row, post_delete_users=True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment