| 1 | import os, datetime, time, hashlib |
|---|
| 2 | |
|---|
| 3 | from indico.web.wsgi import webinterface_handler_config as apache |
|---|
| 4 | |
|---|
| 5 | from MaKaC.common import Config |
|---|
| 6 | from MaKaC.errors import MaKaCError |
|---|
| 7 | |
|---|
| 8 | from MaKaC.webinterface import urlHandlers |
|---|
| 9 | from MaKaC.webinterface.rh import base |
|---|
| 10 | |
|---|
| 11 | from email.Utils import formatdate |
|---|
| 12 | |
|---|
| 13 | import MaKaC.common.TemplateExec as templateEngine |
|---|
| 14 | |
|---|
| 15 | class RHTemplateContentJS(base.RH): |
|---|
| 16 | _uh = urlHandlers.Build("JSContent.py") |
|---|
| 17 | _tplName = '' |
|---|
| 18 | |
|---|
| 19 | def __init__(self, req): |
|---|
| 20 | base.RH.__init__(self, req) |
|---|
| 21 | |
|---|
| 22 | def _needsRefresh( self, creationTime ): |
|---|
| 23 | # TODO: maybe add date verification as well? |
|---|
| 24 | return self._etag != self._generateEtag(creationTime) |
|---|
| 25 | |
|---|
| 26 | def _generateEtag( self, value ): |
|---|
| 27 | """ |
|---|
| 28 | Generates an Etag from a string (just an MD5 hash) |
|---|
| 29 | """ |
|---|
| 30 | return hashlib.md5(str(value)).hexdigest() |
|---|
| 31 | |
|---|
| 32 | def _setHeaders(self): |
|---|
| 33 | # send out the Etag and Last-Modified headers |
|---|
| 34 | creationTime = datetime.datetime.fromtimestamp(os.path.getctime(self._htmlPath)) |
|---|
| 35 | self._req.headers_out["Etag"] = self._generateEtag(creationTime) |
|---|
| 36 | self._req.headers_out["Last-Modified"] = formatdate(time.mktime(creationTime.timetuple())) |
|---|
| 37 | self._req.content_type = "application/x-javascript" |
|---|
| 38 | |
|---|
| 39 | def process( self, params ): |
|---|
| 40 | |
|---|
| 41 | # Check incoming headers |
|---|
| 42 | self._etag = self._req.headers_in.get("If-None-Match", None) |
|---|
| 43 | # (not used) |
|---|
| 44 | self._modifiedSince = self._req.headers_in.get("If-Modified-Since", None) |
|---|
| 45 | |
|---|
| 46 | cfg = Config.getInstance() |
|---|
| 47 | dirName = os.path.join(cfg.getTPLDir(),"js") |
|---|
| 48 | fileName = cfg.getTPLFile( self._tplName ) |
|---|
| 49 | |
|---|
| 50 | if fileName == "": |
|---|
| 51 | fileName = "%s.tpl"%self._tplName |
|---|
| 52 | |
|---|
| 53 | self._tplFile = os.path.join(dirName, fileName) |
|---|
| 54 | |
|---|
| 55 | self._htmlPath = os.path.join(cfg.getTempDir(), fileName+".tmp") |
|---|
| 56 | |
|---|
| 57 | # if the file has already been generated |
|---|
| 58 | if os.access(self._htmlPath, os.R_OK): |
|---|
| 59 | |
|---|
| 60 | # get the OS creation time |
|---|
| 61 | creationTime = datetime.datetime.fromtimestamp(os.path.getctime(self._htmlPath)) |
|---|
| 62 | if not self._needsRefresh(creationTime): |
|---|
| 63 | # if the etag the same, send NOT_MODIFIED |
|---|
| 64 | self._req.status = apache.HTTP_NOT_MODIFIED |
|---|
| 65 | return |
|---|
| 66 | else: |
|---|
| 67 | # Read and send the file |
|---|
| 68 | fh = open(self._htmlPath, "r") |
|---|
| 69 | self._htmlData = fh.read() |
|---|
| 70 | fh.close() |
|---|
| 71 | self._setHeaders() |
|---|
| 72 | return self._htmlData |
|---|
| 73 | # file needs to be regenerated |
|---|
| 74 | return base.RH.process(self, params) |
|---|
| 75 | |
|---|
| 76 | |
|---|
| 77 | def _process( self ): |
|---|
| 78 | try: |
|---|
| 79 | # regenerate file is needed |
|---|
| 80 | self._dict["__rh__"] = self |
|---|
| 81 | self._dict["user"] = None |
|---|
| 82 | |
|---|
| 83 | self._htmlData = templateEngine.render(self._tplFile, self._dict) |
|---|
| 84 | fh = open(self._htmlPath, "w") |
|---|
| 85 | fh.write(self._htmlData) |
|---|
| 86 | fh.close() |
|---|
| 87 | |
|---|
| 88 | self._setHeaders() |
|---|
| 89 | |
|---|
| 90 | except Exception, e: |
|---|
| 91 | return 'indicoError: %s' % e |
|---|
| 92 | |
|---|
| 93 | return self._htmlData |
|---|
| 94 | |
|---|
| 95 | class RHGetVarsJs(RHTemplateContentJS): |
|---|
| 96 | _uh = urlHandlers.Derive(RHTemplateContentJS, "getVars") |
|---|
| 97 | |
|---|
| 98 | _tplName = 'vars.js' |
|---|
| 99 | |
|---|
| 100 | def __init__(self, req): |
|---|
| 101 | RHTemplateContentJS.__init__(self, req) |
|---|
| 102 | self._dict = {} |
|---|
| 103 | |
|---|
| 104 | @classmethod |
|---|
| 105 | def removeTmpVarsFile(cls): |
|---|
| 106 | cfg = Config.getInstance() |
|---|
| 107 | fileName = cfg.getTPLFile( cls._tplName ) |
|---|
| 108 | |
|---|
| 109 | if fileName == "": |
|---|
| 110 | fileName = "%s.tpl"%cls._tplName |
|---|
| 111 | |
|---|
| 112 | htmlPath = os.path.join(cfg.getTempDir(), fileName+".tmp") |
|---|
| 113 | if os.access(htmlPath, os.R_OK): |
|---|
| 114 | os.remove(htmlPath) |
|---|