class ObjectIndex(indexes.SearchIndex, indexes.Indexable): text = indexes.CharField(model_attr='name', document=True) id = indexes.IntegerField(model_attr='id') type = indexes.CharField(model_attr='type') object_ct_id = indexes.FacetIntegerField(model_attr='obj_ct_id') obj_id = indexes.IntegerField(model_attr='obj_id') def get_model(self): return Index
class SolrComplexFacetsMockSearchIndex(indexes.SearchIndex): text = indexes.CharField(document=True, default='') name = indexes.CharField(faceted=True) is_active = indexes.BooleanField(faceted=True) post_count = indexes.IntegerField() post_count_i = indexes.FacetIntegerField(facet_for='post_count') average_rating = indexes.FloatField(faceted=True) pub_date = indexes.DateField(faceted=True) created = indexes.DateTimeField(faceted=True) sites = indexes.MultiValueField(faceted=True)
class ElasticsearchComplexFacetsMockSearchIndex(indexes.SearchIndex, indexes.Indexable): text = indexes.CharField(document=True, default='') name = indexes.CharField(faceted=True) is_active = indexes.BooleanField(faceted=True) post_count = indexes.IntegerField() post_count_i = indexes.FacetIntegerField(facet_for='post_count') average_rating = indexes.FloatField(faceted=True) pub_date = indexes.DateField(faceted=True) created = indexes.DateTimeField(faceted=True) sites = indexes.MultiValueField(faceted=True) def get_model(self): return MockModel
class QuestionIndex(indexes.SearchIndex, indexes.Indexable): text = indexes.CharField(document=True, use_template=True) content_auto = indexes.EdgeNgramField(model_attr='question_text') state = indexes.IntegerField(model_attr='state') region = indexes.MultiValueField() organisation = indexes.CharField(model_attr='organisation__id') #Facets state_facet = indexes.FacetIntegerField(model_attr = 'state') institution_facet = indexes.FacetMultiValueField() promotor_facet = indexes.FacetMultiValueField() faculty_facet = indexes.FacetMultiValueField() education_facet = indexes.FacetMultiValueField() subject_facet = indexes.FacetMultiValueField() key_word_facet = indexes.FacetMultiValueField() def prepare_region(self, obj): return [region.region for region in obj.region.all()] def prepare_institution_facet(self,obj): return [institution.name for institution in obj.institution.all()] def prepare_promotor_facet(self,obj): return ['{0} {1}'.format(promotor.first_name, promotor.last_name) for promotor in obj.promotor.all()] def prepare_faculty_facet(self,obj): return [faculty.name for faculty in obj.faculty.all()] def prepare_education_facet(self, obj): return [education.education for education in obj.education.all()] def prepare_subject_facet(self,obj): return [subject.subject for subject in obj.question_subject.all()] def prepare_key_word_facet(self,obj): return [keyword.key_word for keyword in obj.keyword.all()] def get_model(self): return Question
class VotationIndex(indexes.SearchIndex, indexes.Indexable): text = indexes.CharField(document=True, use_template=True) # faceting fields act_type = indexes.FacetCharField() is_key = indexes.FacetCharField(model_attr='is_key_yesno') organ = indexes.FacetCharField(model_attr='sitting__institution__lowername') votation_date = indexes.FacetDateField(model_attr='sitting__date') n_presents_range = indexes.FacetCharField() gap_yes_no = indexes.FacetIntegerField() gap_yes_no_range = indexes.FacetCharField() # stored fields, used not to touch DB # while showing results url = indexes.CharField(indexed=False, stored=True) act_rendered = indexes.CharField(use_template=True, indexed=False) act_url = indexes.MultiValueField(indexed=True, stored=True) act_descr = indexes.CharField(indexed=False, stored=True, default='') title = indexes.CharField(indexed=False, stored=True, model_attr='act__title', default='') votation_num = indexes.CharField(indexed=False, stored=True, model_attr='idnum') votation_sitting_num = indexes.IntegerField(indexed=False, stored=True, model_attr='sitting__number') votation_n_presents = indexes.IntegerField(indexed=True, stored=True, model_attr='n_presents') votation_n_yes = indexes.IntegerField(indexed=True, stored=True, model_attr='n_yes') votation_n_no = indexes.IntegerField(indexed=True, stored=True, model_attr='n_no') votation_n_abst = indexes.IntegerField(indexed=True, stored=True, model_attr='n_abst') votation_n_maj = indexes.IntegerField(indexed=True, stored=True, model_attr='n_maj') votation_n_rebels = indexes.IntegerField(indexed=True, stored=True, model_attr='n_rebels', faceted=True) votation_outcome = indexes.IntegerField(indexed=False, stored=True, model_attr='outcome') votation_outcome_display = indexes.FacetCharField(stored=True, default='') is_secret = indexes.FacetCharField(stored=True, default='') month = indexes.FacetCharField() # needed to filter votations by person or charge person = indexes.MultiValueField(indexed=True, stored=False) charge = indexes.MultiValueField(indexed=True, stored=False) charge_present = indexes.MultiValueField(indexed=True, stored=False) charge_absent = indexes.MultiValueField(indexed=True, stored=False) charge_rebel = indexes.MultiValueField(indexed=True, stored=False) def get_model(self): return Votation def prepare_url(self, obj): return obj.get_absolute_url() def prepare_act_type(self, obj): activate(settings.LANGUAGE_CODE) res = _('none') related_act = obj.act if not related_act: transitions = Transition.objects.filter(votation=obj) if len(transitions) > 0: related_act = transitions[0].act if related_act: res = related_act.get_type_name() return res def prepare_act_descr(self, obj): res = obj.act_descr related_act = obj.act if not related_act: transitions = Transition.objects.filter(votation=obj) if len(transitions) > 0: related_act = transitions[0].act if related_act: res = related_act.adj_title or related_act.title return res def prepare_act_url(self, obj): res = set([]) related_act = obj.act if not related_act: transitions = Transition.objects.filter(votation=obj) if len(transitions) > 0: related_act = transitions[0].act if related_act: res = set([ related_act.downcast().get_absolute_url() ]) return res def prepare_month(self, obj): return obj.sitting.date.strftime("%B") def prepare_votation_outcome_display(self, obj): return obj.get_outcome_display() def prepare_person(self, obj): return set( [p['person__slug'] for p in obj.charge_set.values('person__slug').distinct()] ) def prepare_charge(self, obj): return set( [p['id'] for p in obj.charge_set.values('id').distinct()] ) | \ set( [p['original_charge__id'] for p in obj.charge_set.values('original_charge__id').distinct()] ) def prepare_charge_present(self, obj): s = set() for cv in obj.chargevote_set.filter( vote__in=[ChargeVote.VOTES.yes, ChargeVote.VOTES.no, ChargeVote.VOTES.abstained, ChargeVote.VOTES.pres, ChargeVote.VOTES.secret]): s.add(cv.charge.id) if cv.charge.original_charge: s.add(cv.charge.original_charge.id) return s def prepare_charge_absent(self, obj): s = set() for cv in obj.chargevote_set.filter(vote=ChargeVote.VOTES.absent): s.add(cv.charge.id) if cv.charge.original_charge: s.add(cv.charge.original_charge.id) return s def prepare_charge_rebel(self, obj): #return set( # [cv['charge__id'] for cv in obj.chargevote_set.filter(is_rebel=True).values('charge__id').distinct()] #) s = set() for cv in obj.chargevote_set.filter(is_rebel=True): s.add(cv.charge.id) return s def prepare_n_presents_range(self, obj): charges_count = obj.charges_count if charges_count > 0: decimal = 10 * obj.n_presents / charges_count return str(10 * decimal) + '% - ' + str(10 * (decimal + 1) - 1) + '%' \ if decimal < 10 else '100%' def prepare_gap_yes_no(self, obj): return abs(obj.n_yes - obj.n_no) def prepare_gap_yes_no_range(self, obj): v = abs(obj.n_yes - obj.n_no) if v <= 2: return '1 - 2' elif 3 <= v <= 6: return '3 - 6' elif 7 <= v <= 15: return '7 - 15' elif 16 <= v <= 22: return '16 - 22' else: return '23+' def prepare_is_secret(self, obj): activate(settings.LANGUAGE_CODE) if obj.is_secret: return _('yes') else: return _('no') def prepare_properties(self, obj): activate(settings.LANGUAGE_CODE) count = obj.majority_vs_minority may_presents = count['majority'].get('YES', 0) + count['majority'].get('NO', 0) + \ count['majority'].get('ABSTAINED', 0) + count['majority'].get('PRES', 0) + \ count['majority'].get('SECRET', 0) min_presents = count['minority'].get('YES', 0) + count['minority'].get('NO', 0) + \ count['minority'].get('ABSTAINED', 0) + count['minority'].get('PRES', 0) + \ count['minority'].get('SECRET', 0) total_presents = may_presents + min_presents may_partecipants = count['majority'].get('YES', 0) + count['majority'].get('NO', 0) min_partecipants = count['minority'].get('YES', 0) + count['minority'].get('NO', 0) total_partecipants = may_partecipants + min_partecipants quorum = (total_partecipants / 2) + 1 total_yes = count['majority'].get('YES', 0) + count['minority'].get('YES', 0) total_no = count['majority'].get('NO', 0) + count['minority'].get('NO', 0) total_abst = count['majority'].get('ABSTAINED', 0) + count['minority'].get('ABSTAINED', 0) total_pres = count['majority'].get('PRES', 0) + count['minority'].get('PRES', 0) if obj.outcome == 1: outcome = 'YES' elif obj.outcome == 2: outcome = 'NO' properties = [] # no numero legale if (may_presents < 16): properties.append(_("minority decisive for legal number")) if ('SECRET' not in count['majority']): # si e no si equivalgono: outcome NO if total_yes == total_no: quorum = total_yes; # minoranza decisiva if (count['majority'][outcome] < quorum): properties.append(_("minority decisive for outcome")) return set(properties)