def process_view(self, request, view_func, view_args, view_kwargs): if "language" in view_kwargs: language = models.Language.objects.get(language=view_kwargs["language"] or "English") translation = translations.get_translation(language) request.translation = translation return None
def get_country_export_data(country, language=None): """ Return all the data that is required for the country scorecard """ language = language or models.Language.objects.get(language="English") translation = translations.get_translation(language) data = target.calc_country_ratings(country, language) data["questions"] = get_country_questions(country) data["agencies"] = get_agency_values(country) data["indicators"] = get_country_indicators(country, data["questions"], data["agencies"]) data["np"], data["p"] = target.get_agency_progress(country) ratings, _ = models.GovScorecardRatings.objects.get_or_create(country=country) comments_override, _ = models.CountryScorecardOverrideComments.objects.get_or_create( country=country, language=language ) try: data["ER1a"] = data["1G"]["target"] data["ER1b"] = data["1G"]["commentary"] data["ER2a"] = data["2Ga"]["target"] data["ER2b"] = data["2Ga"]["commentary"] data["ER3a"] = data["2Gb"]["target"] data["ER3b"] = data["2Gb"]["commentary"] data["ER4a"] = data["3G"]["target"] data["ER4b"] = data["3G"]["commentary"] data["ER4c"] = country.country data["ER5a"] = data["4G"]["target"] data["ER5b"] = data["4G"]["commentary"] data["ER6a"] = data["5Ga"]["target"] data["ER6b"] = data["5Ga"]["commentary"] data["ER7a"] = data["5Gb"]["target"] data["ER7b"] = data["5Gb"]["commentary"] data["ER8a"] = data["6G"]["target"] data["ER8b"] = data["6G"]["commentary"] data["ER9a"] = data["7G"]["target"] data["ER9b"] = data["7G"]["commentary"] data["ER10a"] = data["8G"]["target"] data["ER10b"] = data["8G"]["commentary"] data["file"] = country.country data["TB2"] = translation.gov_tb2 % country.country.upper() data["CD1"] = data["ER1a"] data["CD2"] = comments_override.cd2 or data["questions"]["1"]["comments"] data["HSP1"] = ratings.hsp1 or data["Q2G"]["target"] data["HSP2"] = ratings.hsp2 or data["Q3G"]["target"] data["HSM1"] = ratings.hsm1 or data["Q12G"]["target"] data["HSM2"] = data["questions"]["15"]["latest_value"] data["HSM3"] = data["ER10a"] data["HSM4"] = ratings.hsm4 data["BC1"] = data["questions"]["5"]["baseline_year"] data["BC2"] = data["questions"]["6"]["baseline_value"] data["BC3"] = data["questions"]["5"]["latest_year"] data["BC4"] = data["questions"]["6"]["latest_value"] data["BC5"] = "?????" data["BC6"] = "?????" data["BC7"] = "?????" data["BC8"] = "?????" data["BC9"] = "?????" data["BC10"] = "?????" data["PC1"] = data["indicators"]["3G"]["latest_value"] data["PC2"] = data["indicators"]["3G"]["hs_budget_gap"] data["PC3"] = translation.gov_pc3 % data["PC1"] data["PC4"] = translation.gov_pc4 % data["PC2"] data["PF1"] = data["questions"]["16"]["latest_value"] data["PF2"] = comments_override.pf2 or data["questions"]["16"]["comments"] data["PFM1"] = data["ER6a"] data["PFM2"] = comments_override.pfm2 or data["questions"]["9"]["comments"] data["PR1"] = data["ER7a"] data["PR2"] = comments_override.pr2 or data["questions"]["10"]["comments"] data["TA1"] = data["indicators"]["other"]["coordinated_programmes"] data["TA2"] = "" for agency in data["agencies"]: aqs = data["agencies"][agency] if "4" in aqs: data["TA2"] += "%s %s" % (agency, aqs["4"]["comments"].replace("%", "%%")) data["TA2"] += "\n" data["TA2"] = comments_override.ta2 or data["TA2"] data["PHC1"] = data["indicators"]["other"]["outpatient_visits_baseline"] data["PHC2"] = data["questions"]["19"]["baseline_year"] data["PHC3"] = data["indicators"]["other"]["outpatient_visits_latest"] data["PHC4"] = data["questions"]["19"]["latest_year"] data["PHC5"] = data["indicators"]["other"]["outpatient_visits_change"] data["PHC6"] = data["indicators"]["other"]["outpatient_visits_change_dir"] data["PHC7"] = "" data["HRH1"] = data["indicators"]["other"]["skilled_personnel_baseline"] data["HRH2"] = data["questions"]["17"]["baseline_year"] data["HRH3"] = data["indicators"]["other"]["skilled_personnel_latest"] data["HRH4"] = data["questions"]["17"]["latest_year"] data["HRH5"] = data["indicators"]["other"]["skilled_personnel_change"] data["HRH6"] = data["indicators"]["other"]["skilled_personnel_change_dir"] data["HRH7"] = "" data["HS1"] = data["questions"]["20"]["baseline_value"] data["HS2"] = data["questions"]["20"]["baseline_year"] data["HS3"] = data["questions"]["20"]["latest_value"] data["HS4"] = data["questions"]["20"]["latest_year"] data["HS5"] = data["indicators"]["other"]["health_workforce_spent_change"] data["HS6"] = data["indicators"]["other"]["health_workforce_spent_change_dir"] data["HS7"] = "" data["RF1"] = data["ER8a"] data["RF2"] = comments_override.rf2 or data["questions"]["22"]["latest_value"] data["RF3"] = comments_override.rf3 or data["questions"]["23"]["latest_value"] data["HMIS1"] = ratings.hmis1 or data["Q21G"]["target"] data["HMIS2"] = comments_override.hmis2 or data["questions"]["21"]["comments"] data["JAR1"] = ratings.jar1 or data["Q12G"]["target"] data["JAR2"] = "Field no longer used" data["JAR3"] = "Field no longer used" data["JAR4"] = comments_override.jar4 or data["questions"]["24"]["comments"] data["JAR5"] = "Field no longer used" data["DBR1"] = data["ER8a"] data["DBR2"] = comments_override.dbr2 or data["questions"]["11"]["comments"] group1 = ["MDG1", "MDG2", "MDG3", "MDG4"] group2 = ["MDG5a", "MDG5b", "MDG6a", "MDG6b", "MDG6c", "MDG7a", "MDG7b"] group1_index = "abcde" group2_index = "12345" needs_percent = ["MDG1", "MDG2", "MDG6a", "MDG6b", "MDG7a", "MDG7b"] add_perc = lambda ind: "%" if ind in needs_percent else "" for mdg in group1 + group2: # import pdb; pdb.set_trace() index = group1_index if mdg in group1 else group2_index mdgdata = models.MDGData.objects.get(mdg_target=mdg, country=country) if not mdgdata.latest_value: data[mdg + index[0]] = "" data[mdg + index[1]] = "" data[mdg + index[2]] = "questionmdg" data[mdg + index[3]] = "" data[mdg + index[4]] = "" elif not mdgdata.baseline_value: data[mdg + index[0]] = str(fformat_front(mdgdata.latest_value)) + add_perc(mdg) data[mdg + index[1]] = mdgdata.latest_year data[mdg + index[2]] = "questionmdg" data[mdg + index[3]] = "" data[mdg + index[4]] = "" else: fmt = fformat_two if mdg == "MDG3" else fformat_front data[mdg + index[0]] = str(fmt(mdgdata.latest_value)) + add_perc(mdg) data[mdg + index[1]] = mdgdata.latest_year data[mdg + index[2]] = mdgdata.arrow data[mdg + index[3]] = str(fmt(mdgdata.change)) + add_perc(mdg) data[mdg + index[4]] = mdgdata.baseline_year data["F1"] = country.country data["CN1"] = data["TB2"] data["GN1"] = country.country data["Header"] = country.country for i in range(1, 14): data["P%d" % i] = data["p"].get(i - 1, "pwhite") data["NP%d" % i] = data["np"].get(i - 1, "npwhite") working_draft, _ = models.CountryWorkingDraft.objects.get_or_create(country=country) data["workingdraft"] = "workingdraft" if working_draft.is_draft else "" except Exception, e: traceback.print_exc()
def main(flags, input_queue, message_queue): print('Initialized') for event in longpoll.listen(): try: print(event.from_chat) except Exception as e: print(e) if event.type == VkEventType.MESSAGE_NEW and event.to_me or event.from_chat: try: print(12, 67485637843) f = True vk.messages.markAsRead(peer_id=event.user_id, group_id=group_id()) uid = str(event.user_id) settings = False if uid not in flags: flags[uid] = { 'translate': False, 'voices': False, 'lang_sent': 'ru', 'lang_translate': 'en' } try: if event.text: try: if event.message_data['fwd_messages']: raise ZeroDivisionError except ZeroDivisionError: raise KeyError except Exception: print(end='') text = event.text.lower() m = flags[uid].copy() if 'нач' in text or 'прив' in text: message = 'Привет! Отправь боту голосовое или введи одну из команд:\n\n' \ 'автоперевод включить/выключить - присылать ответ голосовым сообщением\n' \ 'перевод вкл/выкл - переводить ли на другой язык (по умолчанию - английский)\n' \ 'перевод английский - переводить на английский\n\n' \ 'Tips and tricks:\n\n' \ '+ Можно сокращать некоторые запросы (кроме названий языков): авто вкл (автоперевод)\n' \ '+ Хотя в примере показан лишь английский, бот позволяет распознавать, переводить и' \ ' озвучивать 103 языка.' message_queue.put({ 'user_id': event.user_id, 'message': message }) settings = True elif 'авто' in text: if 'вкл' in text: m['voices'] = True elif 'выкл' in text: m['voices'] = False settings = True elif 'перевод' in text: if ' с ' in text and ' на ' in text: txt = text.split() fr0m, to = txt[txt.index('с') + 1], txt[txt.index('на') + 1] if fr0m != to: d = get_translation(to, 'en') if d in langlist: m['lang_translate'] = langlist[d] d = get_translation(fr0m, 'en') if d in langlist: m['lang_sent'] = langlist[d] elif ' на ' in text: h = m['lang_translate'] for i in text.split(): d = get_translation(i, 'en') if d in langlist: m['lang_translate'] = langlist[d] if flags[uid]['lang_translate'] == flags[uid]['lang_sent']: m['lang_sent'] = h elif ' с ' in text: h = m['lang_sent'] for i in text.split(): d = get_translation(i, 'en') if d in langlist: m['lang_sent'] = langlist[d] if flags[uid]['lang_translate'] == flags[uid]['lang_sent']: m['lang_translate'] = h if 'вкл' in text: m['translate'] = True elif 'выкл' in text: m['translate'] = False settings = True if settings: if m['translate']: one = 'включен' else: one = 'выключен' if m['voices']: two = 'включен' else: two = 'выключен' message = 'Перевод: %s\nАвтоперевод: %s\nОсновной язык: %s\nЯзык перевода: %s' % ( one, two, m['lang_sent'], m['lang_translate']) message_queue.put({ 'user_id': event.user_id, 'message': message }) flags[uid] = m with open('prefs.jpg', 'w') as f: json.dump(flags.copy(), f) f = False except Exception as e: print(e) if event.message_data is None: continue if f: input_queue.put(event.message_data) except Exception as e: print(e)
def handler(message_data, message_queue, from_id): try: try: for i in message_data['fwd_messages']: handler(i, message_queue, from_id) except KeyError: print('caught') attachment = message_data['attachments'][0]['audio_message'] audio_url = attachment['link_ogg'] print(str(randint(1, 100)), attachment["access_key"] + ".ogg", audio_url, flags[from_id]['lang_sent']) message = recognize(str(randint(1, 100)), attachment["access_key"] + ".ogg", audio_url, lang=flags[from_id]['lang_sent']) if flags[from_id]['translate']: message = get_translation(message, flags[from_id]['lang_translate']) if flags[from_id]['voices']: audio_url = vk_session.method('docs.getMessagesUploadServer', {'type': 'audio_message', 'peer_id': from_id, 'group_id': group_id()})['upload_url'] print(audio_url) if message_data['from_id'] > 0: s = vk.users.get(user_id=message_data['from_id'], fields="sex")[0]['sex'] else: s = randint(1, 2) print(s) s = {0: 'n', 1: 'f', 2: 'm'}[s] print(s) meow = synthesis(message, detect(message), s) if meow != 0: message_queue.put({ 'user_id': from_id, 'message': meow, }) message_queue.put({ 'user_id': from_id, 'message': 'Извините, но сейчас доступны следующие языки:\n' 'Английский, голландский, датский, испанский, итальянский, корейский, немецкий,' 'польский, португальский, русский, словацкий, турецкий, украинский, французский,' 'шведский, японский' }) else: files = [('file', ('output.mp3', open('output.mp3', 'rb')))] url2 = requests.post(audio_url, files=files).text os.remove('output.mp3') RESPONSE = json.loads(url2)['file'] RESPONSE_2 = vk_session.method('docs.save', {'file': RESPONSE}) pprint(RESPONSE_2) _id = RESPONSE_2['audio_message']['id'] owner_id = RESPONSE_2['audio_message']['owner_id'] document = 'doc%s_%s' % (str(owner_id), str(_id)) message_queue.put({ 'user_id': from_id, 'document': document, }) else: message_queue.put({ 'user_id': from_id, 'message': message, }) except Exception as e: print(e) return
def handle_dialog(res, req): vk_session = vk_api.VkApi(token=token()) uid = req['session']['user_id'] if req['session']['new']: flags[uid] = { 'translate': False, 'voices': False, 'lang_translate': 'en' } res['response']['text'] = 'Привет! Отправь боту голосовое или введи одну из команд:\n\n' \ 'автоперевод включить/выключить - присылать ответ голосовым сообщением\n' \ 'перевод вкл/выкл - переводить ли на другой язык (по умолчанию - английский)\n' \ 'перевод английский - переводить на английский\n\n' \ 'Tips and tricks:\n\n' \ '+ Можно сокращать некоторые запросы (кроме названий языков): авто вкл (автоперевод)\n' \ '+ Хотя в примере показан лишь английский, бот позволяет распознавать, переводить и' \ ' озвучивать 103 языка.' return text = req['request']['original_utterance'] message = text settings = False if 'нач' in text or 'start' in text: settings = True elif 'авто' in text and (' на ' in text or 'вкл' in text or 'выкл' in text): if 'вкл' in text: flags[uid]['voices'] = True settings = True elif 'выкл' in text: flags[uid]['voices'] = False settings = True elif 'перевод' in text and (' на ' in text or 'вкл' in text or 'выкл' in text): if ' на ' in text: for i in text.split(): d = get_translation(i, 'en') if d in langlist: flags[uid]['lang_translate'] = langlist[d] settings = True if 'вкл' in text: flags[uid]['translate'] = True settings = True elif 'выкл' in text: flags[uid]['translate'] = False settings = True else: message = req['request']['original_utterance'] if flags[uid]['translate']: message = get_translation(message, flags[uid]['lang_translate']) logging.info(message) if flags[uid]['voices']: logging.info('voices!') audio_url = vk_session.method( 'docs.getMessagesUploadServer', { 'type': 'audio_message', 'peer_id': peer_id(), 'group_id': group_id() })['upload_url'] logging.info(audio_url) meow = synthesis(message, detect(message)) if meow != 0: res['response']['text'] = 'Извините, но сейчас доступны следующие языки:\n' \ 'Английский, голландский, датский, испанский, итальянский, корейский, немецкий, ' \ 'польский, португальский, русский, словацкий, турецкий, украинский, французский, ' \ 'шведский, японский.' else: files = [('file', ('output.mp3', open('output.mp3', 'rb')))] url2 = requests.post(audio_url, files=files).text os.remove('output.mp3') logging.info('meow!') RESPONSE = json.loads(url2)['file'] RESPONSE_2 = vk_session.method('docs.save', {'file': RESPONSE}) logging.info('RESPONSE_2: %r', RESPONSE_2) _id = RESPONSE_2['audio_message']['id'] owner_id = RESPONSE_2['audio_message']['owner_id'] document = 'doc%s_%s' % (str(owner_id), str(_id)) return else: res['response']['text'] = message logging.info('message: %s' % message) return if settings: if flags[uid]['translate']: one = 'включен' else: one = 'выключен' if flags[uid]['voices']: two = 'включен' else: two = 'выключен' message += '\n\nПеревод: %s\nАвтоперевод: %s\nЯзык перевода: %s' % ( one, two, flags[uid]['lang_translate']) res['response']['text'] = message.strip() logging.info('message: %s' % res['response']['text']) return
def calc_agency_ratings(agency, language=None): """ Returns information for all indicators for the given agency in a dict with the following form { "1DP" : { "base_val" : ..., "cur_val" : ..., "comments" : ..., "target" : ..., }, "2DPa" : { "base_val" : ..., "cur_val" : ..., "comments" : ..., "target" : ..., }, . . . } """ language = language or models.Language.objects.get(language="English") translation = translations.get_translation(language) def ratings_val(obj, tmpl): def _func(indicator): h = indicator.replace("DP", "") d = obj.__dict__ return d.get(tmpl % h, None) return _func def round_to_zero(x): if type(x) == float and round(x, 0) == 0: return 0.0 else: return x targets = get_agency_targets(agency, dp_indicators) indicators = calc_agency_indicators(agency) ratings, _ = models.DPScorecardRatings.objects.get_or_create(agency=agency) comments_override, _ = models.DPScorecardComments.objects.get_or_create(agency=agency, language=language) results = {} ratings_comments = ratings_val(comments_override, "er%s") ratings_target = ratings_val(ratings, "r%s") for indicator in indicators: (base_val, base_year, cur_val, cur_year), comments = indicators[indicator] cur_val = round_to_zero(cur_val) base_val = round_to_zero(base_val) target = targets[indicator] result = { "base_val" : base_val, "base_year" : base_year, "cur_val" : cur_val, "cur_year" : cur_year, "comments" : comments, "commentary" : "", "agency_name" : agency.agency, } result["target"] = ratings_target(indicator) or evaluate_indicator(target, base_val, cur_val) result["target_val"] = target.tick_criterion_value if ratings_comments(indicator): result["commentary"] = ratings_comments(indicator) else: is_base_num = isinstance(base_val, numbers.Real) is_cur_num = isinstance(cur_val, numbers.Real) if is_base_num: result["one_minus_base_val"] = 100 - result["base_val"] if is_cur_num: result["one_minus_cur_val"] = 100 - result["cur_val"] # create commentary if is_base_num and is_cur_num: result["diff_val"] = math.fabs(base_val - cur_val) # This is really dirty but the text is currently formatted using # no decimal places and so this calculation should use the rounded # value diff = round(round(base_val) - round(cur_val)) if diff > 0: result["diff_direction"] = translation.direction_decrease result["one_minus_diff_direction"] = translation.direction_increase elif diff == 0: result["diff_direction"] = translation.direction_nochange result["one_minus_diff_direction"] = translation.direction_nochange else: result["diff_direction"] = translation.direction_increase result["one_minus_diff_direction"] = translation.direction_decrease if result["base_val"] > 0: result["perc_change"] = (result["cur_val"] - result["base_val"]) / float(result["base_val"]) * 100 result["abs_perc_change"] = math.fabs(result["perc_change"]) else: result["perc_change"] = 0 result["abs_perc_change"] = 0 try: template = translation.agency_commentary_text[indicator] if type(template) == Template: result["commentary"] = template.render(Context(result)) else: result["commentary"] = template % result except: pass if result["target"] == Rating.NONE: #if NA_STR in [base_val, cur_val]: result["commentary"] = translation.rating_none_text % agency.agency elif result["target"] == Rating.QUESTION: result["commentary"] = translation.rating_question_text elif result["commentary"] == "": result["commentary"] = translation.rating_question_text results[indicator] = result return results
def calc_country_ratings(country, language=None): """ Returns information for all indicators for the given country in a dict with the following form { "1G" : { "base_val" : ..., "cur_val" : ..., "comments" : ..., "target" : ..., }, "2Ga" : { "base_val" : ..., "cur_val" : ..., "comments" : ..., "target" : ..., }, . . . } """ language = language or models.Language.objects.get(language="English") translation = translations.get_translation(language) gov_commentary_text = translation.gov_commentary_text rating_question_text = translation.rating_question_text rating_none_text = translation.rating_none_text % country.country targets = get_country_targets(country, g_indicators) indicators = calc_country_indicators(country) results = {} ratings, _ = models.GovScorecardRatings.objects.get_or_create(country=country) comment_override, _ = models.GovScorecardComments.objects.get_or_create(country=country, language=language) def ratings_val(obj, tmpl): def _func(indicator): h = indicator.replace("G", "").replace("Q", "") d = obj.__dict__ return d.get(tmpl % h, None) return _func ratings_comments = ratings_val(comment_override, "er%s") ratings_target = ratings_val(ratings, "r%s") for indicator in indicators: (base_val, base_year, cur_val, cur_year), comments = indicators[indicator] target = targets[indicator] result = { "base_val" : base_val, "base_year" : base_year, "cur_val" : cur_val, "cur_year" : cur_year, "comments" : comments, "commentary" : "", "country_name" : country, } result["one_minus_base_val"] = base_val if isinstance(base_val, numbers.Real): result["one_minus_base_val"] = 100 - base_val result["one_minus_cur_val"] = cur_val if isinstance(cur_val, numbers.Real): result["one_minus_cur_val"] = 100 - cur_val result["target"] = ratings_target(indicator) or evaluate_indicator(target, base_val, cur_val) if ratings_comments(indicator): result["commentary"] = ratings_comments(indicator) else: if indicator in gov_commentary_text: target_value = result["target"] if target_value == Rating.QUESTION: commentary = rating_question_text elif target_value == Rating.NONE: commentary = rating_none_text elif target_value == None: raise Exception("This shouldn't really be happening") commentary = "Missing Data" elif "all" in gov_commentary_text[indicator]: commentary = gov_commentary_text[indicator]["all"] else: commentary = gov_commentary_text[indicator][target_value] try: result["commentary"] = commentary % result except TypeError, e: result["commentary"] = ["This text couldn't be generated, possibly because the rating was overriden. Please override the text appropriately as well"] results[indicator] = result