# Path: tutelary/models.py
# class Policy(models.Model):
#     """An individual policy has a name and a JSON policy body.  Changes to
#     policies are audited.
# 
#     """
# 
#     name = models.CharField(max_length=200)
#     """Policy name field."""
# 
#     body = models.TextField()
#     """Policy JSON body."""
# 
#     audit_log = AuditLog()
# 
#     def __str__(self):
#         return self.name
# 
#     def variable_names(self):
#         pat = re.compile(r'\$(?:([_a-z][_a-z0-9]*)|{([_a-z][_a-z0-9]*)})')
#         return set([m[0] for m in re.findall(pat, self.body)])
# 
#     def save(self, *args, **kwargs):
#         super().save(*args, **kwargs)
#         self.refresh()
# 
#     def refresh(self):
#         for pset in _policy_psets([self]):
#             pset.refresh()
# 
# Path: example/exampleapp/models.py
# class Organisation(models.Model):
#     name = models.CharField(max_length=100)
# 
#     class Meta:
#         ordering = ('name',)
# 
#     class TutelaryMeta:
#         perm_type = 'organisation'
#         path_fields = ('name',)
#         actions = [
#             ('organisation.list',   {'permissions_object': None}),
#             ('organisation.create', {'permissions_object': None}),
#             'organisation.delete'
#         ]
# 
#     def __str__(self):
#         return self.name
# 
# class Project(models.Model):
#     name = models.CharField(max_length=100)
#     organisation = models.ForeignKey(Organisation)
# 
#     class Meta:
#         ordering = ('organisation', 'name')
# 
#     class TutelaryMeta:
#         perm_type = 'project'
#         path_fields = ('organisation', 'name')
#         actions = [
#             ('project.list', {'permissions_object': 'organisation'}),
#             ('project.create', {'permissions_object': 'organisation'}),
#             'project.delete'
#         ]
# 
#     def __str__(self):
#         return self.name


from django import forms
from django.contrib.auth.models import User
from tutelary.models import Policy
from .models import Organisation, Project


def user_choices():
    yield (None, '--------')
    for u in User.objects.all():
        yield (u.username, u.username)


class UserSwitchForm(forms.Form):
    username = forms.ChoiceField(label='Select user',
                                 choices=user_choices)


class UserPolicyForm(forms.Form):
    MAX_POLICIES = 8

    policy = forms.ModelChoiceField(queryset=Policy.objects.all(),
                                    required=False)
    organisation = forms.ModelChoiceField(
        queryset=Organisation.objects.all(), empty_label='*', required=False
    )
    project = forms.ModelChoiceField(

# Path: tutelary/models.py
# class Policy(models.Model):
#     """An individual policy has a name and a JSON policy body.  Changes to
#     policies are audited.
# 
#     """
# 
#     name = models.CharField(max_length=200)
#     """Policy name field."""
# 
#     body = models.TextField()
#     """Policy JSON body."""
# 
#     audit_log = AuditLog()
# 
#     def __str__(self):
#         return self.name
# 
#     def variable_names(self):
#         pat = re.compile(r'\$(?:([_a-z][_a-z0-9]*)|{([_a-z][_a-z0-9]*)})')
#         return set([m[0] for m in re.findall(pat, self.body)])
# 
#     def save(self, *args, **kwargs):
#         super().save(*args, **kwargs)
#         self.refresh()
# 
#     def refresh(self):
#         for pset in _policy_psets([self]):
#             pset.refresh()
# 
# Path: example/exampleapp/models.py
# class Organisation(models.Model):
#     name = models.CharField(max_length=100)
# 
#     class Meta:
#         ordering = ('name',)
# 
#     class TutelaryMeta:
#         perm_type = 'organisation'
#         path_fields = ('name',)
#         actions = [
#             ('organisation.list',   {'permissions_object': None}),
#             ('organisation.create', {'permissions_object': None}),
#             'organisation.delete'
#         ]
# 
#     def __str__(self):
#         return self.name
# 
# class Project(models.Model):
#     name = models.CharField(max_length=100)
#     organisation = models.ForeignKey(Organisation)
# 
#     class Meta:
#         ordering = ('organisation', 'name')
# 
#     class TutelaryMeta:
#         perm_type = 'project'
#         path_fields = ('organisation', 'name')
#         actions = [
#             ('project.list', {'permissions_object': 'organisation'}),
#             ('project.create', {'permissions_object': 'organisation'}),
#             'project.delete'
#         ]
# 
#     def __str__(self):
#         return self.name


# Path: example/exampleapp/forms.py
from django import forms
from django.contrib.auth.models import User
from tutelary.models import Policy
from .models import Organisation, Project



def user_choices():
    yield (None, '--------')
    for u in User.objects.all():
        yield (u.username, u.username)


class UserSwitchForm(forms.Form):
    username = forms.ChoiceField(label='Select user',
                                 choices=user_choices)


class UserPolicyForm(forms.Form):
    MAX_POLICIES = 8

    policy = forms.ModelChoiceField(queryset=Policy.objects.all(),
                                    required=False)
    organisation = forms.ModelChoiceField(
        queryset=Organisation.objects.all(), empty_label='*', required=False
    )
    project = forms.ModelChoiceField(

        queryset=Project.objects.all(), empty_label='*', required=False