Ejemplo n.º 1
0
    def setUp(self):
        self.p = Point(-8515941.0, 4953519.0)
        self.instances = [
            make_instance(point=self.p),
            make_instance(point=self.p)
        ]
        self.commander_users = [
            make_commander_user(i, username='******' % i.pk)
            for i in self.instances]
        for i in range(len(self.instances)):
            set_write_permissions(self.instances[i], self.commander_users[i],
                                  'Plot', ['udf:Test choice'])
        self.choice_udfds = [
            UserDefinedFieldDefinition.objects.create(
                instance=i,
                model_type='Plot',
                datatype=json.dumps({'type': 'choice',
                                     'choices': ['a', 'b', 'c']}),
                iscollection=False,
                name='Test choice') for i in self.instances]

        self.plots = [
            Plot(geom=self.p, instance=i) for i in self.instances]

        for i in range(len(self.plots)):
            self.plots[i].save_with_user(self.commander_users[i])

        psycopg2.extras.register_hstore(connection.cursor(), globally=True)
Ejemplo n.º 2
0
    def setUp(self):
        super(ExportTreeTaskTest, self).setUp()

        set_write_permissions(self.instance, self.user, "Plot", ["udf:Test choice"])
        set_write_permissions(self.instance, self.user, "Tree", ["udf:Test int"])

        UserDefinedFieldDefinition.objects.create(
            instance=self.instance,
            model_type="Plot",
            datatype=json.dumps({"type": "choice", "choices": ["a", "b", "c"]}),
            iscollection=False,
            name="Test choice",
        )

        UserDefinedFieldDefinition.objects.create(
            instance=self.instance,
            model_type="Tree",
            datatype=json.dumps({"type": "int"}),
            iscollection=False,
            name="Test int",
        )

        p = Plot(geom=self.instance.center, instance=self.instance, address_street="123 Main Street")
        p.udfs["Test choice"] = "a"

        p.save_with_user(self.user)

        t = Tree(plot=p, instance=self.instance, diameter=2)
        t.udfs["Test int"] = 4

        t.save_with_user(self.user)
Ejemplo n.º 3
0
    def _setup_collection_udfs(self):
        self.plotstew = make_collection_udf(self.instance, model='Plot',
                                            datatype=COLLECTION_UDF_DATATYPE)
        self.treestew = make_collection_udf(self.instance, model='Tree',
                                            datatype=COLLECTION_UDF_DATATYPE)

        set_write_permissions(self.instance, self.commander, 'Plot',
                              [self.plotstew.canonical_name])
        set_write_permissions(self.instance, self.commander, 'Tree',
                              [self.treestew.canonical_name])

        p1, _ = self.create_tree_and_plot(
            plotudfs={self.plotstew.name:
                      [{'action': 'water', 'date': "2013-08-06 00:00:00"},
                       {'action': 'prune', 'date': "2013-09-15 00:00:00"}]},
            treeudfs={self.treestew.name:
                      [{'action': 'water', 'date': "2013-05-15 00:00:00"},
                       {'action': 'water', 'date': None}]})

        p2, _ = self.create_tree_and_plot(
            plotudfs={self.plotstew.name: [
                {'action': 'water', 'date': "2014-11-26 00:00:00"}]},
            treeudfs={self.treestew.name: [
                {'action': 'prune', 'date': "2014-06-23 00:00:00"}]})

        p3, _ = self.create_tree_and_plot(
            plotudfs={self.plotstew.name: [
                {'action': 'water', 'date': "2015-08-05 00:00:00"},
                {'action': 'prune', 'date': "2015-04-13 00:00:00"}]},
            treeudfs={self.treestew.name:
                      [{'action': 'prune', 'date': "2013-06-19 00:00:00"},
                       {'action': 'water', 'date': None}]})

        return (p.pk for p in [p1, p2, p3])
Ejemplo n.º 4
0
    def setUp(self):
        super(ExportTreeTaskTest, self).setUp()

        set_write_permissions(self.instance, self.user,
                              'Plot', ['udf:Test choice'])
        set_write_permissions(self.instance, self.user,
                              'Tree', ['udf:Test int'])

        UserDefinedFieldDefinition.objects.create(
            instance=self.instance,
            model_type='Plot',
            datatype=json.dumps({'type': 'choice',
                                 'choices': ['a', 'b', 'c']}),
            iscollection=False,
            name='Test choice')

        UserDefinedFieldDefinition.objects.create(
            instance=self.instance,
            model_type='Tree',
            datatype=json.dumps({'type': 'int'}),
            iscollection=False,
            name='Test int')

        p = Plot(geom=self.instance.center, instance=self.instance,
                 address_street="123 Main Street")
        p.udfs['Test choice'] = 'a'

        p.save_with_user(self.user)

        t = Tree(plot=p, instance=self.instance, diameter=2)
        t.udfs['Test int'] = 4

        t.save_with_user(self.user)
Ejemplo n.º 5
0
    def test_bulk_update_scalar_choices(self):
        plot_udfd = UserDefinedFieldDefinition.objects.create(
            instance=self.instance,
            model_type='Plot',
            datatype=json.dumps({'type': 'choice',
                                 'choices': ['a', 'b']}),
            iscollection=False,
            name='Test plot choice')

        tree_udfd = UserDefinedFieldDefinition.objects.create(
            instance=self.instance,
            model_type='Tree',
            datatype=json.dumps({'type': 'choice',
                                 'choices': ['x', 'y']}),
            iscollection=False,
            name='Test tree choice')

        set_write_permissions(self.instance, self.user,
                              'Plot', ['udf:Test plot choice'])

        set_write_permissions(self.instance, self.user,
                              'Tree', ['udf:Test tree choice'])

        params = {'choice_changes': [
            {'id': str(plot_udfd.pk), 'changes': [
                {"action": "rename",
                 "original_value": "b",
                 "new_value": "B",
                 "subfield": ""},
                {"action": "add",
                 "original_value": "",
                 "new_value": "p",
                 "subfield": ""}]},
            {'id': str(tree_udfd.pk), 'changes': [
                {"action": "rename",
                 "original_value": "y",
                 "new_value": "Y",
                 "subfield": ""},
                {"action": "add",
                 "original_value": "",
                 "new_value": "q",
                 "subfield": ""}]}]}
        request = self._make_put_request(params)
        udf_bulk_update(request, self.instance)

        plot_udfd.refresh_from_db()
        tree_udfd.refresh_from_db()

        plot_datatype = json.loads(plot_udfd.datatype)
        tree_datatype = json.loads(tree_udfd.datatype)

        self.assertIn('choices', plot_datatype)
        self.assertEqual(set(plot_datatype['choices']), {'a', 'B', 'p'})

        self.assertIn('choices', tree_datatype)
        self.assertEqual(set(tree_datatype['choices']), {'x', 'Y', 'q'})
