move calendar function to separate module

main
Bob Mottram 2020-02-23 09:35:52 +00:00
parent 83902b451a
commit 09e9ff5616
2 changed files with 59 additions and 58 deletions

View File

@ -274,3 +274,61 @@ def getThisWeeksEvents(baseDir: str,nickname: str,domain: str) -> {}:
events=mergeDicts(events,newEvents) events=mergeDicts(events,newEvents)
return events 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

View File

@ -65,6 +65,7 @@ from cache import storePersonInCache
from shares import getValidSharedItemID from shares import getValidSharedItemID
from happening import todaysEventsCheck from happening import todaysEventsCheck
from happening import thisWeeksEventsCheck from happening import thisWeeksEventsCheck
from happening import getCalendarEvents
def updateAvatarImageCache(session,baseDir: str,httpPrefix: str,actor: str,avatarUrl: str,personCache: {},force=False) -> str: def updateAvatarImageCache(session,baseDir: str,httpPrefix: str,actor: str,avatarUrl: str,personCache: {},force=False) -> str:
"""Updates the cached avatar for the given actor """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) firstDayOfMonth=datetime(year, monthNumber, 1, 0, 0)
return int(firstDayOfMonth.strftime("%w"))+1 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: {}, \ def htmlCalendarDay(translate: {}, \
baseDir: str,path: str, \ baseDir: str,path: str, \