'get the template of this page if defined or if closer parent if
defined or DEFAULT_PAGE_TEMPLATE otherwise'

def get_template(self):

    if hasattr(self, '_template_cache'):
        return self._template_cache
    template = None
    if self.template:
        if (self.template != constants.TEMPLATE_INHERITANCE_MAGIC):
            template = self.template
        else:
            try:
                template = list(reversed(self.get_ancestors().exclude(template=constants.TEMPLATE_INHERITANCE_MAGIC).values_list('template', flat=True)))[0]
            except IndexError:
                pass
    if (not template):
        template = get_cms_setting('TEMPLATES')[0][0]
    self._template_cache = template
    return template
