Esempio n. 1
0
    def search_users(self):
        def _add_tracking(item, extra=None):
            item["tracking_code"] = "user-search %d" % item["position"]

        search_term = request.args.get("q", "").lower()
        offset, limit = self.get_page()
        if use_elasticsearch():
            us = UserSearch()
            us.set_paging(offset, limit)
            if app.config.get("DOLLY"):
                us.search_terms(search_term)
            else:
                us.add_text("username", search_term)
                us.add_text("display_name", search_term)
            items = us.users(add_tracking=_add_tracking)
            return dict(users=dict(items=items, total=us.total))

        users = User.query.filter(func.lower(User.username).like(search_term))
        count = users.count()
        items = []
        for user in users.limit(limit).offset(offset):
            items.append(
                dict(
                    id=user.id,
                    username=user.username,
                    display_name=user.display_name,
                    avatar_thumbnail_url=user.avatar.url,
                    resource_url=user.resource_url,
                )
            )
        return dict(users=dict(items=items, total=count))
Esempio n. 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 user')}

        if category == 0:
            category = None

        u = UserSearch()
        offset, limit = request.args.get('start', 0), request.args.get('size', 20)
        u.set_paging(offset, limit)
        if search:
            u.add_text('username', search)
        else:
            u.filter_category(category)
            u.promotion_settings(category)
        processed_users = u.users(include_promo=True)

        ctx['users'] = []

        # loop once to get the raw data
        raw_users = {}

        for user in u.results():
            print user
            u = {}
            u['id'] = user.id
            u['username'] = user.username
            u['explanation'] = user.__dict__['_meta']['explanation']
            u['profile_cover_url'] = user.profile_cover_url
            u['avatar_thumbnail_url'] = user.avatar_thumbnail_url
            u['description'] = user.description
            u['promotion'] = user.promotion
            u['normalised_rank'] = user.normalised_rank
            u['category'] = user.category
            u['brand'] = user.brand
            raw_users[user.id] = u

        # loop again to get the correct order
        for user in processed_users:
            u = raw_users[user['id']]
            promo_string = '|'.join([locale, str(category or 0), str(user['position'] + 1)])
            u['promoted'] = False
            if u.get('promotion', []) and promo_string in u.get('promotion', []):
                u['promoted'] = True
            ctx['users'].append(u)

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