-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into refactor/front-end
- Loading branch information
Showing
84 changed files
with
22,122 additions
and
430 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
# coding=utf-8 | ||
from numpy import matrix | ||
from django.core.cache import cache | ||
from elections.models import Candidate | ||
from popular_proposal.models import PopularProposal | ||
|
||
|
||
class CandidateCommitmentsMatrixGenerator(object): | ||
def __init__(self): | ||
self.cache_key = 'matrix_proposals_candidates' | ||
self.candidate_index_in_matrix = {} | ||
self.proposal_index_in_matrix = {} | ||
|
||
def _set_candidate_index_in_matrix(self, candidate, index): | ||
self.candidate_index_in_matrix[candidate.id] = index | ||
|
||
def _get_candidate_index_in_matrix(self): | ||
cache_key = 'candidate_index_in_matrix' | ||
if cache.get(cache_key) is not None: | ||
return cache.get(cache_key) | ||
else: | ||
cache.set('candidate_index_in_matrix', self.candidate_index_in_matrix) | ||
return self.candidate_index_in_matrix | ||
|
||
def _get_proposal_index_in_matrix(self): | ||
cache_key = 'proposal_index_in_matrix' | ||
if cache.get(cache_key) is not None: | ||
return cache.get(cache_key) | ||
else: | ||
cache.set('proposal_index_in_matrix', self.proposal_index_in_matrix) | ||
return self.proposal_index_in_matrix | ||
|
||
def _set_proposal_index_in_matrix(self, proposal, index): | ||
self.proposal_index_in_matrix[proposal.id] = index | ||
|
||
def set_cache(self, time=7200): | ||
m = self._get_matrix_with_all_proposals() | ||
cache.set(self.cache_key, m, time) | ||
cache.set('candidate_index_in_matrix', self.candidate_index_in_matrix) | ||
cache.set('proposal_index_in_matrix', self.proposal_index_in_matrix) | ||
return m | ||
|
||
def get_matrix_with_all_proposals(self): | ||
if cache.get(self.cache_key) is not None: | ||
return cache.get(self.cache_key) | ||
return self.set_cache() | ||
|
||
def _get_matrix_with_all_proposals(self): | ||
_C = [] | ||
candidate_index = 0 | ||
all_proposals = PopularProposal.objects.all() | ||
for candidate in Candidate.objects.filter(commitments__isnull=False): | ||
vector = [] | ||
self._set_candidate_index_in_matrix(candidate, candidate_index) | ||
candidate_index += 1 | ||
proposal_index = 0 | ||
for p in all_proposals: | ||
self._set_proposal_index_in_matrix(p, proposal_index) | ||
proposal_index += 1 | ||
if candidate.commitments.filter(proposal=p, commited=True).exists(): | ||
vector.append(1) | ||
else: | ||
vector.append(0) | ||
_C.append(vector) | ||
return matrix(_C) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
Empty file.
16 changes: 16 additions & 0 deletions
16
medianaranja2/management/commands/set_cache_for_medianaranja.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
from django.core.management.base import BaseCommand | ||
from medianaranja2.candidate_proposals_matrix_generator import CandidateCommitmentsMatrixGenerator | ||
|
||
|
||
class Command(BaseCommand): | ||
help = "Updates the cache for medianaranja" | ||
|
||
def add_arguments(self, parser): | ||
parser.add_argument('time', type=int, default=7200, nargs="*") | ||
|
||
def handle(self, *args, **options): | ||
matrix_generator = CandidateCommitmentsMatrixGenerator() | ||
time = options['time'][0] | ||
m = matrix_generator.set_cache(time) | ||
shape = m.shape | ||
self.stdout.write("Saved matrix of dimensions (%d, %d) for %d miliseconds" % (shape[0], shape[1], time)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# -*- coding: utf-8 -*- | ||
# Generated by Django 1.11.9 on 2018-08-30 19:23 | ||
from __future__ import unicode_literals | ||
|
||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('merepresenta', '0017_auto_20180815_1604'), | ||
] | ||
|
||
operations = [ | ||
migrations.AlterField( | ||
model_name='candidate', | ||
name='bio', | ||
field=models.TextField(blank=True, default=''), | ||
), | ||
migrations.AlterField( | ||
model_name='candidate', | ||
name='lgbt_desc', | ||
field=models.ManyToManyField(blank=True, to='merepresenta.LGBTQDescription'), | ||
), | ||
migrations.AlterField( | ||
model_name='candidate', | ||
name='renovacao_politica', | ||
field=models.CharField(blank=True, default='', max_length=512), | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.