# Path: competencies/models.py
# class Organization(models.Model):
#     name = models.CharField(max_length=500)
#     org_type = models.CharField(max_length=500, default='school')
#     public = models.BooleanField(default=False)
#     owner = models.ForeignKey(User)
#     editors = models.ManyToManyField(User, related_name='org_editors')
# 
#     # Allow organizations to rename taxonomy elements.
#     alias_sa = models.CharField(max_length=500, default='subject area')
#     alias_sda = models.CharField(max_length=500, default='subdiscipline area')
#     alias_ca = models.CharField(max_length=500, default='competency area')
#     alias_eu = models.CharField(max_length=500, default='essential understanding')
#     alias_lt = models.CharField(max_length=500, default='learning target')
# 
#     class Meta:
#         unique_together = ('name', 'owner',)
# 
#     def __str__(self):
#         return self.name


from competencies.models import Organization
    from copy import deepcopy
    links = [rel.get_accessor_name() for rel in element._meta.get_all_related_objects()]
    for link in links:
        objects = getattr(element, link).all()
        for object in objects:
            try:
                if visibility_mode == 'private':
                    if object.public:
                        object.public = False
                        object.save()
                elif visibility_mode == 'public':
                    if not object.public:
                        object.public = True
                        object.save()
            except Exception as e:
                # Must not be a public/ private object
                #print("Can't set object private:", object, e)
                pass
            
            # Check if this object has related objects, if so continue cascading.
            if object._meta.get_all_related_objects():
                cascade_visibility_down(object, visibility_mode)

def cascade_public_up(element):
    """Sets visibility to public for all ancestors of an element. (cascades up)."""
    # Does nothing to given element.
    # Does not affect organization; that is managed separately.
    #   This allows an organization to be set up, and then made visible all at once.
    # Only sets elements public, because I can't see any reason to cascade private upwards.

    element = element.get_parent()

# Path: competencies/models.py
# class Organization(models.Model):
#     name = models.CharField(max_length=500)
#     org_type = models.CharField(max_length=500, default='school')
#     public = models.BooleanField(default=False)
#     owner = models.ForeignKey(User)
#     editors = models.ManyToManyField(User, related_name='org_editors')
# 
#     # Allow organizations to rename taxonomy elements.
#     alias_sa = models.CharField(max_length=500, default='subject area')
#     alias_sda = models.CharField(max_length=500, default='subdiscipline area')
#     alias_ca = models.CharField(max_length=500, default='competency area')
#     alias_eu = models.CharField(max_length=500, default='essential understanding')
#     alias_lt = models.CharField(max_length=500, default='learning target')
# 
#     class Meta:
#         unique_together = ('name', 'owner',)
# 
#     def __str__(self):
#         return self.name


# Path: competencies/utils.py
from competencies.models import Organization
    from copy import deepcopy

    links = [rel.get_accessor_name() for rel in element._meta.get_all_related_objects()]
    for link in links:
        objects = getattr(element, link).all()
        for object in objects:
            try:
                if visibility_mode == 'private':
                    if object.public:
                        object.public = False
                        object.save()
                elif visibility_mode == 'public':
                    if not object.public:
                        object.public = True
                        object.save()
            except Exception as e:
                # Must not be a public/ private object
                #print("Can't set object private:", object, e)
                pass
            
            # Check if this object has related objects, if so continue cascading.
            if object._meta.get_all_related_objects():
                cascade_visibility_down(object, visibility_mode)

def cascade_public_up(element):
    """Sets visibility to public for all ancestors of an element. (cascades up)."""
    # Does nothing to given element.
    # Does not affect organization; that is managed separately.
    #   This allows an organization to be set up, and then made visible all at once.
    # Only sets elements public, because I can't see any reason to cascade private upwards.

    element = element.get_parent()

    while element.__class__ != Organization: