def to_dict_reverse(self): """ Convert back link between two persons to indexable presentation. """ return { "date_established": self.date_established_human, "date_finished": self.date_finished_human, "date_confirmed": self.date_confirmed_human, "relationship_type": self.from_relationship_type, "relationship_type_en": translate_into(self.from_relationship_type), "is_pep": self.from_person.is_pep, "person_uk": "%s %s %s" % (self.from_person.first_name_uk, self.from_person.patronymic_uk, self.from_person.last_name_uk), "person_en": "%s %s %s" % (self.from_person.first_name_en, self.from_person.patronymic_en, self.from_person.last_name_en), }
def to_dict(self): """ Convert Person model to an indexable presentation for ES. """ d = model_to_dict( self, fields=[ "id", "last_name", "first_name", "patronymic", "dob", "last_name_en", "first_name_en", "patronymic_en", "dob_details", "is_pep", "names", "wiki_uk", "wiki_en", "city_of_birth_uk", "city_of_birth_en", "reputation_sanctions_uk", "reputation_sanctions_en", "reputation_convictions_uk", "reputation_convictions_en", "reputation_assets_uk", "reputation_assets_en", "reputation_crimes_uk", "reputation_crimes_en", "reputation_manhunt_uk", "reputation_manhunt_en", "also_known_as_uk", "also_known_as_en", "last_change", "inn", "inn_source", "passport", "passport_source", ], ) d["related_persons"] = [ i.to_dict() for i in self.to_persons.prefetch_related("to_person") ] + [ i.to_dict_reverse() for i in self.from_persons.prefetch_related("from_person") ] d["related_countries"] = [ i.to_dict() for i in self.person2country_set.prefetch_related("to_country") ] d["related_companies"] = [ i.to_company_dict() for i in self.person2company_set.prefetch_related("to_company") ] d["declarations"] = [ i.to_dict() for i in Declaration.objects.filter(person=self, confirmed="a") ] manhunt_records = self.manhunt_records if manhunt_records: curr_lang = get_language() activate("uk") d["reputation_manhunt_uk"] = render_to_string( "_manhunt_records_uk.jinja", {"manhunt_records": manhunt_records }) + (d["reputation_manhunt_uk"] or "") activate("en") d["reputation_manhunt_en"] = render_to_string( "_manhunt_records_en.jinja", {"manhunt_records": manhunt_records }) + (d["reputation_manhunt_en"] or "") activate(curr_lang) d["inn_source"] = (settings.SITE_URL + self.inn_source.doc.url if self.inn_source else "") d["passport_source"] = (settings.SITE_URL + self.passport_source.doc.url if self.passport_source else "") d["photo"] = settings.SITE_URL + self.photo.url if self.photo else "" d["photo_path"] = self.photo.name if self.photo else "" d["date_of_birth"] = self.date_of_birth d["terminated"] = self.terminated d["last_modified"] = self.last_modified d["died"] = self.died if d["terminated"]: d["reason_of_termination"] = self.get_reason_of_termination_display( ) d["reason_of_termination_en"] = translate_into( self.get_reason_of_termination_display(), "en") d["termination_date_human"] = self.termination_date_human last_workplace = self.last_workplace if last_workplace: d["last_workplace"] = last_workplace["company"] d["last_job_title"] = last_workplace["position"] d["last_job_id"] = last_workplace["company_id"] last_workplace_en = self.last_workplace_en d["last_workplace_en"] = last_workplace_en["company"] d["last_job_title_en"] = last_workplace_en["position"] d["type_of_official"] = self.get_type_of_official_display() d["type_of_official_en"] = translate_into( self.get_type_of_official_display(), "en") d["full_name"] = self.full_name d["full_name_en"] = self.full_name_en def generate_suggestions(last_name, first_name, patronymic, *args): if not last_name: return [] return [ { "input": " ".join([last_name, first_name, patronymic]), "weight": 5 }, { "input": " ".join([first_name, patronymic, last_name]), "weight": 2 }, { "input": " ".join([first_name, last_name]), "weight": 2 }, ] input_variants = [ generate_suggestions(d["last_name"], d["first_name"], d["patronymic"]) ] input_variants += list( map(lambda x: generate_suggestions(*parse_fullname(x)), self.parsed_names)) d["full_name_suggest"] = list(chain.from_iterable(input_variants)) d["_id"] = d["id"] return d
def to_dict(self): d = model_to_dict( self, fields=[ "id", "name_uk", "short_name_uk", "name_en", "short_name_en", "state_company", "edrpou", "wiki", "city", "street", "other_founders", "other_recipient", "other_owners", "other_managers", "bank_name", "also_known_as", ], ) d["related_persons"] = [ i.to_person_dict() for i in self.from_persons.prefetch_related("from_person") ] d["related_countries"] = [ i.to_dict() for i in self.from_countries.prefetch_related("to_country") ] d["related_companies"] = [ i.to_dict() for i in self.to_companies.prefetch_related("to_company") ] + [ i.to_dict_reverse() for i in self.from_companies.prefetch_related("from_company") ] d["status"] = self.get_status_display() d["status_en"] = translate_into(self.get_status_display()) d["founded"] = self.founded_human d["category"] = self.category d["closed"] = self.closed_on_human d["last_modified"] = self.last_modified suggestions = [] names = set([ d["name_uk"], d["short_name_uk"], ]) for name in list(names): for ua_table in ALL_UKRAINIAN: names.add(translit(name, ua_table)) names.add(d["name_en"]) names.add(d["short_name_en"]) for field in names: if not field: continue chunks = list( map(lambda x: x.strip("'\",.-“”«»"), field.split(" "))) for i in xrange(len(chunks)): variant = copy(chunks) variant = [variant[i]] + variant[:i] + variant[i + 1:] suggestions.append(" ".join(variant)) if self.edrpou: edrpou_chunks = list( filter( None, map( unicode.strip, re.split("([a-z]+)", self.edrpou, flags=re.IGNORECASE), ), )) suggestions += edrpou_chunks suggestions.append(self.edrpou.lstrip("0")) if self.edrpou.isdigit(): suggestions.append(self.edrpou.rjust(8, "0")) d["code_chunks"] = edrpou_chunks d["name_suggest"] = [{"input": x} for x in set(suggestions)] d["name_suggest_output"] = d["short_name_uk"] or d["name_uk"] d["name_suggest_output_en"] = d["short_name_en"] or d["name_en"] d["_id"] = d["id"] return d