Esempio n. 1
0
    def test_get_restricted_users(self):
        """
        Tests that users are properly identified as restricted.
        """

        resource = ResourceInstance.objects.get(
            resourceinstanceid=self.resource_instance_id)
        assign_perm("no_access_to_resourceinstance", self.group, resource)
        ben = self.user
        jim = User.objects.get(username="******")
        sam = User.objects.get(username="******")
        admin = User.objects.get(username="******")
        assign_perm("view_resourceinstance", ben, resource)
        assign_perm("change_resourceinstance", jim, resource)

        restrictions = get_restricted_users(resource)

        results = [
            jim.id in restrictions["cannot_read"],
            ben.id in restrictions["cannot_write"],
            sam.id in restrictions["cannot_delete"],
            sam.id in restrictions["no_access"],
            admin.id not in restrictions["cannot_read"],
            admin.id not in restrictions["cannot_write"],
            admin.id not in restrictions["cannot_delete"],
            admin.id not in restrictions["no_access"],
        ]

        self.assertTrue(all(results) is True)
Esempio n. 2
0
    def get_documents_to_index(self,
                               fetchTiles=True,
                               datatype_factory=None,
                               node_datatypes=None):
        """
        Gets all the documents nessesary to index a single resource
        returns a tuple of a document and list of terms

        Keyword Arguments:
        fetchTiles -- instead of fetching the tiles from the database get them off the model itself
        datatype_factory -- refernce to the DataTypeFactory instance
        node_datatypes -- a dictionary of datatypes keyed to node ids

        """

        document = {}
        document["displaydescription"] = None
        document["resourceinstanceid"] = str(self.resourceinstanceid)
        document["graph_id"] = str(self.graph_id)
        document["map_popup"] = None
        document["displayname"] = None
        document["root_ontology_class"] = self.get_root_ontology()
        document["legacyid"] = self.legacyid
        document["displayname"] = self.displayname
        document["displaydescription"] = self.displaydescription
        document["map_popup"] = self.map_popup

        tiles = list(models.TileModel.objects.filter(
            resourceinstance=self)) if fetchTiles else self.tiles

        restrictions = get_restricted_users(self)
        document["tiles"] = tiles
        document["permissions"] = {
            "users_without_read_perm": restrictions["cannot_read"]
        }
        document["permissions"]["users_without_edit_perm"] = restrictions[
            "cannot_write"]
        document["permissions"]["users_without_delete_perm"] = restrictions[
            "cannot_delete"]
        document["permissions"]["users_with_no_access"] = restrictions[
            "no_access"]
        document["strings"] = []
        document["dates"] = []
        document["domains"] = []
        document["geometries"] = []
        document["points"] = []
        document["numbers"] = []
        document["date_ranges"] = []
        document["ids"] = []
        document["provisional_resource"] = "true" if sum(
            [len(t.data) for t in tiles]) == 0 else "false"

        terms = []

        for tile in document["tiles"]:
            for nodeid, nodevalue in tile.data.items():
                datatype = node_datatypes[nodeid]
                if nodevalue != "" and nodevalue != [] and nodevalue != {} and nodevalue is not None:
                    datatype_instance = datatype_factory.get_instance(datatype)
                    datatype_instance.append_to_document(
                        document, nodevalue, nodeid, tile)
                    node_terms = datatype_instance.get_search_terms(
                        nodevalue, nodeid)
                    for index, term in enumerate(node_terms):
                        terms.append({
                            "_id":
                            str(nodeid) + str(tile.tileid) + str(index),
                            "_source": {
                                "value": term,
                                "nodeid": nodeid,
                                "nodegroupid": tile.nodegroup_id,
                                "tileid": tile.tileid,
                                "resourceinstanceid": tile.resourceinstance_id,
                                "provisional": False,
                            },
                        })

            if tile.provisionaledits is not None:
                provisionaledits = tile.provisionaledits
                if len(provisionaledits) > 0:
                    if document["provisional_resource"] == "false":
                        document["provisional_resource"] = "partial"
                    for user, edit in provisionaledits.items():
                        if edit["status"] == "review":
                            for nodeid, nodevalue in edit["value"].items():
                                datatype = node_datatypes[nodeid]
                                if nodevalue != "" and nodevalue != [] and nodevalue != {} and nodevalue is not None:
                                    datatype_instance = datatype_factory.get_instance(
                                        datatype)
                                    datatype_instance.append_to_document(
                                        document, nodevalue, nodeid, tile,
                                        True)
                                    node_terms = datatype_instance.get_search_terms(
                                        nodevalue, nodeid)
                                    for index, term in enumerate(node_terms):
                                        terms.append({
                                            "_id":
                                            str(nodeid) + str(tile.tileid) +
                                            str(index),
                                            "_source": {
                                                "value": term,
                                                "nodeid": nodeid,
                                                "nodegroupid":
                                                tile.nodegroup_id,
                                                "tileid": tile.tileid,
                                                "resourceinstanceid":
                                                tile.resourceinstance_id,
                                                "provisional": True,
                                            },
                                        })

        return document, terms