<commit_before># -*- coding: utf-8 -*-
from django.db import models
from django.utils.translation import ugettext_lazy as _
from django.contrib.gis.geos import Point, LineString

from caminae.authent.models import StructureRelated
from caminae.core.models import Path, MapEntityMixin, TopologyMixin
from caminae.core.factories import PathFactory, TopologyMixinFactory, PathAggregationFactory
from caminae.common.models import Organism


class Intervention(MapEntityMixin, StructureRelated):
    in_maintenance = models.BooleanField(verbose_name=_(u"Whether the intervention is currently happening"))
    name = models.CharField(verbose_name=_(u"Name"), max_length=128)
    date = models.DateField(auto_now_add=True, verbose_name=_(u"Date"))
    comments = models.TextField(blank=True, verbose_name=_(u"Comments"))

    ## Technical information ##
    length = models.FloatField(default=0.0, verbose_name=_(u"Length"))
    width = models.FloatField(default=0.0, verbose_name=_(u"Width"))
    height = models.FloatField(default=0.0, verbose_name=_(u"Height"))
    area = models.IntegerField(default=0, verbose_name=_(u"Area"))
    slope = models.IntegerField(default=0, verbose_name=_(u"Slope"))

    ## Costs ##
    material_cost = models.FloatField(default=0.0, verbose_name=_(u"Material cost"))
    heliport_cost = models.FloatField(default=0.0, verbose_name=_(u"Heliport cost"))
    subcontract_cost = models.FloatField(default=0.0, verbose_name=_(u"Subcontract cost"))

    #TODO: remove this --> abstract class
    date_insert = models.DateTimeField(verbose_name=_(u"Insertion date"), auto_now_add=True)
    date_update = models.DateTimeField(verbose_name=_(u"Update date"), auto_now=True)
    deleted = models.BooleanField(verbose_name=_(u"Deleted"))

    ## Relations ##
    topology = models.ForeignKey(TopologyMixin, null=True,
                related_name="interventions",
                verbose_name=_(u"Interventions"))

    stake = models.ForeignKey('core.Stake', null=True,
            related_name='interventions', verbose_name=_("Stake"))

    status = models.ForeignKey('InterventionStatus', verbose_name=_("Intervention status"))

    typology = models.ForeignKey('InterventionTypology', null=True, blank=True,
            verbose_name=_(u"Intervention typology"))

    disorders = models.ManyToManyField('InterventionDisorder', related_name="interventions",
            verbose_name=_(u"Disorders"))

    jobs = models.ManyToManyField('InterventionJob', through='ManDay',
            verbose_name=_(u"Jobs"))

    project = models.ForeignKey('Project', null=True, blank=True,
            verbose_name=_(u"Project"))

    def initFromPathsList(self, pathlist, constraints=None):
        # TODO: pathlist is now a geom
        topology = TopologyMixinFactory.create(geom=pathlist)
        self.topology = topology
        self.save()

    def initFromInfrastructure(self, infrastructure):
        raise NotImplementedError

    def initFromPoint(self, point):
        """
        Initialize the intervention topology from a Point.
        """
        closest_paths = Path.objects.distance(point).order_by('-distance')
        if not closest_paths:
            closest_paths = [PathFactory.create()]
            # TODO: raise not possible without path !

        # TODO: compute this from point position
        path = closest_paths[0]
        distance = 0
        position = position = 0.5
        fakeline = LineString(Point(point.x, point.y, 0), Point(point.x+0.1, point.y+0.1, 0))
        
        # Create topologies objects
        topology = TopologyMixinFactory.create(offset=distance, geom=fakeline)
        PathAggregationFactory.create(topo_object=topology,
                                      path=path,
                                      start_position=position,
                                      end_position=position)
        self.topology = topology
        self.save()

    @property
    def geom(self):
        if self.topology:
            if len(self.topology.paths) > 1:
                pathaggr = self.topology.paths[0]
                if pathaggr.start_position == pathaggr.end_position:
                    return self.topology.geom.coords[0]  # return Point
                else:
                    return self.topology.geom
        return Point(0,0) # TODO: None ?

    @property
    def name_display(self):<commit_msg>Fix fake intervention save (ref #114)<commit_after># -*- coding: utf-8 -*-
from django.db import models
from django.conf import settings
from django.utils.translation import ugettext_lazy as _
from django.contrib.gis.geos import Point, LineString

from caminae.authent.models import StructureRelated
from caminae.core.models import MapEntityMixin, TopologyMixin
from caminae.core.factories import TopologyMixinFactory
from caminae.common.models import Organism


class Intervention(MapEntityMixin, StructureRelated):
    in_maintenance = models.BooleanField(verbose_name=_(u"Whether the intervention is currently happening"))
    name = models.CharField(verbose_name=_(u"Name"), max_length=128)
    date = models.DateField(auto_now_add=True, verbose_name=_(u"Date"))
    comments = models.TextField(blank=True, verbose_name=_(u"Comments"))

    ## Technical information ##
    length = models.FloatField(default=0.0, verbose_name=_(u"Length"))
    width = models.FloatField(default=0.0, verbose_name=_(u"Width"))
    height = models.FloatField(default=0.0, verbose_name=_(u"Height"))
    area = models.IntegerField(default=0, verbose_name=_(u"Area"))
    slope = models.IntegerField(default=0, verbose_name=_(u"Slope"))

    ## Costs ##
    material_cost = models.FloatField(default=0.0, verbose_name=_(u"Material cost"))
    heliport_cost = models.FloatField(default=0.0, verbose_name=_(u"Heliport cost"))
    subcontract_cost = models.FloatField(default=0.0, verbose_name=_(u"Subcontract cost"))

    #TODO: remove this --> abstract class
    date_insert = models.DateTimeField(verbose_name=_(u"Insertion date"), auto_now_add=True)
    date_update = models.DateTimeField(verbose_name=_(u"Update date"), auto_now=True)
    deleted = models.BooleanField(verbose_name=_(u"Deleted"))

    ## Relations ##
    topology = models.ForeignKey(TopologyMixin, null=True,
                related_name="interventions",
                verbose_name=_(u"Interventions"))

    stake = models.ForeignKey('core.Stake', null=True,
            related_name='interventions', verbose_name=_("Stake"))

    status = models.ForeignKey('InterventionStatus', verbose_name=_("Intervention status"))

    typology = models.ForeignKey('InterventionTypology', null=True, blank=True,
            verbose_name=_(u"Intervention typology"))

    disorders = models.ManyToManyField('InterventionDisorder', related_name="interventions",
            verbose_name=_(u"Disorders"))

    jobs = models.ManyToManyField('InterventionJob', through='ManDay',
            verbose_name=_(u"Jobs"))

    project = models.ForeignKey('Project', null=True, blank=True,
            verbose_name=_(u"Project"))

    def initFromPathsList(self, pathlist, constraints=None):
        # TODO: pathlist is now a geom
        topology = TopologyMixinFactory.create(geom=pathlist)
        self.topology = topology
        self.save()

    def initFromInfrastructure(self, infrastructure):
        raise NotImplementedError

    def initFromPoint(self, point):
        """
        Initialize the intervention topology from a Point.
        """
        # TODO : compute offset etc.
        fakeline = LineString(Point(point.x, point.y, 0), Point(point.x+0.1, point.y+0.1, 0))
        topology = TopologyMixinFactory.create(geom=fakeline)
        self.topology = topology
        self.save()

    @property
    def geom(self):
        if self.topology:
            if len(list(self.topology.geom.coords)) == 2:
                fakepoint = Point(*self.topology.geom.coords[0], srid=settings.SRID)  # return Point from fakeline
                return fakepoint
            else:
                return self.topology.geom
        return Point(0,0,0)

    @property
    def name_display(self):
{'fraction_non_alphanumeric': 0.11283950617283951, 'fraction_numerical': 0.008024691358024692, 'mean_word_length': 3.4781647318960753, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 3, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1657885', 'n_tokens_mistral': 2438, 'n_tokens_neox': 2326, 'n_words': 449}