<commit_before>from datetime import date

from django.db import models

from smart_selects.db_fields import ChainedForeignKey


class ExternalCRMManager(models.Manager):
    """Lets one query remote views on behalf of CRM user

    The inspiration has been gained from:
    :seealso: http://stackoverflow.com/a/28222392
    """

    def for_user(self, crm_user_id):
        """Hacks setting @user SQL variable needed for user-personalized queries"""
        assert isinstance(crm_user_id, int)
        qs = self.get_queryset()
        try:
            next(iter(qs.raw('set @user:={user_id}'.format(user_id=crm_user_id))))
        except TypeError:
            pass  # hack for pre-setting SQL variable before query
        return qs


class Area(models.Model):
    """Describes the area where electors live"""
    area_id = models.IntegerField(unique=True, primary_key=True)
    area_name = models.CharField(null=True, max_length=250)
    region = models.ForeignKey('Region', to_field='region_id', on_delete=models.DO_NOTHING, null=True)

    objects = ExternalCRMManager()

    def __str__(self):
        return '{}'.format(self.area_name)

    class Meta:
        db_route = 'external_app'
        managed = False
        db_table = 'DIR_AREAS'


class Building(models.Model):
    """Describes the building"""
    building_id = models.IntegerField(unique=True, primary_key=True)
    building_number = models.CharField(null=True, max_length=20)
    street = models.ForeignKey('Street', to_field='street_id', on_delete=models.DO_NOTHING)

    objects = ExternalCRMManager()

    def __str__(self):
        return '{}'.format(self.building_number)

    class Meta:
        db_route = 'external_app'
        managed = False
        db_table = 'DIR_BUILDINGS'


class Region(models.Model):
    """Describes the region where electors live"""
    region_id = models.IntegerField(unique=True, primary_key=True)
    region_name = models.CharField(max_length=250)

    objects = ExternalCRMManager()

    def __str__(self):
        return '{}'.format(self.region_name)

    class Meta:
        db_route = 'external_app'
        managed = False
        db_table = 'sms_view_regions'


class Locality(models.Model):
    """Describes the locality where electors live"""
    locality_id = models.IntegerField(unique=True, primary_key=True)
    locality_name = models.CharField(max_length=56)
    area = models.ForeignKey('Area', to_field='area_id', on_delete=models.DO_NOTHING)

    objects = ExternalCRMManager()

    def __str__(self):
        return '{}'.format(self.locality_name)

    class Meta:
        db_route = 'external_app'
        managed = False
        db_table = 'DIR_LOCALITIES'
        # unique_together = ('area_id', 'locality_id')


class Street(models.Model):
    """Describes the area where electors live"""
    street_id = models.IntegerField(unique=True, primary_key=True)
    street_name = models.CharField(max_length=500)
    locality = models.ForeignKey('Locality', to_field='locality_id', on_delete=models.DO_NOTHING)

    objects = ExternalCRMManager()

    def __str__(self):
        return '{}'.format(self.street_name)

    class Meta:
        db_route = 'external_app'
        managed = False
        db_table = 'DIR_STREETS'
        # unique_together = ('street_id', 'locality_id')


class Project(models.Model):
    """Describes the project in terms of which the elector is contacted"""
    project_id = models.IntegerField(unique=True, primary_key=True)
    project_name = models.CharField(max_length=255)

    objects = ExternalCRMManager()

    def __str__(self):
        return '{}'.format(self.project_name)

    class Meta:
        db_route = 'external_app'
        managed = False
        db_table = 'DB_PROJECTS'


class ProjectContact(models.Model):
    """Describes the contacts in project"""
    contact_id = models.IntegerField(unique=True, primary_key=True)
    contact_name = models.CharField(max_length=255)
    contact_project = models.ForeignKey('Project', to_field='project_id', on_delete=models.DO_NOTHING)

    objects = ExternalCRMManager()

    def __str__(self):
        return '{}'.format(self.contact_name)

    class Meta:
        db_route = 'external_app'
        managed = False
        db_table = 'DB_CONTACTS'
        # unique_together = ('contact_id', 'project_id')


class FollowerContact(models.Model):
    """Describes the contact with elector"""
    id = models.IntegerField(unique=True, primary_key=True)
    contact_date = models.DateField(null=True)
    follower = models.ForeignKey('Follower', to_field='follower_id', on_delete=models.DO_NOTHING)
    contact = models.ForeignKey('ProjectContact', to_field='contact_id', on_delete=models.DO_NOTHING, null=True)
    follower_status = models.ForeignKey('FollowerStatus', to_field='follower_status_id', on_delete=models.DO_NOTHING,
                                        null=True)

    def __str__(self):
        return '{} {}, контакт {} {}'.format(self.follower.lastname, self.follower.firstname, self.contact_date,
                                             self.contact.contact_name)

    objects = ExternalCRMManager()

    class Meta:
        db_route = 'external_app'
        managed = False
        db_table = 'DB_FOLLOWER_CONTACTS'
        # unique_together = ('id', 'contact_date', 'follower_id', 'contact_id')


class Candidate(models.Model):
    """Describes the elections candidate"""
    candidate_id = models.IntegerField(unique=True, primary_key=True)
    candidate_name = models.CharField(max_length=250)

    objects = ExternalCRMManager()

    def __str__(self):
        return '{}'.format(self.candidate_name)

    class Meta:
        db_route = 'external_app'<commit_msg>Make model columns correspond DB columns<commit_after>from datetime import date

from django.db import models

from smart_selects.db_fields import ChainedForeignKey


