<commit_before>from django.db import models, connections, transaction
from NACO import normalizeSimplified
from django.core.exceptions import ValidationError
import requests
try:
    # the json module was included in the stdlib in python 2.6
    # http://docs.python.org/library/json.html
    import json
except ImportError:
    # simplejson 2.0.9 is available for python 2.4+
    # http://pypi.python.org/pypi/simplejson/2.0.9
    # simplejson 1.7.3 is available for python 2.3+
    # http://pypi.python.org/pypi/simplejson/1.7.3
    import simplejson as json

NOTE_TYPE_CHOICES = (
    (0, 'Biographical/Historical'),
    (1, 'Deletion Information'),
    (2, 'Nonpublic'),
    (3, 'Source'),
    (4, 'Other'),
)
RECORD_STATUS_CHOICES = (
    (0, 'Active'),
    (1, 'Deleted'),
    (2, 'Suppressed'),
)
NAME_TYPE_CHOICES = (
    (0, 'Personal'),
    (1, 'Organization'),
    (2, 'Event'),
    (3, 'Software'),
    (4, 'Building'),
)
DATE_DISPLAY_LABELS = {
    0: {
        'type': 'Personal',
        'begin': 'Date of Birth',
        'end': 'Date of Death'
    },
    1: {
        'type': 'Organization',
        'begin': 'Founded Date',
        'end': 'Defunct'
    },
    2: {
        'type': 'Event',
        'begin': 'Begin Date',
        'end': 'End Date'
    },
    3: {
        'type': 'Software',
        'begin': 'Begin Date',
        'end': 'End Date'
    },
    4: {
        'type': 'Building',
        'begin': 'Erected Date',
        'end': 'Demolished Date',
    },
    None: {
        'type': None,
        'begin': 'Born/Founded Date',
        'end': 'Died/Defunct Date'
    },
}
VARIANT_TYPE_CHOICES = (
    (0, 'Acronym'),
    (1, 'Abbreviation'),
    (2, 'Translation'),
    (3, 'Expansion'),
    (4, 'Other'),
)
LOCATION_STATUS_CHOICES = (
    (0, "current"),
    (1, "former"),
)


class Identifier_Type(models.Model):
    label = models.CharField(
        max_length=255,
        help_text="What kind of data is this? Personal website? Twitter?",
    )
    icon_path = models.CharField(
        max_length=255,
        blank=True,
        help_text="Path to icon image?",
    )
    homepage = models.URLField(
        blank=True,
        help_text="Homepage of label. Twitter.com, Facebook.com, etc",
    )

    class Meta:
        ordering = ["label"]
        verbose_name = "Identifier Type"

    def __unicode__(self):
        return self.label


class Identifier(models.Model):
    type = models.ForeignKey(
        "Identifier_Type",
        help_text="Catagorize this record's identifiers here",
    )
    belong_to_name = models.ForeignKey("Name")
    value = models.CharField(max_length=500)
    visible = models.BooleanField(default=True)
    order = models.IntegerField(default=0)

    class Meta:
        ordering = ["order", "type"]

    def __unicode__(self):
        return self.value


class Note(models.Model):
    note = models.TextField(help_text="Enter notes about this record here")
    note_type = models.IntegerField(choices=NOTE_TYPE_CHOICES)
    belong_to_name = models.ForeignKey("Name")

    def __unicode__(self):
        return self.note


class Variant(models.Model):
    belong_to_name = models.ForeignKey("Name")
    variant_type = models.IntegerField(
        max_length=50,
        choices=VARIANT_TYPE_CHOICES,
        help_text="Choose variant type.",
    )
    variant = models.CharField(
        max_length=255,
        help_text="Fill in the other name variants, if any.",
    )
    normalized_variant = models.CharField(
        max_length=255,
        help_text="NACO normalized variant text",
        editable=False,
    )

    def save(self):

        self.normalized_variant = normalizeSimplified(self.variant)
        super(Variant, self).save()

    def __unicode__(self):
        return self.variant


class TicketingManager(models.Manager):

    def create(self):

        # create the connection to the DB
        cursor = connections[self.db].cursor()

        # update and replace the stub (should autoincrement id)
        sql = "REPLACE INTO `%s` " % (self.model._meta.db_table)
        sql += "(`stub`) VALUES (%s)"

        result = cursor.execute(sql, [self.model.STUB_DEFAULT])
        transaction.commit_unless_managed(using=self.db)

        # return id (last row value) to the BaseTicketing model
        return self.model(id=cursor.lastrowid, stub=self.model.STUB_DEFAULT)


class BaseTicketing(models.Model):

    # Explicitly set the id of the model, even though it is the same
    # as the one Django gives it.
    id = models.AutoField(null=False, primary_key=True)

    # This is just the smallest placeholder we can create that we can
    # replace into to generate a new id.
    STUB_DEFAULT = True
    stub = models.BooleanField(null=False, default=STUB_DEFAULT, unique=True)

    # Override the default manager
    objects = TicketingManager()

    @property
    def ticket(self):
        """Alias for id"""
        return self.id<commit_msg>Use the manager methods instead of raw sql.<commit_after>from django.db import models, connections, transaction
