<commit_before>from __future__ import absolute_import, unicode_literals
import sys
from django.conf import settings
from django.contrib.auth.models import User
from django.contrib.contenttypes.models import ContentType
from django.contrib.contenttypes import generic
from django.core import urlresolvers
from django.db import models
from django.utils.encoding import force_unicode
from django.utils.translation import ugettext_lazy as _

from .managers import LocationManager

try:
    from tree_select.db_fields import TreeForeignKey
except ImportError:
    TreeForeignKey = models.ForeignKey

if "notification" in settings.INSTALLED_APPS:
    from notification import models as notification
else:
    notification = None

try:
    str = unicode  # Python 2.* compatible
except NameError:
    pass

current_module = sys.modules[__name__]

CITY_TYPES = (
    ('city', _('city')),
    ('urban_village', _('urban village')),
    ('village', _('village')),
    ('settlement', _('settlement')),
    ('farm', _('farm')),
)

CITY_ABBREVIATED_TYPES = (
    ('city', _('city')),
    ('urban_village', _('urban village')),
    ('village', _('village')),
    ('settlement', _('settlement')),
    ('farm', _('farm')),
)

CITY_ABBREVIATED_TYPES_DICT = dict(CITY_ABBREVIATED_TYPES)

STREET_TYPES = (
    ('avenue', _('avenue')),
    ('street', _('street')),
    ('passage', _('passage')),
    ('highway', _('highway')),
    ('lane', _('lane')),
    ('boulevard', _('boulevard')),
    ('square', _('square')),
    ('embankment', _('embankment')),
    ('quarter', _('quarter')),
)

STREET_ABBREVIATED_TYPES = (
    ('avenue', _('ave.')),
    ('street', _('st.')),
    ('passage', _('passage')),
    ('highway', _('highway')),
    ('lane', _('lane')),
    ('boulevard', _('boulevard')),
    ('square', _('square')),
    ('embankment', _('embankment')),
    ('quarter', _('quarter')),
)

STREET_ABBREVIATED_TYPES_DICT = dict(STREET_ABBREVIATED_TYPES)

GEONAME_NONEXISTENT = 0
GEONAME_EXISTENT = 1
GEONAME_INDEPENDENT = 2

GEONAME_STATUSES = (
    (GEONAME_NONEXISTENT, _('nonexistent'), ),
    (GEONAME_EXISTENT, _('existent'), ),
    (GEONAME_INDEPENDENT, _('independent'), ),
)


# TODO: Add django-versioning here
class Location(models.Model):
    """Base location model"""
    content_type = models.ForeignKey(
        ContentType,
        editable=False,
        null=True,
        related_name="%(app_label)s_%(class)s_related"
    )
    parent = TreeForeignKey(
        'self',
        verbose_name=_("parent node"),
        blank=True,
        null=True,  # null for top level
        related_name="children"
    )
    name = models.CharField(_("official name"), max_length=255, db_index=True)
    name_ascii = models.CharField(
        _("ascii name"),
        max_length=255,
        help_text=_("latin (ascii) transliteration of name"),
        db_index=True
    )
    active = models.BooleanField(default=True, db_index=True)
    creator = models.ForeignKey(
        User,
        blank=True,
        null=True,
        on_delete=models.SET_NULL,
        related_name="%(app_label)s_%(class)s_related"
    )
    child_class = models.CharField(
        _("child class"),
        max_length=80,
        blank=True,
        db_index=True
    )
    body = models.TextField(_("text"), blank=True)
    geoname_id = models.PositiveIntegerField(
        unique=True,
        blank=True,
        null=True
    )
    geoname_status = models.PositiveSmallIntegerField(
        db_index=True,
        blank=True,
        null=True,
        default=GEONAME_NONEXISTENT,
        choices=GEONAME_STATUSES
    )

    objects = LocationManager()

    class Meta:
        ordering = ['name', ]
        unique_together = (('parent', 'name', ), ('parent', 'name_ascii', ), )
        verbose_name = _("location")
        verbose_name_plural = _("locations")

    def __str__(self):
        return self.name

    def __bytes__(self):
        return str(self).encode('utf-8')

    def save(self, *args, **kwargs):
        """Sets content_type and calls parent method."""
        if not self.content_type:
            self.content_type = ContentType.objects.get_for_model(
                self.__class__
            )
        return super(Location, self).save(*args, **kwargs)

    def get_absolute_url(self):
        return urlresolvers.reverse('geo_location_detail', args=[self.pk])

    def get_hierarchical_name(self, sep=', ', reverse=True):
        """returns children QuerySet instance for given parent_id"""
        parts = []
        current = self.get_real()
        while current.parent:
            parts.append(force_unicode(current))
            current = current.parent.get_real()
        if reverse:
            parts.reverse()
        return sep.join(parts)

    def get_ancestors(self):
        """returns ancestors"""
        parts = []
        current = self
        while current:
            current = current.get_real()
            parts.append(current)
            current = current.parent
        parts.reverse()
        parts.pop()
        return parts

    def get_children(self):
        """Fix for MTI"""
        return Location.objects.filter(parent=self)

    def _descendants(self):
        r = list(self.get_children())
        for i in r:
            r += i._descendants()
        return r

    def get_descendants(self, include_self=False):
        r = []
        if include_self:
            r.append(self)
        r += self._descendants()
        return r

    def get_real(self):  # or get_downcast(self)
        """returns instance of real class"""
        model = self.content_type.model_class()
        if model == self.__class__:
            return self
        return model.objects.get(pk=self.pk)

    def get_child_class(self):
        """Returns child class"""
        if self.child_class:
            return models.get_model(*self.child_class.rsplit('.', 1))
        return models.get_model('geo', 'country')

    def is_allowed(self, user, perm=None):
        """Checks permissions."""
        base, model = perm.rsplit("_", 1)
        model = "location"
        perm = "_".join((base, model, ))

        if perm in ('geo.view_location',
                    'geo.browse_location', ):
            return True
        if perm == 'geo.add_location':
            return False
        if perm in ('geo.change_location',
                    'geo.delete_location', ):
            return False
        return False


