Skip to content

Models

hypha.apply.categories.models

Option

Bases: Orderable

value class-attribute instance-attribute

value = CharField(max_length=255)

category class-attribute instance-attribute

category = ParentalKey('Category', related_name='options')

Category

Bases: ClusterableModel

Used to manage the global select questions used in most of the application form Also used in the front end by editors when writing about projects.

When used in a form: name -> field label and help_text -> help_text

name class-attribute instance-attribute

name = CharField(max_length=255)

filter_on_dashboard class-attribute instance-attribute

filter_on_dashboard = BooleanField(default=False, help_text=gettext_lazy('Make available to filter on dashboard'))

help_text class-attribute instance-attribute

help_text = CharField(max_length=255, blank=True)

panels class-attribute instance-attribute

panels = [FieldPanel('name'), FieldPanel('filter_on_dashboard'), FieldPanel('help_text'), InlinePanel('options', label=gettext_lazy('Options'))]

Meta

verbose_name_plural class-attribute instance-attribute
verbose_name_plural = 'Categories'

MetaTerm

Bases: Indexed, MP_Node

Hierarchal "Meta" terms

name class-attribute instance-attribute

name = CharField(max_length=50, unique=True, help_text=gettext_lazy('Keep the name short, ideally one word.'))

is_archived class-attribute instance-attribute

is_archived = BooleanField(default=False, verbose_name=gettext_lazy('Archived'), help_text=gettext_lazy('Archived terms can be viewed but not set on content.'))

filter_on_dashboard class-attribute instance-attribute

filter_on_dashboard = BooleanField(default=True, help_text=gettext_lazy('Make available to filter on dashboard'))

available_to_applicants class-attribute instance-attribute

available_to_applicants = BooleanField(default=False, help_text=gettext_lazy('Make available to applicants'))

help_text class-attribute instance-attribute

help_text = RichTextField(features=['h2', 'h3', 'bold', 'italic', 'link', 'hr', 'ol', 'ul'], blank=True)

node_order_index class-attribute instance-attribute

node_order_index = IntegerField(blank=True, default=0, editable=False)

node_child_verbose_name class-attribute instance-attribute

node_child_verbose_name = 'child'

node_order_by class-attribute instance-attribute

node_order_by = ['node_order_index', 'name']

panels class-attribute instance-attribute

panels = [FieldPanel('name'), FieldPanel('parent'), MultiFieldPanel([FieldPanel('is_archived'), FieldPanel('filter_on_dashboard'), FieldPanel('available_to_applicants'), FieldPanel('help_text')], heading=gettext_lazy('Options'))]

search_fields class-attribute instance-attribute

search_fields = [SearchField('name')]

Meta

verbose_name class-attribute instance-attribute
verbose_name = 'Meta Term'
verbose_name_plural class-attribute instance-attribute
verbose_name_plural = 'Meta Terms'

get_as_listing_header

get_as_listing_header()
Source code in hypha/apply/categories/models.py
def get_as_listing_header(self):
    depth = self.get_depth()
    rendered = render_to_string(
        "categories/admin/includes/meta_term_list_header.html",
        {
            "depth": depth,
            "depth_minus_1": depth - 1,
            "is_root": self.is_root(),
            "name": self.name,
            "is_archived": self.is_archived,
        },
    )
    return rendered

get_parent

get_parent(*args, **kwargs)
Source code in hypha/apply/categories/models.py
def get_parent(self, *args, **kwargs):
    return super().get_parent(*args, **kwargs)

delete

delete()
Source code in hypha/apply/categories/models.py
def delete(self):
    if self.is_root():
        raise PermissionDenied("Cannot delete root term.")
    else:
        super().delete()

get_root_descendants classmethod

get_root_descendants()
Source code in hypha/apply/categories/models.py
@classmethod
def get_root_descendants(cls):
    # Meta terms queryset without Root node
    root_node = cls.get_first_root_node()
    if root_node:
        return root_node.get_descendants()
    return cls.objects.none()

MetaTermChoiceField

Bases: ModelChoiceField

label_from_instance

label_from_instance(obj)
Source code in hypha/apply/categories/models.py
def label_from_instance(self, obj):
    depth_line = "-" * (obj.get_depth() - 1)
    return "{} {}".format(depth_line, super().label_from_instance(obj))

MetaTermForm

MetaTermForm(*args, **kwargs)

Bases: WagtailAdminModelForm

Source code in hypha/apply/categories/models.py
def __init__(self, *args, **kwargs):
    super().__init__(*args, **kwargs)
    instance = kwargs["instance"]

    if instance.is_root() or MetaTerm.objects.count() == 0:
        self.fields["parent"].disabled = True
        self.fields["parent"].required = False
        self.fields["parent"].empty_label = "N/A - Root Term"
        self.fields["parent"].widget = forms.HiddenInput()

        self.fields["name"].label += " (Root - First term can be named root)"
    elif instance.id:
        self.fields["parent"].initial = instance.get_parent()

parent class-attribute instance-attribute

parent = MetaTermChoiceField(required=True, queryset=all(), empty_label=None)

clean_parent

clean_parent()
Source code in hypha/apply/categories/models.py
def clean_parent(self):
    parent = self.cleaned_data["parent"]

    if parent and parent.is_archived:
        raise forms.ValidationError(
            "The parent is archived therefore can not add child under it."
        )

    return parent

save

save(commit=True, *args, **kwargs)
Source code in hypha/apply/categories/models.py
def save(self, commit=True, *args, **kwargs):
    instance = super().save(*args, **kwargs, commit=False)
    parent = self.cleaned_data["parent"]

    if not commit:
        return instance

    if instance.id is None:
        if MetaTerm.objects.all().count() == 0:
            MetaTerm.add_root(instance=instance)
        else:
            instance = parent.add_child(instance=instance)
    else:
        instance.save()
        if instance.get_parent() != parent:
            instance.move(parent, pos="sorted-child")
    return instance