Skip to content

Commit

Permalink
see #628 added indexing; templates untouched
Browse files Browse the repository at this point in the history
  • Loading branch information
scaloni committed Jan 24, 2017
1 parent 467efc6 commit 6945ac9
Show file tree
Hide file tree
Showing 9 changed files with 88 additions and 14 deletions.
9 changes: 9 additions & 0 deletions open_municipio/acts/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,13 +215,22 @@ class Meta:


class ActSearchForm(RangeFacetedSearchForm):
charge_supporting = forms.IntegerField(required=False)
charge_not_supporting = forms.IntegerField(required=False)

def __init__(self, *args, **kwargs):
super(ActSearchForm, self).__init__(*args, **kwargs)

def search(self):
sqs = super(ActSearchForm, self).search()

# add filters for charges
if self.is_valid():
if self.cleaned_data.get('charge_supporting'):
sqs = sqs.filter_and(charge_supporting=self.cleaned_data['charge_supporting'])
if self.cleaned_data.get('charge_not_supporting'):
sqs = sqs.filter_and(charge_not_supporting=self.cleaned_data['charge_not_supporting'])

# default sorting
if (self.is_valid() and not self.cleaned_data.get('order_by')) or not self.is_valid():
sqs = sqs.order_by('-pub_date')
Expand Down
65 changes: 64 additions & 1 deletion open_municipio/acts/search_indexes.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
from haystack import indexes
from open_municipio.acts.models import Act, Speech, ActSupport, Amendment
from open_municipio.people.models import Institution
from open_municipio.votations.models import Votation, ChargeVote
from open_municipio.attendances.models import Attendance, ChargeAttendance
from django.utils.translation import activate
from django.conf import settings
from django.utils.translation import ugettext_lazy as _
Expand Down Expand Up @@ -41,6 +43,9 @@ class ActIndex(indexes.SearchIndex, indexes.Indexable):
title = indexes.CharField(indexed=True, stored=True)
adj_title = indexes.CharField(indexed=True, stored=True)

charge_supporting = indexes.MultiValueField(indexed=True, stored=False)
charge_not_supporting = indexes.MultiValueField(indexed=True, stored=False)

logger = logging.getLogger('import')

def get_model(self):
Expand Down Expand Up @@ -172,6 +177,65 @@ def prepare_iter_duration(self, obj):
def prepare_multiple_supporters(self, obj):
return _("yes") if obj.presenter_set.count() > 1 else _("no")

def prepare_charge_supporting(self, obj):
s = set()

last_transition = obj.downcast().get_last_final_transitions()

#if not last_transition:
# self.logger.warning("no last transition for '%s'" % obj.title)

if last_transition:

#if not last_transition.votation and not last_transition.attendance:
# self.logger.warning("no votation nor attendance for %s %s '%s'" % (obj.emitting_institution, obj.presentation_date, obj.title))

# find by votation
try:
for cv in last_transition.votation.chargevote_set.filter(
vote=ChargeVote.VOTES.yes):
s.add(cv.charge.id)
if cv.charge.original_charge: s.add(cv.charge.original_charge.id)
except Exception, e:
pass

# find by attendance
try:
for ca in last_transition.attendance.chargeattendance_set.filter(
value=ChargeAttendance.VALUES.pres):
s.add(ca.charge.id)
if ca.charge.original_charge: s.add(ca.charge.original_charge.id)
except Exception, e:
pass

return s

def prepare_charge_not_supporting(self, obj):
s = set()

last_transition = obj.downcast().get_last_final_transitions()
if last_transition:

# find by votation
try:
for cv in last_transition.votation.chargevote_set.exclude(
vote=ChargeVote.VOTES.yes):
s.add(cv.charge.id)
if cv.charge.original_charge: s.add(cv.charge.original_charge.id)
except Exception, e:
pass

# find by attendance
try:
for ca in last_transition.attendance.chargeattendance_set.exclude(
value=ChargeAttendance.VALUES.pres):
s.add(ca.charge.id)
if ca.charge.original_charge: s.add(ca.charge.original_charge.id)
except Exception, e:
pass

return s


class SpeechIndex(indexes.SearchIndex, indexes.Indexable):
text = indexes.CharField(document=True, use_template=True)
Expand Down Expand Up @@ -234,5 +298,4 @@ def prepare_act_url(self, obj):

return res


locale.setlocale(locale.LC_ALL, '')
9 changes: 6 additions & 3 deletions open_municipio/acts/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,10 +141,13 @@ def extra_context(self):
except ObjectDoesNotExist:
pass

charge_id = self.request.GET.get('charge', None)
if charge_id:
charge_keys = ['charge', 'charge_supporting', 'charge_not_supporting']
charge_values = [self.request.GET.get(k, None) for k in charge_keys]
charge_values = [v for v in charge_values if v is not None]