class Country(Location):
    """Country model"""
    iso_alpha2 = models.CharField(max_length=2, unique=True)
    iso_alpha3 = models.CharField(max_length=3, unique=True)

    # objects = LocationManager()<commit_msg>get_base()<commit_after>from __future__ import absolute_import, unicode_literals
import sys
from django.conf import settings
from django.contrib.auth.models import User
from django.contrib.contenttypes.models import ContentType
from django.contrib.contenttypes import generic
from django.core import urlresolvers
from django.db import models
from django.utils.encoding import force_unicode
from django.utils.translation import ugettext_lazy as _

from .managers import LocationManager

try:
    from tree_select.db_fields import TreeForeignKey
except ImportError:
    TreeForeignKey = models.ForeignKey

if "notification" in settings.INSTALLED_APPS:
    from notification import models as notification
else:
    notification = None

try:
    str = unicode  # Python 2.* compatible
except NameError:
    pass

current_module = sys.modules[__name__]

CITY_TYPES = (
    ('city', _('city')),
    ('urban_village', _('urban village')),
    ('village', _('village')),
    ('settlement', _('settlement')),
    ('farm', _('farm')),
)

CITY_ABBREVIATED_TYPES = (
    ('city', _('city')),
    ('urban_village', _('urban village')),
    ('village', _('village')),
    ('settlement', _('settlement')),
    ('farm', _('farm')),
)

CITY_ABBREVIATED_TYPES_DICT = dict(CITY_ABBREVIATED_TYPES)

STREET_TYPES = (
    ('avenue', _('avenue')),
    ('street', _('street')),
    ('passage', _('passage')),
    ('highway', _('highway')),
    ('lane', _('lane')),
    ('boulevard', _('boulevard')),
    ('square', _('square')),
    ('embankment', _('embankment')),
    ('quarter', _('quarter')),
)

STREET_ABBREVIATED_TYPES = (
    ('avenue', _('ave.')),
    ('street', _('st.')),
    ('passage', _('passage')),
    ('highway', _('highway')),
    ('lane', _('lane')),
    ('boulevard', _('boulevard')),
    ('square', _('square')),
    ('embankment', _('embankment')),
    ('quarter', _('quarter')),
)

STREET_ABBREVIATED_TYPES_DICT = dict(STREET_ABBREVIATED_TYPES)

GEONAME_NONEXISTENT = 0
GEONAME_EXISTENT = 1
GEONAME_INDEPENDENT = 2

GEONAME_STATUSES = (
    (GEONAME_NONEXISTENT, _('nonexistent'), ),
    (GEONAME_EXISTENT, _('existent'), ),
    (GEONAME_INDEPENDENT, _('independent'), ),
)