class ExternalCRMManager(models.Manager):
    """Lets one query remote views on behalf of CRM user

    The inspiration has been gained from:
    :seealso: http://stackoverflow.com/a/28222392
    """

    def for_user(self, crm_user_id):
        """Hacks setting @user SQL variable needed for user-personalized queries"""
        assert isinstance(crm_user_id, int)
        qs = self.get_queryset()
        try:
            next(iter(qs.raw('set @user:={user_id}'.format(user_id=crm_user_id))))
        except TypeError:
            pass  # hack for pre-setting SQL variable before query
        return qs


class Area(models.Model):
    """Describes the area where electors live"""
    area_id = models.IntegerField(unique=True, primary_key=True)
    area_name = models.CharField(null=True, max_length=250)
    region = models.ForeignKey('Region', to_field='region_id', on_delete=models.DO_NOTHING, null=True)

    objects = ExternalCRMManager()

    def __str__(self):
        return '{}'.format(self.area_name)

    class Meta:
        db_route = 'external_app'
        managed = False
        db_table = 'DIR_AREAS'


class Building(models.Model):
    """Describes the building"""
    building_id = models.IntegerField(unique=True, primary_key=True)
    building_number = models.CharField(null=True, max_length=20)
    street = models.ForeignKey('Street', to_field='street_id', on_delete=models.DO_NOTHING)

    objects = ExternalCRMManager()

    def __str__(self):
        return '{}'.format(self.building_number)

    class Meta:
        db_route = 'external_app'
        managed = False
        db_table = 'DIR_BUILDINGS'


class Region(models.Model):
    """Describes the region where electors live"""
    region_id = models.IntegerField(unique=True, primary_key=True)
    region_name = models.CharField(max_length=250)

    objects = ExternalCRMManager()

    def __str__(self):
        return '{}'.format(self.region_name)

    class Meta:
        db_route = 'external_app'
        managed = False
        db_table = 'sms_view_regions'


class Locality(models.Model):
    """Describes the locality where electors live"""
    locality_id = models.IntegerField(unique=True, primary_key=True)
    locality_name = models.CharField(max_length=56)
    area = models.ForeignKey('Area', to_field='area_id', on_delete=models.DO_NOTHING)

    objects = ExternalCRMManager()

    def __str__(self):
        return '{}'.format(self.locality_name)

    class Meta:
        db_route = 'external_app'
        managed = False
        db_table = 'DIR_LOCALITIES'
        # unique_together = ('area_id', 'locality_id')


class Street(models.Model):
    """Describes the area where electors live"""
    street_id = models.IntegerField(unique=True, primary_key=True)
    street_name = models.CharField(max_length=500)
    locality = models.ForeignKey('Locality', to_field='locality_id', on_delete=models.DO_NOTHING)

    objects = ExternalCRMManager()

    def __str__(self):
        return '{}'.format(self.street_name)

    class Meta:
        db_route = 'external_app'
        managed = False
        db_table = 'DIR_STREETS'
        # unique_together = ('street_id', 'locality_id')


class Project(models.Model):
    """Describes the project in terms of which the elector is contacted"""
    project_id = models.IntegerField(unique=True, primary_key=True)
    project_name = models.CharField(max_length=255)

    objects = ExternalCRMManager()

    def __str__(self):
        return '{}'.format(self.project_name)

    class Meta:
        db_route = 'external_app'
        managed = False
        db_table = 'DB_PROJECTS'


class ProjectContact(models.Model):
    """Describes the contacts in project"""
    contact_id = models.IntegerField(unique=True, primary_key=True)
    contact_name = models.CharField(max_length=255)
    contact_project = models.ForeignKey('Project', to_field='project_id', on_delete=models.DO_NOTHING)

    objects = ExternalCRMManager()

    def __str__(self):
        return '{}'.format(self.contact_name)

    class Meta:
        db_route = 'external_app'
        managed = False
        db_table = 'DB_CONTACTS'
        # unique_together = ('contact_id', 'project_id')


class FollowerContact(models.Model):
    """Describes the contact with elector"""
    id = models.IntegerField(db_column='follower_contact_id', unique=True, primary_key=True)
    contact_date = models.DateField(db_column='follower_contact_date', null=True)
    follower = models.ForeignKey('Follower', to_field='follower_id', on_delete=models.DO_NOTHING)
    contact = models.ForeignKey('ProjectContact', to_field='contact_id',
                                db_column='FOLLOWER_CONTACT_PROJECT_CONTACT_ID',
                                on_delete=models.DO_NOTHING, null=True)
    follower_status = models.ForeignKey('FollowerStatus', to_field='follower_status_id', on_delete=models.DO_NOTHING,
                                        null=True, db_column='FOLLOWER_CONTACT_STATUS_ID')

    def __str__(self):
        return '{} {}, контакт {} {}'.format(self.follower.lastname, self.follower.firstname, self.contact_date,
                                             self.contact.contact_name)

    objects = ExternalCRMManager()

    class Meta:
        db_route = 'external_app'
        managed = False
        db_table = 'DB_FOLLOWER_CONTACTS'
        # unique_together = ('id', 'contact_date', 'follower_id', 'contact_id')


class Candidate(models.Model):
    """Describes the elections candidate"""
    candidate_id = models.IntegerField(unique=True, primary_key=True)
    candidate_name = models.CharField(max_length=250)

    objects = ExternalCRMManager()

    def __str__(self):
        return '{}'.format(self.candidate_name)

    class Meta:
        db_route = 'external_app'
{'fraction_non_alphanumeric': 0.10928118030536971, 'fraction_numerical': 0.0051466803911477095, 'mean_word_length': 3.392991710625471, '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': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5912464', 'n_tokens_mistral': 3703, 'n_tokens_neox': 3516, 'n_words': 720}