from NACO import normalizeSimplified
from django.core.exceptions import ValidationError
import requests
try:
    # the json module was included in the stdlib in python 2.6
    # http://docs.python.org/library/json.html
    import json
except ImportError:
    # simplejson 2.0.9 is available for python 2.4+
    # http://pypi.python.org/pypi/simplejson/2.0.9
    # simplejson 1.7.3 is available for python 2.3+
    # http://pypi.python.org/pypi/simplejson/1.7.3
    import simplejson as json

NOTE_TYPE_CHOICES = (
    (0, 'Biographical/Historical'),
    (1, 'Deletion Information'),
    (2, 'Nonpublic'),
    (3, 'Source'),
    (4, 'Other'),
)
RECORD_STATUS_CHOICES = (
    (0, 'Active'),
    (1, 'Deleted'),
    (2, 'Suppressed'),
)
NAME_TYPE_CHOICES = (
    (0, 'Personal'),
    (1, 'Organization'),
    (2, 'Event'),
    (3, 'Software'),
    (4, 'Building'),
)
DATE_DISPLAY_LABELS = {
    0: {
        'type': 'Personal',
        'begin': 'Date of Birth',
        'end': 'Date of Death'
    },
    1: {
        'type': 'Organization',
        'begin': 'Founded Date',
        'end': 'Defunct'
    },
    2: {
        'type': 'Event',
        'begin': 'Begin Date',
        'end': 'End Date'
    },
    3: {
        'type': 'Software',
        'begin': 'Begin Date',
        'end': 'End Date'
    },
    4: {
        'type': 'Building',
        'begin': 'Erected Date',
        'end': 'Demolished Date',
    },
    None: {
        'type': None,
        'begin': 'Born/Founded Date',
        'end': 'Died/Defunct Date'
    },
}
VARIANT_TYPE_CHOICES = (
    (0, 'Acronym'),
    (1, 'Abbreviation'),
    (2, 'Translation'),
    (3, 'Expansion'),
    (4, 'Other'),
)
LOCATION_STATUS_CHOICES = (
    (0, "current"),
    (1, "former"),
)


class Identifier_Type(models.Model):
    label = models.CharField(
        max_length=255,
        help_text="What kind of data is this? Personal website? Twitter?",
    )
    icon_path = models.CharField(
        max_length=255,
        blank=True,
        help_text="Path to icon image?",
    )
    homepage = models.URLField(
        blank=True,
        help_text="Homepage of label. Twitter.com, Facebook.com, etc",
    )

    class Meta:
        ordering = ["label"]
        verbose_name = "Identifier Type"

    def __unicode__(self):
        return self.label


class Identifier(models.Model):
    type = models.ForeignKey(
        "Identifier_Type",
        help_text="Catagorize this record's identifiers here",
    )
    belong_to_name = models.ForeignKey("Name")
    value = models.CharField(max_length=500)
    visible = models.BooleanField(default=True)
    order = models.IntegerField(default=0)

    class Meta:
        ordering = ["order", "type"]

    def __unicode__(self):
        return self.value


class Note(models.Model):
    note = models.TextField(help_text="Enter notes about this record here")
    note_type = models.IntegerField(choices=NOTE_TYPE_CHOICES)
    belong_to_name = models.ForeignKey("Name")

    def __unicode__(self):
        return self.note


class Variant(models.Model):
    belong_to_name = models.ForeignKey("Name")
    variant_type = models.IntegerField(
        max_length=50,
        choices=VARIANT_TYPE_CHOICES,
        help_text="Choose variant type.",
    )
    variant = models.CharField(
        max_length=255,
        help_text="Fill in the other name variants, if any.",
    )
    normalized_variant = models.CharField(
        max_length=255,
        help_text="NACO normalized variant text",
        editable=False,
    )

    def save(self):

        self.normalized_variant = normalizeSimplified(self.variant)
        super(Variant, self).save()

    def __unicode__(self):
        return self.variant


class TicketingManager(models.Manager):
    def create(self, *args, **kwargs):

        (obj, created) = self.get_or_create(stub=self.model.STUB_DEFAULT)
        if not created:
            with transaction.atomic():
                obj.delete()
                obj = self.create(stub=self.model.STUB_DEFAULT)
        return obj


class BaseTicketing(models.Model):

    # Explicitly set the id of the model, even though it is the same
    # as the one Django gives it.
    id = models.AutoField(null=False, primary_key=True)

    # This is just the smallest placeholder we can create that we can
    # replace into to generate a new id.
    STUB_DEFAULT = True
    stub = models.BooleanField(null=False, default=STUB_DEFAULT, unique=True)

    # Override the default manager
    objects = TicketingManager()

    @property
    def ticket(self):
        """Alias for id"""
        return self.id
{'fraction_non_alphanumeric': 0.12720298569355173, 'fraction_numerical': 0.012647729628861704, 'mean_word_length': 2.618529632408102, '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': '2431432', 'n_tokens_mistral': 3166, 'n_tokens_neox': 3019, 'n_words': 758}