Exemplo n.º 1
0
    def forwards(self, orm):

        # Keep track of previous paths associated to trails
        by_paths = {}
        for path, trail in db.execute('SELECT id, sentier FROM l_t_troncon;'):
            by_paths.setdefault(trail, []).append(path)

        # Deleting trail reference from path
        db.delete_column('l_t_troncon', 'sentier')

        # Fields are now in topology model
        db.delete_column('l_t_sentier', 'date_update')
        db.delete_column('l_t_sentier', 'date_insert')

        # Adding field 'Trail.topo_object'
        db.add_column('l_t_sentier',
                      'topo_object',
                      self.gf('django.db.models.fields.related.OneToOneField')(
                          to=orm['core.Topology'],
                          null=True,
                          db_column='evenement'),
                      keep_default=False)

        # Restore NAIVELY previous trails
        from geotrek.core.models import Topology

        trails = db.execute('SELECT id FROM l_t_sentier')
        print "Migrating %s trails" % len(trails)

        for (trail, ) in trails:
            topo = Topology()
            topo.kind = 'TRAIL'
            topo.save()
            print "Created empty topology %s" % topo.pk

            db.execute('UPDATE l_t_sentier SET evenement = %s WHERE id = %s',
                       (topo.pk, trail))

            trail_paths = by_paths.get(trail, [])
            print "%s paths for trail %s" % (len(trail_paths), trail)
            for path in trail_paths:
                db.execute(
                    'INSERT INTO e_r_evenement_troncon (troncon, evenement, pk_debut, pk_fin, ordre) VALUES (%s,%s, 0.0, 1.0, 1)',
                    (path, topo.pk))

        db.execute(
            'ALTER TABLE l_t_sentier ALTER COLUMN evenement SET NOT NULL')
        db.execute(
            'ALTER TABLE l_t_sentier DROP CONSTRAINT l_t_sentier_pkey CASCADE')
        db.execute('ALTER TABLE l_t_sentier DROP COLUMN IF EXISTS id')
        db.execute(
            'ALTER TABLE l_t_sentier ADD CONSTRAINT l_t_sentier_pkey PRIMARY KEY (evenement)'
        )
Exemplo n.º 2
0
    def __init__(self, *args, target_type=None, target_id=None, **kwargs):
        super(InterventionForm, self).__init__(*args, **kwargs)

        if not self.instance.pk:
            # New intervention. We have to set its target.
            if target_type and target_id:
                # Point target to an existing topology
                ct = ContentType.objects.get_for_id(target_type)
                self.instance.target = ct.get_object_for_this_type(
                    id=target_id)
                # Set POST URL
                self.helper.form_action += '?target_type={}&target_id={}'.format(
                    target_type, target_id)
            else:
                # Point target to a new topology
                self.instance.target = Topology()
        # Else: existing intervention. Target is already set

        self.fields['topology'].initial = self.instance.target

        if self.instance.target.__class__ == Topology:
            # Intervention has its own topology
            title = _("On {}".format(_("Paths")))
            self.fields['topology'].label = \
                '<img src="{prefix}images/path-16.png" title="{title}">{title}'.format(
                    prefix=settings.STATIC_URL, title=title
            )
        else:
            # Intervention on an existing topology
            icon = self.instance.target._meta.model_name
            title = _("On {}".format(str(self.instance.target)))
            self.fields['topology'].label = \
                '<img src="{prefix}images/{icon}-16.png" title="{title}"><a href="{url}">{title}</a>'.format(
                    prefix=settings.STATIC_URL, icon=icon, title=title,
                    url=self.instance.target.get_detail_url()
            )
            # Topology is readonly
            self.fields['topology'].required = False
            self.fields['topology'].widget = TopologyReadonlyWidget()

        # Length is not editable in AltimetryMixin
        self.fields['length'].initial = self.instance.length
        editable = bool(self.instance.geom
                        and (self.instance.geom.geom_type == 'Point'
                             or self.instance.geom.geom_type == 'LineString'))
        self.fields['length'].widget.attrs['readonly'] = editable