def body_calendar(request): obj = Users_trace() obj.date = datetime.datetime.now() obj.ip = str(request.META['REMOTE_ADDR']) obj.save() #Grigorian date today. gr_day = int(request.GET['uday']) gr_month = int(request.GET['umonth']) gr_year = int(request.GET['uyear']) cal = AncientCalendar(gr_day, gr_month, gr_year) dayold = cal.getJulianDate() daynew = cal.getGrigorianDate() weekday = cal.getWeekday(verbose='on') weekname = cal.getWeekdayname().format(red="<span style='color:red'>", end="</span>") tone = cal.getTone() saints = cal.getSaint().format(red="<span style='color:red'>", end="</span>") food = cal.getFast() bows = cal.getBow() d = {'dayold':dayold,'daynew':daynew,'weekday':weekday,'weekname':weekname,'tone':tone,'saints':saints,'food':food,'bows':bows} #d.update({'weekcolor':weekcolor}) st = int(request.GET['uday']) if st < 10: d2 = '0'+ str(st) else: d2 = str(st) st = int(request.GET['umonth']) if st < 10: d3 = '0'+ str(st) else: d3 = str(st) userdate= str(request.GET['uyear']) + '/'+d3+'/'+d2 d.update({'udate': userdate}) t = get_template("info.html") c = Context(d) html = t.render(c) return HttpResponse(html)
def calendar(gr_day, gr_month, gr_year): """Print formatted calendar. """ cal = AncientCalendar(gr_day, gr_month, gr_year) out = '' out += '\n\n' + \ textwrap.fill(cal.getGrigorianDate(verbose='on'), initial_indent=' ', subsequent_indent=' ') + '\n' + \ textwrap.fill(cal.getJulianDate(verbose='on'), initial_indent=' ', subsequent_indent=' ') + '\n\n' + \ textwrap.fill(cal.getWeekday(verbose='on'), initial_indent=' ', subsequent_indent=' ') + '\n' +\ textwrap.fill(cal.getTone(), initial_indent=' ', subsequent_indent=' ') + '\n\n' + \ textwrap.fill(cal.getWeekdayname().format(red='\033[31m', bold='\033[33m', end='\033[0m', sx='ⵛ', gl='ⵛ', tw='⊕', pl='☩', redgui=''), width=100, initial_indent=' ', subsequent_indent=' ') + '\n\n' + \ textwrap.fill(cal.getSaint().format(red='\033[31m', end='\033[0m', sx='ⵛ', gl='ⵛ', pl='☩', tw='⊕', redgui=''), width=100, initial_indent=' ', subsequent_indent=' ') + '\n\n' + \ textwrap.fill(cal.getFast(), initial_indent=' ', subsequent_indent=' ') + '\n' +\ textwrap.fill(cal.getBow(), width=110, initial_indent=' ', subsequent_indent=' ') + '\n\n' return out
def calendar_xml(request): xml = str(request.GET['xml']) if xml == '': d={'errorno':'1','text':'Tage "xml" in GET not found'} t = get_template("error.html") c = Context(d) html = t.render(c) return HttpResponse(html) n1 = xml.find('<date>') if n1 == -1: d={'errorno':'2','text':'Tage <date> not found'} t = get_template("error.html") c = Context(d) html = t.render(c) return HttpResponse(html) n2 = xml.find('</date>') if n2 == -1: d={'errorno':'3','text':'Tage </date> not found'} t = get_template("error.html") c = Context(d) html = t.render(c) return HttpResponse(html) if n2 - n1 <10: d={'errorno':'4','text':'Tags <date></date> is bad'} t = get_template("error.html") c = Context(d) html = t.render(c) return HttpResponse(html) date_string = xml[n1+6:n2].strip() if len(date_string) != 10: d={'errorno':'5','text':'Date format is 01/01/2000'} t = get_template("error.html") c = Context(d) html = t.render(c) return HttpResponse(html) gr_day = int(date_string[0:2]) gr_month = int(date_string[4:5]) gr_year = int(int(date_string[7:10])) cal = AncientCalendar(gr_day, gr_month, gr_year) dayold = cal.getJulianDate() daynew = cal.getGrigorianDate() weekday = cal.getWeekday(verbose='on') weekname = cal.getWeekdayname()#.format(red="<span style='color:red'>", end="</span>") tone = cal.getTone() saints = cal.getSaint()#.format(red="<span style='color:red'>", end="</span>") food = cal.getFast() bows = cal.getBow() d = {'dayold':dayold,'daynew':daynew,'weekday':weekday,'weekname':weekname,'tone':tone,'saints':saints,'food':food,'bows':bows,'userdate':date_string} #d.update({'weekcolor':weekcolor}) t = get_template("xml.html") c = Context(d) html = t.render(c) return HttpResponse(html)
def search_saints(search_string, mode="text"): """Search saints and feasts in year menology. :param search_string: search input string (e.g. saint name). :param mode: text or html. """ month_word = { 1: "января", 2: "февраля", 3: "марта", 4: "апреля", 5: "мая", 6: "июня", 7: "июля", 8: "августа", 9: "сентября", 10: "октября", 11: "ноября", 12: "декабря", } search_string = search_string.decode("utf8") year = datetime.date.today().year year_menology = menology cal_out = [] # List of weekday names. for month in range(1, 13): days_in_month = calendar.monthrange(year, month)[1] for day in range(1, days_in_month + 1): cal = AncientCalendar(day, month, year) weekdayname = cal.getWeekdayname() cal_out.extend([[gr_to_ju_in_search(day, month, year), weekdayname]]) for i in cal_out: year_menology[i[0][1]][i[0][0]]["weekday"] = i[1] d = year_menology out = [] pattern = re.compile(ur"{0}*".format(search_string), re.I | re.U) # Ищем в menology строку; если есть, добавляем в out. for key, value in d.iteritems(): for key1, value1 in d[key].iteritems(): for key2, value2 in d[key][key1].iteritems(): if re.search(pattern, str(value2).decode("utf8")): out.extend([[ju_to_gr_in_search(key1, key, year), [key1, key], [value2]]]) # Меняем элементы вложенного списка на значения словаря с названиями месяцев. for item in out: item[0][1], item[1][1] = month_word[item[0][1]], month_word[item[1][1]] string_out = "" # Вывод результата. if len(out) == 0: return u"Ваш запрос — «{}» не найден!".format(search_string) elif mode == "text": for item in out: string_out += ( textwrap.fill(str(item[0][0]), initial_indent=" ") + " " + item[0][1] + " по н. ст." + "\n" + textwrap.fill(str(item[1][0]), initial_indent=" ") + " " + item[1][1] + " по ст. ст." + "\n" + textwrap.fill(str(item[2][0]).lstrip(), width=100, initial_indent=" ", subsequent_indent=" ") + "\n\n" ) return string_out.decode("utf8") elif mode == "html": for item in out: string_out += ( '<span class="date">' + str(item[0][0]) + " " + item[0][1] + " по н. ст." + "<br>" + "\n" + str(item[1][0]) + " " + item[1][1] + " по ст. ст." + "</span>" + "<br>" + "\n" + '<span class="saint">' + str(item[2][0]).rstrip() + "\n" + "</span>" + "<br><br>" + "\n\n" ) return string_out.decode("utf8")
def search_feasts(search_string): """ Search saints and feasts in year menology. :param search_string: search input string (e.g. saint or feast name). :return list, witch contains gregorian & julian dates and search result. """ if len(search_string) < 3: return dict(count=0, result=[]) # TODO: Fix endings in the words: Андрей, Андриан endings = re.compile( ur"(ый|ой|ая|ое|ые|ому|а|ем|о|ов|у|е|й|" ur"ого|ал|ему|и|ство|ых|ох|ия|ий|ь|я|он|ют|ат|ин|ея)$", re.U ) search_string_clean = re.sub(endings, u"", search_string) pattern = re.compile(ur"(\b%s\w+\b)" % search_string_clean, re.I | re.U) if len(search_string) <= 5: endings = re.compile(ur"(а|ы|у|и|я)$", re.U) search_string_clean = re.sub(endings, u"", search_string) pattern = re.compile(ur"(\b%s[а-я]{0,2}\b)" % search_string_clean, re.I | re.U) date = datetime.date.today() year = convert_gregorian_to_julian(date.day, date.month, date.year)[2] year_menology = menology cal_out = [] # List of weekday names. for month in range(1, 13): days_in_month = calendar.monthrange(year, month)[1] for day in range(1, days_in_month + 1): cal = AncientCalendar(day, month, year, calendar="julian") weekdayname = cal.get_daily_feast() cal_out.extend([[list((day, month, year)), weekdayname]]) # Construct year menology with movable feasts. for item in cal_out: year_menology[item[0][1]][item[0][0]]["weekday"] = item[1] d = year_menology out = [] # Search string in menology. for _month, value in d.iteritems(): for _day, value1 in d[_month].iteritems(): for key2, content in d[_month][_day].iteritems(): if re.search(pattern, unicode(content)): _date = (_day, _month, year) out.extend( [ [ list(convert_julian_day_to_julian_am(convert_julian_to_julian_day(*_date))), list(convert_julian_to_gregorian(*_date)), clean_string(content), ] ] ) # Highliting search_string. start_str = u"{highlight_start}" end_str = u"{highlight_end}" pattern_highlite = re.compile(ur"(\b%s\w*\b)" % search_string_clean, re.I | re.U) tr = lambda m: start_str + m.group() + end_str result = [[t[0], t[1], pattern_highlite.sub(tr, t[2])] for t in out] # Count of serach results. count = len([re.findall(pattern, item[2]) for item in out]) return dict(count=count, result=result)