# Path: casepro/cases/models.py
# class Partner(models.Model):
#     """
#     Corresponds to a partner organization
#     """
# 
#     org = models.ForeignKey(Org, verbose_name=_("Organization"), related_name="partners", on_delete=models.PROTECT)
# 
#     name = models.CharField(verbose_name=_("Name"), max_length=128, help_text=_("Name of this partner organization"))
# 
#     description = models.CharField(verbose_name=_("Description"), null=True, blank=True, max_length=255)
# 
#     primary_contact = models.ForeignKey(
#         User,
#         verbose_name=_("Primary Contact"),
#         related_name="partners_primary",
#         null=True,
#         blank=True,
#         on_delete=models.PROTECT,
#     )
# 
#     is_restricted = models.BooleanField(
#         default=True,
#         verbose_name=_("Restricted Access"),
#         help_text=_("Whether this partner's access is restricted by labels"),
#     )
# 
#     labels = models.ManyToManyField(
#         Label, verbose_name=_("Labels"), related_name="partners", help_text=_("Labels that this partner can access")
#     )
# 
#     users = models.ManyToManyField(User, related_name="partners", help_text=_("Users that belong to this partner"))
# 
#     logo = models.ImageField(verbose_name=_("Logo"), upload_to="partner_logos", null=True, blank=True)
# 
#     is_active = models.BooleanField(default=True, help_text="Whether this partner is active")
# 
#     @classmethod
#     def create(cls, org, name, description, primary_contact, restricted, labels, logo=None):
#         if labels and not restricted:
#             raise ValueError("Can't specify labels for a partner which is not restricted")
# 
#         partner = cls.objects.create(
#             org=org,
#             name=name,
#             description=description,
#             primary_contact=primary_contact,
#             logo=logo,
#             is_restricted=restricted,
#         )
# 
#         if restricted:
#             partner.labels.add(*labels)
# 
#         return partner
# 
#     @classmethod
#     def get_all(cls, org):
#         return cls.objects.filter(org=org, is_active=True)
# 
#     def get_labels(self):
#         return self.labels.filter(is_active=True) if self.is_restricted else Label.get_all(self.org)
# 
#     def get_users(self):
#         return self.users.all()
# 
#     def get_managers(self):
#         return self.get_users().filter(org_editors=self.org_id)
# 
#     def get_analysts(self):
#         return self.get_users().filter(org_viewers=self.org_id)
# 
#     def release(self):
#         self.is_active = False
#         self.save(update_fields=("is_active",))
# 
#     def as_json(self, full=True):
#         result = {"id": self.pk, "name": self.name}
# 
#         if full:
#             result["restricted"] = self.is_restricted
# 
#         return result
# 
#     def __str__(self):
#         return self.name
# 
# Path: casepro/profiles/models.py
# PARTNER_ROLES = {ROLE_MANAGER, ROLE_ANALYST}  # roles that are tied to a partner
# 
# ROLE_ORG_CHOICES = (
#     (ROLE_ADMIN, _("Administrator")),
#     (ROLE_MANAGER, _("Partner Manager")),
#     (ROLE_ANALYST, _("Partner Data Analyst")),
# )
# 
# ROLE_PARTNER_CHOICES = ((ROLE_MANAGER, _("Manager")), (ROLE_ANALYST, _("Data Analyst")))


from django import forms
from django.contrib.auth.models import User
from django.contrib.auth.password_validation import validate_password
from django.utils.translation import gettext_lazy as _
from casepro.cases.models import Partner
from .models import PARTNER_ROLES, ROLE_ORG_CHOICES, ROLE_PARTNER_CHOICES
            current_password = cleaned_data.get("current_password", "")
            if not self.instance.check_password(current_password):
                self.add_error("current_password", _("Please enter your current password."))

        # if creating new user with password or updating password of existing user, confirmation must match
        password = cleaned_data.get("password") or cleaned_data.get("new_password")
        if password:
            confirm_password = cleaned_data.get("confirm_password", "")
            if password != confirm_password:
                self.add_error("confirm_password", _("Passwords don't match."))

        return cleaned_data

    class Meta:
        model = User
        exclude = ()


class OrgUserForm(UserForm):
    """
    Form for user editing at org-level
    """

    role = forms.ChoiceField(label=_("Role"), choices=ROLE_ORG_CHOICES, required=True)

    partner = forms.ModelChoiceField(label=_("Partner Organization"), queryset=Partner.objects.none(), required=False)

    def __init__(self, *args, **kwargs):
        super(OrgUserForm, self).__init__(*args, **kwargs)


