# Path: casepro/contacts/models.py
# class Field(models.Model):
#     """
#     A custom contact field in RapidPro
#     """
# 
#     TYPE_TEXT = "T"
#     TYPE_DECIMAL = "N"
#     TYPE_DATETIME = "D"
#     TYPE_STATE = "S"
#     TYPE_DISTRICT = "I"
# 
#     TEMBA_TYPES = {
#         "text": TYPE_TEXT,
#         "numeric": TYPE_DECIMAL,
#         "datetime": TYPE_DATETIME,
#         "state": TYPE_STATE,
#         "district": TYPE_DISTRICT,
#     }
# 
#     org = models.ForeignKey(Org, verbose_name=_("Organization"), related_name="fields", on_delete=models.PROTECT)
# 
#     key = models.CharField(verbose_name=_("Key"), max_length=36)
# 
#     label = models.CharField(verbose_name=_("Label"), max_length=36, null=True)
# 
#     value_type = models.CharField(verbose_name=_("Value data type"), max_length=1, default=TYPE_TEXT)
# 
#     is_active = models.BooleanField(default=True, help_text="Whether this field is active")
# 
#     is_visible = models.BooleanField(default=False, help_text=_("Whether this field is visible to partner users"))
# 
#     @classmethod
#     def get_all(cls, org, visible=None):
#         qs = cls.objects.filter(org=org, is_active=True)
#         if visible is not None:
#             qs = qs.filter(is_visible=visible)
#         return qs
# 
#     @classmethod
#     def lock(cls, org, key):
#         return get_redis_connection().lock(FIELD_LOCK_KEY % (org.pk, key), timeout=60)
# 
#     def __str__(self):
#         return self.key
# 
#     def as_json(self):
#         """
#         Prepares a contact for JSON serialization
#         """
#         return {"key": self.key, "label": self.label, "value_type": self.value_type}
# 
#     class Meta:
#         unique_together = ("org", "key")
# 
# class Group(models.Model):
#     """
#     A contact group in RapidPro
#     """
# 
#     org = models.ForeignKey(Org, verbose_name=_("Organization"), related_name="groups", on_delete=models.PROTECT)
# 
#     uuid = models.CharField(max_length=36, unique=True)
# 
#     name = models.CharField(max_length=64)
# 
#     count = models.IntegerField(null=True)
# 
#     is_dynamic = models.BooleanField(default=False, help_text=_("Whether this group is dynamic"))
# 
#     created_on = models.DateTimeField(auto_now_add=True, help_text=_("When this group was created"))
# 
#     is_active = models.BooleanField(default=True, help_text=_("Whether this group is active"))
# 
#     is_visible = models.BooleanField(default=False, help_text=_("Whether this group is visible to partner users"))
# 
#     suspend_from = models.BooleanField(
#         default=False, help_text=_("Whether contacts should be suspended from this group during a case")
#     )
# 
#     @classmethod
#     def get_all(cls, org, visible=None, dynamic=None):
#         qs = cls.objects.filter(org=org, is_active=True)
# 
#         if visible is not None:
#             qs = qs.filter(is_visible=visible)
#         if dynamic is not None:
#             qs = qs.filter(is_dynamic=dynamic)
# 
#         return qs
# 
#     @classmethod
#     def get_suspend_from(cls, org):
#         return cls.get_all(org, dynamic=False).filter(suspend_from=True)
# 
#     @classmethod
#     def lock(cls, org, uuid):
#         return get_redis_connection().lock(GROUP_LOCK_KEY % (org.pk, uuid), timeout=60)
# 
#     def as_json(self, full=True):
#         if full:
#             return {"id": self.pk, "name": self.name, "count": self.count, "is_dynamic": self.is_dynamic}
#         else:
#             return {"id": self.pk, "name": self.name}
# 
#     def __str__(self):
#         return self.name


from dash.orgs.models import Org
from timezone_field import TimeZoneFormField
from django import forms
from django.conf import settings
from django.contrib.auth.models import User
from django.utils.translation import gettext_lazy as _
from casepro.contacts.models import Field, Group
        label=_("Suspend groups"),
        help_text=_("Groups to remove contacts from when creating cases"),
        required=False,
    )

    followup_flow = forms.ChoiceField(
        choices=(),
        label=_("Follow-up Flow"),
        help_text=_("Flow to start after a case is closed"),
        required=False,
    )

    def __init__(self, *args, **kwargs):
        org = kwargs.pop("org")
        super(OrgEditForm, self).__init__(*args, **kwargs)

        self.fields["banner_text"].initial = org.get_banner_text()

        field_choices = []
        for field in Field.objects.filter(org=org, is_active=True).order_by("label"):
            field_choices.append((field.pk, "%s (%s)" % (field.label, field.key)))

        self.fields["contact_fields"].choices = field_choices
        self.fields["contact_fields"].initial = [f.pk for f in Field.get_all(org, visible=True)]

        group_choices = []
        for group in Group.get_all(org, dynamic=False).order_by("name"):
            group_choices.append((group.pk, group.name))

        self.fields["suspend_groups"].choices = group_choices

