__filename__ = "webapp_calendar.py" __author__ = "Bob Mottram" __license__ = "AGPL3+" __version__ = "1.2.0" __maintainer__ = "Bob Mottram" __email__ = "bob@libreserver.org" __status__ = "Production" __module_group__ = "Calendar" import os from datetime import datetime from datetime import date from utils import getDisplayName from utils import getConfigParam from utils import getNicknameFromActor from utils import getDomainFromActor from utils import locatePost from utils import loadJson from utils import weekDayOfMonthStart from utils import getAltPath from utils import removeDomainPort from utils import acctDir from utils import local_actor_url from utils import replaceUsersWithAt from happening import getTodaysEvents from happening import getCalendarEvents from webapp_utils import setCustomBackground from webapp_utils import htmlHeaderWithExternalStyle from webapp_utils import htmlFooter from webapp_utils import htmlHideFromScreenReader from webapp_utils import htmlKeyboardNavigation def htmlCalendarDeleteConfirm(cssCache: {}, translate: {}, base_dir: str, path: str, http_prefix: str, domain_full: str, postId: str, postTime: str, year: int, monthNumber: int, dayNumber: int, callingDomain: str) -> str: """Shows a screen asking to confirm the deletion of a calendar event """ nickname = getNicknameFromActor(path) actor = local_actor_url(http_prefix, nickname, domain_full) domain, port = getDomainFromActor(actor) messageId = actor + '/statuses/' + postId postFilename = locatePost(base_dir, nickname, domain, messageId) if not postFilename: return None post_json_object = loadJson(postFilename) if not post_json_object: return None deletePostStr = None cssFilename = base_dir + '/epicyon-profile.css' if os.path.isfile(base_dir + '/epicyon.css'): cssFilename = base_dir + '/epicyon.css' instanceTitle = \ getConfigParam(base_dir, 'instanceTitle') deletePostStr = \ htmlHeaderWithExternalStyle(cssFilename, instanceTitle, None) deletePostStr += \ '

' + postTime + ' ' + str(year) + '/' + \ str(monthNumber) + \ '/' + str(dayNumber) + '

' deletePostStr += '
' deletePostStr += '

' + \ translate['Delete this event'] + '

' postActor = getAltPath(actor, domain_full, callingDomain) deletePostStr += \ '
\n' deletePostStr += ' \n' deletePostStr += ' \n' deletePostStr += ' \n' deletePostStr += \ ' \n' deletePostStr += \ ' \n' deletePostStr += \ ' \n' deletePostStr += \ ' \n' deletePostStr += '
\n' deletePostStr += '
\n' deletePostStr += htmlFooter() return deletePostStr def _htmlCalendarDay(person_cache: {}, cssCache: {}, translate: {}, base_dir: str, path: str, year: int, monthNumber: int, dayNumber: int, nickname: str, domain: str, dayEvents: [], monthName: str, actor: str) -> str: """Show a day within the calendar """ accountDir = acctDir(base_dir, nickname, domain) calendarFile = accountDir + '/.newCalendar' if os.path.isfile(calendarFile): try: os.remove(calendarFile) except OSError: print('EX: _htmlCalendarDay unable to delete ' + calendarFile) cssFilename = base_dir + '/epicyon-calendar.css' if os.path.isfile(base_dir + '/calendar.css'): cssFilename = base_dir + '/calendar.css' calActor = actor if '/users/' in actor: calActor = '/users/' + actor.split('/users/')[1] instanceTitle = getConfigParam(base_dir, 'instanceTitle') calendarStr = htmlHeaderWithExternalStyle(cssFilename, instanceTitle, None) calendarStr += '
\n' calendarStr += '\n' calendarStr += '\n' if dayEvents: for eventPost in dayEvents: eventTime = None eventDescription = None eventPlace = None postId = None senderName = '' senderActor = None eventIsPublic = False # get the time place and description for ev in eventPost: if ev['type'] == 'Event': if ev.get('postId'): postId = ev['postId'] if ev.get('startTime'): eventDate = \ datetime.strptime(ev['startTime'], "%Y-%m-%dT%H:%M:%S%z") eventTime = eventDate.strftime("%H:%M").strip() if 'public' in ev: if ev['public'] is True: eventIsPublic = True if ev.get('sender'): # get display name from sending actor if ev.get('sender'): senderActor = ev['sender'] dispName = \ getDisplayName(base_dir, senderActor, person_cache) if dispName: senderName = \ '' + \ dispName + ': ' if ev.get('name'): eventDescription = ev['name'].strip() elif ev['type'] == 'Place': if ev.get('name'): eventPlace = ev['name'] # prepend a link to the sender of the calendar item if senderName and eventDescription: # if the sender is also mentioned within the event # description then this is a reminder senderActor2 = replaceUsersWithAt(senderActor) if senderActor not in eventDescription and \ senderActor2 not in eventDescription: eventDescription = senderName + eventDescription else: eventDescription = \ translate['Reminder'] + ': ' + eventDescription deleteButtonStr = '' if postId: deleteButtonStr = \ '\n' eventClass = 'calendar__day__event' calItemClass = 'calItem' if eventIsPublic: eventClass = 'calendar__day__event__public' calItemClass = 'calItemPublic' if eventTime and eventDescription and eventPlace: calendarStr += \ '' + \ '' + deleteButtonStr + '\n' elif eventTime and eventDescription and not eventPlace: calendarStr += \ '' + \ '' + deleteButtonStr + '\n' elif not eventTime and eventDescription and not eventPlace: calendarStr += \ '' + \ '' + deleteButtonStr + '\n' elif not eventTime and eventDescription and eventPlace: calendarStr += \ '' + \ '' + \ '' + deleteButtonStr + '\n' elif eventTime and not eventDescription and eventPlace: calendarStr += \ '' + \ '' + \ deleteButtonStr + '\n' calendarStr += '\n' calendarStr += '
\n' calendarStr += \ ' \n' calendarStr += \ '

