Exemplo n.º 1
0
    def test_create_constraint(self):
        """Test constraints creation"""

        editing_layer = Layer.objects.get(name='editing_layer')
        constraint_layer = Layer.objects.get(name=self.constraint_layer_name)
        constraint = GeoConstraint(layer=editing_layer,
                                   constraint_layer=constraint_layer)
        # Test validation
        constraint.clean()
        constraint.save()

        # Check layer types (PG or SL)
        with self.assertRaises(ValidationError) as ex:
            GeoConstraint(layer=editing_layer,
                          constraint_layer=Layer(layer_type='GDAL')).clean()

        with self.assertRaises(ValidationError) as ex:
            GeoConstraint(layer=Layer(layer_type='GDAL'),
                          constraint_layer=constraint_layer).clean()

        # Check if constraints layer is polygon
        with self.assertRaises(ValidationError) as ex:
            GeoConstraint(layer=constraint_layer,
                          constraint_layer=editing_layer).clean()

        # Check self constraint
        with self.assertRaises(ValidationError) as ex:
            GeoConstraint(layer=constraint_layer,
                          constraint_layer=constraint_layer).clean()

        rule = GeoConstraintRule(constraint=constraint,
                                 user=self.test_user1,
                                 rule='int_f=1')
        rule.save()

        # Test validation
        with self.assertRaises(ValidationError) as ex:
            rule2 = GeoConstraintRule(constraint=constraint,
                                      user=self.test_user1,
                                      group=self.group,
                                      rule='int_f=1')
            rule2.clean()

        # Test constraints for user
        rules = GeoConstraintRule.get_constraints_for_user(
            self.test_user1, editing_layer)
        self.assertEqual(len(rules), 1)
        self.assertEqual(rules[0], rule)

        # Test the other path with group
        rule3 = GeoConstraintRule(constraint=constraint,
                                  group=self.group,
                                  rule='int_f=1')
        rule3.save()
        rules = GeoConstraintRule.get_constraints_for_user(
            self.test_user3, editing_layer)
        self.assertEqual(len(rules), 1)
        self.assertEqual(rules[0], rule3)

        # Test we need a user OR a group
        with self.assertRaises(ValidationError) as ex:
            rule4 = GeoConstraintRule(constraint=constraint, rule='int_f=1')
            rule4.clean()

        # Test we get nothing for the other layer and user
        rules = GeoConstraintRule.get_constraints_for_user(
            self.test_user2, constraint_layer)
        self.assertEqual(len(rules), 0)

        # Test inactive constraints for user
        constraint.active = False
        constraint.save()
        rules = GeoConstraintRule.get_constraints_for_user(
            self.test_user3, editing_layer)
        self.assertEqual(len(rules), 1)
        self.assertEqual(rules[0], rule3)
        rules = GeoConstraintRule.get_active_constraints_for_user(
            self.test_user3, editing_layer)
        self.assertEqual(len(rules), 0)
Exemplo n.º 2
0
def set_initconfig_value(sender, **kwargs):
    """
    Set base editing data for initconfig
    """
    Project = apps.get_app_config(kwargs['projectType']).get_model('project')
    project_layers = {
        pl.pk: pl
        for pl in Project.objects.get(pk=kwargs['project']).layer_set.all()
    }

    # get every layer editable for the project, il list == 0 return
    layers_to_edit = G3WEditingLayer.objects.filter(
        app_name=kwargs['projectType'])
    editable_layers_id = []
    editable_layers_constraints = {}
    for el in layers_to_edit:

        # check for permissions
        if el.layer_id in project_layers and sender.request.user.has_perm(
                'change_layer', project_layers[el.layer_id]):
            editable_layers_id.append(el.layer_id)

            # check if layers has constraints
            constraints = GeoConstraintRule.get_constraints_for_user(
                sender.request.user, project_layers[el.layer_id])
            envelope = []
            for constraint in constraints:
                geom = constraint.get_constraint_geometry()
                if geom[1] > 0:
                    env = geom[0].envelope
                    xmin, ymin = env[0][0]
                    xmax, ymax = env[0][2]
                    if 'xmin' in envelope and xmin > envelope['xmin']:
                        xmin = envelope['xmin']
                    if 'ymin' in envelope and ymin > envelope['ymin']:
                        ymin = envelope['ymin']
                    if 'xmax' in envelope and xmax < envelope['xmax']:
                        xmax = envelope['xmax']
                    if 'ymax' in envelope and ymax < envelope['ymax']:
                        ymax = envelope['ymax']
                    envelope = [xmin, ymin, xmax, ymax]

            if len(envelope) > 0:
                # FIXME: if qgs_layer_id is not unique it shouldn't be used as a key here:
                editable_layers_constraints.update({
                    project_layers[el.layer_id].qgs_layer_id: {
                        'geometry_api_url':
                        reverse('geoconstraint-api-geometry',
                                kwargs={'layer_id': el.layer_id}),
                        'bbox':
                        envelope
                    }
                })

    if len(editable_layers_id) == 0:
        return None

    toret = {
        'editing': {
            'gid': "{}:{}".format(kwargs['projectType'], kwargs['project']),
        },
    }

    if len(editable_layers_constraints) > 0:
        toret['editing']['constraints'] = editable_layers_constraints

    return toret