Ejemplo n.º 6
0
def _setup_collection_udfs(instance, user):
    plotstew = make_collection_udf(instance, model='Plot',
                                   datatype=COLLECTION_UDF_DATATYPE)
    treestew = make_collection_udf(instance, model='Tree',
                                   datatype=COLLECTION_UDF_DATATYPE)

    set_write_permissions(instance, user, 'Plot', [plotstew.canonical_name])
    set_write_permissions(instance, user, 'Tree', [treestew.canonical_name])

    return plotstew, treestew
Ejemplo n.º 7
0
    def setUp(self):
        self.p = Point(-8515941.0, 4953519.0)
        self.instance = make_instance(point=self.p)

        self.udf = make_collection_udf(self.instance, "Stewardship")

        self.commander_user = make_commander_user(self.instance)
        set_write_permissions(self.instance, self.commander_user, "Plot", ["udf:Stewardship"])

        self.plot = Plot(geom=self.p, instance=self.instance)
        self.plot.save_with_user(self.commander_user)
Ejemplo n.º 8
0
Archivo: udfs.py Proyecto: heath/OTM2
    def setUp(self):
        self.instance = make_instance()
        self.p = Point(-8515941.0, 4953519.0)

        make_collection_udf(self.instance, 'Stewardship')

        self.commander_user = make_commander_user(self.instance)
        set_write_permissions(self.instance, self.commander_user,
                              'Plot', ['udf:Stewardship'])

        self.plot = Plot(geom=self.p, instance=self.instance)
        self.plot.save_with_user(self.commander_user)
Ejemplo n.º 9
0
    def test_pending_udf_audits(self):
        UserDefinedFieldDefinition.objects.create(
            instance=self.instance,
            model_type='Plot',
            datatype=json.dumps({'type': 'choice',
                                 'choices': ['1', '2', '3']}),
            iscollection=False,
            name='times climbed')

        set_write_permissions(self.instance, self.commander_user,
                              'Plot', ['udf:times climbed'])

        FieldPermission.objects.create(
            model_name='Plot',
            field_name='udf:times climbed',
            permission_level=FieldPermission.WRITE_WITH_AUDIT,
            role=self.pending_user.get_instance_user(self.instance).role,
            instance=self.instance)

        initial_plot = Plot(geom=self.p1, instance=self.instance)
        initial_plot.udfs['times climbed'] = '2'
        initial_plot.save_with_user(self.pending_user)

        udf_audit = Audit.objects.get(model='Plot', field='udf:times climbed',
                                      model_id=initial_plot.pk)
        approve_or_reject_audit_and_apply(udf_audit, self.commander_user,
                                          approved=True)

        geom_audit = Audit.objects.get(model='Plot', field='geom',
                                       model_id=initial_plot.pk)
        approve_or_reject_audit_and_apply(geom_audit, self.commander_user,
                                          approved=True)

        readonly_audit = Audit.objects.get(model='Plot', field='readonly',
                                           model_id=initial_plot.pk)
        approve_or_reject_audit_and_apply(readonly_audit,
                                          self.commander_user, approved=True)

        insert_audit = Audit.objects.get(model='Plot', field='id',
                                         model_id=initial_plot.pk)

        approve_or_reject_audit_and_apply(insert_audit,
                                          self.commander_user, approved=True)

        new_plot = Plot.objects.get(pk=initial_plot.pk)

        self.assertEqual(new_plot.pk, initial_plot.pk)
        self.assertEqual(new_plot.readonly, False)
        self.assertEqual(new_plot.geom, self.p1)
        self.assertEqual(new_plot.udfs['times climbed'], '2')
Ejemplo n.º 10
0
    def setUp(self):
        User._system_user.save_base()

        self.instance = make_instance()
        self.user = make_commander_user(self.instance)

        set_write_permissions(self.instance, self.user,
                              'Plot', ['udf:Test choice'])

        self.udf = UserDefinedFieldDefinition.objects.create(
            instance=self.instance,
            model_type='Plot',
            datatype=json.dumps({'type': 'choice',
                                 'choices': ['a', 'b', 'c']}),
            iscollection=False,
            name='Test choice')
Ejemplo n.º 11
0
    def setUp(self):
        User._system_user.save_base()

        self.instance = make_instance()
        create_stewardship_udfs(self.instance)
        self.user = make_commander_user(self.instance)

        set_write_permissions(self.instance, self.user, "Plot", ["udf:Test choice"])

        self.udf = UserDefinedFieldDefinition.objects.create(
            instance=self.instance,
            model_type="Plot",
            datatype=json.dumps({"type": "choice", "choices": ["a", "b", "c"]}),
            iscollection=False,
            name="Test choice",
        )
