Skip to content

Commit

Permalink
Merge 0ecb79a into cd74147
Browse files Browse the repository at this point in the history
  • Loading branch information
lfalvarez committed Jul 31, 2018
2 parents cd74147 + 0ecb79a commit f0b71a9
Show file tree
Hide file tree
Showing 20 changed files with 477 additions and 49 deletions.
27 changes: 27 additions & 0 deletions merepresenta/migrations/0010_volunteerprofile.py
@@ -0,0 +1,27 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.11.9 on 2018-07-31 16:04
from __future__ import unicode_literals

from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
('elections', '0033_auto_20180717_2345'),
('merepresenta', '0009_candidate_facebook_contacted'),
]

operations = [
migrations.CreateModel(
name='VolunteerProfile',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('area', models.ForeignKey(null=True, on_delete=django.db.models.deletion.CASCADE, related_name='volunteers', to='elections.Area')),
('user', models.OneToOneField(on_delete=django.db.models.deletion.CASCADE, related_name='volunteer_profile', to=settings.AUTH_USER_MODEL)),
],
),
]
21 changes: 21 additions & 0 deletions merepresenta/migrations/0011_auto_20180731_1604.py
@@ -0,0 +1,21 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.11.9 on 2018-07-31 16:04
from __future__ import unicode_literals

from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

dependencies = [
('merepresenta', '0010_volunteerprofile'),
]

operations = [
migrations.AlterField(
model_name='volunteergetscandidateemaillog',
name='contact',
field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.CASCADE, to='backend_candidate.CandidacyContact'),
),
]
19 changes: 19 additions & 0 deletions merepresenta/migrations/0012_auto_20180731_1749.py
@@ -0,0 +1,19 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.11.9 on 2018-07-31 17:49
from __future__ import unicode_literals

from django.db import migrations


class Migration(migrations.Migration):

dependencies = [
('merepresenta', '0011_auto_20180731_1604'),
]

operations = [
migrations.AlterModelTable(
name='volunteerprofile',
table='voluntarios_profile',
),
]
21 changes: 18 additions & 3 deletions merepresenta/models.py
Expand Up @@ -13,6 +13,7 @@
import datetime



class MeRepresentaPopularProposal(PopularProposal):
class Meta:
proxy = True
Expand Down Expand Up @@ -84,17 +85,20 @@ class VolunteerInCandidate(models.Model):
class VolunteerGetsCandidateEmailLog(models.Model):
volunteer = models.ForeignKey(User)
candidate = models.ForeignKey(Candidate)
contact = models.ForeignKey(CandidacyContact)
contact = models.ForeignKey(CandidacyContact, null=True)
created = models.DateTimeField(auto_now=True)
updated = models.DateTimeField(auto_now_add=True)


def save(self, *args, **kwargs):
creating = self.pk is None
if creating:
## Contact can be none, since a volunteer can
## say that she/he contacted the candidate through other means
if creating and self.contact:
context = {
'candidate': self.candidate.name
}

send_mail(context, 'contato_novo_com_candidato', to=[self.contact.mail],)
return super(VolunteerGetsCandidateEmailLog, self).save(*args, **kwargs)

Expand All @@ -110,4 +114,15 @@ class Partido(models.Model):
initials = models.CharField(max_length=1024, null=True)
number = models.CharField(max_length=1024, null=True)
mark = models.IntegerField(null=True)
coaligacao = models.ForeignKey(Coaligacao, null=True)
coaligacao = models.ForeignKey(Coaligacao, null=True)




##### VOLUNTEERS PART!!!
## I wrote this as part of #MeRepresenta, this means that we haven't needed volunteers doing research on candidates before
## This is why I kept it here until now
## But as I'm coding it I am becoming aware that this could be a good feature to have in the feature, this is why I'm keeping this in
## an inner module, so when I have more time and the need from another NGO to have the volunteers backend
## I can grab it and move it to another application
from merepresenta.voluntarios.models import VolunteerProfile
20 changes: 20 additions & 0 deletions merepresenta/templates/voluntarios/index.html
Expand Up @@ -9,9 +9,29 @@
<div class="alert alert-success" role="alert">
<h4 class="alert-heading">Bemvinda!</h4>
<p>Issa é a lista dos candidatos para pesquizar.</p>


<!--form method="GET" action="">
<div class="form-group">
<label for="exampleFormControlInput1">Escolha seu estado</label>
{{filter.form.elections__area}}
</div>
<button type="submit" class="btn btn-primary">Pesquisa</button>
</form-->

<form method="POST" action="{% url 'update_area_of_volunteer' %}">
{% csrf_token %}
<div class="form-group">
<label for="exampleFormControlInput1">Escolha seu estado</label>
{{update_area_form.area}}
</div>
<button type="submit" class="btn btn-primary">Pesquisa</button>
</form>
</div>

{% endblock header %}
{% block content %}

<div class="container">
<table class="table">
<thead>
Expand Down
Empty file.
8 changes: 6 additions & 2 deletions merepresenta/tests/management_command_tests.py
Expand Up @@ -4,6 +4,8 @@
from elections.models import Area
from merepresenta.models import Candidate, Partido, Coaligacao
from django.core.management import call_command
from django.conf import settings
from django.test import override_settings


class ImporterTestCase(VolunteersTestCaseBase):
Expand All @@ -19,15 +21,17 @@ def setUp(self):
'area': {'election_name': 'DEPUTADO FEDERAL', 'slug': 'RJ', 'area_name': u"Río de Janeiro"}
}


