Can I use a view configured for a superclass if Pyramid has set up a view for the class?

I am creating a simple pyramid CMS that uses bypass. There is a class called Collectionthat has some type subclasses NewsCollection, GalleriesCollectionetc.

I need two kinds of views to display these collections. Representation of frontent, html and backend, json view (the admin panel uses dgrid to display data). The backend view can be shared - it uploads json data in each case. The appearance of the interface should not - a custom template will be created for each data type.

The problem is this: when I adjust the view as follows:

@view_config(context=Collection, xhr=True, renderer='json', accept='application/json')

It works correctly. However, as soon as I add any view configured for NewsCollection, this takes precedence. Even if I put predicates specifically for conflict with the above configuration (for example accept='text/html'), still the above view will not be called. Instead, I get a predicate mismatch.

My question is: can I do anything to make the view configured for Collectioninvoked when there are also views for NewsCollection? Or do I need to use a different design (e.g. sending a URL or adding the same view multiple times for different types of resources)

+5
source share
1 answer

- , : https://github.com/Pylons/pyramid/issues/409

, Pyramid - context - . context, .

, ( ) - , , - .

( UPDATE: 2013 , , 1.4)

- :

def context_implements(*types):
    """
    A custom predicate to implement matching views to resources which
    implement more than one interface - in this situation Pyramid has
    trouble matching views to the second registered interface. See
    https://github.com/Pylons/pyramid/issues/409#issuecomment-3578518

    Accepts a list of interfaces - if ANY of them are implemented the function
    returns True
    """
    def inner(context, request):
        for typ in types:
            if typ.providedBy(context):
                return True
        return False
    return inner


@view_config(context=ICollection,
    custom_predicates=(context_implements(INewsCollection),)
    )
def news_collection_view(context, request):
    ....
+6
source

All Articles