<commit_before>"""

   Zoning models
   (not MapEntity : just layers, on which intersections with objects is done in triggers)

"""
from operator import attrgetter

from django.contrib.gis.db import models
from django.conf import settings
from django.utils.translation import ugettext_lazy as _

from geotrek.common.utils import uniquify, intersecting
from geotrek.core.models import Topology, Path
from geotrek.maintenance.models import Intervention, Project
from geotrek.tourism.models import TouristicContent, TouristicEvent


class RestrictedAreaType(models.Model):
    name = models.CharField(max_length=200, verbose_name=_(u"Name"), db_column='nom')

    class Meta:
        db_table = 'f_b_zonage'
        verbose_name = _(u"Restricted area type")

    def __unicode__(self):
        return self.name


class RestrictedAreaManager(models.GeoManager):
    def get_queryset(self):
        return super(RestrictedAreaManager, self).get_queryset().select_related('area_type')


class RestrictedArea(models.Model):
    name = models.CharField(max_length=250, db_column='zonage', verbose_name=_(u"Name"))
    geom = models.MultiPolygonField(srid=settings.SRID, spatial_index=False)
    area_type = models.ForeignKey(RestrictedAreaType, verbose_name=_(u"Restricted area"), db_column='type')

    # Override default manager
    objects = RestrictedAreaManager()

    class Meta:
        ordering = ['area_type', 'name']
        db_table = 'l_zonage_reglementaire'
        verbose_name = _(u"Restricted area")
        verbose_name_plural = _(u"Restricted areas")

    def __unicode__(self):
        return "%s - %s" % (self.area_type.name, self.name)


class RestrictedAreaEdge(Topology):
    topo_object = models.OneToOneField(Topology, parent_link=True,
                                       db_column='evenement')
    restricted_area = models.ForeignKey(RestrictedArea, verbose_name=_(u"Restricted area"),
                                        db_column='zone')

    # Override default manager
    objects = Topology.get_manager_cls(models.GeoManager)()

    class Meta:
        db_table = 'f_t_zonage'
        verbose_name = _(u"Restricted area edge")
        verbose_name_plural = _(u"Restricted area edges")

    def __unicode__(self):
        return _(u"Restricted area edge") + u": %s" % self.restricted_area

    @classmethod
    def path_area_edges(cls, path):
        return cls.objects.existing()\
                          .select_related('restricted_area')\
                          .select_related('restricted_area__area_type')\
                          .filter(aggregations__path=path).distinct('pk')

    @classmethod
    def topology_area_edges(cls, topology):
        return cls.overlapping(topology)\
                  .select_related('restricted_area')\
                  .select_related('restricted_area__area_type')


if settings.TREKKING_TOPOLOGY_ENABLED:
    Path.add_property('area_edges', RestrictedAreaEdge.path_area_edges, _(u"Restricted area edges"))
    Path.add_property('areas', lambda self: uniquify(intersecting(RestrictedArea, self, distance=0), self.area_edges) if self.topology.ispoint() else uniquify(map(attrgetter('restricted_area'))),
                      _(u"Restricted areas"))
    Topology.add_property('area_edges', RestrictedAreaEdge.topology_area_edges, _(u"Restricted area edges"))
    Topology.add_property('areas', lambda self: uniquify(intersecting(RestrictedArea, self)) if self.is_point() else uniquify(map(attrgetter('restricted_area'), self.area_edges)),
                          _(u"Restricted areas"))
    Intervention.add_property('area_edges', lambda self: self.topology.area_edges if self.topology else [],
                              _(u"Restricted area edges"))
    Intervention.add_property('areas', lambda self: self.topology.areas if self.topology else [],
                              _(u"Restricted areas"))
    Project.add_property('area_edges', lambda self: self.edges_by_attr('area_edges'), _(u"Restricted area edges"))
    Project.add_property('areas', lambda self: uniquify(map(attrgetter('restricted_area'), self.area_edges)),
                         _(u"Restricted areas"))
else:
    Topology.add_property('areas', lambda self: uniquify(intersecting(RestrictedArea, self, distance=0)),
                          _(u"Restricted areas"))

TouristicContent.add_property('areas', lambda self: intersecting(RestrictedArea, self, distance=0),
                              _(u"Restricted areas"))
TouristicEvent.add_property('areas', lambda self: intersecting(RestrictedArea, self, distance=0),
                            _(u"Restricted areas"))


class City(models.Model):
    code = models.CharField(primary_key=True, max_length=6, db_column='insee')
    name = models.CharField(max_length=128, db_column='commune', verbose_name=_(u"Name"))
    geom = models.MultiPolygonField(srid=settings.SRID, spatial_index=False)<commit_msg>fix zoning intersection part 1<commit_after>"""

   Zoning models
   (not MapEntity : just layers, on which intersections with objects is done in triggers)

"""
from operator import attrgetter