# Path: casepro/cases/models.py
# class Partner(models.Model):
#     """
#     Corresponds to a partner organization
#     """
# 
#     org = models.ForeignKey(Org, verbose_name=_("Organization"), related_name="partners", on_delete=models.PROTECT)
# 
#     name = models.CharField(verbose_name=_("Name"), max_length=128, help_text=_("Name of this partner organization"))
# 
#     description = models.CharField(verbose_name=_("Description"), null=True, blank=True, max_length=255)
# 
#     primary_contact = models.ForeignKey(
#         User,
#         verbose_name=_("Primary Contact"),
#         related_name="partners_primary",
#         null=True,
#         blank=True,
#         on_delete=models.PROTECT,
#     )
# 
#     is_restricted = models.BooleanField(
#         default=True,
#         verbose_name=_("Restricted Access"),
#         help_text=_("Whether this partner's access is restricted by labels"),
#     )
# 
#     labels = models.ManyToManyField(
#         Label, verbose_name=_("Labels"), related_name="partners", help_text=_("Labels that this partner can access")
#     )
# 
#     users = models.ManyToManyField(User, related_name="partners", help_text=_("Users that belong to this partner"))
# 
#     logo = models.ImageField(verbose_name=_("Logo"), upload_to="partner_logos", null=True, blank=True)
# 
#     is_active = models.BooleanField(default=True, help_text="Whether this partner is active")
# 
#     @classmethod
#     def create(cls, org, name, description, primary_contact, restricted, labels, logo=None):
#         if labels and not restricted:
#             raise ValueError("Can't specify labels for a partner which is not restricted")
# 
#         partner = cls.objects.create(
#             org=org,
#             name=name,
#             description=description,
#             primary_contact=primary_contact,
#             logo=logo,
#             is_restricted=restricted,
#         )
# 
#         if restricted:
#             partner.labels.add(*labels)
# 
#         return partner
# 
#     @classmethod
#     def get_all(cls, org):
#         return cls.objects.filter(org=org, is_active=True)
# 
#     def get_labels(self):
#         return self.labels.filter(is_active=True) if self.is_restricted else Label.get_all(self.org)
# 
#     def get_users(self):
#         return self.users.all()
# 
#     def get_managers(self):
#         return self.get_users().filter(org_editors=self.org_id)
# 
#     def get_analysts(self):
#         return self.get_users().filter(org_viewers=self.org_id)
# 
#     def release(self):
#         self.is_active = False
#         self.save(update_fields=("is_active",))
# 
#     def as_json(self, full=True):
#         result = {"id": self.pk, "name": self.name}
# 
#         if full:
#             result["restricted"] = self.is_restricted
# 
#         return result
# 
#     def __str__(self):
#         return self.name
# 
# Path: casepro/profiles/models.py
# PARTNER_ROLES = {ROLE_MANAGER, ROLE_ANALYST}  # roles that are tied to a partner
# 
# ROLE_ORG_CHOICES = (
#     (ROLE_ADMIN, _("Administrator")),
#     (ROLE_MANAGER, _("Partner Manager")),
#     (ROLE_ANALYST, _("Partner Data Analyst")),
# )
# 
# ROLE_PARTNER_CHOICES = ((ROLE_MANAGER, _("Manager")), (ROLE_ANALYST, _("Data Analyst")))


# Path: casepro/profiles/forms.py
from django import forms
from django.contrib.auth.models import User
from django.contrib.auth.password_validation import validate_password
from django.utils.translation import gettext_lazy as _
from casepro.cases.models import Partner
from .models import PARTNER_ROLES, ROLE_ORG_CHOICES, ROLE_PARTNER_CHOICES

            current_password = cleaned_data.get("current_password", "")
            if not self.instance.check_password(current_password):
                self.add_error("current_password", _("Please enter your current password."))

        # if creating new user with password or updating password of existing user, confirmation must match
        password = cleaned_data.get("password") or cleaned_data.get("new_password")
        if password:
            confirm_password = cleaned_data.get("confirm_password", "")
            if password != confirm_password:
                self.add_error("confirm_password", _("Passwords don't match."))

        return cleaned_data

    class Meta:
        model = User
        exclude = ()


class OrgUserForm(UserForm):
    """
    Form for user editing at org-level
    """

    role = forms.ChoiceField(label=_("Role"), choices=ROLE_ORG_CHOICES, required=True)

    partner = forms.ModelChoiceField(label=_("Partner Organization"), queryset=Partner.objects.none(), required=False)

    def __init__(self, *args, **kwargs):
        super(OrgUserForm, self).__init__(*args, **kwargs)


        self.fields["partner"].queryset = Partner.get_all(self.org).order_by("name")