Projet

Général

Profil

0001-try-parent-path-if-theme-retrieval-fails-for-current.patch

Benjamin Dauvergne, 03 octobre 2016 21:57

Télécharger (4,06 ko)

Voir les différences:

Subject: [PATCH] try parent path if theme retrieval fails for current path
 (fixes #13385)

 hobo/context_processors.py | 58 ++++++++++++++++++++++++++++++++--------------
 1 file changed, 41 insertions(+), 17 deletions(-)
hobo/context_processors.py
30 30
    template_vars.update({'statics_hash': statics_hash.hexdigest()})
31 31
    return template_vars
32 32

  
33

  
33 34
class RemoteTemplate(object):
34 35
    def __init__(self, source):
35
        self.source = source
36
        self.source = urlparse.urlunparse(urlparse.urlparse(source)[:3] + ('', '', ''))
37
        self.disable_recurse = False
36 38

  
37 39
    @property
38 40
    def cache_key(self):
39
         return hashlib.md5(urlparse.urlunparse(
40
             urlparse.urlparse(self.source)[:3] + ('', '', ''))).hexdigest()
41
        return hashlib.md5(self.source).hexdigest()
41 42

  
42 43
    def get_template(self):
44
        template_body = self.get_template_body()
45
        if template_body is None:
46
            logger.error('failed to retrieve theme')
47
            raise Exception('failed to retrieve theme')
48
        return Template(template_body)
49

  
50
    def get_template_body(self):
43 51
        item = cache.get(self.cache_key)
44 52
        if item is None:
45
            template_body = self.update_content()
46
            if template_body is None:
47
                raise Exception('Failed to retrieve theme')
53
            template_body = self.request_template_body()
48 54
        else:
49 55
            template_body, expiry_time = item
50 56
            if expiry_time < datetime.datetime.now():
51 57
                # stale value, put it back into the cache for other consumers and
52 58
                # update the content in a different thread
53 59
                self.cache(template_body)
54
                threading.Thread(target=self.update_content).start()
55
        return Template(template_body)
60
                # for background refresh we do not care about retrieval miss
61
                self.disable_recurse = True
62
                threading.Thread(target=self.request_template_body).start()
63
        return template_body
56 64

  
57
    def update_content(self):
58
        r = requests.get(settings.THEME_SKELETON_URL, params={'source': self.source})
59
        if r.status_code != 200:
60
            logger.error('failed to retrieve theme')
65
    def get_template_body_recurse(self):
66
        '''We were unable to retrieve a base template for this source URL, look for one level up.'''
67
        if self.disable_recurse:
68
            return None
69
        parsed = urlparse.urlparse(self.source)
70
        if not parsed.path or parsed.path == '/':
61 71
            return None
62
        self.cache(r.text)
72
        if parsed.path.endswith('/'):  # cd ..
73
            new_path = urlparse.urljoin(parsed.path, '..')
74
        else:
75
            new_path = urlparse.urljoin(parsed.path, '.')
76
        self.source = urlparse.urlunparse(parsed[:2] + (new_path, '', '', ''))
77
        return self.get_template()
78

  
79
    def request_template_body(self):
80
        try:
81
            r = requests.get(settings.THEME_SKELETON_URL, params={'source': self.source})
82
        except requests.RequestException:
83
            return self.recurse()
84
        if r.status_code != 200:
85
            return self.recurse()
86
        self.cache_template_body(r.text)
63 87
        return r.text
64 88

  
65
    def cache(self, template_body):
89
    def cache_template_body(self, template_body):
66 90
        expiry_time = datetime.datetime.now() + datetime.timedelta(seconds=CACHE_REFRESH_TIMEOUT)
67
        cache.set(self.cache_key,
68
                (template_body, expiry_time),
69
                2592000) # bypass cache level expiration time
91
        cache.set(self.cache_key, (template_body, expiry_time),
92
                  2592000)  # bypass cache level expiration time
93

  
70 94

  
71 95
def theme_base(request):
72 96
    # this context processor adds two variables to context:
73
-