Caldav report based on matched text

main
Bob Mottram 2022-02-23 18:46:20 +00:00
parent 26610e4555
commit 0b36feebc2
2 changed files with 134 additions and 74 deletions

View File

@ -206,9 +206,26 @@ def _is_happening_post(post_json_object: {}) -> bool:
return True return True
def _event_text_match(content: str, text_match: str) -> bool:
"""Returns true of the content matches the search text
"""
if not text_match:
return True
if '+' not in text_match:
if text_match.strip().lower() in content.lower():
return True
else:
match_list = text_match.split('+')
for possible_match in match_list:
if possible_match.strip().lower() in content.lower():
return True
return False
def get_todays_events(base_dir: str, nickname: str, domain: str, def get_todays_events(base_dir: str, nickname: str, domain: str,
curr_year: int, curr_month_number: int, curr_year: int, curr_month_number: int,
curr_day_of_month: int) -> {}: curr_day_of_month: int,
text_match: str) -> {}:
"""Retrieves calendar events for today """Retrieves calendar events for today
Returns a dictionary of lists containing Event and Place activities Returns a dictionary of lists containing Event and Place activities
""" """
@ -247,6 +264,12 @@ def get_todays_events(base_dir: str, nickname: str, domain: str,
if not _is_happening_post(post_json_object): if not _is_happening_post(post_json_object):
continue continue
if post_json_object.get('object'):
if post_json_object['object'].get('content'):
content = post_json_object['object']['content']
if not _event_text_match(content, text_match):
continue
public_event = is_public_post(post_json_object) public_event = is_public_post(post_json_object)
post_event = [] post_event = []
@ -438,13 +461,15 @@ def _icalendar_day(base_dir: str, nickname: str, domain: str,
def get_todays_events_icalendar(base_dir: str, nickname: str, domain: str, def get_todays_events_icalendar(base_dir: str, nickname: str, domain: str,
year: int, month_number: int, year: int, month_number: int,
day_number: int, person_cache: {}, day_number: int, person_cache: {},
http_prefix: str) -> str: http_prefix: str,
text_match: str) -> str:
"""Returns today's events in icalendar format """Returns today's events in icalendar format
""" """
day_events = None day_events = None
events = \ events = \
get_todays_events(base_dir, nickname, domain, get_todays_events(base_dir, nickname, domain,
year, month_number, day_number) year, month_number, day_number,
text_match)
if events: if events:
if events.get(str(day_number)): if events.get(str(day_number)):
day_events = events[str(day_number)] day_events = events[str(day_number)]
@ -472,12 +497,13 @@ def get_month_events_icalendar(base_dir: str, nickname: str, domain: str,
year: int, year: int,
month_number: int, month_number: int,
person_cache: {}, person_cache: {},
http_prefix: str) -> str: http_prefix: str,
text_match: str) -> str:
"""Returns today's events in icalendar format """Returns today's events in icalendar format
""" """
month_events = \ month_events = \
get_calendar_events(base_dir, nickname, domain, year, get_calendar_events(base_dir, nickname, domain, year,
month_number) month_number, text_match)
ical_str = \ ical_str = \
'BEGIN:VCALENDAR\n' + \ 'BEGIN:VCALENDAR\n' + \
@ -622,7 +648,8 @@ def get_this_weeks_events(base_dir: str, nickname: str, domain: str) -> {}:
def get_calendar_events(base_dir: str, nickname: str, domain: str, def get_calendar_events(base_dir: str, nickname: str, domain: str,
year: int, month_number: int) -> {}: year: int, month_number: int,
text_match: str) -> {}:
"""Retrieves calendar events """Retrieves calendar events
Returns a dictionary indexed by day number of lists containing Returns a dictionary indexed by day number of lists containing
Event and Place activities Event and Place activities
@ -646,9 +673,17 @@ def get_calendar_events(base_dir: str, nickname: str, domain: str,
continue continue
post_json_object = load_json(post_filename) post_json_object = load_json(post_filename)
if not post_json_object:
continue
if not _is_happening_post(post_json_object): if not _is_happening_post(post_json_object):
continue continue
if post_json_object.get('object'):
if post_json_object['object'].get('content'):
content = post_json_object['object']['content']
if not _event_text_match(content, text_match):
continue
post_event = [] post_event = []
day_of_month = None day_of_month = None
for tag in post_json_object['object']['tag']: for tag in post_json_object['object']['tag']:
@ -1008,6 +1043,16 @@ def dav_report_response(base_dir: str, nickname: str, domain: str,
query_end_time_str = end_time_str.split('"')[1] query_end_time_str = end_time_str.split('"')[1]
query_end_time = _dav_date_from_string(query_end_time_str) query_end_time = _dav_date_from_string(query_end_time_str)
text_match = ''
if ':text-match' in xml_str_lower:
match_str = xml_str_lower.split(':text-match')[1]
if '>' in match_str and '<' in match_str:
text_match = match_str.split('>')[1]
if '<' in text_match:
text_match = text_match.split('<')[0]
else:
text_match = ''
ical_events = None ical_events = None
etag = None etag = None
events_href = '' events_href = ''
@ -1035,28 +1080,31 @@ def dav_report_response(base_dir: str, nickname: str, domain: str,
search_date.year, search_date.year,
search_date.month, search_date.month,
search_date.day, person_cache, search_date.day, person_cache,
http_prefix) http_prefix, text_match)
events_href = \ events_href = \
http_prefix + '://' + domain_full + '/users/' + \ http_prefix + '://' + domain_full + '/users/' + \
nickname + '/calendar?year=' + \ nickname + '/calendar?year=' + \
str(search_date.year) + '?month=' + \ str(search_date.year) + '?month=' + \
str(search_date.month) + '?day=' + str(search_date.day) str(search_date.month) + '?day=' + str(search_date.day)
if ical_events: if ical_events:
etag = md5(ical_events).hexdigest() if 'VEVENT' in ical_events:
responses = \ etag = md5(ical_events).hexdigest()
' <d:response>\n' + \ responses = \
' <d:href>' + events_href + '</d:href>\n' + \ ' <d:response>\n' + \
' <d:propstat>\n' + \ ' <d:href>' + events_href + \
' <d:prop>\n' + \ '</d:href>\n' + \
' <d:getetag>"' + \ ' <d:propstat>\n' + \
etag + '"</d:getetag>\n' + \ ' <d:prop>\n' + \
' <c:calendar-data>' + ical_events + \ ' <d:getetag>"' + \
' </c:calendar-data>\n' + \ etag + '"</d:getetag>\n' + \
' </d:prop>\n' + \ ' <c:calendar-data>' + \
' <d:status>HTTP/1.1 200 OK' + \ ical_events + \
'</d:status>\n' + \ ' </c:calendar-data>\n' + \
' </d:propstat>\n' + \ ' </d:prop>\n' + \
' </d:response>\n' ' <d:status>HTTP/1.1 200 OK' + \
'</d:status>\n' + \
' </d:propstat>\n' + \
' </d:response>\n'
elif query_start_day == 1 and query_start_day >= 28: elif query_start_day == 1 and query_start_day >= 28:
# calendar for a month # calendar for a month
ical_events = \ ical_events = \
@ -1064,28 +1112,32 @@ def dav_report_response(base_dir: str, nickname: str, domain: str,
query_start_year, query_start_year,
query_start_month, query_start_month,
person_cache, person_cache,
http_prefix) http_prefix,
text_match)
events_href = \ events_href = \
http_prefix + '://' + domain_full + '/users/' + \ http_prefix + '://' + domain_full + '/users/' + \
nickname + '/calendar?year=' + \ nickname + '/calendar?year=' + \
str(query_start_year) + '?month=' + \ str(query_start_year) + '?month=' + \
str(query_start_month) str(query_start_month)
if ical_events: if ical_events:
etag = md5(ical_events).hexdigest() if 'VEVENT' in ical_events:
responses = \ etag = md5(ical_events).hexdigest()
' <d:response>\n' + \ responses = \
' <d:href>' + events_href + '</d:href>\n' + \ ' <d:response>\n' + \
' <d:propstat>\n' + \ ' <d:href>' + events_href + \
' <d:prop>\n' + \ '</d:href>\n' + \
' <d:getetag>"' + \ ' <d:propstat>\n' + \
etag + '"</d:getetag>\n' + \ ' <d:prop>\n' + \
' <c:calendar-data>' + ical_events + \ ' <d:getetag>"' + \
' </c:calendar-data>\n' + \ etag + '"</d:getetag>\n' + \
' </d:prop>\n' + \ ' <c:calendar-data>' + \
' <d:status>HTTP/1.1 200 OK' + \ ical_events + \
'</d:status>\n' + \ ' </c:calendar-data>\n' + \
' </d:propstat>\n' + \ ' </d:prop>\n' + \
' </d:response>\n' ' <d:status>HTTP/1.1 200 OK' + \
'</d:status>\n' + \
' </d:propstat>\n' + \
' </d:response>\n'
if not responses: if not responses:
all_events = '' all_events = ''
for year in range(query_start_year, query_end_year+1): for year in range(query_start_year, query_end_year+1):
@ -1107,30 +1159,32 @@ def dav_report_response(base_dir: str, nickname: str, domain: str,
nickname, domain, nickname, domain,
year, month, year, month,
person_cache, person_cache,
http_prefix) http_prefix,
text_match)
events_href = \ events_href = \
http_prefix + '://' + domain_full + '/users/' + \ http_prefix + '://' + domain_full + '/users/' + \
nickname + '/calendar?year=' + \ nickname + '/calendar?year=' + \
str(year) + '?month=' + \ str(year) + '?month=' + \
str(month) str(month)
if ical_events: if ical_events:
all_events += ical_events if 'VEVENT' in ical_events:
responses += \ all_events += ical_events
' <d:response>\n' + \ responses += \
' <d:href>' + events_href + \ ' <d:response>\n' + \
'</d:href>\n' + \ ' <d:href>' + events_href + \
' <d:propstat>\n' + \ '</d:href>\n' + \
' <d:prop>\n' + \ ' <d:propstat>\n' + \
' <d:getetag>"' + \ ' <d:prop>\n' + \
etag + '"</d:getetag>\n' + \ ' <d:getetag>"' + \
' <c:calendar-data>' + \ etag + '"</d:getetag>\n' + \
ical_events + \ ' <c:calendar-data>' + \
' </c:calendar-data>\n' + \ ical_events + \
' </d:prop>\n' + \ ' </c:calendar-data>\n' + \
' <d:status>HTTP/1.1 200 OK' + \ ' </d:prop>\n' + \
'</d:status>\n' + \ ' <d:status>HTTP/1.1 200 OK' + \
' </d:propstat>\n' + \ '</d:status>\n' + \
' </d:response>\n' ' </d:propstat>\n' + \
' </d:response>\n'
etag = md5(all_events).hexdigest() etag = md5(all_events).hexdigest()
# today's calendar events # today's calendar events
@ -1139,26 +1193,28 @@ def dav_report_response(base_dir: str, nickname: str, domain: str,
get_todays_events_icalendar(base_dir, nickname, domain, get_todays_events_icalendar(base_dir, nickname, domain,
search_date.year, search_date.month, search_date.year, search_date.month,
search_date.day, person_cache, search_date.day, person_cache,
http_prefix) http_prefix, text_match)
events_href = \ events_href = \
http_prefix + '://' + domain_full + '/users/' + \ http_prefix + '://' + domain_full + '/users/' + \
nickname + '/calendar?year=' + \ nickname + '/calendar?year=' + \
str(search_date.year) + '?month=' + \ str(search_date.year) + '?month=' + \
str(search_date.month) + '?day=' + str(search_date.day) str(search_date.month) + '?day=' + str(search_date.day)
if ical_events: if ical_events:
etag = md5(ical_events).hexdigest() if 'VEVENT' in ical_events:
responses = \ etag = md5(ical_events).hexdigest()
' <d:response>\n' + \ responses = \
' <d:href>' + events_href + '</d:href>\n' + \ ' <d:response>\n' + \
' <d:propstat>\n' + \ ' <d:href>' + events_href + '</d:href>\n' + \
' <d:prop>\n' + \ ' <d:propstat>\n' + \
' <d:getetag>"' + etag + '"</d:getetag>\n' + \ ' <d:prop>\n' + \
' <c:calendar-data>' + ical_events + \ ' <d:getetag>"' + etag + \
' </c:calendar-data>\n' + \ '"</d:getetag>\n' + \
' </d:prop>\n' + \ ' <c:calendar-data>' + ical_events + \
' <d:status>HTTP/1.1 200 OK</d:status>\n' + \ ' </c:calendar-data>\n' + \
' </d:propstat>\n' + \ ' </d:prop>\n' + \
' </d:response>\n' ' <d:status>HTTP/1.1 200 OK</d:status>\n' + \
' </d:propstat>\n' + \
' </d:response>\n'
if not ical_events or not etag: if not ical_events or not etag:
return None return None

View File

@ -267,6 +267,7 @@ def html_calendar(person_cache: {}, css_cache: {}, translate: {},
""" """
domain = remove_domain_port(domain_full) domain = remove_domain_port(domain_full)
text_match = ''
default_year = 1970 default_year = 1970
default_month = 0 default_month = 0
month_number = default_month month_number = default_month
@ -320,11 +321,13 @@ def html_calendar(person_cache: {}, css_cache: {}, translate: {},
year, month_number, year, month_number,
day_number, day_number,
person_cache, person_cache,
http_prefix) http_prefix,
text_match)
day_events = None day_events = None
events = \ events = \
get_todays_events(base_dir, nickname, domain, get_todays_events(base_dir, nickname, domain,
year, month_number, day_number) year, month_number, day_number,
text_match)
if events: if events:
if events.get(str(day_number)): if events.get(str(day_number)):
day_events = events[str(day_number)] day_events = events[str(day_number)]
@ -337,10 +340,11 @@ def html_calendar(person_cache: {}, css_cache: {}, translate: {},
if icalendar: if icalendar:
return get_month_events_icalendar(base_dir, nickname, domain, return get_month_events_icalendar(base_dir, nickname, domain,
year, month_number, person_cache, year, month_number, person_cache,
http_prefix) http_prefix, text_match)
events = \ events = \
get_calendar_events(base_dir, nickname, domain, year, month_number) get_calendar_events(base_dir, nickname, domain, year, month_number,
text_match)
prev_year = year prev_year = year
prev_month_number = month_number - 1 prev_month_number = month_number - 1