# Path: backend/flindt/role/models.py
# class Role(FlindtBaseModel):
#     """
#     The Role model is used to store information about roles.
# 
#     It is primarily designed to capture information about roles
#     in the holacratic sense of the word, but can also be used to
#     reflect a more traditional organisation.
#     """
#     name = models.TextField()
#     purpose = models.TextField(blank=True, default='')
#     accountabilities = models.TextField(blank=True, default='')  # Used to store JSON
#     domains = models.TextField(blank=True, default='')  # Used to store JSON
#     parent = models.ForeignKey('Role', related_name='children', blank=True, null=True, on_delete=models.CASCADE)
#     users = models.ManyToManyField(User, blank=True)
#     archived = models.BooleanField(default=False)
# 
#     def __str__(self):
#         return self.name
# 
#     @property
#     def is_circle(self):
#         """
#         True if the circle has children.
#         """
#         return self.children.exists() is True
# 
#     @property
#     def is_anchor(self):
#         """
#         True if the circle has no parents.
#         """
#         return self.parent is None
# 
#     def descendants(self):
#         """
#         Return all descendants of `self` in a list.
# 
#         TODO: FEED-50: descendants should be a method on a manager returning a
#         queryset.
#         """
#         descendants = []
#         children = self.children.all()
#         descendants.extend(children)
#         for child in children:
#             descendants.extend(child.descendants())
#         return descendants
# 
#     def archive(self):
#         """
#         Archive this role and all roles under it.
# 
#         TODO: FEED-50: If descendants is a queryset, replace this with one
#         update()
#         """
#         self.archived = True
#         self.save()
#         for role in self.descendants():
#             role.archived = True
#             role.save()
#         logger.info('role: {} and all its descendants have been archived.'.format(self))


import json
from rest_framework import serializers
from .models import Role
    # SerializerMethodField to override the getter.
    domains = serializers.SerializerMethodField()

    def get_accountabilities(self, obj):
        """
        Override the getter for accountabilities to try and convert the domains to a
        correct json format.
        """

        # Try to load the obj.accountabilities as a json. If this fails return the
        # string.
        try:
            return json.loads(obj.accountabilities)
        except ValueError:
            return obj.accountabilities

    def get_domains(self, obj):
        """
        Override the getter for domains to try and convert the domains to a
        correct json format.
        """

        # Try to load the obj.domains as a json. If this fails return the
        # string.
        try:
            return json.loads(obj.domains)
        except ValueError:
            return obj.domains

    class Meta:

# Path: backend/flindt/role/models.py
# class Role(FlindtBaseModel):
#     """
#     The Role model is used to store information about roles.
# 
#     It is primarily designed to capture information about roles
#     in the holacratic sense of the word, but can also be used to
#     reflect a more traditional organisation.
#     """
#     name = models.TextField()
#     purpose = models.TextField(blank=True, default='')
#     accountabilities = models.TextField(blank=True, default='')  # Used to store JSON
#     domains = models.TextField(blank=True, default='')  # Used to store JSON
#     parent = models.ForeignKey('Role', related_name='children', blank=True, null=True, on_delete=models.CASCADE)
#     users = models.ManyToManyField(User, blank=True)
#     archived = models.BooleanField(default=False)
# 
#     def __str__(self):
#         return self.name
# 
#     @property
#     def is_circle(self):
#         """
#         True if the circle has children.
#         """
#         return self.children.exists() is True
# 
#     @property
#     def is_anchor(self):
#         """
#         True if the circle has no parents.
#         """
#         return self.parent is None
# 
#     def descendants(self):
#         """
#         Return all descendants of `self` in a list.
# 
#         TODO: FEED-50: descendants should be a method on a manager returning a
#         queryset.
#         """
#         descendants = []
#         children = self.children.all()
#         descendants.extend(children)
#         for child in children:
#             descendants.extend(child.descendants())
#         return descendants
# 
#     def archive(self):
#         """
#         Archive this role and all roles under it.
# 
#         TODO: FEED-50: If descendants is a queryset, replace this with one
#         update()
#         """
#         self.archived = True
#         self.save()
#         for role in self.descendants():
#             role.archived = True
#             role.save()
#         logger.info('role: {} and all its descendants have been archived.'.format(self))


# Path: backend/flindt/role/serializers.py
import json
from rest_framework import serializers
from .models import Role

    # SerializerMethodField to override the getter.
    domains = serializers.SerializerMethodField()

    def get_accountabilities(self, obj):
        """
        Override the getter for accountabilities to try and convert the domains to a
        correct json format.
        """

        # Try to load the obj.accountabilities as a json. If this fails return the
        # string.
        try:
            return json.loads(obj.accountabilities)
        except ValueError:
            return obj.accountabilities

    def get_domains(self, obj):
        """
        Override the getter for domains to try and convert the domains to a
        correct json format.
        """

        # Try to load the obj.domains as a json. If this fails return the
        # string.
        try:
            return json.loads(obj.domains)
        except ValueError:
            return obj.domains

    class Meta:

        model = Role