if len(charge_values) > 0:
try:
extra['charge'] = InstitutionCharge.objects.get(pk=charge_id)
extra['charge'] = InstitutionCharge.objects.get(pk=charge_values[0])
except ObjectDoesNotExist:
pass

Expand Down
5 changes: 2 additions & 3 deletions open_municipio/people/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -648,9 +648,8 @@ def update_presence_cache(self):
self.n_present_votations = self.chargevote_set.exclude(vote=absent).count()
self.n_absent_votations = self.chargevote_set.filter(vote=absent).count()

attendance_absent = ChargeAttendance.VALUES.absent
self.n_present_attendances = self.chargeattendance_set.exclude(value=attendance_absent).count()
self.n_absent_attendances = self.chargeattendance_set.filter(value=attendance_absent).count()
self.n_present_attendances = self.chargeattendance_set.filter(value=ChargeAttendance.VALUES.pres).count()
self.n_absent_attendances = self.chargeattendance_set.exclude(value=ChargeAttendance.VALUES.pres).count()
self.save()

@property
Expand Down
6 changes: 3 additions & 3 deletions open_municipio/templates/acts/act_search.html
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,11 @@ <h4><a href="{{ result.url }}">{{ result.adj_title|default:result.title }}</a></
<a href="{{ result.object.get_last_transition.attendance.get_absolute_url }}">
{% endif %}
<span class="pull-right text-right search_act_status">
<span class="label
<span class="label
{% if result.object.status == "APPROVED" or result.object.status == "ANSWERED" %}label-success
{% elif result.object.status == 'REJECTED' or result.object.status == "NOTANSWERED" %}label-important
{% endif %} ">{{ result.object.downcast.get_status_display|title }}
</span>
</span>
{% if not result.pub_date == result.final_date %}<br /><small>in {{ result.pub_date|timesince:result.final_date }}</small>{% endif %}
</span>
{% if result.object.get_last_transition.votation or result.object.get_last_transition.attendance %}
Expand All @@ -120,7 +120,7 @@ <h4><a href="{{ result.url }}">{{ result.adj_title|default:result.title }}</a></
{% if not n_results %}
<section class="search-empty">
<p>
La ricerca di <strong>{{ form.q.value }}</strong> non è andata a buon fine.<br/>
La ricerca{% if form.q.value %} di <strong>{{ form.q.value }}</strong>{% endif %} non è andata a buon fine.<br/>
Controlla che tutte le parole siano state digitate correttamente.<br/>
oppure prova con parole diverse.
</p>
Expand Down
2 changes: 1 addition & 1 deletion open_municipio/templates/acts/speech_search.html
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ <h4><a href="{{ result.object.get_absolute_url }}">
{% if not n_results %}
<section class="search-empty">
<p>
La ricerca di <strong>{{ form.q.value }}</strong> non è andata a buon fine.<br/>
La ricerca{% if form.q.value %} di <strong>{{ form.q.value }}</strong>{% endif %} non è andata a buon fine.<br/>
Controlla che tutte le parole siano state digitate correttamente.</br>
oppure prova con parole diverse.
</p>
Expand Down
2 changes: 1 addition & 1 deletion open_municipio/templates/events/event_search.html
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ <h2>{% if not query %}Trovati {% endif %} {{ n_results }} eventi</h2>
{% if not n_results %}
<section class="search-empty">
<p>
La ricerca di <strong>{{ form.q.value }}</strong> non è andata a buon fine.<br/>
La ricerca{% if form.q.value %} di <strong>{{ form.q.value }}</strong>{% endif %} non è andata a buon fine.<br/>
Controlla che tutte le parole siano state digitate correttamente.</br>
oppure prova con parole diverse.
</p>
Expand Down
2 changes: 1 addition & 1 deletion open_municipio/templates/people/charge_search.html
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ <h2>{% if not query %}Trovate {% endif %} {{ n_results }} cariche</h2>
{% else %}
<section class="search-empty">
<p>
La ricerca di <strong>{{ form.q.value }}</strong> non è andata a buon fine.<br/>
La ricerca{% if form.q.value %} di <strong>{{ form.q.value }}</strong>{% endif %} non è andata a buon fine.<br/>
Controlla che tutte le parole siano state digitate correttamente.</br>
oppure prova con parole diverse.
</p>
Expand Down
2 changes: 1 addition & 1 deletion open_municipio/templates/votations/votation_search.html
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ <h2>{% if not query %}Trovate {% endif %} {{ n_results }} votazioni</h2>
{% if not n_results %}
<section class="search-empty">
<p>
La ricerca di <strong>{{ form.q.value }}</strong> non è andata a buon fine.<br/>
La ricerca{% if form.q.value %} di <strong>{{ form.q.value }}</strong>{% endif %} non è andata a buon fine.<br/>
Controlla che tutte le parole siano state digitate correttamente.</br>
oppure prova con parole diverse.
</p>
Expand Down

0 comments on commit 6945ac9

Please sign in to comment.