Beispiel #1
0
def get_es_channels(locale, paging, category, category_boosts=None,
                    prefix_boosts=None, add_tracking=None, enable_promotion=True):
    cs = ChannelSearch(locale)
    cs.set_paging(*paging)
    # Boost popular channels based on ...
    if category_boosts:
        for boost in category_boosts:
            cs.add_filter(filters.category_boost(*boost))
    if prefix_boosts:
        for boost in prefix_boosts:
            cs.add_filter(filters.channel_prefix_boost(*boost))
    cs.add_filter(filters.boost_from_field_value('editorial_boost'))
    cs.add_filter(filters.negatively_boost_favourites())
    cs.add_filter(filters.channel_rank_boost(locale))
    cs.filter_category(category)
    if enable_promotion:
        cs.promotion_settings(category)
    cs.date_sort(request.args.get('date_order'))
    if request.args.get('user_id'):
        cs.add_term('owner', request.args.get('user_id'))
    channels = cs.channels(with_owners=True, add_tracking=add_tracking)
    return channels, cs.total
Beispiel #2
0
    def ranking_locale(self, locale):
        category = int(request.args.get('category', 0))
        search = request.args.get('search')

        toggle_locale = {'en-us': 'en-gb', 'en-gb': 'en-us'}.get(locale)
        ctx = {
            'locale_base': self.url + '/locale/' + locale + '/',
            'locale_toggle_name': toggle_locale,
            'locale_toggle': self.url + '/locale/' + toggle_locale + '/',
            'categories': category_list(),
            'image_cdn': app.config['IMAGE_CDN'],
            'category': category,
            'locale': locale,
            'search_term': request.args.get('search', 'Search for a channel')}

        if category == 0:
            category = None

        cs = ChannelSearch(locale)
        offset, limit = request.args.get('start', 0), request.args.get('size', 20)
        cs.set_paging(offset, limit)
        if search:
            cs.add_text('title', search)
        else:
            cs.add_filter(filters.boost_from_field_value('editorial_boost'))
            cs.add_filter(filters.channel_rank_boost(locale))
            cs.add_filter(filters.negatively_boost_favourites())
            cs.filter_category(category)
            cs.promotion_settings(category)
        processed_channels = cs.channels(with_owners=True)

        ctx['channels'] = []

        # loop once to get the raw data
        raw_channels = {}
        for channel in cs.results():
            c = {}
            c['id'] = channel.id
            c['title'] = channel.title
            c['editorial_boost'] = channel.editorial_boost
            try:
                c['date_added'] = channel.date_added[:10]
            except TypeError:
                c['date_added'] = channel.date_added.isoformat()[:10]
            c['cover_thumbnail_large_url'] = channel.cover_thumbnail_large_url
            c['explanation'] = channel.__dict__['_meta']['explanation']
            c['subscriber_frequency'] = channel.subscriber_frequency
            c['subscriber_count'] = channel.subscriber_count
            c['video_update_frequency'] = channel.update_frequency
            c['subscriber_count'] = channel.subscriber_count
            c['promotion'] = channel.promotion
            c['gbcount'] = channel.locales['en-gb']['view_count']
            c['uscount'] = channel.locales['en-us']['view_count']
            c['normalised_rank'] = channel.normalised_rank
            raw_channels[channel.id] = c

        # loop again to get the correct order
        for channel in processed_channels:
            c = raw_channels[channel['id']]
            promo_string = '|'.join([locale, str(category or 0), str(channel['position'] + 1)])
            c['promoted'] = False
            if c.get('promotion', []) and promo_string in c.get('promotion', []):
                c['promoted'] = True
            ctx['channels'].append(c)

        return self.render('admin/ranking.html', **ctx)