Skip to content

Commit

Permalink
Merge pull request #61 from eea/fix_fetchtemplates
Browse files Browse the repository at this point in the history
 [refs #90567] Refactor fetchtemplates
  • Loading branch information
melish committed Jan 26, 2018
2 parents b7c48a2 + 11de47c commit 4f502b1
Showing 1 changed file with 35 additions and 28 deletions.
63 changes: 35 additions & 28 deletions gemet/thesaurus/management/commands/fetchtemplates.py
@@ -1,3 +1,4 @@
import logging
import os
import requests

Expand All @@ -24,12 +25,26 @@ class Command(BaseCommand):
help = 'Get zope template and prepare it for use'

def handle(self, *args, **options):

zope_path = os.path.join(
settings.BASE_DIR, 'gemet', 'thesaurus', 'templates', 'zope')

resp = requests.get(HEADER_URL)
if resp.status_code == 200:
header_files = [
'header_before_title_cached.html',
'header_after_title_cached.html',
'header_before_login_cached.html',
'header_after_login_cached.html',
'footer_cached.html',
]

try:
failed = False
resp = requests.get(HEADER_URL)
if resp.status_code != 200:
failed = True
except requests.exceptions.ConnectionError:
failed = True

if not failed:
header_text = prepare_html(resp.text)

title_start = header_text.find('<title>')
Expand All @@ -42,34 +57,26 @@ def handle(self, *args, **options):
HEADER_AFTER_TITLE = header_text[title_end:head_end]
HEADER_BEFORE_LOGIN = header_text[head_end:login_start]
HEADER_AFTER_LOGIN = header_text[login_end:]
else:
with open(os.path.join(zope_path,
'header_before_title_cached.html')) as f:
HEADER_BEFORE_TITLE = f.read()
with open(os.path.join(zope_path,
'header_after_title_cached.html')) as f:
HEADER_AFTER_TITLE = f.read()
with open(os.path.join(zope_path,
'header_before_login_cached.html')) as f:
HEADER_BEFORE_LOGIN = f.read()
with open(os.path.join(zope_path,
'header_after_login_cached.html')) as f:
HEADER_AFTER_LOGIN = f.read()

resp = requests.get(FOOTER_URL)
if resp.status_code == 200:
resp = requests.get(FOOTER_URL)
FOOTER_TEXT = prepare_html(resp.text)
templates = {
'header_before_title.html': HEADER_BEFORE_TITLE,
'header_after_title.html': HEADER_AFTER_TITLE,
'header_before_login.html': HEADER_BEFORE_LOGIN,
'header_after_login.html': HEADER_AFTER_LOGIN,
'footer.html': FOOTER_TEXT,
}
else:
with open(os.path.join(zope_path, 'footer_cached.html')) as f:
FOOTER_TEXT = f.read()

templates = {
'header_before_title.html': HEADER_BEFORE_TITLE,
'header_after_title.html': HEADER_AFTER_TITLE,
'header_before_login.html': HEADER_BEFORE_LOGIN,
'header_after_login.html': HEADER_AFTER_LOGIN,
'footer.html': FOOTER_TEXT,
}
files = ['header_before_title.html', 'header_after_title.html',
'header_before_login.html', 'header_after_login.html',
'footer.html']
templates = {}
for template, file_name in zip(files, header_files):
with open(os.path.join(zope_path, file_name)) as f:
templates[template] = f.read()
logger = logging.getLogger('django')
logger.info('Zope templates were taken from cache.')

for template_name, content in templates.iteritems():
template_path = os.path.join(zope_path, template_name)
Expand Down

0 comments on commit 4f502b1

Please sign in to comment.