Ejemplo n.º 12
0
    def _setup_udfs(self):
        set_write_permissions(self.instance, self.commander,
                              'Plot',
                              ['udf:Test string', 'udf:Test date'])
        set_write_permissions(self.instance, self.commander,
                              'Tree',
                              ['udf:Test float'])

        UserDefinedFieldDefinition.objects.create(
            instance=self.instance,
            model_type='Plot',
            datatype=json.dumps({'type': 'string'}),
            iscollection=False,
            name='Test string')

        UserDefinedFieldDefinition.objects.create(
            instance=self.instance,
            model_type='Plot',
            datatype=json.dumps({'type': 'date'}),
            iscollection=False,
            name='Test date')

        UserDefinedFieldDefinition.objects.create(
            instance=self.instance,
            model_type='Tree',
            datatype=json.dumps({'type': 'float'}),
            iscollection=False,
            name='Test float')

        psycopg2.extras.register_hstore(connection.cursor(), globally=True)

        p1, _ = self.create_tree_and_plot(
            plotudfs={'Test string': 'testing foo',
                      'Test date': datetime(2010, 1, 9)},
            treeudfs={'Test float': 9.2})

        p2, _ = self.create_tree_and_plot(
            plotudfs={'Test string': 'testing baz or fish',
                      'Test date': datetime(2012, 1, 9)},
            treeudfs={'Test float': 12.0})

        p3, _ = self.create_tree_and_plot(
            plotudfs={'Test string': 'baz',
                      'Test date': datetime(2014, 1, 9)},
            treeudfs={'Test float': 2.2})

        return (p.pk for p in [p1, p2, p3])
Ejemplo n.º 13
0
    def setUp(self):
        self.p = Point(-8515941.0, 4953519.0)
        self.instance = make_instance(point=self.p)

        def make_and_save_type(dtype):
            UserDefinedFieldDefinition.objects.create(
                instance=self.instance,
                model_type='Plot',
                datatype=json.dumps({'type': dtype}),
                iscollection=False,
                name='Test %s' % dtype)

        allowed_types = 'float', 'int', 'string', 'user', 'date'

        addl_fields = ['udf:Test %s' % ttype for ttype in allowed_types]
        addl_fields.append('udf:Test choice')
        addl_fields.append('udf:Test multichoice')

        self.commander_user = make_commander_user(self.instance)
        set_write_permissions(self.instance, self.commander_user,
                              'Plot', addl_fields)

        for dtype in allowed_types:
            make_and_save_type(dtype)

        self.choice_udfd = UserDefinedFieldDefinition.objects.create(
            instance=self.instance,
            model_type='Plot',
            datatype=json.dumps({'type': 'choice',
                                 'choices': ['a', 'b', 'c']}),
            iscollection=False,
            name='Test choice')

        self.multichoice_udfd = UserDefinedFieldDefinition.objects.create(
            instance=self.instance,
            model_type='Plot',
            datatype=json.dumps({'type': 'multichoice',
                                 'choices': ['a', 'b', 'c']}),
            iscollection=False,
            name='Test multichoice')

        self.plot = Plot(geom=self.p, instance=self.instance)
        self.plot.save_with_user(self.commander_user)

        psycopg2.extras.register_hstore(connection.cursor(), globally=True)
Ejemplo n.º 14
0
    def setUp(self):
        self.p = Point(-8515941.0, 4953519.0)
        self.instance = make_instance(point=self.p)

        def make_and_save_type(dtype):
            UserDefinedFieldDefinition.objects.create(
                instance=self.instance,
                model_type="Plot",
                datatype=json.dumps({"type": dtype}),
                iscollection=False,
                name="Test %s" % dtype,
            )

        allowed_types = "float", "int", "string", "user", "date"

        addl_fields = ["udf:Test %s" % ttype for ttype in allowed_types]
        addl_fields.append("udf:Test choice")
        addl_fields.append("udf:Test multichoice")

        self.commander_user = make_commander_user(self.instance)
        set_write_permissions(self.instance, self.commander_user, "Plot", addl_fields)

        for dtype in allowed_types:
            make_and_save_type(dtype)

        self.choice_udfd = UserDefinedFieldDefinition.objects.create(
            instance=self.instance,
            model_type="Plot",
            datatype=json.dumps({"type": "choice", "choices": ["a", "b", "c"]}),
            iscollection=False,
            name="Test choice",
        )

        self.multichoice_udfd = UserDefinedFieldDefinition.objects.create(
            instance=self.instance,
            model_type="Plot",
            datatype=json.dumps({"type": "multichoice", "choices": ["a", "b", "c"]}),
            iscollection=False,
            name="Test multichoice",
        )

        self.plot = Plot(geom=self.p, instance=self.instance)
        self.plot.save_with_user(self.commander_user)

        psycopg2.extras.register_hstore(connection.cursor(), globally=True)
Ejemplo n.º 15
0
    def setUp(self):
        self.p = Point(-8515941.0, 4953519.0)
        self.instance = make_instance(point=self.p)

        def make_and_save_type(dtype):
            UserDefinedFieldDefinition.objects.create(
                instance=self.instance,
                model_type='Plot',
                datatype=json.dumps({'type': dtype}),
                iscollection=False,
                name='Test %s' % dtype)

        allowed_types = 'float', 'int', 'string', 'user', 'date'

        addl_fields = ['udf:Test %s' % ttype for ttype in allowed_types]
        addl_fields.append('udf:Test choice')
        addl_fields.append('udf:Test multichoice')

        self.commander_user = make_commander_user(self.instance)
        set_write_permissions(self.instance, self.commander_user,
                              'Plot', addl_fields)

        for dtype in allowed_types:
            make_and_save_type(dtype)

        self.choice_udfd = UserDefinedFieldDefinition.objects.create(
            instance=self.instance,
            model_type='Plot',
            datatype=json.dumps({'type': 'choice',
                                 'choices': ['a', 'b', 'c']}),
            iscollection=False,
            name='Test choice')

        self.multichoice_udfd = UserDefinedFieldDefinition.objects.create(
            instance=self.instance,
            model_type='Plot',
            datatype=json.dumps({'type': 'multichoice',
                                 'choices': ['a', 'b', 'c']}),
            iscollection=False,
            name='Test multichoice')

        self.plot = Plot(geom=self.p, instance=self.instance)
        self.plot.save_with_user(self.commander_user)

        psycopg2.extras.register_hstore(connection.cursor(), globally=True)
