def get_context(self, name: str, value: Link, attrs: dict): context = { 'widget': { 'name': name, 'link': value, 'type_fields': self.type_fields(value), 'other_fields': self.other_fields(value, name), 'allow_target': value.config('allow_target'), 'allow_label': value.config('allow_label'), 'allowed_types': value.config('types'), 'allow_no_follow': value.config('allow_no_follow'), } } return context
def other_fields(link: Link, field_name: str) -> dict: """ All fields (except the type fields) that need to get rendered in the template. """ label = CharField() target = BooleanField() no_follow = BooleanField() link_type = ChoiceField( choices=type_manager.type_choices(link.config('types'))) fields = { 'label': label.widget.render( '{}_link_label'.format(field_name), link.data('label'), attrs={'id': 'id_{}_link_label'.format(field_name)}), 'target': target.widget.render( '{}_link_target'.format(field_name), link.data('target') is not None, attrs={'id': 'id_{}_link_target'.format(field_name)}), 'link_type': link_type.widget.render( '{}_link_type'.format(field_name), link.data('type'), attrs={'id': 'id_{}_link_type'.format(field_name)}), 'no_follow': no_follow.widget.render( '{}_link_no_follow'.format(field_name), link.data('no_follow'), attrs={'id': 'id_{}_link_no_follow'.format(field_name)}) } return fields
def type_fields(link: Link) -> dict: """ Generate all fields for the different link types that are allowed in this link instance. """ types = {} for link_type in link.config('types'): instance = type_manager.instance(link_type, link) types[link_type] = { 'markup': instance.render(), 'instance': instance, } return types