Skip to content

Commit

Permalink
refactors to make package+template configurable per site as per #14
Browse files Browse the repository at this point in the history
  • Loading branch information
WolfgangFahl committed Mar 8, 2021
1 parent db3e522 commit 1e18cb5
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 10 deletions.
26 changes: 24 additions & 2 deletions frontend/site.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
@author: wf
'''
import os
from wikibot.smw import SMWClient
import jinja2
import sys

class Site(object):
'''
Expand All @@ -24,6 +24,7 @@ def __init__(self,name:str,defaultPage:str="Main Page",lang:str="en"):
self.name=name
self.defaultPage=defaultPage
self.lang=lang
self.configured=False

def configure(self,config:dict):
'''
Expand All @@ -38,5 +39,26 @@ def configure(self,config:dict):
self.templateFolder=config['templateFolder']
else:
self.templateFolder=self.name
if "packageName" in config:
self.packageName=config["packageName"]
else:
self.packageName=self.name
if "packageFolder" in config:
self.packageFolder=config["packageFolder"]
else:
self.packageFolder=self.name
self.configured=True

def open(self):
'''
open this site
'''
if not self.configured:
raise Exception("need to configure site before opening it")
# https://stackoverflow.com/a/14276993/1497139
# http://code.nabla.net/doc/jinja2/api/jinja2/loaders/jinja2.loaders.PackageLoader.html
sys.path.insert(0,self.packageFolder)
self.templateEnv = jinja2.Environment( loader=jinja2.PackageLoader(self.packageName, self.templateFolder))



18 changes: 16 additions & 2 deletions frontend/wikicms.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
from frontend.site import Site
from bs4 import BeautifulSoup
import traceback

import requests
from flask import Response

Expand Down Expand Up @@ -46,6 +45,7 @@ def open(self):
self.wiki=WikiClient.ofWikiId(self.site.wikiId)
self.wiki.login()
self.smwclient=SMWClient(self.wiki.getSite())
self.site.open()

def errMsg(self,ex):
if self.debug:
Expand All @@ -70,7 +70,6 @@ def wikiPage(self,pagePath):
else:
wikipage=pagePath
return wikipage


def checkPath(self,pagePath):
'''
Expand Down Expand Up @@ -193,4 +192,19 @@ def getContent(self,pagePath:str,dofilterEditSections=True):
pageTitle,content,error=self.getPageContent(pagePath, dofilterEditSections)
frame=self.getFrame(pageTitle)
return pageTitle,content,error

def renderTemplate(self,templateFile,args):
'''
render the given templateFile with the given arguments
Args:
templateFile(str): the template file to be used
args(): same arguments a for dict constructor
Returns:
str: the rendered result
'''
template = self.site.templateEnv.get_template( templateFile )
result=template.render(args)
return result

29 changes: 26 additions & 3 deletions tests/test_frontend.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import unittest
from frontend.wikicms import Frontend
from tests.test_webserver import TestWebServer

import os

class TestFrontend(unittest.TestCase):
'''
Expand Down Expand Up @@ -50,13 +50,36 @@ def testProxy(self):
self.assertEqual("200 OK",imageResponse.status)
self.assertEqual(79499,len(imageResponse.data))

def createPackage(self,packageFolder,templateFolder,moduleName,moduleCode,templateCode):
moduleFolder="%s/%s" % (packageFolder,moduleName)
os.makedirs(moduleFolder,exist_ok=True)
absTemplateFolder="%s/%s" % (moduleFolder,templateFolder)
os.makedirs(absTemplateFolder,exist_ok=True)
modulePath="%s/__init__.py" % moduleFolder
print(moduleCode,file=open(modulePath,"w"))
templatePath="%s/test.html" % (absTemplateFolder)
print(templateCode,file=open(templatePath,"w"))

def testIssue14Templates(self):
'''
test template handling
'''
frontend=self.server.enableFrontend('wiki')
print (frontend.site)
packageFolder='/tmp/www.wikicms'
templateFolder='templates'
moduleName='bitplan'
moduleCode="""
def test():
pass
"""
templateCode="""
{{ msg }}
"""
self.createPackage(packageFolder, templateFolder, moduleName, moduleCode, templateCode)
frontend=self.server.enableFrontend('www')
#self.assertEqual(templateFolder,frontend.site.templateFolder)
self.assertEqual(moduleName,frontend.site.packageName)
html=frontend.renderTemplate("test.html",{"msg":"Hello world!"})
self.assertTrue("Hello world!" in html)

def testIssue14(self):
'''
Expand Down
9 changes: 6 additions & 3 deletions tests/test_webserver.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,13 @@ def initServer():
'defaultPage':'Sharks'
},
{
'site': 'wiki',
'site': 'www',
'wikiId':'wiki',
'template':'bootstrap.html',
'defaultPage':'Welcome'
'template':'design.html',
'defaultPage':'Welcome',
'packageFolder': '/tmp/www.wikicms',
'packageName': 'bitplan',
'templateFolder': 'templates'
}
]
for frontendConfigs in server.frontendConfigs:
Expand Down

0 comments on commit 1e18cb5

Please sign in to comment.