from django.contrib.gis.db import models
from django.conf import settings
from django.utils.translation import ugettext_lazy as _

from geotrek.common.utils import uniquify, intersecting
from geotrek.core.models import Topology, Path
from geotrek.maintenance.models import Intervention, Project
from geotrek.tourism.models import TouristicContent, TouristicEvent


class RestrictedAreaType(models.Model):
    name = models.CharField(max_length=200, verbose_name=_(u"Name"), db_column='nom')

    class Meta:
        db_table = 'f_b_zonage'
        verbose_name = _(u"Restricted area type")

    def __unicode__(self):
        return self.name


class RestrictedAreaManager(models.GeoManager):
    def get_queryset(self):
        return super(RestrictedAreaManager, self).get_queryset().select_related('area_type')


class RestrictedArea(models.Model):
    name = models.CharField(max_length=250, db_column='zonage', verbose_name=_(u"Name"))
    geom = models.MultiPolygonField(srid=settings.SRID, spatial_index=False)
    area_type = models.ForeignKey(RestrictedAreaType, verbose_name=_(u"Restricted area"), db_column='type')

    # Override default manager
    objects = RestrictedAreaManager()

    class Meta:
        ordering = ['area_type', 'name']
        db_table = 'l_zonage_reglementaire'
        verbose_name = _(u"Restricted area")
        verbose_name_plural = _(u"Restricted areas")

    def __unicode__(self):
        return "%s - %s" % (self.area_type.name, self.name)


class RestrictedAreaEdge(Topology):
    topo_object = models.OneToOneField(Topology, parent_link=True,
                                       db_column='evenement')
    restricted_area = models.ForeignKey(RestrictedArea, verbose_name=_(u"Restricted area"),
                                        db_column='zone')

    # Override default manager
    objects = Topology.get_manager_cls(models.GeoManager)()

    class Meta:
        db_table = 'f_t_zonage'
        verbose_name = _(u"Restricted area edge")
        verbose_name_plural = _(u"Restricted area edges")

    def __unicode__(self):
        return _(u"Restricted area edge") + u": %s" % self.restricted_area

    @classmethod
    def path_area_edges(cls, path):
        return cls.objects.existing()\
                          .select_related('restricted_area')\
                          .select_related('restricted_area__area_type')\
                          .filter(aggregations__path=path).distinct('pk')

    @classmethod
    def topology_area_edges(cls, topology):
        return cls.overlapping(topology)\
                  .select_related('restricted_area')\
                  .select_related('restricted_area__area_type')


if settings.TREKKING_TOPOLOGY_ENABLED:
    Path.add_property('area_edges', RestrictedAreaEdge.path_area_edges, _(u"Restricted area edges"))
    Path.add_property('areas', lambda self: uniquify(intersecting(RestrictedArea, self, distance=0), self.area_edges) if self.topology.ispoint() else uniquify(map(attrgetter('restricted_area'))),
                      _(u"Restricted areas"))
    Topology.add_property('area_edges', RestrictedAreaEdge.topology_area_edges, _(u"Restricted area edges"))
    Topology.add_property('areas', lambda self: uniquify(intersecting(RestrictedArea, self)) if self.topology.is_point() else uniquify(map(attrgetter('restricted_area'), self.area_edges)),
                          _(u"Restricted areas"))
    Intervention.add_property('area_edges', lambda self: self.topology.area_edges if self.topology else [],
                              _(u"Restricted area edges"))
    Intervention.add_property('areas', lambda self: self.topology.areas if self.topology else [],
                              _(u"Restricted areas"))
    Project.add_property('area_edges', lambda self: self.edges_by_attr('area_edges'), _(u"Restricted area edges"))
    Project.add_property('areas', lambda self: uniquify(map(attrgetter('restricted_area'), self.area_edges)),
                         _(u"Restricted areas"))
else:
    Topology.add_property('areas', lambda self: uniquify(intersecting(RestrictedArea, self, distance=0)),
                          _(u"Restricted areas"))

TouristicContent.add_property('areas', lambda self: intersecting(RestrictedArea, self, distance=0),
                              _(u"Restricted areas"))
TouristicEvent.add_property('areas', lambda self: intersecting(RestrictedArea, self, distance=0),
                            _(u"Restricted areas"))


class City(models.Model):
    code = models.CharField(primary_key=True, max_length=6, db_column='insee')
    name = models.CharField(max_length=128, db_column='commune', verbose_name=_(u"Name"))
    geom = models.MultiPolygonField(srid=settings.SRID, spatial_index=False)
{'fraction_non_alphanumeric': 0.1079626244160065, 'fraction_numerical': 0.002945358521226894, 'mean_word_length': 3.5294388224471023, 'pattern_counts': {'":': 2, '<': 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': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '738059', 'n_tokens_mistral': 3012, 'n_tokens_neox': 2804, 'n_words': 561}