Ejemplo n.º 16
0
    def test_delete_udf_deletes_perms_value(self):
        set_write_permissions(self.instance, self.commander_user, "Plot", ["udf:Test string"])

        udf_def = UserDefinedFieldDefinition(
            instance=self.instance,
            model_type="Plot",
            datatype=json.dumps({"type": "string"}),
            iscollection=False,
            name="Test string",
        )

        udf_def.save()

        qs = FieldPermission.objects.filter(field_name="udf:Test string", model_name="Plot")

        self.assertTrue(qs.exists())
        udf_def.delete()
        self.assertFalse(qs.exists())
Ejemplo n.º 17
0
    def setUp(self):
        User._system_user.save_base()

        self.instance = make_instance()
        self.user = make_commander_user(self.instance)

        set_write_permissions(self.instance, self.user, 'Plot',
                              ['udf:Test choice'])

        self.udf = UserDefinedFieldDefinition.objects.create(
            instance=self.instance,
            model_type='Plot',
            datatype=json.dumps({
                'type': 'choice',
                'choices': ['a', 'b', 'c']
            }),
            iscollection=False,
            name='Test choice')
Ejemplo n.º 18
0
    def test_parse_collection_udf_nested_pass_numeric_comparison(self):
        self._setup_tree_and_collection_udf()
        agility = make_collection_udf(self.instance, model='Tree',
                                      name='Agility',
                                      datatype=[{'type': 'float',
                                                 'name': 'current'}])
        set_write_permissions(self.instance, self.commander,
                              'Tree', ['udf:Agility'])
        new_tree = Tree(instance=self.instance, plot=self.plot)
        new_tree.udfs[agility.name] = [{'current': '1.5'}]
        new_tree.save_with_user(self.commander)

        pred = search._parse_query_dict(
            {'udf:tree:%s.current' % agility.pk: {'MIN': 1}},
            mapping=search.DEFAULT_MAPPING)

        target = ('AND', {('tree__id__in', (new_tree.pk,))})

        self.assertEqual(destructure_query_set(pred), target)
Ejemplo n.º 19
0
    def test_delete_udf_deletes_perms_value(self):
        set_write_permissions(self.instance, self.commander_user, 'Plot',
                              ['udf:Test string'])

        udf_def = UserDefinedFieldDefinition(instance=self.instance,
                                             model_type='Plot',
                                             datatype=json.dumps(
                                                 {'type': 'string'}),
                                             iscollection=False,
                                             name='Test string')

        udf_def.save()

        qs = FieldPermission.objects.filter(field_name='udf:Test string',
                                            model_name='Plot')

        self.assertTrue(qs.exists())
        udf_def.delete()
        self.assertFalse(qs.exists())
Ejemplo n.º 20
0
Archivo: udfs.py Proyecto: atogle/OTM2
    def test_delete_udf_deletes_perms_value(self):
        set_write_permissions(self.instance, self.commander_user,
                              'Plot', ['udf:Test string'])

        udf_def = UserDefinedFieldDefinition(
            instance=self.instance,
            model_type='Plot',
            datatype=json.dumps({'type': 'string'}),
            iscollection=False,
            name='Test string')

        udf_def.save()

        qs = FieldPermission.objects.filter(
            field_name='udf:Test string',
            model_name='Plot')

        self.assertTrue(qs.exists())
        udf_def.delete()
        self.assertFalse(qs.exists())
Ejemplo n.º 21
0
    def test_delete_udf_deletes_perms_collection(self):
        set_write_permissions(self.instance, self.commander_user, "Plot", ["udf:Test choice"])

        udf_def = UserDefinedFieldDefinition(
            instance=self.instance,
            model_type="Plot",
            datatype=json.dumps(
                [{"name": "pick", "type": "choice", "choices": ["a", "b", "c"]}, {"type": "int", "name": "height"}]
            ),
            iscollection=True,
            name="Test choice",
        )

        udf_def.save()

        qs = FieldPermission.objects.filter(field_name="udf:Test choice", model_name="Plot")

        self.assertTrue(qs.exists())
        udf_def.delete()
        self.assertFalse(qs.exists())
Ejemplo n.º 22
0
    def _setup_tree_and_collection_udf(self):
        instance = make_instance()

        self.plotstew = make_collection_udf(instance, model='Plot',
                                            datatype=COLLECTION_UDF_DATATYPE)
        self.treestew = make_collection_udf(instance, model='Tree',
                                            datatype=COLLECTION_UDF_DATATYPE)

        commander = make_commander_user(instance)
        set_write_permissions(instance, commander, 'Plot', ['udf:Stewardship'])
        set_write_permissions(instance, commander, 'Tree', ['udf:Stewardship'])

        d1 = {'action': 'prune', 'date': "2014-05-3 00:00:00"}
        d2 = {'action': 'water', 'date': "2014-04-29 00:00:00"}

        self.plot = Plot(instance=instance, geom=instance.center)
        self.plot.udfs[self.plotstew.name] = [d1]
        self.plot.save_with_user(commander)

        self.tree = Tree(instance=instance, plot=self.plot)
        self.tree.udfs[self.treestew.name] = [d2]
        self.tree.save_with_user(commander)