' + str(dayNumber) + ' ' + monthName + \ '


' + str(year) + '\n' calendarStr += '
\n' + \
                    translate['Delete this event'] + ' |
' + eventTime + \ '' + \ '' + \ eventPlace + '
' + eventDescription + \ '
' + eventTime + \ '' + \ eventDescription + '
' + \ '' + \ eventDescription + '
' + \ eventPlace + '
' + eventDescription + \ '
' + eventTime + \ '' + \ '' + \ eventPlace + '
\n' calendarStr += htmlFooter() return calendarStr def htmlCalendar(person_cache: {}, cssCache: {}, translate: {}, base_dir: str, path: str, http_prefix: str, domain_full: str, text_mode_banner: str, accessKeys: {}) -> str: """Show the calendar for a person """ domain = removeDomainPort(domain_full) monthNumber = 0 dayNumber = None year = 1970 actor = http_prefix + '://' + domain_full + path.replace('/calendar', '') if '?' in actor: first = True for p in actor.split('?'): if not first: if '=' in p: if p.split('=')[0] == 'year': numStr = p.split('=')[1] if numStr.isdigit(): year = int(numStr) elif p.split('=')[0] == 'month': numStr = p.split('=')[1] if numStr.isdigit(): monthNumber = int(numStr) elif p.split('=')[0] == 'day': numStr = p.split('=')[1] if numStr.isdigit(): dayNumber = int(numStr) first = False actor = actor.split('?')[0] currDate = datetime.now() if year == 1970 and monthNumber == 0: year = currDate.year monthNumber = currDate.month nickname = getNicknameFromActor(actor) setCustomBackground(base_dir, 'calendar-background', 'calendar-background') months = ( 'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December' ) monthName = translate[months[monthNumber - 1]] if dayNumber: dayEvents = None events = \ getTodaysEvents(base_dir, nickname, domain, year, monthNumber, dayNumber) if events: if events.get(str(dayNumber)): dayEvents = events[str(dayNumber)] return _htmlCalendarDay(person_cache, cssCache, translate, base_dir, path, year, monthNumber, dayNumber, nickname, domain, dayEvents, monthName, actor) events = \ getCalendarEvents(base_dir, nickname, domain, year, monthNumber) prevYear = year prevMonthNumber = monthNumber - 1 if prevMonthNumber < 1: prevMonthNumber = 12 prevYear = year - 1 nextYear = year nextMonthNumber = monthNumber + 1 if nextMonthNumber > 12: nextMonthNumber = 1 nextYear = year + 1 print('Calendar year=' + str(year) + ' month=' + str(monthNumber) + ' ' + str(weekDayOfMonthStart(monthNumber, year))) if monthNumber < 12: daysInMonth = \ (date(year, monthNumber + 1, 1) - date(year, monthNumber, 1)).days else: daysInMonth = \ (date(year + 1, 1, 1) - date(year, monthNumber, 1)).days # print('daysInMonth ' + str(monthNumber) + ': ' + str(daysInMonth)) cssFilename = base_dir + '/epicyon-calendar.css' if os.path.isfile(base_dir + '/calendar.css'): cssFilename = base_dir + '/calendar.css' calActor = actor if '/users/' in actor: calActor = '/users/' + actor.split('/users/')[1] instanceTitle = \ getConfigParam(base_dir, 'instanceTitle') headerStr = htmlHeaderWithExternalStyle(cssFilename, instanceTitle, None) # the main graphical calendar as a table calendarStr = '
\n' calendarStr += '\n' calendarStr += '\n' calendarStr += '\n' days = ('Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat') for d in days: calendarStr += ' \n' calendarStr += '\n' calendarStr += '\n' calendarStr += '\n' # beginning of the links used for accessibility navLinks = {} timelineLinkStr = htmlHideFromScreenReader('🏠') + ' ' + \ translate['Switch to timeline view'] navLinks[timelineLinkStr] = calActor + '/inbox' dayOfMonth = 0 dow = weekDayOfMonthStart(monthNumber, year) for weekOfMonth in range(1, 7): if dayOfMonth == daysInMonth: continue calendarStr += ' \n' for dayNumber in range(1, 8): if (weekOfMonth > 1 and dayOfMonth < daysInMonth) or \ (weekOfMonth == 1 and dayNumber >= dow): dayOfMonth += 1 isToday = False if year == currDate.year: if currDate.month == monthNumber: if dayOfMonth == currDate.day: isToday = True if events.get(str(dayOfMonth)): url = calActor + '/calendar?year=' + \ str(year) + '?month=' + \ str(monthNumber) + '?day=' + str(dayOfMonth) dayDescription = monthName + ' ' + str(dayOfMonth) dayLink = '' + \ str(dayOfMonth) + '' # accessibility menu links menuOptionStr = \ htmlHideFromScreenReader('📅') + ' ' + \ dayDescription navLinks[menuOptionStr] = url # there are events for this day if not isToday: calendarStr += \ ' \n' else: calendarStr += \ ' \n' else: # No events today if not isToday: calendarStr += \ ' \n' else: calendarStr += \ ' \n' else: calendarStr += ' \n' calendarStr += ' \n' calendarStr += '\n' calendarStr += '
\n' calendarStr += \ ' ' calendarStr += \ ' ' + translate['Previous month'] + \
        '\n' calendarStr += ' ' calendarStr += '

' + monthName + '

\n' calendarStr += \ ' ' calendarStr += \ ' ' + translate['Next month'] + \
        '\n' calendarStr += '
' + \ translate[d] + '
' + \ dayLink + '' + \ dayLink + '' + \ str(dayOfMonth) + '' + str(dayOfMonth) + '
\n' # end of the links used for accessibility nextMonthStr = \ htmlHideFromScreenReader('→') + ' ' + translate['Next month'] navLinks[nextMonthStr] = calActor + '/calendar?year=' + str(nextYear) + \ '?month=' + str(nextMonthNumber) prevMonthStr = \ htmlHideFromScreenReader('←') + ' ' + translate['Previous month'] navLinks[prevMonthStr] = calActor + '/calendar?year=' + str(prevYear) + \ '?month=' + str(prevMonthNumber) navAccessKeys = { } screenReaderCal = \ htmlKeyboardNavigation(text_mode_banner, navLinks, navAccessKeys, monthName) newEventStr = \ '
\n

\n' + \ '➕ ' + \ translate['Add to the calendar'] + '\n

\n
\n' calStr = \ headerStr + screenReaderCal + calendarStr + newEventStr + htmlFooter() return calStr