コード例 #1
0
        def data_facets(ctx, data):

            storeSession = util.getStoreSession(ctx)
            avatar = util.getAvatar(ctx)

            @defer.deferredGenerator
            def loadCategories(facets):
                d = defer.waitForDeferred(avatar.getCategoryManager(storeSession))
                yield d
                categories = d.getResult()
                rv = []
                for f in facets:
                    facetData = FacetData()
                    facetData.label = f[2]
                    facetData.textid = f[1]
                    rv.append(facetData)
                    d = defer.waitForDeferred(categories.loadCategories(facetData.textid))
                    yield d
                    facetData.tree = d.getResult().children
                yield rv

            d = avatar.getCategoryManager(storeSession)
            d.addCallback(lambda categories: categories.loadFacets())
            d.addCallback(loadCategories)
            return d
コード例 #2
0
    def child(self, ctx, name):

        # Attribute names should be strings not unicode
        # (unicode can happen if the data is requested directly from a template)
        name = str(name)

        # XXX hack!
        if name == 'childLink':
            return url.URL.fromContext(ctx).child(self.original.id)
        elif name == 'id':
            return self.original.id

        realm = util.getAvatar(ctx).realm
        try:
            language = inevow.ILanguages(ctx)[0]
        except IndexError:
            language = realm.defaultLanguage

        # We need to look through normal and system attributes
        funcs = [self.original.getAttributeValues, self.original.getSystemAttributeValues]
        for func in funcs:
            # Get the attributes and the language found for each attribute
            data, attrLangs = func(language, realm.fallbackLanguages, True)
            # No data, try next func
            data = data.get(name, NOTFOUND)
            if data is not NOTFOUND:
                break
        else:
            raise KeyError('Attribute %r missing' % name)

        # Get the language found for the attribute
        lang = attrLangs.get(name)

        # Return data now if not translatable
        if lang is None:
            return data

        # We have a language, create a localised value
        for l in realm.languages:
            if lang == l.code and l.publiccssclass:
                return LocalizedValue(data, l.publiccssclass, lang)

        raise Exception('Error creating localised value for attribute %r, ' \
                'language %r' % (name, lang))
コード例 #3
0
    def locateChild( self, ctx, segments ):

        def notFound( failure ):
            log.err(failure)
            return appserver.NotFound

        # Get hold of the store session and avatar from the context
        storeSession = util.getStoreSession(ctx)
        avatar = util.getAvatar(ctx)

        # First segment may be a language. It should be ok making this
        # assumption because the next segment is the id/version which can't look
        # like a country code.
        languageCodes = [l.code for l in avatar.realm.languages]
        if segments[0] not in languageCodes:
            # Try the current language buf it that's not a configured language
            # then just use the default.
            try:
                lang = inevow.ILanguages(ctx)[0]
            except IndexError:
                lang = None
            if lang not in languageCodes:
                lang = avatar.realm.defaultLanguage
        else:
            lang = segments[0]
            segments = segments[1:]

        # (Remaining) segments can be either (itemId,) or (itemId, assetName).
        if len(segments) == 1:
            itemId, assetName = segments[0], None
        elif len(segments) == 2:
            itemId, assetName = segments
        else:
            return appserver.NotFound

        d = self._getResource( avatar, storeSession, itemId, assetName, lang )
        d.addCallback(lambda resource: (resource, ()))
        d.addErrback( notFound )
        return d