def get_link(cls, obj): linktype = obj.glossary.get('link_type') if linktype == 'exturl': return '{ext_url}'.format(**obj.glossary) if linktype == 'email': return 'mailto:{mail_to}'.format(**obj.glossary) if linktype == 'phonenumber': return 'tel:{phone_number}'.format(**obj.glossary) # otherwise resolve by model if linktype == 'cmspage': relobj = get_related_object(obj.glossary, 'cms_page') if relobj: href = relobj.get_absolute_url() try: element_ids = relobj.cascadepage.glossary['element_ids'] section = obj.glossary['section'] href = '{}#{}'.format(href, element_ids[section]) except (KeyError, ObjectDoesNotExist): pass return href elif linktype == 'download': relobj = get_related_object(obj.glossary, 'download_file') if isinstance(relobj, FilerFileModel): return relobj.url return linktype
def test_get_related_object(): properties = { 'tenant': { 'model': 'auth.user', 'pk': 2 }, } tenant = get_related_object(properties, 'tenant') assert isinstance(tenant, get_user_model()) assert tenant.username == "Mary" assert get_related_object(properties, 'xyz') is None
def test_get_related_object_deprecated(): from entangled.forms import get_related_object properties = { 'tenant': { 'model': 'auth.user', 'pk': 2 }, } with pytest.deprecated_call(): tenant = get_related_object(properties, 'tenant') assert isinstance(tenant, get_user_model())
def _preset_section(self, instance): """ Field ``cms_page`` may refer onto any CMS page, which itself may contain bookmarks. This method creates the list of bookmarks. """ self.base_fields['section'].choices = self.base_fields['section'].choices[:1] try: cascade_page = get_related_object(instance.glossary, 'cms_page').cascadepage for key, val in cascade_page.glossary.get('element_ids', {}).items(): self.base_fields['section'].choices.append((key, val)) except (AttributeError, ObjectDoesNotExist): pass
def render(self, context, instance, placeholder): context = super().render(context, instance, placeholder) icon_font = get_related_object(instance.glossary, 'icon_font') symbol = instance.glossary.get('symbol') if icon_font and symbol: prefix = icon_font.config_data.get('css_prefix_text', 'icon-') context.update({ 'stylesheet_url': icon_font.get_stylesheet_url(), 'icon_font_class': mark_safe('{}{}'.format(prefix, symbol)), }) return context
def download_name(self): link_type = self.glossary.get('link_type') if link_type == 'download': relobj = get_related_object(self.glossary, 'download_file') if isinstance(relobj, FilerFileModel): return mark_safe(relobj.original_filename)
def image(self): if not hasattr(self, '_image_file'): self._image_file = get_related_object(self.glossary, 'image_file') return self._image_file