Ejemplo n.º 23
0
    def setUp(self):
        self.p = Point(-8515941.0, 4953519.0)
        self.instance = make_instance(point=self.p)
        self.commander_user = make_commander_user(self.instance)
        set_write_permissions(self.instance, self.commander_user, "Plot", ["udf:Test choice"])

        UserDefinedFieldDefinition.objects.create(
            instance=self.instance,
            model_type="Plot",
            datatype=json.dumps({"type": "choice", "choices": ["a", "b", "c"]}),
            iscollection=False,
            name="Test choice",
        )

        UserDefinedFieldDefinition.objects.create(
            instance=self.instance,
            model_type="Plot",
            datatype=json.dumps({"type": "string"}),
            iscollection=False,
            name="Test unauth",
        )

        UserDefinedFieldDefinition.objects.create(
            instance=self.instance,
            model_type="Plot",
            datatype=json.dumps(
                [
                    {"type": "choice", "name": "a choice", "choices": ["a", "b", "c"]},
                    {"type": "string", "name": "a string"},
                ]
            ),
            iscollection=True,
            name="Test collection",
        )

        self.plot = Plot(geom=self.p, instance=self.instance)
        self.plot.save_with_user(self.commander_user)

        psycopg2.extras.register_hstore(connection.cursor(), globally=True)
Ejemplo n.º 24
0
    def _setup_tree_and_collection_udf(self):
        instance = make_instance()

        self.plotstew = make_collection_udf(instance, model='Plot',
                                            datatype=COLLECTION_UDF_DATATYPE)
        self.treestew = make_collection_udf(instance, model='Tree',
                                            datatype=COLLECTION_UDF_DATATYPE)

        commander = make_commander_user(instance)
        set_write_permissions(instance, commander, 'Plot', ['udf:Stewardship'])
        set_write_permissions(instance, commander, 'Tree', ['udf:Stewardship'])

        d1 = {'action': 'prune', 'date': "2014-05-3 00:00:00"}
        d2 = {'action': 'water', 'date': "2014-04-29 00:00:00"}

        self.plot = Plot(instance=instance, geom=instance.center)
        self.plot.udfs[self.plotstew.name] = [d1]
        self.plot.save_with_user(commander)

        self.tree = Tree(instance=instance, plot=self.plot)
        self.tree.udfs[self.treestew.name] = [d2]
        self.tree.save_with_user(commander)
Ejemplo n.º 25
0
    def setUp(self):
        # Just in case - cleanup other bad test cases
        Instance.objects.all().delete()

        self.instance = make_instance()
        plot_stew, tree_stew = tuple(
            create_stewardship_udfs(self.instance))
        self.cudf = plot_stew
        self.user = make_commander_user(self.instance)

        self.udf = UserDefinedFieldDefinition.objects.create(
            instance=self.instance,
            model_type='Plot',
            datatype=json.dumps({'type': 'choice',
                                 'choices': ['a', 'b', 'c']}),
            iscollection=False,
            name='Test choice')

        self.plot = Plot(instance=self.instance, geom=self.instance.center)

        set_write_permissions(self.instance, self.user,
                              'Plot', ['udf:Test choice', 'udf:Stewardship'])
Ejemplo n.º 26
0
    def setUp(self):
        self.instance = make_instance()
        self.commander_user = make_commander_user(self.instance)
        set_write_permissions(self.instance, self.commander_user,
                              'Plot', ['udf:Test choice'])

        self.p = Point(-8515941.0, 4953519.0)

        UserDefinedFieldDefinition.objects.create(
            instance=self.instance,
            model_type='Plot',
            datatype=json.dumps({'type': 'choice',
                                 'choices': ['a', 'b', 'c']}),
            iscollection=False,
            name='Test choice')

        UserDefinedFieldDefinition.objects.create(
            instance=self.instance,
            model_type='Plot',
            datatype=json.dumps({'type': 'string'}),
            iscollection=False,
            name='Test unauth')

        UserDefinedFieldDefinition.objects.create(
            instance=self.instance,
            model_type='Plot',
            datatype=json.dumps([{'type': 'choice',
                                  'name': 'a choice',
                                  'choices': ['a', 'b', 'c']},
                                 {'type': 'string',
                                  'name': 'a string'}]),
            iscollection=True,
            name='Test collection')

        self.plot = Plot(geom=self.p, instance=self.instance)
        self.plot.save_with_user(self.commander_user)

        psycopg2.extras.register_hstore(connection.cursor(), globally=True)
Ejemplo n.º 27
0
    def setUp(self):
        # Just in case - cleanup other bad test cases
        Instance.objects.all().delete()

        self.instance = make_instance()
        plot_stew, tree_stew = tuple(create_stewardship_udfs(self.instance))
        self.cudf = plot_stew
        self.user = make_commander_user(self.instance)

        self.udf = UserDefinedFieldDefinition.objects.create(
            instance=self.instance,
            model_type='Plot',
            datatype=json.dumps({
                'type': 'choice',
                'choices': ['a', 'b', 'c']
            }),
            iscollection=False,
            name='Test choice')

        self.plot = Plot(instance=self.instance, geom=self.instance.center)

        set_write_permissions(self.instance, self.user, 'Plot',
                              ['udf:Test choice', 'udf:Stewardship'])
Ejemplo n.º 28
0
    def setUp(self):
        self.p = Point(-8515941.0, 4953519.0)
        self.instances = [make_instance(point=self.p), make_instance(point=self.p)]
        self.commander_users = [make_commander_user(i, username="******" % i.pk) for i in self.instances]
        for i in range(len(self.instances)):
            set_write_permissions(self.instances[i], self.commander_users[i], "Plot", ["udf:Test choice"])
        self.choice_udfds = [
            UserDefinedFieldDefinition.objects.create(
                instance=i,
                model_type="Plot",
                datatype=json.dumps({"type": "choice", "choices": ["a", "b", "c"]}),
                iscollection=False,
                name="Test choice",
            )
            for i in self.instances
        ]

        self.plots = [Plot(geom=self.p, instance=i) for i in self.instances]

        for i in range(len(self.plots)):
            self.plots[i].save_with_user(self.commander_users[i])

        psycopg2.extras.register_hstore(connection.cursor(), globally=True)
