Пример #1
0
 def test_methods_for_traversal(self):
     methods = permit.methods_for_traversal('wat','baz',self.narrowedConfig)
     print 'Result of testing methods for traversal: %s' % str(methods)
     self.assertIn('GET', methods)
     self.assertIn('POST', methods)
Пример #2
0
def get_collection(parent=None, parent_id=None, resource=None, user=None, config=None, depth=0):
    """ Requires a resource with a parent scope (which may be index if provided
        by the url router). Parent must come with an id.
        We set the depth to track our recursions and condition the views
        (deeper calls generally return less).

        User is a User instance.

        We will only recurse on GET (and perhaps someday OPTIONS), because it is idempotent.

        All the legal checks are in the handler. Once we recurse we assume
        our configuration is only throwing valid responses.
    """
    allowed = permit.methods_for_traversal(parent or "index", resource, config)
    if not depth:
        # Since we're already GETting this, no need to offer it to the client
        # again.
        allowed.remove("GET")

    this_model = help.model_for(resource)
    # We get to use the model's manager directly if it's the root,
    # or if there is a parent we use the parent's manager.
    if parent:
        # And now a bit of magic naming. We expect the verbose name of the resource
        # to be an attribute of the parent model.
        parent_model = help.model_for(parent)
        parent_inst = parent_model.objects.get(pk=parent_id)
        collection = getattr(parent_inst, resource)
    else:
        collection = this_model.collection

    # Now we need our query filter.
    q = help.build_Q_set(user, resource)

    result = {
        "meta": {
            "resource": resource,
            "url": collection.url(),
            "methods": [m for m in allowed],
            "count": collection.count(),
        }
    }
    if parent and not depth:
        result["traversals"] = [{"url": parent_inst.url, "method": "GET"}]
    if VIEW_TRAVERSE_DEPTH - depth:
        # We don't retrieve collection.all() here. We have to figure:
        logger.info("q = %s" % str(q))
        if q:
            instances = collection.filter(q).all()
        else:
            instances = collection.all()
        logger.info("The query was = %s" % str(instances.query))
        result["objects"] = [
            get_instance(
                resource=resource,
                instance=inst,
                parent=parent,
                parent_id=parent_id,
                user=user,
                config=help.instance_permissions(user, inst),
                depth=depth + 1,
            )
            for inst in instances
        ]

    return result