@override_settings(FILTERABLE_AREAS_TYPE=['state',])
def test_creates_elements(self):
rj_area = Area.objects.get(identifier='RJ')

class JustForNowProcessor(TSEProcessorMixin):
pass
processor = JustForNowProcessor()
result = processor.do_something(self.data)
area = result['area']
self.assertEquals(area, rj_area)
print rj_area.id, area.id
self.assertEquals(area.id, rj_area.id)
election = result['election']
self.assertEquals(election.name, 'DEPUTADO FEDERAL')
self.assertEquals(election.area, area)
Expand Down
10 changes: 9 additions & 1 deletion merepresenta/tests/model_tests.py
Expand Up @@ -12,6 +12,7 @@
from django.utils import timezone
import datetime
from django.core.urlresolvers import reverse
from merepresenta.voluntarios.models import VolunteerProfile


class MeRepresentaPopularProposalTestCase(TestCase):
Expand Down Expand Up @@ -117,4 +118,11 @@ def test_candidate_has_partido(self):
email_repeated=False)


self.assertEquals(candidate.partido, partido)
self.assertEquals(candidate.partido, partido)


class VolunteerProfileTestCase(TestCase):
def test_instanciate(self):
u = User.objects.create_user(username='user', is_staff=True)
i = VolunteerProfile.objects.create(user=u)
self.assertIsNone(i.area)
76 changes: 76 additions & 0 deletions merepresenta/tests/volunteers/filter_tests.py
@@ -0,0 +1,76 @@
# coding=utf-8
from django.test import TestCase, override_settings
from merepresenta.models import Candidate, NON_WHITE_KEY, NON_MALE_KEY
from merepresenta.tests.volunteers import VolunteersTestCaseBase
from backend_candidate.models import CandidacyContact
from elections.models import PersonalData, Election, Area
from django.core.urlresolvers import reverse
from django.contrib.auth.models import User
from social_django.models import UserSocialAuth
from merepresenta.models import VolunteerInCandidate, VolunteerGetsCandidateEmailLog
from django.core import mail
from merepresenta.voluntarios.filters import CandidateFilter


PASSWORD="admin123"
ESTADO_IDENTIFIER = 'TO'


@override_settings(FILTERABLE_AREAS_TYPE=['state',])
class CandidateFilterTests(VolunteersTestCaseBase):
def setUp(self):
super(CandidateFilterTests, self).setUp()
self.volunteer = User.objects.create_user(username="voluntario",
password=PASSWORD,
is_staff=True)
self.area = Area.objects.get(identifier=ESTADO_IDENTIFIER)
self.election = Election.objects.create(name="Deputado Estadual")
self.election.area = self.area
self.election.save()
self.candidate = Candidate.objects.get(id=5)
self.election.candidates.add(self.candidate)
self.candidate2 = Candidate.objects.get(id=4)


def test_filter(self):
data = {'elections__area': '',}
_filter = CandidateFilter(data=data)
form = _filter.form
self.assertTrue(form.is_valid())
qs = _filter.qs
self.assertIn(self.candidate, qs)
self.assertIn(self.candidate2, qs)

data = {'elections__area': self.area.id}
_filter = CandidateFilter(data=data)
qs = _filter.qs
self.assertIn(self.candidate, qs)
self.assertNotIn(self.candidate2, qs)



@override_settings(ROOT_URLCONF='merepresenta.stand_alone_urls')
class VolunteersIndexViewWithFilterTests(VolunteersTestCaseBase):
def setUp(self):
super(VolunteersIndexViewWithFilterTests, self).setUp()

def create_ordered_candidates(self):
Candidate.objects.filter(id__in=[4, 5]).update(gender=NON_MALE_KEY)
cs = Candidate.objects.filter(id__in=[4,5])
self.assertEquals(cs[0].is_women, 1)
self.assertEquals(cs[1].is_women, 1)
c = Candidate.objects.get(id=5)
c.race = NON_WHITE_KEY["possible_values"][0]
c.save()

def test_get_index(self):
url = reverse('volunteer_index')

u = User.objects.create_user(username="new_user", password="abc", is_staff=True)
self.client.login(username=u.username, password="abc")
response = self.client.get(url)

self.create_ordered_candidates()
response = self.client.get(url)
self.assertEquals(response.status_code, 200)
self.assertTrue(response.context['filter'])
26 changes: 26 additions & 0 deletions merepresenta/tests/volunteers/forms_tests.py
@@ -0,0 +1,26 @@
# coding=utf-8
from django.test import TestCase, override_settings
from merepresenta.voluntarios.forms import UpdateAreaForm
from elections.models import Area
from django.contrib.auth.models import User
from merepresenta.voluntarios.models import VolunteerProfile


class UpdateAreaForVolunteerForm(TestCase):
def setUp(self):
super(UpdateAreaForVolunteerForm, self).setUp()
self.area = Area.objects.create(name='area')
self.volunteer = User.objects.create_user(username='volunteer', is_staff=True)
self.profile = VolunteerProfile.objects.create(user=self.volunteer)


def test_instanciate_and_save(self):
data = {
'area': self.area.id
}
form = UpdateAreaForm(data=data, instance=self.profile)
self.assertTrue(form.is_valid())
profile = form.save()

self.assertEquals(profile.area, self.area)

0 comments on commit f0b71a9

Please sign in to comment.