Ejemplo n.º 29
0
    def setUp(self):
        super(ExportTreeTaskTest, self).setUp()

        set_write_permissions(self.instance, self.user, 'Plot',
                              ['udf:Test choice'])
        set_write_permissions(self.instance, self.user, 'Tree',
                              ['udf:Test int'])

        UserDefinedFieldDefinition.objects.create(instance=self.instance,
                                                  model_type='Plot',
                                                  datatype=json.dumps({
                                                      'type':
                                                      'choice',
                                                      'choices':
                                                      ['a', 'b', 'c']
                                                  }),
                                                  iscollection=False,
                                                  name='Test choice')

        UserDefinedFieldDefinition.objects.create(instance=self.instance,
                                                  model_type='Tree',
                                                  datatype=json.dumps(
                                                      {'type': 'int'}),
                                                  iscollection=False,
                                                  name='Test int')

        p = Plot(geom=Point(0, 0),
                 instance=self.instance,
                 address_street="123 Main Street")
        p.udfs['Test choice'] = 'a'

        p.save_with_user(self.user)

        t = Tree(plot=p, instance=self.instance, diameter=2)
        t.udfs['Test int'] = 4

        t.save_with_user(self.user)
Ejemplo n.º 30
0
    def setUp(self):
        self.p = Point(-8515941.0, 4953519.0)
        self.instance = make_instance(point=self.p)
        self.commander_user = make_commander_user(self.instance)
        set_write_permissions(self.instance, self.commander_user,
                              'Plot', ['udf:Test choice'])

        UserDefinedFieldDefinition.objects.create(
            instance=self.instance,
            model_type='Plot',
            datatype=json.dumps({'type': 'choice',
                                 'choices': ['a', 'b', 'c']}),
            iscollection=False,
            name='Test choice')

        UserDefinedFieldDefinition.objects.create(
            instance=self.instance,
            model_type='Plot',
            datatype=json.dumps({'type': 'string'}),
            iscollection=False,
            name='Test unauth')

        UserDefinedFieldDefinition.objects.create(
            instance=self.instance,
            model_type='Plot',
            datatype=json.dumps([{'type': 'choice',
                                  'name': 'a choice',
                                  'choices': ['a', 'b', 'c']},
                                 {'type': 'string',
                                  'name': 'a string'}]),
            iscollection=True,
            name='Test collection')

        self.plot = Plot(geom=self.p, instance=self.instance)
        self.plot.save_with_user(self.commander_user)

        psycopg2.extras.register_hstore(connection.cursor(), globally=True)
Ejemplo n.º 31
0
Archivo: udfs.py Proyecto: atogle/OTM2
    def test_delete_udf_deletes_perms_collection(self):
        set_write_permissions(self.instance, self.commander_user,
                              'Plot', ['udf:Test choice'])

        udf_def = UserDefinedFieldDefinition(
            instance=self.instance,
            model_type='Plot',
            datatype=json.dumps([{'name': 'pick',
                                  'type': 'choice',
                                  'choices': ['a', 'b', 'c']},
                                 {'type': 'int',
                                  'name': 'height'}]),
            iscollection=True,
            name='Test choice')

        udf_def.save()

        qs = FieldPermission.objects.filter(
            field_name='udf:Test choice',
            model_name='Plot')

        self.assertTrue(qs.exists())
        udf_def.delete()
        self.assertFalse(qs.exists())
Ejemplo n.º 32
0
    def test_delete_udf_deletes_perms_collection(self):
        set_write_permissions(self.instance, self.commander_user,
                              'Plot', ['udf:Test choice'])

        udf_def = UserDefinedFieldDefinition(
            instance=self.instance,
            model_type='Plot',
            datatype=json.dumps([{'name': 'pick',
                                  'type': 'choice',
                                  'choices': ['a', 'b', 'c']},
                                 {'type': 'int',
                                  'name': 'height'}]),
            iscollection=True,
            name='Test choice')

        udf_def.save()

        qs = FieldPermission.objects.filter(
            field_name='udf:Test choice',
            model_name='Plot')

        self.assertTrue(qs.exists())
        udf_def.delete()
        self.assertFalse(qs.exists())
Ejemplo n.º 33
0
    def test_parse_collection_udf_nested_pass_numeric_comparison(self):
        self._setup_tree_and_collection_udf()
        agility = make_collection_udf(self.instance,
                                      model='Tree',
                                      name='Agility',
                                      datatype=[{
                                          'type': 'float',
                                          'name': 'current'
                                      }])
        set_write_permissions(self.instance, self.commander, 'Tree',
                              ['udf:Agility'])
        new_tree = Tree(instance=self.instance, plot=self.plot)
        new_tree.udfs[agility.name] = [{'current': '1.5'}]
        new_tree.save_with_user(self.commander)

        pred = search._parse_predicate(
            {'udf:tree:%s.current' % agility.pk: {
                'MIN': 1
            }},
            mapping=search.DEFAULT_MAPPING)

        target = ('AND', {('tree__id__in', (new_tree.pk, ))})

        self.assertEqual(self.destructure_query_set(pred), target)
Ejemplo n.º 34
0
    def setUp(self):
        self.instance = make_instance()
        self.commander_user = make_commander_user(self.instance)
        set_write_permissions(self.instance, self.commander_user,
                              'Plot',
                              ['udf:Test choice', 'udf:Test string',
                               'udf:Test int', 'udf:Test date',
                               'udf:Test float'])

        self.p = Point(-8515941.0, 4953519.0)

        UserDefinedFieldDefinition.objects.create(
            instance=self.instance,
            model_type='Plot',
            datatype=json.dumps({'type': 'choice',
                                 'choices': ['a', 'b', 'c']}),
            iscollection=False,
            name='Test choice')

        UserDefinedFieldDefinition.objects.create(
            instance=self.instance,
            model_type='Plot',
            datatype=json.dumps({'type': 'string'}),
            iscollection=False,
            name='Test string')

        UserDefinedFieldDefinition.objects.create(
            instance=self.instance,
            model_type='Plot',
            datatype=json.dumps({'type': 'date'}),
            iscollection=False,
            name='Test date')

        UserDefinedFieldDefinition.objects.create(
            instance=self.instance,
            model_type='Plot',
            datatype=json.dumps({'type': 'int'}),
            iscollection=False,
            name='Test int')

        UserDefinedFieldDefinition.objects.create(
            instance=self.instance,
            model_type='Plot',
            datatype=json.dumps({'type': 'float'}),
            iscollection=False,
            name='Test float')

        self.plot = Plot(geom=self.p, instance=self.instance)
        self.plot.save_with_user(self.commander_user)

        psycopg2.extras.register_hstore(connection.cursor(), globally=True)

        def create_and_save_with_choice(c, n=1):
            plots = []
            for i in xrange(n):
                plot = Plot(geom=self.p, instance=self.instance)
                plot.udfs['Test choice'] = c
                plot.save_with_user(self.commander_user)
                plots.append(plot)

            return {plot.pk for plot in plots}

        self.choice_a = create_and_save_with_choice('a', n=2)
        self.choice_b = create_and_save_with_choice('b', n=3)
        self.choice_c = create_and_save_with_choice('c', n=7)