# TODO: Add django-versioning here
class Location(models.Model):
    """Base location model"""
    content_type = models.ForeignKey(
        ContentType,
        editable=False,
        null=True,
        related_name="%(app_label)s_%(class)s_related"
    )
    parent = TreeForeignKey(
        'self',
        verbose_name=_("parent node"),
        blank=True,
        null=True,  # null for top level
        related_name="children"
    )
    name = models.CharField(_("official name"), max_length=255, db_index=True)
    name_ascii = models.CharField(
        _("ascii name"),
        max_length=255,
        help_text=_("latin (ascii) transliteration of name"),
        db_index=True
    )
    active = models.BooleanField(default=True, db_index=True)
    creator = models.ForeignKey(
        User,
        blank=True,
        null=True,
        on_delete=models.SET_NULL,
        related_name="%(app_label)s_%(class)s_related"
    )
    child_class = models.CharField(
        _("child class"),
        max_length=80,
        blank=True,
        db_index=True
    )
    body = models.TextField(_("text"), blank=True)
    geoname_id = models.PositiveIntegerField(
        unique=True,
        blank=True,
        null=True
    )
    geoname_status = models.PositiveSmallIntegerField(
        db_index=True,
        blank=True,
        null=True,
        default=GEONAME_NONEXISTENT,
        choices=GEONAME_STATUSES
    )

    objects = LocationManager()

    class Meta:
        ordering = ['name', ]
        unique_together = (('parent', 'name', ), ('parent', 'name_ascii', ), )
        verbose_name = _("location")
        verbose_name_plural = _("locations")

    def __str__(self):
        return self.name

    def __bytes__(self):
        return str(self).encode('utf-8')

    def save(self, *args, **kwargs):
        """Sets content_type and calls parent method."""
        if not self.content_type:
            self.content_type = ContentType.objects.get_for_model(
                self.__class__
            )
        return super(Location, self).save(*args, **kwargs)

    def delete(self, *a, **kw):
        """Disallows to delete subclass instance without Location instance."""
        return super(Location, self.get_base()).delete(*a, **kw)

    def get_absolute_url(self):
        return urlresolvers.reverse('geo_location_detail', args=[self.pk])

    def get_hierarchical_name(self, sep=', ', reverse=True):
        """returns children QuerySet instance for given parent_id"""
        parts = []
        current = self.get_real()
        while current.parent:
            parts.append(force_unicode(current))
            current = current.parent.get_real()
        if reverse:
            parts.reverse()
        return sep.join(parts)

    def get_ancestors(self):
        """returns ancestors"""
        parts = []
        current = self
        while current:
            current = current.get_real()
            parts.append(current)
            current = current.parent
        parts.reverse()
        parts.pop()
        return parts

    def get_children(self):
        """Fix for MTI"""
        return Location.objects.filter(parent=self)

    def _descendants(self):
        r = list(self.get_children())
        for i in r:
            r += i._descendants()
        return r

    def get_descendants(self, include_self=False):
        r = []
        if include_self:
            r.append(self)
        r += self._descendants()
        return r

    def get_real(self):  # or get_downcast(self)
        """returns instance of real class"""
        model = self.content_type.model_class()
        if model != type(self):
            try:
                return model.objects.get(pk=self.pk)
            except model.DoesNotExist:
                pass
        return self

    def get_base(self):
        """Returns Location instance"""
        if type(self) != Location:
            return Location._base_manager.get(pk=self.pk)
        return self

    def get_child_class(self):
        """Returns child class"""
        if self.child_class:
            return models.get_model(*self.child_class.rsplit('.', 1))
        return models.get_model('geo', 'country')

    def is_allowed(self, user, perm=None):
        """Checks permissions."""
        base, model = perm.rsplit("_", 1)
        model = "location"
        perm = "_".join((base, model, ))

        if perm in ('geo.view_location',
                    'geo.browse_location', ):
            return True
        if perm == 'geo.add_location':
            return False
        if perm in ('geo.change_location',
                    'geo.delete_location', ):
            return False
        return False


class Country(Location):
    """Country model"""
    iso_alpha2 = models.CharField(max_length=2, unique=True)
    iso_alpha3 = models.CharField(max_length=3, unique=True)

    # objects = LocationManager()
{'fraction_non_alphanumeric': 0.13484947882498724, 'fraction_numerical': 0.0027698811866754135, 'mean_word_length': 2.6941303177167475, '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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3043668', 'n_tokens_mistral': 4385, 'n_tokens_neox': 4204, 'n_words': 891}