# Path: casepro/contacts/models.py
# class Field(models.Model):
#     """
#     A custom contact field in RapidPro
#     """
# 
#     TYPE_TEXT = "T"
#     TYPE_DECIMAL = "N"
#     TYPE_DATETIME = "D"
#     TYPE_STATE = "S"
#     TYPE_DISTRICT = "I"
# 
#     TEMBA_TYPES = {
#         "text": TYPE_TEXT,
#         "numeric": TYPE_DECIMAL,
#         "datetime": TYPE_DATETIME,
#         "state": TYPE_STATE,
#         "district": TYPE_DISTRICT,
#     }
# 
#     org = models.ForeignKey(Org, verbose_name=_("Organization"), related_name="fields", on_delete=models.PROTECT)
# 
#     key = models.CharField(verbose_name=_("Key"), max_length=36)
# 
#     label = models.CharField(verbose_name=_("Label"), max_length=36, null=True)
# 
#     value_type = models.CharField(verbose_name=_("Value data type"), max_length=1, default=TYPE_TEXT)
# 
#     is_active = models.BooleanField(default=True, help_text="Whether this field is active")
# 
#     is_visible = models.BooleanField(default=False, help_text=_("Whether this field is visible to partner users"))
# 
#     @classmethod
#     def get_all(cls, org, visible=None):
#         qs = cls.objects.filter(org=org, is_active=True)
#         if visible is not None:
#             qs = qs.filter(is_visible=visible)
#         return qs
# 
#     @classmethod
#     def lock(cls, org, key):
#         return get_redis_connection().lock(FIELD_LOCK_KEY % (org.pk, key), timeout=60)
# 
#     def __str__(self):
#         return self.key
# 
#     def as_json(self):
#         """
#         Prepares a contact for JSON serialization
#         """
#         return {"key": self.key, "label": self.label, "value_type": self.value_type}
# 
#     class Meta:
#         unique_together = ("org", "key")
# 
# class Group(models.Model):
#     """
#     A contact group in RapidPro
#     """
# 
#     org = models.ForeignKey(Org, verbose_name=_("Organization"), related_name="groups", on_delete=models.PROTECT)
# 
#     uuid = models.CharField(max_length=36, unique=True)
# 
#     name = models.CharField(max_length=64)
# 
#     count = models.IntegerField(null=True)
# 
#     is_dynamic = models.BooleanField(default=False, help_text=_("Whether this group is dynamic"))
# 
#     created_on = models.DateTimeField(auto_now_add=True, help_text=_("When this group was created"))
# 
#     is_active = models.BooleanField(default=True, help_text=_("Whether this group is active"))
# 
#     is_visible = models.BooleanField(default=False, help_text=_("Whether this group is visible to partner users"))
# 
#     suspend_from = models.BooleanField(
#         default=False, help_text=_("Whether contacts should be suspended from this group during a case")
#     )
# 
#     @classmethod
#     def get_all(cls, org, visible=None, dynamic=None):
#         qs = cls.objects.filter(org=org, is_active=True)
# 
#         if visible is not None:
#             qs = qs.filter(is_visible=visible)
#         if dynamic is not None:
#             qs = qs.filter(is_dynamic=dynamic)
# 
#         return qs
# 
#     @classmethod
#     def get_suspend_from(cls, org):
#         return cls.get_all(org, dynamic=False).filter(suspend_from=True)
# 
#     @classmethod
#     def lock(cls, org, uuid):
#         return get_redis_connection().lock(GROUP_LOCK_KEY % (org.pk, uuid), timeout=60)
# 
#     def as_json(self, full=True):
#         if full:
#             return {"id": self.pk, "name": self.name, "count": self.count, "is_dynamic": self.is_dynamic}
#         else:
#             return {"id": self.pk, "name": self.name}
# 
#     def __str__(self):
#         return self.name


# Path: casepro/orgs_ext/forms.py
from dash.orgs.models import Org
from timezone_field import TimeZoneFormField
from django import forms
from django.conf import settings
from django.contrib.auth.models import User
from django.utils.translation import gettext_lazy as _
from casepro.contacts.models import Field, Group

        label=_("Suspend groups"),
        help_text=_("Groups to remove contacts from when creating cases"),
        required=False,
    )

    followup_flow = forms.ChoiceField(
        choices=(),
        label=_("Follow-up Flow"),
        help_text=_("Flow to start after a case is closed"),
        required=False,
    )

    def __init__(self, *args, **kwargs):
        org = kwargs.pop("org")
        super(OrgEditForm, self).__init__(*args, **kwargs)

        self.fields["banner_text"].initial = org.get_banner_text()

        field_choices = []
        for field in Field.objects.filter(org=org, is_active=True).order_by("label"):
            field_choices.append((field.pk, "%s (%s)" % (field.label, field.key)))

        self.fields["contact_fields"].choices = field_choices
        self.fields["contact_fields"].initial = [f.pk for f in Field.get_all(org, visible=True)]

        group_choices = []
        for group in Group.get_all(org, dynamic=False).order_by("name"):
            group_choices.append((group.pk, group.name))

        self.fields["suspend_groups"].choices = group_choices

        self.fields["suspend_groups"].initial = [g.pk for g in Group.get_suspend_from(org)]