Ejemplo n.º 35
0
    def setUp(self):
        self.instance = make_instance()
        self.commander_user = make_commander_user(self.instance)
        set_write_permissions(self.instance, self.commander_user, 'Plot', [
            'udf:Test choice', 'udf:Test string', 'udf:Test int',
            'udf:Test date', 'udf:Test float'
        ])

        self.p = Point(-8515941.0, 4953519.0)

        UserDefinedFieldDefinition.objects.create(instance=self.instance,
                                                  model_type='Plot',
                                                  datatype=json.dumps({
                                                      'type':
                                                      'choice',
                                                      'choices':
                                                      ['a', 'b', 'c']
                                                  }),
                                                  iscollection=False,
                                                  name='Test choice')

        UserDefinedFieldDefinition.objects.create(instance=self.instance,
                                                  model_type='Plot',
                                                  datatype=json.dumps(
                                                      {'type': 'string'}),
                                                  iscollection=False,
                                                  name='Test string')

        UserDefinedFieldDefinition.objects.create(instance=self.instance,
                                                  model_type='Plot',
                                                  datatype=json.dumps(
                                                      {'type': 'date'}),
                                                  iscollection=False,
                                                  name='Test date')

        UserDefinedFieldDefinition.objects.create(instance=self.instance,
                                                  model_type='Plot',
                                                  datatype=json.dumps(
                                                      {'type': 'int'}),
                                                  iscollection=False,
                                                  name='Test int')

        UserDefinedFieldDefinition.objects.create(instance=self.instance,
                                                  model_type='Plot',
                                                  datatype=json.dumps(
                                                      {'type': 'float'}),
                                                  iscollection=False,
                                                  name='Test float')

        self.plot = Plot(geom=self.p, instance=self.instance)
        self.plot.save_with_user(self.commander_user)

        psycopg2.extras.register_hstore(connection.cursor(), globally=True)

        def create_and_save_with_choice(c, n=1):
            plots = []
            for i in xrange(n):
                plot = Plot(geom=self.p, instance=self.instance)
                plot.udfs['Test choice'] = c
                plot.save_with_user(self.commander_user)
                plots.append(plot)

            return {plot.pk for plot in plots}

        self.choice_a = create_and_save_with_choice('a', n=2)
        self.choice_b = create_and_save_with_choice('b', n=3)
        self.choice_c = create_and_save_with_choice('c', n=7)
Ejemplo n.º 36
0
    def setUp(self):
        self.p = Point(0, 0)
        self.instance = make_instance(point=self.p)
        self.commander_user = make_commander_user(self.instance)
        set_write_permissions(
            self.instance,
            self.commander_user,
            "Plot",
            ["udf:Test choice", "udf:Test string", "udf:Test int", "udf:Test date", "udf:Test float"],
        )

        UserDefinedFieldDefinition.objects.create(
            instance=self.instance,
            model_type="Plot",
            datatype=json.dumps({"type": "choice", "choices": ["a", "b", "c"]}),
            iscollection=False,
            name="Test choice",
        )

        UserDefinedFieldDefinition.objects.create(
            instance=self.instance,
            model_type="Plot",
            datatype=json.dumps({"type": "string"}),
            iscollection=False,
            name="Test string",
        )

        UserDefinedFieldDefinition.objects.create(
            instance=self.instance,
            model_type="Plot",
            datatype=json.dumps({"type": "date"}),
            iscollection=False,
            name="Test date",
        )

        UserDefinedFieldDefinition.objects.create(
            instance=self.instance,
            model_type="Plot",
            datatype=json.dumps({"type": "int"}),
            iscollection=False,
            name="Test int",
        )

        UserDefinedFieldDefinition.objects.create(
            instance=self.instance,
            model_type="Plot",
            datatype=json.dumps({"type": "float"}),
            iscollection=False,
            name="Test float",
        )

        self.plot = Plot(geom=self.p, instance=self.instance)
        self.plot.save_with_user(self.commander_user)

        psycopg2.extras.register_hstore(connection.cursor(), globally=True)

        def create_and_save_with_choice(c, n=1):
            plots = []
            for i in xrange(n):
                plot = Plot(geom=self.p, instance=self.instance)
                plot.udfs["Test choice"] = c
                plot.save_with_user(self.commander_user)
                plots.append(plot)

            return {plot.pk for plot in plots}

        self.choice_a = create_and_save_with_choice("a", n=2)
        self.choice_b = create_and_save_with_choice("b", n=3)
        self.choice_c = create_and_save_with_choice("c", n=7)
