From 09e9ff5616d9013de4511de338df94dc52b5d7d2 Mon Sep 17 00:00:00 2001 From: Bob Mottram Date: Sun, 23 Feb 2020 09:35:52 +0000 Subject: [PATCH] move calendar function to separate module --- happening.py | 58 ++++++++++++++++++++++++++++++++++++++++++++++++ webinterface.py | 59 +------------------------------------------------ 2 files changed, 59 insertions(+), 58 deletions(-) diff --git a/happening.py b/happening.py index 8f4d0d69..fdf5c2e5 100644 --- a/happening.py +++ b/happening.py @@ -274,3 +274,61 @@ def getThisWeeksEvents(baseDir: str,nickname: str,domain: str) -> {}: events=mergeDicts(events,newEvents) return events + +def getCalendarEvents(baseDir: str,nickname: str,domain: str,year: int,monthNumber: int) -> {}: + """Retrieves calendar events + Returns a dictionary indexed by day number of lists containing Event and Place activities + """ + calendarFilename=baseDir+'/accounts/'+nickname+'@'+domain+'/calendar/'+str(year)+'/'+str(monthNumber)+'.txt' + events={} + if not os.path.isfile(calendarFilename): + return events + calendarPostIds=[] + recreateEventsFile=False + with open(calendarFilename,'r') as eventsFile: + for postId in eventsFile: + postId=postId.replace('\n','') + postFilename=locatePost(baseDir,nickname,domain,postId) + if not postFilename: + recreateEventsFile=True + else: + postJsonObject=loadJson(postFilename) + if postJsonObject: + if postJsonObject.get('object'): + if isinstance(postJsonObject['object'], dict): + if postJsonObject['object'].get('tag'): + postEvent=[] + dayOfMonth=None + for tag in postJsonObject['object']['tag']: + if not tag.get('type'): + continue + if tag['type']!='Event' and tag['type']!='Place': + continue + if tag['type']=='Event': + # tag is an event + if not tag.get('startTime'): + continue + eventTime= \ + datetime.strptime(tag['startTime'], \ + "%Y-%m-%dT%H:%M:%S%z") + if int(eventTime.strftime("%Y"))==year and \ + int(eventTime.strftime("%m"))==monthNumber: + dayOfMonth=str(int(eventTime.strftime("%d"))) + postEvent.append(tag) + else: + # tag is a place + postEvent.append(tag) + if postEvent and dayOfMonth: + calendarPostIds.append(postId) + if not events.get(dayOfMonth): + events[dayOfMonth]=[] + events[dayOfMonth].append(postEvent) + + # if some posts have been deleted then regenerate the calendar file + if recreateEventsFile: + calendarFile=open(calendarFilename, "w") + for postId in calendarPostIds: + calendarFile.write(postId+'\n') + calendarFile.close() + + return events diff --git a/webinterface.py b/webinterface.py index 762c8f4b..7a654048 100644 --- a/webinterface.py +++ b/webinterface.py @@ -65,6 +65,7 @@ from cache import storePersonInCache from shares import getValidSharedItemID from happening import todaysEventsCheck from happening import thisWeeksEventsCheck +from happening import getCalendarEvents def updateAvatarImageCache(session,baseDir: str,httpPrefix: str,actor: str,avatarUrl: str,personCache: {},force=False) -> str: """Updates the cached avatar for the given actor @@ -3636,64 +3637,6 @@ def weekDayOfMonthStart(monthNumber: int,year: int) -> int: firstDayOfMonth=datetime(year, monthNumber, 1, 0, 0) return int(firstDayOfMonth.strftime("%w"))+1 -def getCalendarEvents(baseDir: str,nickname: str,domain: str,year: int,monthNumber: int) -> {}: - """Retrieves calendar events - Returns a dictionary indexed by day number of lists containing Event and Place activities - """ - calendarFilename=baseDir+'/accounts/'+nickname+'@'+domain+'/calendar/'+str(year)+'/'+str(monthNumber)+'.txt' - events={} - if not os.path.isfile(calendarFilename): - return events - calendarPostIds=[] - recreateEventsFile=False - with open(calendarFilename,'r') as eventsFile: - for postId in eventsFile: - postId=postId.replace('\n','') - postFilename=locatePost(baseDir,nickname,domain,postId) - if not postFilename: - recreateEventsFile=True - else: - postJsonObject=loadJson(postFilename) - if postJsonObject: - if postJsonObject.get('object'): - if isinstance(postJsonObject['object'], dict): - if postJsonObject['object'].get('tag'): - postEvent=[] - dayOfMonth=None - for tag in postJsonObject['object']['tag']: - if not tag.get('type'): - continue - if tag['type']!='Event' and tag['type']!='Place': - continue - if tag['type']=='Event': - # tag is an event - if not tag.get('startTime'): - continue - eventTime= \ - datetime.strptime(tag['startTime'], \ - "%Y-%m-%dT%H:%M:%S%z") - if int(eventTime.strftime("%Y"))==year and \ - int(eventTime.strftime("%m"))==monthNumber: - dayOfMonth=str(int(eventTime.strftime("%d"))) - postEvent.append(tag) - else: - # tag is a place - postEvent.append(tag) - if postEvent and dayOfMonth: - calendarPostIds.append(postId) - if not events.get(dayOfMonth): - events[dayOfMonth]=[] - events[dayOfMonth].append(postEvent) - - # if some posts have been deleted then regenerate the calendar file - if recreateEventsFile: - calendarFile=open(calendarFilename, "w") - for postId in calendarPostIds: - calendarFile.write(postId+'\n') - calendarFile.close() - - return events - def htmlCalendarDay(translate: {}, \ baseDir: str,path: str, \