From 86b058a1e2c1840cdc8c6db6f720eaac1b0ee4d6 Mon Sep 17 00:00:00 2001 From: Bob Mottram Date: Sun, 23 Feb 2020 15:32:47 +0000 Subject: [PATCH] Tidying --- webinterface.py | 1169 ++++++++++++++++++++++++++++++++++++----------- 1 file changed, 891 insertions(+), 278 deletions(-) diff --git a/webinterface.py b/webinterface.py index 21ada677..3db1a422 100644 --- a/webinterface.py +++ b/webinterface.py @@ -68,7 +68,9 @@ from happening import thisWeeksEventsCheck from happening import getCalendarEvents from happening import getTodaysEvents -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 """ if not avatarUrl: @@ -95,7 +97,8 @@ def updateAvatarImageCache(session,baseDir: str,httpPrefix: str,actor: str,avata print('avatar image url: '+avatarUrl) result=session.get(avatarUrl, headers=sessionHeaders, params=None) if result.status_code<200 or result.status_code>202: - print('Avatar image download failed with status '+str(result.status_code)) + print('Avatar image download failed with status '+ \ + str(result.status_code)) # remove partial download if os.path.isfile(avatarImageFilename): os.remove(avatarImageFilename) @@ -108,10 +111,16 @@ def updateAvatarImageCache(session,baseDir: str,httpPrefix: str,actor: str,avata print('Failed to download avatar image: '+str(avatarUrl)) print(e) if '/channel/' not in actor: - sessionHeaders = {'Accept': 'application/activity+json; profile="https://www.w3.org/ns/activitystreams"'} + sessionHeaders = { + 'Accept': 'application/activity+json; profile="https://www.w3.org/ns/activitystreams"' + } else: - sessionHeaders = {'Accept': 'application/ld+json; profile="https://www.w3.org/ns/activitystreams"'} - personJson = getJson(session,actor,sessionHeaders,None,__version__,httpPrefix,None) + sessionHeaders = { + 'Accept': 'application/ld+json; profile="https://www.w3.org/ns/activitystreams"' + } + personJson = \ + getJson(session,actor,sessionHeaders,None,__version__, \ + httpPrefix,None) if personJson: if not personJson.get('id'): return None @@ -123,7 +132,8 @@ def updateAvatarImageCache(session,baseDir: str,httpPrefix: str,actor: str,avata return None if not personCache.get(actor): return None - if personCache[actor]['actor']['publicKey']['publicKeyPem']!=personJson['publicKey']['publicKeyPem']: + if personCache[actor]['actor']['publicKey']['publicKeyPem']!= \ + personJson['publicKey']['publicKeyPem']: print("ERROR: public keys don't match when downloading actor for "+actor) return None storePersonInCache(baseDir,actor,personJson,personCache) @@ -156,7 +166,8 @@ def getPersonAvatarUrl(baseDir: str,personUrl: str,personCache: {}) -> str: return personJson['icon']['url'] return None -def htmlSearchEmoji(translate: {},baseDir: str,httpPrefix: str,searchStr: str) -> str: +def htmlSearchEmoji(translate: {},baseDir: str,httpPrefix: str, \ + searchStr: str) -> str: """Search results for emoji """ @@ -199,9 +210,15 @@ def htmlSearchEmoji(translate: {},baseDir: str,httpPrefix: str,searchStr: str) - for emojiName,filename in results.items(): if os.path.isfile(baseDir+'/emoji/'+filename): if not headingShown: - emojiForm+='
'+translate['Copy the text then paste it into your post']+'
' + emojiForm+= \ + '
'+ \ + translate['Copy the text then paste it into your post']+ \ + '
' headingShown=True - emojiForm+='

:'+emojiName+':

' + emojiForm+= \ + '

:'+emojiName+ \ + ':

' emojiForm+='' emojiForm+=htmlFooter() @@ -229,7 +246,8 @@ def htmlSearchSharedItems(translate: {}, \ currPage=1 ctr=0 sharedItemsForm='' - searchStrLower=searchStr.replace('%2B','+').replace('%40','@').replace('%3A',':').replace('%23','#').lower().strip('\n') + searchStrLower= \ + searchStr.replace('%2B','+').replace('%40','@').replace('%3A',':').replace('%23','#').lower().strip('\n') searchStrLowerList=searchStrLower.split('+') cssFilename=baseDir+'/epicyon-profile.css' if os.path.isfile(baseDir+'/epicyon.css'): @@ -238,9 +256,11 @@ def htmlSearchSharedItems(translate: {}, \ with open(cssFilename, 'r') as cssFile: sharedItemsCSS=cssFile.read() if httpPrefix!='https': - sharedItemsCSS=sharedItemsCSS.replace('https://',httpPrefix+'://') + sharedItemsCSS= \ + sharedItemsCSS.replace('https://',httpPrefix+'://') sharedItemsForm=htmlHeader(cssFilename,sharedItemsCSS) - sharedItemsForm+='

'+translate['Shared Items Search']+'

' + sharedItemsForm+= \ + '

'+translate['Shared Items Search']+'

' resultsExist=False for subdir, dirs, files in os.walk(baseDir+'/accounts'): for handle in dirs: @@ -274,26 +294,42 @@ def htmlSearchSharedItems(translate: {}, \ if matched: if currPage==pageNumber: sharedItemsForm+='
' - sharedItemsForm+='' + sharedItemsForm+= \ + '' if sharedItem.get('imageUrl'): sharedItemsForm+='' - sharedItemsForm+='Item image' + sharedItemsForm+= \ + 'Item image' sharedItemsForm+='

'+sharedItem['summary']+'

' sharedItemsForm+='

'+translate['Type']+': '+sharedItem['itemType']+' ' sharedItemsForm+=''+translate['Category']+': '+sharedItem['category']+' ' sharedItemsForm+=''+translate['Location']+': '+sharedItem['location']+'

' contactActor=httpPrefix+'://'+domainFull+'/users/'+contactNickname - sharedItemsForm+='

' + sharedItemsForm+= \ + '

' if actor.endswith('/users/'+contactNickname): - sharedItemsForm+=' ' + sharedItemsForm+= \ + ' ' sharedItemsForm+='

' if not resultsExist and currPage>1: # previous page link, needs to be a POST - sharedItemsForm+='
' - sharedItemsForm+=' ' - sharedItemsForm+='
' - sharedItemsForm+='
' - sharedItemsForm+=' '+translate['Page up']+'' + sharedItemsForm+= \ + '' + sharedItemsForm+= \ + ' ' + sharedItemsForm+= \ + '
' + sharedItemsForm+= \ + '
' + sharedItemsForm+= \ + ' '+translate['Page up']+'' sharedItemsForm+='
' sharedItemsForm+='' resultsExist=True @@ -302,11 +338,20 @@ def htmlSearchSharedItems(translate: {}, \ currPage+=1 if currPage>pageNumber: # next page link, needs to be a POST - sharedItemsForm+='
' - sharedItemsForm+=' ' - sharedItemsForm+='
' - sharedItemsForm+='
' - sharedItemsForm+=' '+translate['Page down']+'' + sharedItemsForm+= \ + '' + sharedItemsForm+= \ + ' ' + sharedItemsForm+= \ + '
' + sharedItemsForm+= \ + '
' + sharedItemsForm+= \ + ' '+translate['Page down']+'' sharedItemsForm+='
' sharedItemsForm+='' break @@ -327,7 +372,8 @@ def htmlModerationInfo(translate: {},baseDir: str,httpPrefix: str) -> str: infoCSS=infoCSS.replace('https://',httpPrefix+'://') infoForm=htmlHeader(cssFilename,infoCSS) - infoForm+='

'+translate['Moderation Information']+'

' + infoForm+= \ + '

'+translate['Moderation Information']+'

' infoShown=False suspendedFilename=baseDir+'/accounts/suspended.txt' @@ -337,7 +383,9 @@ def htmlModerationInfo(translate: {},baseDir: str,httpPrefix: str) -> str: infoForm+='
' infoForm+='
'+translate['Suspended accounts']+'' infoForm+='
'+translate['These are currently suspended'] - infoForm+=' ' + infoForm+= \ + ' ' infoForm+='
' infoShown=True @@ -346,13 +394,20 @@ def htmlModerationInfo(translate: {},baseDir: str,httpPrefix: str) -> str: with open(blockingFilename, "r") as f: blockedStr = f.read() infoForm+='
' - infoForm+='
'+translate['Blocked accounts and hashtags']+'' - infoForm+='
'+translate['These are globally blocked for all accounts on this instance'] - infoForm+=' ' + infoForm+= \ + '
'+translate['Blocked accounts and hashtags']+'' + infoForm+= \ + '
'+translate['These are globally blocked for all accounts on this instance'] + infoForm+= \ + ' ' infoForm+='
' infoShown=True if not infoShown: - infoForm+='

'+translate['Any blocks or suspensions made by moderators will be shown here.']+'

' + infoForm+= \ + '

'+ \ + translate['Any blocks or suspensions made by moderators will be shown here.']+ \ + '

' infoForm+=htmlFooter() return infoForm @@ -388,7 +443,8 @@ def htmlHashtagSearch(nickname: str,domain: str,port: int, \ with open(cssFilename, 'r') as cssFile: hashtagSearchCSS = cssFile.read() if httpPrefix!='https': - hashtagSearchCSS=hashtagSearchCSS.replace('https://',httpPrefix+'://') + hashtagSearchCSS= \ + hashtagSearchCSS.replace('https://',httpPrefix+'://') # ensure that the page number is in bounds if not pageNumber: @@ -503,7 +559,9 @@ def htmlSkillsSearch(translate: {},baseDir: str, \ skillLevelStr='0'+skillLevelStr if skillLevel<10: skillLevelStr='0'+skillLevelStr - indexStr=skillLevelStr+';'+actor+';'+actorJson['name']+';'+actorJson['icon']['url'] + indexStr= \ + skillLevelStr+';'+actor+';'+actorJson['name']+ \ + ';'+actorJson['icon']['url'] if indexStr not in results: results.append(indexStr) if not instanceOnly: @@ -534,7 +592,9 @@ def htmlSkillsSearch(translate: {},baseDir: str, \ skillLevelStr='0'+skillLevelStr if skillLevel<10: skillLevelStr='0'+skillLevelStr - indexStr=skillLevelStr+';'+actor+';'+actorJson['name']+';'+actorJson['icon']['url'] + indexStr= \ + skillLevelStr+';'+actor+';'+actorJson['name']+ \ + ';'+actorJson['icon']['url'] if indexStr not in results: results.append(indexStr) @@ -548,7 +608,9 @@ def htmlSkillsSearch(translate: {},baseDir: str, \ if httpPrefix!='https': skillSearchCSS=skillSearchCSS.replace('https://',httpPrefix+'://') skillSearchForm=htmlHeader(cssFilename,skillSearchCSS) - skillSearchForm+='

'+translate['Skills search']+': '+skillsearch+'

' + skillSearchForm+= \ + '

'+translate['Skills search']+': '+ \ + skillsearch+'

' if len(results)==0: skillSearchForm+='
'+translate['No results']+'
' @@ -562,7 +624,9 @@ def htmlSkillsSearch(translate: {},baseDir: str, \ actorName=skillMatchFields[2] avatarUrl=skillMatchFields[3] skillSearchForm+='' + skillSearchForm+= \ + ''+actorName+'' ctr+=1 if ctr>=postsPerPage: break @@ -580,7 +644,8 @@ def scheduledPostsExist(baseDir: str,nickname: str,domain: str) -> bool: return True return False -def htmlEditProfile(translate: {},baseDir: str,path: str,domain: str,port: int,httpPrefix: str) -> str: +def htmlEditProfile(translate: {},baseDir: str,path: str, \ + domain: str,port: int,httpPrefix: str) -> str: """Shows the edit profile screen """ imageFormats='.png, .jpg, .jpeg, .gif, .webp' @@ -674,12 +739,21 @@ def htmlEditProfile(translate: {},baseDir: str,path: str,domain: str,port: int,h skillCtr=1 if skills: for skillDesc,skillValue in skills.items(): - skillsStr+='

' - skillsStr+='

' + skillsStr+= \ + '

' + skillsStr+= \ + '

' skillCtr+=1 - skillsStr+='

' - skillsStr+='

' \ + skillsStr+= \ + '

' + skillsStr+= \ + '

' \ cssFilename=baseDir+'/epicyon-profile.css' if os.path.isfile(baseDir+'/epicyon.css'): @@ -698,14 +772,24 @@ def htmlEditProfile(translate: {},baseDir: str,path: str,domain: str,port: int,h instanceDescriptionShort=getConfigParam(baseDir,'instanceDescriptionShort') instanceTitle=getConfigParam(baseDir,'instanceTitle') instanceStr='
' - instanceStr+=' ' - instanceStr+='
' - instanceStr+=' ' - instanceStr+='
' - instanceStr+=' ' - instanceStr+=' ' - instanceStr+=' ' - instanceStr+=' '+translate['Instance Title']+'' + instanceStr+= \ + '
' + instanceStr+= \ + ' ' + instanceStr+= \ + '
' + instanceStr+= \ + ' ' + instanceStr+= \ + ' ' + instanceStr+= \ + ' ' + instanceStr+= \ + ' ' instanceStr+='
' @@ -717,7 +801,10 @@ def htmlEditProfile(translate: {},baseDir: str,path: str,domain: str,port: int,h moderatorsStr='
' moderatorsStr+=' '+translate['Moderators']+'
' moderatorsStr+=' '+translate['A list of moderator nicknames. One per line.'] - moderatorsStr+=' ' + moderatorsStr+= \ + ' ' moderatorsStr+='
' themesDropdown= '
' @@ -731,20 +818,31 @@ def htmlEditProfile(translate: {},baseDir: str,path: str,domain: str,port: int,h themesDropdown+='
' themesDropdown+='
' themeName=getConfigParam(baseDir,'theme') - themesDropdown=themesDropdown.replace('