Ejemplo n.º 37
0
    def test_pending_udf_audits(self):
        UserDefinedFieldDefinition.objects.create(instance=self.instance,
                                                  model_type='Plot',
                                                  datatype=json.dumps({
                                                      'type':
                                                      'choice',
                                                      'choices':
                                                      ['1', '2', '3']
                                                  }),
                                                  iscollection=False,
                                                  name='times_climbed')

        set_write_permissions(self.instance, self.commander_user, 'Plot',
                              ['udf:times_climbed'])

        FieldPermission.objects.create(
            model_name='Plot',
            field_name='udf:times_climbed',
            permission_level=FieldPermission.WRITE_WITH_AUDIT,
            role=self.pending_user.get_instance_user(self.instance).role,
            instance=self.instance)

        initial_plot = Plot(geom=self.p1, instance=self.instance)
        initial_plot.udfs['times_climbed'] = '2'
        initial_plot.save_with_user(self.pending_user)

        udf_audit = Audit.objects.get(model='Plot',
                                      field='udf:times_climbed',
                                      model_id=initial_plot.pk)
        approve_or_reject_audit_and_apply(udf_audit,
                                          self.commander_user,
                                          approved=True)

        geom_audit = Audit.objects.get(model='Plot',
                                       field='geom',
                                       model_id=initial_plot.pk)
        approve_or_reject_audit_and_apply(geom_audit,
                                          self.commander_user,
                                          approved=True)

        readonly_audit = Audit.objects.get(model='Plot',
                                           field='readonly',
                                           model_id=initial_plot.pk)
        approve_or_reject_audit_and_apply(readonly_audit,
                                          self.commander_user,
                                          approved=True)

        insert_audit = Audit.objects.get(model='Plot',
                                         field='id',
                                         model_id=initial_plot.pk)

        approve_or_reject_audit_and_apply(insert_audit,
                                          self.commander_user,
                                          approved=True)

        new_plot = Plot.objects.get(pk=initial_plot.pk)

        self.assertEqual(new_plot.pk, initial_plot.pk)
        self.assertEqual(new_plot.readonly, False)
        self.assertEqual(new_plot.geom, self.p1)
        self.assertEqual(new_plot.udfs['times_climbed'], '2')
Ejemplo n.º 38
0
    def test_bulk_update_scalar_choices(self):
        plot_udfd = UserDefinedFieldDefinition.objects.create(
            instance=self.instance,
            model_type='Plot',
            datatype=json.dumps({
                'type': 'choice',
                'choices': ['a', 'b']
            }),
            iscollection=False,
            name='Test plot choice')

        tree_udfd = UserDefinedFieldDefinition.objects.create(
            instance=self.instance,
            model_type='Tree',
            datatype=json.dumps({
                'type': 'choice',
                'choices': ['x', 'y']
            }),
            iscollection=False,
            name='Test tree choice')

        set_write_permissions(self.instance, self.user, 'Plot',
                              ['udf:Test plot choice'])

        set_write_permissions(self.instance, self.user, 'Tree',
                              ['udf:Test tree choice'])

        params = {
            'choice_changes': [{
                'id':
                str(plot_udfd.pk),
                'changes': [{
                    "action": "rename",
                    "original_value": "b",
                    "new_value": "B",
                    "subfield": ""
                }, {
                    "action": "add",
                    "original_value": "",
                    "new_value": "p",
                    "subfield": ""
                }]
            }, {
                'id':
                str(tree_udfd.pk),
                'changes': [{
                    "action": "rename",
                    "original_value": "y",
                    "new_value": "Y",
                    "subfield": ""
                }, {
                    "action": "add",
                    "original_value": "",
                    "new_value": "q",
                    "subfield": ""
                }]
            }]
        }
        request = self._make_put_request(params)
        udf_bulk_update(request, self.instance)

        plot_udfd.refresh_from_db()
        tree_udfd.refresh_from_db()

        plot_datatype = json.loads(plot_udfd.datatype)
        tree_datatype = json.loads(tree_udfd.datatype)

        self.assertIn('choices', plot_datatype)
        self.assertEqual(set(plot_datatype['choices']), {'a', 'B', 'p'})

        self.assertIn('choices', tree_datatype)
        self.assertEqual(set(tree_datatype['choices']), {'x', 'Y', 'q'})
Ejemplo n.º 39
0
    def _setup_collection_udfs(self):
        self.plotstew = make_collection_udf(self.instance,
                                            model='Plot',
                                            datatype=COLLECTION_UDF_DATATYPE)
        self.treestew = make_collection_udf(self.instance,
                                            model='Tree',
                                            datatype=COLLECTION_UDF_DATATYPE)

        set_write_permissions(self.instance, self.commander, 'Plot',
                              [self.plotstew.canonical_name])
        set_write_permissions(self.instance, self.commander, 'Tree',
                              [self.treestew.canonical_name])

        p1, __ = self.create_tree_and_plot(plotudfs={
            self.plotstew.name: [{
                'action': 'water',
                'date': "2013-08-06 00:00:00"
            }, {
                'action': 'prune',
                'date': "2013-09-15 00:00:00"
            }]
        },
                                           treeudfs={
                                               self.treestew.name: [{
                                                   'action':
                                                   'water',
                                                   'date':
                                                   "2013-05-15 00:00:00"
                                               }, {
                                                   'action':
                                                   'water',
                                                   'date':
                                                   None
                                               }]
                                           })

        p2, __ = self.create_tree_and_plot(plotudfs={
            self.plotstew.name: [{
                'action': 'water',
                'date': "2014-11-26 00:00:00"
            }]
        },
                                           treeudfs={
                                               self.treestew.name: [{
                                                   'action':
                                                   'prune',
                                                   'date':
                                                   "2014-06-23 00:00:00"
                                               }]
                                           })

        p3, __ = self.create_tree_and_plot(plotudfs={
            self.plotstew.name: [{
                'action': 'water',
                'date': "2015-08-05 00:00:00"
            }, {
                'action': 'prune',
                'date': "2015-04-13 00:00:00"
            }]
        },
                                           treeudfs={
                                               self.treestew.name: [{
                                                   'action':
                                                   'prune',
                                                   'date':
                                                   "2013-06-19 00:00:00"
                                               }, {
                                                   'action':
                                                   'water',
                                                   'date':
                                                   None
                                               }]
                                           })

        return (p.pk for p in [p1, p2, p3])