def get_phrase_by_id(id): """ This function returns phrase by id :param id: phrase id :return: phrase """ try: settings = PhraseSettings.objects.all()[0] except IndexError: settings = PhraseSettings() settings.save() try: phrase = Phrases.objects.get(pk=int(id)) except Phrases.DoesNotExist: return '' if settings.lang_detect == 'auto': lang = get_language()[0:2] else: lang = settings.lang_detect data = {} data['bg_color'] = ''.join(['#', settings.background_color]) data['phrase_color'] = ''.join(['#', settings.phrase_color]) data['author_color'] = ''.join(['#', settings.author_color]) if lang == 'uk': data['phrase'] = phrase.phrase_uk data['author'] = phrase.author.name_uk elif lang == 'ru': data['phrase'] = phrase.phrase_ru data['author'] = phrase.author.name_ru else: data['phrase'] = phrase.phrase_en data['author'] = phrase.author.name_en return data
def get_random_phrase(pop_up=False): """ This function returns random phrase depending of admin settings and site language. If there is no settings data in db, it would create them by default :param pop_up: :return: phrase with html or without """ try: # if settings are not exist we must create them settings = PhraseSettings.objects.all()[0] except IndexError: settings = PhraseSettings() settings.save() if settings.status == 'off': # if status of settings disabled we must return empty string return False if settings.lang_detect == 'auto': lang = get_language()[0:2] else: lang = settings.lang_detect try: if lang == 'uk': phrase = Phrases.objects.filter(showed=False, phrase_status='published', phrase_uk__isnull=False).order_by('?')[0] elif lang == 'ru': phrase = Phrases.objects.filter(showed=False, phrase_status='published', phrase_ru__isnull=False).order_by('?')[0] elif lang == 'en': phrase = Phrases.objects.filter(showed=False, phrase_status='published', phrase_en__isnull=False).order_by('?')[0] phrase.showed = True phrase.save() except IndexError: # if all phrases were shown, we must update all as 'not shown' Phrases.objects.filter(showed=True).update(showed=False) try: if lang == 'uk': phrase = Phrases.objects.filter(showed=False, phrase_status='published', phrase_uk__isnull=False).order_by('?')[0] elif lang == 'ru': phrase = Phrases.objects.filter(showed=False, phrase_status='published', phrase_ru__isnull=False).order_by('?')[0] elif lang == 'en': phrase = Phrases.objects.filter(showed=False, phrase_status='published', phrase_en__isnull=False).order_by('?')[0] phrase.showed = True phrase.save() except IndexError: # this means that we have no published phrases in db return False data = {} if pop_up: data['bg_color'] = ''.join(['#', settings.background_color]) data['phrase_color'] = ''.join(['#', settings.phrase_color]) data['author_color'] = ''.join(['#', settings.author_color]) if lang == 'uk': data['phrase'] = phrase.phrase_uk data['author'] = phrase.author.name_uk elif lang == 'ru': data['phrase'] = phrase.phrase_ru data['author'] = phrase.author.name_ru else: data['phrase'] = phrase.phrase_en data['author'] = phrase.author.name_en return data