<commit_before>from __future__ import unicode_literals

from django.db import models
from django.utils.encoding import python_2_unicode_compatible
from django.utils.translation import ugettext_lazy as _
from redis_cache import get_redis_connection
from temba_client.v2.types import Contact as TembaContact, ObjectRef as TembaObjectRef

SAVE_GROUPS_ATTR = '__data__groups'
SAVE_FIELDS_ATTR = '__data__fields'

CONTACT_LOCK_KEY = 'contact-lock:%s'


@python_2_unicode_compatible
class Group(models.Model):
    """
    A contact group in RapidPro
    """
    org = models.ForeignKey('orgs.Org', verbose_name=_("Organization"), related_name="new_groups")

    uuid = models.CharField(max_length=36, unique=True)

    name = models.CharField(max_length=64)

    count = models.IntegerField(null=True)

    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 create(cls, org, uuid, name):
        return cls.objects.create(org=org, uuid=uuid, name=name)

    @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 get_suspend_from(cls, org):
        return cls.get_all(org).filter(suspend_from=True)

    @classmethod
    def sync_identity(cls, instance):
        return instance.uuid

    @classmethod
    def sync_get_kwargs(cls, org, incoming):
        return {
            'org': org,
            'uuid': incoming.uuid,
            'name': incoming.name,
            'count': incoming.count,
        }

    @classmethod
    def sync_update_required(cls, local, incoming):
        return local.name != incoming.name or local.count != incoming.count

    def as_json(self):
        return {'id': self.pk, 'uuid': self.uuid, 'name': self.name, 'count': self.count}

    def __str__(self):
        return self.name


@python_2_unicode_compatible
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,
                   'decimal': TYPE_DECIMAL,
                   'datetime': TYPE_DATETIME,
                   'state': TYPE_STATE,
                   'district': TYPE_DISTRICT}

    org = models.ForeignKey('orgs.Org', verbose_name=_("Organization"), related_name="fields")

    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")

    @classmethod
    def create(cls, org, key, label=None):
        return cls.objects.create(org=org, key=key, label=label)

    @classmethod
    def sync_identity(cls, instance):
        return instance.key<commit_msg>Fixed wrong datatype code for decimal contact fields<commit_after>from __future__ import unicode_literals

from django.db import models
from django.utils.encoding import python_2_unicode_compatible
from django.utils.translation import ugettext_lazy as _
from redis_cache import get_redis_connection
from temba_client.v2.types import Contact as TembaContact, ObjectRef as TembaObjectRef

SAVE_GROUPS_ATTR = '__data__groups'
SAVE_FIELDS_ATTR = '__data__fields'

CONTACT_LOCK_KEY = 'contact-lock:%s'


@python_2_unicode_compatible
class Group(models.Model):
    """
    A contact group in RapidPro
    """
    org = models.ForeignKey('orgs.Org', verbose_name=_("Organization"), related_name="new_groups")

    uuid = models.CharField(max_length=36, unique=True)

    name = models.CharField(max_length=64)

    count = models.IntegerField(null=True)

    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 create(cls, org, uuid, name):
        return cls.objects.create(org=org, uuid=uuid, name=name)

    @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 get_suspend_from(cls, org):
        return cls.get_all(org).filter(suspend_from=True)

    @classmethod
    def sync_identity(cls, instance):
        return instance.uuid

    @classmethod
    def sync_get_kwargs(cls, org, incoming):
        return {
            'org': org,
            'uuid': incoming.uuid,
            'name': incoming.name,
            'count': incoming.count,
        }

    @classmethod
    def sync_update_required(cls, local, incoming):
        return local.name != incoming.name or local.count != incoming.count

    def as_json(self):
        return {'id': self.pk, 'uuid': self.uuid, 'name': self.name, 'count': self.count}

    def __str__(self):
        return self.name


@python_2_unicode_compatible
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('orgs.Org', verbose_name=_("Organization"), related_name="fields")

    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")

    @classmethod
    def create(cls, org, key, label=None):
        return cls.objects.create(org=org, key=key, label=label)

    @classmethod
    def sync_identity(cls, instance):
        return instance.key
{'fraction_non_alphanumeric': 0.10670511896178803, 'fraction_numerical': 0.003749098774333093, 'mean_word_length': 3.30272952853598, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2613260', 'n_tokens_mistral': 2170, 'n_tokens_neox': 2107, 'n_words': 488}